diff options
author | Julian Taylor <jtaylor.debian@googlemail.com> | 2016-02-06 23:50:58 +0100 |
---|---|---|
committer | Julian Taylor <jtaylor.debian@googlemail.com> | 2016-02-06 23:56:07 +0100 |
commit | 0ec441d60b6d10d68fd2cb86e51980160730707c (patch) | |
tree | 2aae2eb3b5dc02ee475856bbe4baea1e95ac9e93 | |
parent | 5002e81bd8ec315a8214876d3b3237acf0adb851 (diff) | |
download | numpy-0ec441d60b6d10d68fd2cb86e51980160730707c.tar.gz |
BUG: raise IOError on not a file in python2
The change in 5225e4c2007 did not account for PyFile_AsFile does not
raise an error on invalid input, add the handling to equalize our
wr5225e4c2007 apper function.
closes gh-7200
-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_) |