summaryrefslogtreecommitdiff
path: root/numpy/core/include
diff options
context:
space:
mode:
authorMatthias C. M. Troffaes <matthias.troffaes@gmail.com>2015-11-05 11:37:02 +0000
committerMatthias C. M. Troffaes <matthias.troffaes@gmail.com>2016-09-08 11:48:19 +0100
commita3db73a8b2f53b0602b1aadd31c48836c07b82dc (patch)
treed81997e66439fa6c7471e0a021d4152e638425eb /numpy/core/include
parentcb7f4078bf94cde6233b9128f138ba18e04c1d1d (diff)
downloadnumpy-a3db73a8b2f53b0602b1aadd31c48836c07b82dc.tar.gz
BUG: npy_PyFile_Dup2 - fix PyFile_AsFile failing on io style classes
This bug only manifests itself in the Python 2 code path. Falls back to the current Python 3 code path also on Python 2 because the Python 3 code path is written precisely to handle this situation. Also fix tests, and clarify in the documentation that the current implementation requires the stream to be seekable.
Diffstat (limited to 'numpy/core/include')
-rw-r--r--numpy/core/include/numpy/npy_3kcompat.h48
1 files changed, 21 insertions, 27 deletions
diff --git a/numpy/core/include/numpy/npy_3kcompat.h b/numpy/core/include/numpy/npy_3kcompat.h
index cdab1bbe8..c0aa1eb2e 100644
--- a/numpy/core/include/numpy/npy_3kcompat.h
+++ b/numpy/core/include/numpy/npy_3kcompat.h
@@ -148,7 +148,7 @@ PyUnicode_Concat2(PyObject **left, PyObject *right)
/*
* PyFile_* compatibility
*/
-#if defined(NPY_PY3K)
+
/*
* Get a FILE* handle to the file represented by the Python object
*/
@@ -160,6 +160,13 @@ npy_PyFile_Dup2(PyObject *file, char *mode, npy_off_t *orig_pos)
npy_off_t pos;
FILE *handle;
+ /* For Python 2 PyFileObject, use PyFile_AsFile */
+#if !defined(NPY_PY3K)
+ if (PyFile_Check(file)) {
+ return PyFile_AsFile(file);
+ }
+#endif
+
/* Flush first to ensure things end up in the file in the correct order */
ret = PyObject_CallMethod(file, "flush", "");
if (ret == NULL) {
@@ -257,6 +264,13 @@ npy_PyFile_DupClose2(PyObject *file, FILE* handle, npy_off_t orig_pos)
PyObject *ret, *io, *io_raw;
npy_off_t position;
+ /* For Python 2 PyFileObject, do nothing */
+#if !defined(NPY_PY3K)
+ if (PyFile_Check(file)) {
+ return 0;
+ }
+#endif
+
position = npy_ftell(handle);
/* Close the FILE* handle */
@@ -314,6 +328,12 @@ static NPY_INLINE int
npy_PyFile_Check(PyObject *file)
{
int fd;
+ /* For Python 2, check if it is a PyFileObject */
+#if !defined(NPY_PY3K)
+ if (PyFile_Check(file)) {
+ return 1;
+ }
+#endif
fd = PyObject_AsFileDescriptor(file);
if (fd == -1) {
PyErr_Clear();
@@ -322,32 +342,6 @@ npy_PyFile_Check(PyObject *file)
return 1;
}
-#else
-
-static NPY_INLINE FILE *
-npy_PyFile_Dup2(PyObject *file,
- const char *NPY_UNUSED(mode), npy_off_t *NPY_UNUSED(orig_pos))
-{
- FILE * fp = PyFile_AsFile(file);
- if (fp == NULL) {
- PyErr_SetString(PyExc_IOError,
- "first argument must be an open file");
- return NULL;
- }
- return fp;
-}
-
-static NPY_INLINE int
-npy_PyFile_DupClose2(PyObject *NPY_UNUSED(file), FILE* NPY_UNUSED(handle),
- npy_off_t NPY_UNUSED(orig_pos))
-{
- return 0;
-}
-
-#define npy_PyFile_Check PyFile_Check
-
-#endif
-
static NPY_INLINE PyObject*
npy_PyFile_OpenFile(PyObject *filename, const char *mode)
{