Skip to content

Commit

Permalink
Prevent mpl_PyFile_Dup from failing when given BytesIO or similar
Browse files Browse the repository at this point in the history
  • Loading branch information
bytbox committed Mar 14, 2014
1 parent 7dc06fb commit ba5c886
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/_backend_agg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2168,6 +2168,7 @@ RendererAgg::write_rgba(const Py::Tuple& args)
}
else
{
PyErr_Clear();
PyObject* write_method = PyObject_GetAttrString(py_fileobj.ptr(),
"write");
if (!(write_method && PyCallable_Check(write_method)))
Expand Down
29 changes: 13 additions & 16 deletions src/file_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ mpl_PyFile_Dup(PyObject *file, char *mode, mpl_off_t *orig_pos)
/* Record the original raw file handle position */
*orig_pos = npy_ftell(handle);
if (*orig_pos == -1) {
PyErr_SetString(PyExc_IOError, "obtaining file position failed");
return NULL;
// handle is a stream, so we don't have to worry about this
return handle;
}

/* Seek raw handle to the Python-side position */
Expand Down Expand Up @@ -145,20 +145,17 @@ mpl_PyFile_DupClose(PyObject *file, FILE* handle, mpl_off_t orig_pos)
if (fd == -1) {
return -1;
}
if (npy_lseek(fd, orig_pos, SEEK_SET) == -1) {
PyErr_SetString(PyExc_IOError, "seeking file failed");
return -1;
}

if (position == -1) {
PyErr_SetString(PyExc_IOError, "obtaining file position failed");
return -1;
}

/* Seek Python-side handle to the FILE* handle position */
ret = PyObject_CallMethod(file, "seek", MPL_OFF_T_PYFMT "i", position, 0);
if (ret == NULL) {
return -1;
if (npy_lseek(fd, orig_pos, SEEK_SET) != -1) {
if (position == -1) {
PyErr_SetString(PyExc_IOError, "obtaining file position failed");
return -1;
}

/* Seek Python-side handle to the FILE* handle position */
ret = PyObject_CallMethod(file, "seek", MPL_OFF_T_PYFMT "i", position, 0);
if (ret == NULL) {
return -1;
}
}
Py_DECREF(ret);
return 0;
Expand Down

0 comments on commit ba5c886

Please sign in to comment.