[Python-checkins] cpython (merge 3.2 -> default): Issue #15247: FileIO now raises an error when given a file descriptor pointing
antoine.pitrou
python-checkins at python.org
Fri Jul 6 19:02:34 CEST 2012
http://hg.python.org/cpython/rev/19bd61ed3b3b
changeset: 77955:19bd61ed3b3b
parent: 77953:30e8f2242190
parent: 77954:9cf9527358a5
user: Antoine Pitrou <solipsis at pitrou.net>
date: Fri Jul 06 18:52:58 2012 +0200
summary:
Issue #15247: FileIO now raises an error when given a file descriptor pointing to a directory.
files:
Lib/test/test_fileio.py | 8 ++++++++
Misc/NEWS | 3 +++
Modules/_io/fileio.c | 17 +++++------------
3 files changed, 16 insertions(+), 12 deletions(-)
diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py
--- a/Lib/test/test_fileio.py
+++ b/Lib/test/test_fileio.py
@@ -128,6 +128,14 @@
else:
self.fail("Should have raised IOError")
+ @unittest.skipIf(os.name == 'nt', "test only works on a POSIX-like system")
+ def testOpenDirFD(self):
+ fd = os.open('.', os.O_RDONLY)
+ with self.assertRaises(IOError) as cm:
+ _FileIO(fd, 'r')
+ os.close(fd)
+ self.assertEqual(cm.exception.errno, errno.EISDIR)
+
#A set of functions testing that we get expected behaviour if someone has
#manually closed the internal file descriptor. First, a decorator:
def ClosedFD(func):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -23,6 +23,9 @@
Library
-------
+- Issue #15247: FileIO now raises an error when given a file descriptor
+ pointing to a directory.
+
- Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
- Issue #15166: Implement imp.get_tag() using sys.implementation.cache_tag.
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -169,22 +169,15 @@
directories, so we need a check. */
static int
-dircheck(fileio* self, const char *name)
+dircheck(fileio* self, PyObject *nameobj)
{
#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
struct stat buf;
if (self->fd < 0)
return 0;
if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
- char *msg = strerror(EISDIR);
- PyObject *exc;
- if (internal_close(self))
- return -1;
-
- exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
- EISDIR, msg, name);
- PyErr_SetObject(PyExc_IOError, exc);
- Py_XDECREF(exc);
+ errno = EISDIR;
+ PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
return -1;
}
#endif
@@ -406,9 +399,9 @@
PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
goto error;
}
- if (dircheck(self, name) < 0)
- goto error;
}
+ if (dircheck(self, nameobj) < 0)
+ goto error;
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
/* don't translate newlines (\r\n <=> \n) */
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list