#4764 set IOError.filename when trying to open a directory on POSIX platforms

This commit is contained in:
Benjamin Peterson 2008-12-29 17:47:42 +00:00
parent 732479f50b
commit fe231b07e4
3 changed files with 18 additions and 2 deletions

View File

@ -125,6 +125,19 @@ class AutoFileTests(unittest.TestCase):
class OtherFileTests(unittest.TestCase): class OtherFileTests(unittest.TestCase):
def testOpenDir(self):
this_dir = os.path.dirname(__file__)
for mode in (None, "w"):
try:
if mode:
f = open(this_dir, mode)
else:
f = open(this_dir)
except IOError as e:
self.assertEqual(e.filename, this_dir)
else:
self.fail("opening a directory didn't raise an IOError")
def testModeStrings(self): def testModeStrings(self):
# check invalid mode strings # check invalid mode strings
for mode in ("", "aU", "wU+"): for mode in ("", "aU", "wU+"):

View File

@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #4764: IOError.filename is set when trying to open a directory on POSIX
systems.
- Issue #4759: None is now allowed as the first argument of - Issue #4759: None is now allowed as the first argument of
bytearray.translate(). It was always allowed for bytes.translate(). bytearray.translate(). It was always allowed for bytes.translate().

View File

@ -132,8 +132,8 @@ dircheck(PyFileObject* f)
if (fstat(fileno(f->f_fp), &buf) == 0 && if (fstat(fileno(f->f_fp), &buf) == 0 &&
S_ISDIR(buf.st_mode)) { S_ISDIR(buf.st_mode)) {
char *msg = strerror(EISDIR); char *msg = strerror(EISDIR);
PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(is)", PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(isO)",
EISDIR, msg); EISDIR, msg, f->f_name);
PyErr_SetObject(PyExc_IOError, exc); PyErr_SetObject(PyExc_IOError, exc);
Py_XDECREF(exc); Py_XDECREF(exc);
return NULL; return NULL;