]> granicus.if.org Git - python/commitdiff
raise an OSError for invalid fds #4991
authorBenjamin Peterson <benjamin@python.org>
Mon, 19 Jan 2009 00:08:08 +0000 (00:08 +0000)
committerBenjamin Peterson <benjamin@python.org>
Mon, 19 Jan 2009 00:08:08 +0000 (00:08 +0000)
Lib/test/test_fileio.py
Misc/NEWS
Modules/_fileio.c

index d8cf415ed868748cdd85b8b1e19ca32655372e5f..c089608d3c948fff8e996135c4fdde6d9a852966 100644 (file)
@@ -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"
index 2ed95a5ec516a39596ac5c4bc31f95c6050e9dd7..293ab4aa2e2b43831876553af302fa65d583d809 100644 (file)
--- 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
index 2dc3d743b85f8279c525df2af15cebcc02d74d4e..0f09ecd8a3b0df09138c8f7084fdca9ece0cb86e 100644 (file)
@@ -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();