From: Benjamin Peterson Date: Mon, 1 Sep 2008 14:13:43 +0000 (+0000) Subject: #3703 unhelpful _fileio.FileIO error message when trying to open a directory X-Git-Tag: v2.6rc1~101 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f22c26ecf4c28e16d262cdb1305411286fb925c9;p=thirdparty%2FPython%2Fcpython.git #3703 unhelpful _fileio.FileIO error message when trying to open a directory Reviewer: Gregory P. Smith --- diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index a8b15fa1f843..5a0e1a6b34ec 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -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): diff --git a/Misc/NEWS b/Misc/NEWS index c41f10cfd951..5ccce4b3baf3 100644 --- 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 ----------------- diff --git a/Modules/_fileio.c b/Modules/_fileio.c index d6f004f66182..c3d61b440e90 100644 --- a/Modules/_fileio.c +++ b/Modules/_fileio.c @@ -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;