From ac671610c0f520e65b026a72ad6984c72393710f Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Wed, 11 Jul 2018 14:46:21 -0500 Subject: [PATCH] Backport PR #11636: Don't flush file object opened for reading --- 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 84340655bedc..d6935059419a 100644 --- a/src/file_compat.h +++ b/src/file_compat.h @@ -60,12 +60,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;