This commit is contained in:
Benjamin Peterson 2016-09-12 22:07:14 -07:00
commit 5a715cfc57
1 changed files with 17 additions and 15 deletions

View File

@ -497,16 +497,12 @@ error:
static int static int
fp_setreadl(struct tok_state *tok, const char* enc) fp_setreadl(struct tok_state *tok, const char* enc)
{ {
PyObject *readline = NULL, *stream = NULL, *io = NULL; PyObject *readline, *io, *stream;
_Py_IDENTIFIER(open); _Py_IDENTIFIER(open);
_Py_IDENTIFIER(readline); _Py_IDENTIFIER(readline);
int fd; int fd;
long pos; long pos;
io = PyImport_ImportModuleNoBlock("io");
if (io == NULL)
goto cleanup;
fd = fileno(tok->fp); fd = fileno(tok->fp);
/* Due to buffering the file offset for fd can be different from the file /* Due to buffering the file offset for fd can be different from the file
* position of tok->fp. If tok->fp was opened in text mode on Windows, * position of tok->fp. If tok->fp was opened in text mode on Windows,
@ -517,27 +513,33 @@ fp_setreadl(struct tok_state *tok, const char* enc)
if (pos == -1 || if (pos == -1 ||
lseek(fd, (off_t)(pos > 0 ? pos - 1 : pos), SEEK_SET) == (off_t)-1) { lseek(fd, (off_t)(pos > 0 ? pos - 1 : pos), SEEK_SET) == (off_t)-1) {
PyErr_SetFromErrnoWithFilename(PyExc_OSError, NULL); PyErr_SetFromErrnoWithFilename(PyExc_OSError, NULL);
goto cleanup; return 0;
} }
io = PyImport_ImportModuleNoBlock("io");
if (io == NULL)
return 0;
stream = _PyObject_CallMethodId(io, &PyId_open, "isisOOO", stream = _PyObject_CallMethodId(io, &PyId_open, "isisOOO",
fd, "r", -1, enc, Py_None, Py_None, Py_False); fd, "r", -1, enc, Py_None, Py_None, Py_False);
Py_DECREF(io);
if (stream == NULL) if (stream == NULL)
goto cleanup; return 0;
readline = _PyObject_GetAttrId(stream, &PyId_readline); readline = _PyObject_GetAttrId(stream, &PyId_readline);
Py_DECREF(stream);
if (readline == NULL)
return 0;
Py_XSETREF(tok->decoding_readline, readline); Py_XSETREF(tok->decoding_readline, readline);
if (pos > 0) { if (pos > 0) {
if (PyObject_CallObject(readline, NULL) == NULL) { PyObject *bufobj = PyObject_CallObject(readline, NULL);
readline = NULL; if (bufobj == NULL)
goto cleanup; return 0;
} Py_DECREF(bufobj);
} }
cleanup: return 1;
Py_XDECREF(stream);
Py_XDECREF(io);
return readline != NULL;
} }
/* Fetch the next byte from TOK. */ /* Fetch the next byte from TOK. */