diff options
-rw-r--r-- | numpy/core/include/numpy/npy_3kcompat.h | 8 | ||||
-rw-r--r-- | numpy/core/src/multiarray/methods.c | 2 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 8 |
3 files changed, 15 insertions, 3 deletions
diff --git a/numpy/core/include/numpy/npy_3kcompat.h b/numpy/core/include/numpy/npy_3kcompat.h index 6a11cf960..db60a312c 100644 --- a/numpy/core/include/numpy/npy_3kcompat.h +++ b/numpy/core/include/numpy/npy_3kcompat.h @@ -320,7 +320,13 @@ static NPY_INLINE FILE * npy_PyFile_Dup2(PyObject *file, const char *NPY_UNUSED(mode), npy_off_t *NPY_UNUSED(orig_pos)) { - return PyFile_AsFile(file); + 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 diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 84d4e2c9e..56b6086ff 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -583,8 +583,6 @@ array_tofile(PyArrayObject *self, PyObject *args, PyObject *kwds) fd = npy_PyFile_Dup2(file, "wb", &orig_pos); if (fd == NULL) { - PyErr_SetString(PyExc_IOError, - "first argument must be a string or open file"); goto fail; } if (PyArray_ToFile(self, fd, sep, format) < 0) { diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 3498b8a51..d57e7c106 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -3582,6 +3582,14 @@ class TestIO(object): def tearDown(self): shutil.rmtree(self.tempdir) + def test_nofile(self): + # this should probably be supported as a file + # but for now test for proper errors + b = io.BytesIO() + assert_raises(IOError, np.fromfile, b, np.uint8, 80) + d = np.ones(7); + assert_raises(IOError, lambda x: x.tofile(b), d) + def test_bool_fromstring(self): v = np.array([True, False, True, False], dtype=np.bool_) y = np.fromstring('1 0 -2.3 0.0', sep=' ', dtype=np.bool_) |