From 61dd35a0286b28549c4143c681b145a08b56956e Mon Sep 17 00:00:00 2001 From: Andreas Gustafsson Date: Wed, 11 Jul 2018 20:49:34 +0300 Subject: [PATCH] Fix bug #11635: Do not call the flush method on a file object opened for reading, because it returns an error on BSD systems, with the end result that "import matplotlib.pyplot" fails with the error message "TypeError: First argument must be a path or file object reading bytes". --- src/file_compat.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/file_compat.h b/src/file_compat.h index a1d93f3f318f..428db6b4bda9 100644 --- a/src/file_compat.h +++ b/src/file_compat.h @@ -59,12 +59,15 @@ static NPY_INLINE FILE *mpl_PyFile_Dup(PyObject *file, char *mode, mpl_off_t *or mpl_off_t pos; FILE *handle; - /* Flush first to ensure things end up in the file in the correct order */ - ret = PyObject_CallMethod(file, (char *)"flush", (char *)""); - if (ret == NULL) { - return NULL; + if (mode[0] != 'r') { + /* Flush first to ensure things end up in the file in the correct order */ + ret = PyObject_CallMethod(file, (char *)"flush", (char *)""); + if (ret == NULL) { + return NULL; + } + Py_DECREF(ret); } - Py_DECREF(ret); + fd = PyObject_AsFileDescriptor(file); if (fd == -1) { return NULL;