8000 BUG: raise IOError on not a file in python2 · numpy/numpy@0ec441d · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 0ec441d

Browse files
committed
BUG: raise IOError on not a file in python2
The change in 5225e4c 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
1 parent 5002e81 commit 0ec441d

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

numpy/core/include/numpy/npy_3kcompat.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,13 @@ static NPY_INLINE FILE *
320320
npy_PyFile_Dup2(PyObject *file,
321321
const char *NPY_UNUSED(mode), npy_off_t *NPY_UNUSED(orig_pos))
322322
{
323-
return PyFile_AsFile(file);
323+
FILE * fp = PyFile_AsFile(file);
324+
if (fp == NULL) {
325+
PyErr_SetString(PyExc_IOError,
326+
"first argument must be an open file");
327+
return NULL;
328+
}
329+
return fp;
324330
}
325331

326332
static NPY_INLINE int

numpy/core/src/multiarray/methods.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,6 @@ array_tofile(PyArrayObject *self, PyObject *args, PyObject *kwds)
583583

584584
fd = npy_PyFile_Dup2(file, "wb", &orig_pos);
585585
if (fd == NULL) {
586-
PyErr_SetString(PyExc_IOError,
587-
"first argument must be a string or open file");
588586
goto fail;
589587
}
590588
if (PyArray_ToFile(self, fd, sep, format) < 0) {

numpy/core/tests/test_multiarray.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3582,6 +3582,14 @@ def setUp(self):
35823582
def tearDown(self):
35833583
shutil.rmtree(self.tempdir)
35843584

3585+
def test_nofile(self):
3586+
# this should probably be supported as a file
3587+
# but for now test for proper errors
3588+
b = io.BytesIO()
3589+
assert_raises(IOError, np.fromfile, b, np.uint8, 80)
3590+
d = np.ones(7);
3591+
assert_raises(IOError, lambda x: x.tofile(b), d)
3592+
35853593
def test_bool_fromstring(self):
35863594
v = np.array([True, False, True, False], dtype=np.bool_)
35873595
y = np.fromstring('1 0 -2.3 0.0', sep=' ', dtype=np.bool_)

0 commit comments

Comments
 (0)
0