mirror of https://github.com/python/cpython.git
Issue #15247: FileIO now raises an error when given a file descriptor pointing to a directory.
This commit is contained in:
parent
01cca5e451
commit
9235b254dc
|
@ -127,6 +127,14 @@ def testOpendir(self):
|
||||||
else:
|
else:
|
||||||
self.fail("Should have raised IOError")
|
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
|
#A set of functions testing that we get expected behaviour if someone has
|
||||||
#manually closed the internal file descriptor. First, a decorator:
|
#manually closed the internal file descriptor. First, a decorator:
|
||||||
def ClosedFD(func):
|
def ClosedFD(func):
|
||||||
|
|
|
@ -87,6 +87,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #15247: FileIO now raises an error when given a file descriptor
|
||||||
|
pointing to a directory.
|
||||||
|
|
||||||
- Issue #5346: Preserve permissions of mbox, MMDF and Babyl mailbox
|
- Issue #5346: Preserve permissions of mbox, MMDF and Babyl mailbox
|
||||||
files on flush().
|
files on flush().
|
||||||
|
|
||||||
|
|
|
@ -166,22 +166,15 @@ fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
directories, so we need a check. */
|
directories, so we need a check. */
|
||||||
|
|
||||||
static int
|
static int
|
||||||
dircheck(fileio* self, const char *name)
|
dircheck(fileio* self, PyObject *nameobj)
|
||||||
{
|
{
|
||||||
#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
|
#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
|
||||||
struct stat buf;
|
struct stat buf;
|
||||||
if (self->fd < 0)
|
if (self->fd < 0)
|
||||||
return 0;
|
return 0;
|
||||||
if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
|
if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
|
||||||
char *msg = strerror(EISDIR);
|
errno = EISDIR;
|
||||||
PyObject *exc;
|
PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
|
||||||
if (internal_close(self))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
|
|
||||||
EISDIR, msg, name);
|
|
||||||
PyErr_SetObject(PyExc_IOError, exc);
|
|
||||||
Py_XDECREF(exc);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -373,9 +366,9 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
|
||||||
PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
|
PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if (dircheck(self, name) < 0)
|
|
||||||
goto error;
|
|
||||||
}
|
}
|
||||||
|
if (dircheck(self, nameobj) < 0)
|
||||||
|
goto error;
|
||||||
|
|
||||||
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
|
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
|
||||||
/* don't translate newlines (\r\n <=> \n) */
|
/* don't translate newlines (\r\n <=> \n) */
|
||||||
|
|
Loading…
Reference in New Issue