summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/core/src/multiarray/methods.c11
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.c5
-rw-r--r--numpy/core/src/private/npy_3kcompat.h31
3 files changed, 38 insertions, 9 deletions
diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c
index ede1658a6..bc01750ac 100644
--- a/numpy/core/src/multiarray/methods.c
+++ b/numpy/core/src/multiarray/methods.c
@@ -489,9 +489,8 @@ array_tofile(PyArrayObject *self, PyObject *args, PyObject *kwds)
return NULL;
}
- if (PyString_Check(file) || PyUnicode_Check(file)) {
- file = PyObject_CallFunction((PyObject *)&PyFile_Type,
- "Os", file, "wb");
+ if (PyBytes_Check(file) || PyUnicode_Check(file)) {
+ file = npy_PyFile_OpenFile(file, "wb");
if (file == NULL) {
return NULL;
}
@@ -499,7 +498,7 @@ array_tofile(PyArrayObject *self, PyObject *args, PyObject *kwds)
else {
Py_INCREF(file);
}
- fd = PyFile_AsFile(file);
+ fd = npy_PyFile_AsFile(file, "wb");
if (fd == NULL) {
PyErr_SetString(PyExc_IOError, "first argument must be a " \
"string or open file");
@@ -1516,8 +1515,8 @@ PyArray_Dump(PyObject *self, PyObject *file, int protocol)
if (cpick == NULL) {
return -1;
}
- if PyString_Check(file) {
- file = PyFile_FromString(PyString_AS_STRING(file), "wb");
+ if (PyString_Check(file)) {
+ file = npy_PyFile_OpenFile(file, "wb");
if (file == NULL) {
return -1;
}
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c
index 63a5d1147..cad89d2a0 100644
--- a/numpy/core/src/multiarray/multiarraymodule.c
+++ b/numpy/core/src/multiarray/multiarraymodule.c
@@ -1713,8 +1713,7 @@ array_fromfile(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *keywds)
return NULL;
}
if (PyString_Check(file) || PyUnicode_Check(file)) {
- file = PyObject_CallFunction((PyObject *)&PyFile_Type,
- "Os", file, "rb");
+ file = npy_PyFile_OpenFile(file, "rb");
if (file == NULL) {
return NULL;
}
@@ -1722,7 +1721,7 @@ array_fromfile(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *keywds)
else {
Py_INCREF(file);
}
- fp = PyFile_AsFile(file);
+ fp = npy_PyFile_AsFile(file, "rb");
if (fp == NULL) {
PyErr_SetString(PyExc_IOError,
"first argument must be an open file");
diff --git a/numpy/core/src/private/npy_3kcompat.h b/numpy/core/src/private/npy_3kcompat.h
index cb4573e88..954ac2898 100644
--- a/numpy/core/src/private/npy_3kcompat.h
+++ b/numpy/core/src/private/npy_3kcompat.h
@@ -2,6 +2,7 @@
#define _NPY_3KCOMPAT_H_
#include <Python.h>
+#include <stdio.h>
#include "npy_config.h"
/*
@@ -61,6 +62,36 @@ static NPY_INLINE int PyInt_Check(PyObject *op) {
#endif
/*
+ * PyFile_AsFile
+ */
+#if defined(NPY_PY3K)
+static NPY_INLINE FILE*
+npy_PyFile_AsFile(PyObject *file, char *mode)
+{
+ int fd;
+ fd = PyObject_AsFileDescriptor(file);
+ if (fd == -1) {
+ return NULL;
+ }
+#warning XXX -- who releases the FILE* returned by fdopen? fclose() closes the file...
+ return fdopen(fd, mode);
+}
+#else
+#define npy_PyFile_AsFile(file, mode) PyFile_AsFile(file)
+#endif
+
+static NPY_INLINE PyObject*
+npy_PyFile_OpenFile(PyObject *filename, char *mode)
+{
+ PyObject *open;
+ open = PyDict_GetItemString(PyEval_GetBuiltins(), "open");
+ if (open == NULL) {
+ return NULL;
+ }
+ return PyObject_CallFunction(open, "Os", filename, mode);
+}
+
+/*
* PyObject_Cmp
*/
#if defined(NPY_PY3K)