diff --git a/src/_backend_agg.cpp b/src/_backend_agg.cpp index edbd3f2ad2f0..bf4b9dc27732 100644 --- a/src/_backend_agg.cpp +++ b/src/_backend_agg.cpp @@ -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))) @@ -2177,7 +2178,11 @@ RendererAgg::write_rgba(const Py::Tuple& args) "Object does not appear to be a 8-bit string path or a Python file-like object"); } + #if PY3K + PyObject_CallFunction(write_method, (char *)"y#", pixBuffer, NUMBYTES); + #else PyObject_CallFunction(write_method, (char *)"s#", pixBuffer, NUMBYTES); + #endif Py_XDECREF(write_method); } diff --git a/src/file_compat.h b/src/file_compat.h index 102c8b70977d..d2b3fb3ef2b1 100644 --- a/src/file_compat.h +++ b/src/file_compat.h @@ -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 */ @@ -145,22 +145,19 @@ 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; - } + 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; + /* 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); } - Py_DECREF(ret); return 0; }