]> granicus.if.org Git - python/commitdiff
#4764 set IOError.filename when trying to open a directory on POSIX platforms
authorBenjamin Peterson <benjamin@python.org>
Mon, 29 Dec 2008 17:47:42 +0000 (17:47 +0000)
committerBenjamin Peterson <benjamin@python.org>
Mon, 29 Dec 2008 17:47:42 +0000 (17:47 +0000)
Lib/test/test_file.py
Misc/NEWS
Objects/fileobject.c

index 96f6da202355a0b6c4c20b47b2905ebd55d69a1f..b4f494ba96a234fe4d6806db56f66a0437d5b580 100644 (file)
@@ -125,6 +125,19 @@ class AutoFileTests(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):
         # check invalid mode strings
         for mode in ("", "aU", "wU+"):
index a5b3d2da191ca010d3bcbcdeb0a95f4364677612..c315af0f5e91ccc46eecb57b6e460a16a12a3a7d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1
 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
   bytearray.translate().  It was always allowed for bytes.translate().
 
index b2051d79fe45737dce9d19e5b7d63ab179e8aed6..e01f38efdd0dc87247ac2a9d25a5852759892d43 100644 (file)
@@ -132,8 +132,8 @@ dircheck(PyFileObject* f)
        if (fstat(fileno(f->f_fp), &buf) == 0 &&
            S_ISDIR(buf.st_mode)) {
                char *msg = strerror(EISDIR);
-               PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(is)",
-                                                     EISDIR, msg);
+               PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(isO)",
+                                                     EISDIR, msg, f->f_name);
                PyErr_SetObject(PyExc_IOError, exc);
                Py_XDECREF(exc);
                return NULL;