cpython: 9cf9527358a5 (original) (raw)
Mercurial > cpython
changeset 77954:9cf9527358a5 3.2
Issue #15247: FileIO now raises an error when given a file descriptor pointing to a directory. [#15247]
Antoine Pitrou solipsis@pitrou.net | |
---|---|
date | Fri, 06 Jul 2012 18:48:24 +0200 |
parents | c97d78415f5a |
children | 19bd61ed3b3b 5020afc0b7c9 |
files | Lib/test/test_fileio.py Misc/NEWS Modules/_io/fileio.c |
diffstat | 3 files changed, 16 insertions(+), 12 deletions(-)[+] [-] Lib/test/test_fileio.py 8 Misc/NEWS 3 Modules/_io/fileio.c 17 |
line wrap: on
line diff
--- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -127,6 +127,14 @@ class AutoFileTests(unittest.TestCase): 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)[](#l1.9)
with self.assertRaises(IOError) as cm:[](#l1.10)
_FileIO(fd, 'r')[](#l1.11)
os.close(fd)[](#l1.12)
self.assertEqual(cm.exception.errno, errno.EISDIR)[](#l1.13)
+ #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):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -87,6 +87,9 @@ Core and Builtins Library ------- +- Issue #15247: FileIO now raises an error when given a file descriptor
--- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -166,22 +166,15 @@ fileio_new(PyTypeObject *type, PyObject 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);[](#l3.15)
PyObject *exc;[](#l3.16)
if (internal_close(self))[](#l3.17)
return -1;[](#l3.18)
exc = PyObject_CallFunction(PyExc_IOError, "(iss)",[](#l3.20)
EISDIR, msg, name);[](#l3.21)
PyErr_SetObject(PyExc_IOError, exc);[](#l3.22)
Py_XDECREF(exc);[](#l3.23)
errno = EISDIR;[](#l3.24)
}PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);[](#l3.25) return -1;[](#l3.26)
#endif @@ -373,9 +366,9 @@ fileio_init(PyObject *oself, PyObject *a PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); goto error; }
#if defined(MS_WINDOWS) || defined(CYGWIN) /* don't translate newlines (\r\n <=> \n) */