Kaydet (Commit) 5848d1ff authored tarafından Benjamin Peterson's avatar Benjamin Peterson

raise an OSError for invalid fds #4991

üst b6e112bd
...@@ -176,6 +176,10 @@ class OtherFileTests(unittest.TestCase): ...@@ -176,6 +176,10 @@ class OtherFileTests(unittest.TestCase):
f.close() f.close()
os.unlink(TESTFN) os.unlink(TESTFN)
def testInvalidFd(self):
self.assertRaises(ValueError, _fileio._FileIO, -10)
self.assertRaises(OSError, _fileio._FileIO, 10)
def testBadModeArgument(self): def testBadModeArgument(self):
# verify that we get a sensible error message for bad mode argument # verify that we get a sensible error message for bad mode argument
bad_mode = "qwerty" bad_mode = "qwerty"
......
...@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1 ...@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #4991: Passing invalid file descriptors to io.FileIO now raises an
OSError.
- Issue #4807: Port the _winreg module to Windows CE. - Issue #4807: Port the _winreg module to Windows CE.
- Issue #4935: The overflow checking code in the expandtabs() method common - Issue #4935: The overflow checking code in the expandtabs() method common
......
...@@ -119,6 +119,24 @@ dircheck(PyFileIOObject* self, char *name) ...@@ -119,6 +119,24 @@ dircheck(PyFileIOObject* self, char *name)
return 0; return 0;
} }
static int
check_fd(int fd)
{
#if defined(HAVE_FSTAT)
struct stat buf;
if (fstat(fd, &buf) < 0 && errno == EBADF) {
PyObject *exc;
char *msg = strerror(EBADF);
exc = PyObject_CallFunction(PyExc_OSError, "(is)",
EBADF, msg);
PyErr_SetObject(PyExc_OSError, exc);
Py_XDECREF(exc);
return -1;
}
#endif
return 0;
}
static int static int
fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
...@@ -151,6 +169,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) ...@@ -151,6 +169,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
"Negative filedescriptor"); "Negative filedescriptor");
return -1; return -1;
} }
if (check_fd(fd))
return -1;
} }
else { else {
PyErr_Clear(); PyErr_Clear();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment