]> granicus.if.org Git - python/commitdiff
#3703 unhelpful _fileio.FileIO error message when trying to open a directory
authorBenjamin Peterson <benjamin@python.org>
Mon, 1 Sep 2008 14:13:43 +0000 (14:13 +0000)
committerBenjamin Peterson <benjamin@python.org>
Mon, 1 Sep 2008 14:13:43 +0000 (14:13 +0000)
Reviewer: Gregory P. Smith

Lib/test/test_fileio.py
Misc/NEWS
Modules/_fileio.c

index a8b15fa1f843743f3497a6e77ecce4955c40ff39..5a0e1a6b34ec7a288ed8c430207f7d64cfe5e333 100644 (file)
@@ -101,6 +101,17 @@ class AutoFileTests(unittest.TestCase):
             # should raise on closed file
             self.assertRaises(ValueError, method)
 
+    def testOpendir(self):
+        # Issue 3703: opening a directory should fill the errno
+        # Windows always returns "[Errno 13]: Permission denied
+        # Unix calls dircheck() and returns "[Errno 21]: Is a directory"
+        try:
+            _fileio._FileIO('.', 'r')
+        except IOError as e:
+            self.assertNotEqual(e.errno, 0)
+        else:
+            self.fail("Should have raised IOError")
+
 
 class OtherFileTests(unittest.TestCase):
 
index c41f10cfd9513feb026e3dbfe70e5ec2814bdb9d..5ccce4b3baf31d0ec65b72e0741f88b62a5ed5bd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -52,6 +52,9 @@ Library
 
 - Fixed two format strings in the _collections module.
 
+- #3703 _fileio.FileIO gave unhelpful error message when trying to open a
+   directory.
+
 Extension Modules
 -----------------
 
index d6f004f66182bd797593da77f5146f0b71d9e1b2..c3d61b440e90a045cf6534fee81f54abe39a32ea 100644 (file)
@@ -262,7 +262,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
 #endif
                        self->fd = open(name, flags, 0666);
                Py_END_ALLOW_THREADS
-               if (self->fd < 0 || dircheck(self) < 0) {
+               if (self->fd < 0) {
 #ifdef MS_WINDOWS
                        PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename);
 #else
@@ -270,6 +270,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
 #endif
                        goto error;
                }
+               if(dircheck(self) < 0)
+                       goto error;
        }
 
        goto done;