summaryrefslogtreecommitdiff
path: root/numpy/core/include
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2017-04-14 19:29:44 -0600
committerGitHub <noreply@github.com>2017-04-14 19:29:44 -0600
commit6c91e9ee0d9e9fc16e504780ccf7ede26d1114c8 (patch)
tree78075c826d7ef3230a7743034d80cb87997ab84e /numpy/core/include
parent0f3846aaabc7c1278e9aeab7aafa0b8d4a1b264e (diff)
parentd1bb606012b069600ae60e37b7675122fdde29b8 (diff)
downloadnumpy-6c91e9ee0d9e9fc16e504780ccf7ede26d1114c8.tar.gz
Merge pull request #6632 from mcmtroffaes/feature/fromfile-ioopen-bug
TST/BUG: fromfile - fix test and expose bug with io class argument
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)
{