Index: Lib/test/test_array.py =================================================================== --- Lib/test/test_array.py (revision 75038) +++ Lib/test/test_array.py (working copy) @@ -8,6 +8,7 @@ from weakref import proxy import array, cStringIO from cPickle import loads, dumps, HIGHEST_PROTOCOL +import os class ArraySubclass(array.array): pass @@ -183,6 +184,16 @@ self.assertEqual(a, b) self.assertRaises(EOFError, b.fromfile, f, 1) f.close() + + # Issue #5395: Check if fromfile raises a propper IOError + # instead of EOFError. + f = open(test_support.TESTFN, 'rb') + os.close(f.fileno()) + self.assertRaises(IOError, b.fromfile, f, len(a)) + try: + f.close() + except IOError: + pass finally: if not f.closed: f.close() Index: Modules/arraymodule.c =================================================================== --- Modules/arraymodule.c (revision 75038) +++ 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; } }