summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_fileio.py4
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_fileio.c20
3 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py
index d8cf415..c089608 100644
--- a/Lib/test/test_fileio.py
+++ b/Lib/test/test_fileio.py
@@ -176,6 +176,10 @@ class OtherFileTests(unittest.TestCase):
f.close()
os.unlink(TESTFN)
+ def testInvalidFd(self):
+ self.assertRaises(ValueError, _fileio._FileIO, -10)
+ self.assertRaises(OSError, _fileio._FileIO, 10)
+
def testBadModeArgument(self):
# verify that we get a sensible error message for bad mode argument
bad_mode = "qwerty"
diff --git a/Misc/NEWS b/Misc/NEWS
index 2ed95a5..293ab4a 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1
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 #4935: The overflow checking code in the expandtabs() method common
diff --git a/Modules/_fileio.c b/Modules/_fileio.c
index 2dc3d74..0f09ecd 100644
--- a/Modules/_fileio.c
+++ b/Modules/_fileio.c
@@ -119,6 +119,24 @@ dircheck(PyFileIOObject* self, char *name)
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
fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
@@ -151,6 +169,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
"Negative filedescriptor");
return -1;
}
+ if (check_fd(fd))
+ return -1;
}
else {
PyErr_Clear();