Index: Lib/test/test_array.py =================================================================== --- Lib/test/test_array.py (revision 75254) +++ Lib/test/test_array.py (working copy) @@ -188,6 +188,30 @@ f.close() test_support.unlink(test_support.TESTFN) + def test_fromfile_ioerror(self): + # Issue #5395: Check if fromfile raises a proper IOError + # instead of EOFError. + import os + a = array.array(self.typecode, self.example) + f = open(test_support.TESTFN, 'wb') + try: + f.write(a) + f.close() + b = array.array(self.typecode) + f = open(test_support.TESTFN, 'rb') + # Close only the file descriptor, so f seems still open. + os.close(f.fileno()) + self.assertRaises(IOError, b.fromfile, f, len(a)) + finally: + if not f.closed: + try: + f.close() + except IOError: + # In case the file descriptor has already been closed + # f.close() fails. + pass + test_support.unlink(test_support.TESTFN) + def test_filewrite(self): a = array.array(self.typecode, 2*self.example) f = open(test_support.TESTFN, 'wb') Index: Modules/arraymodule.c =================================================================== --- Modules/arraymodule.c (revision 75254) +++ Modules/arraymodule.c (working copy) @@ -1228,8 +1228,14 @@ PyMem_RESIZE(item, char, Py_SIZE(self)*itemsize); self->ob_item = item; self->allocated = Py_SIZE(self); - PyErr_SetString(PyExc_EOFError, - "not enough items in file"); + if (ferror(fp)) { + PyErr_SetFromErrno(PyExc_IOError); + clearerr(fp); + } + else { + PyErr_SetString(PyExc_EOFError, + "not enough items in file"); + } return NULL; } }