[Python-checkins] r68755 - in python/trunk: Lib/test/test_fileio.py Misc/NEWS Modules/_fileio.c

benjamin.peterson python-checkins at python.org
Mon Jan 19 01:08:08 CET 2009


Author: benjamin.peterson
Date: Mon Jan 19 01:08:08 2009
New Revision: 68755

Log:
raise an OSError for invalid fds #4991

Modified:
   python/trunk/Lib/test/test_fileio.py
   python/trunk/Misc/NEWS
   python/trunk/Modules/_fileio.c

Modified: python/trunk/Lib/test/test_fileio.py
==============================================================================
--- python/trunk/Lib/test/test_fileio.py	(original)
+++ python/trunk/Lib/test/test_fileio.py	Mon Jan 19 01:08:08 2009
@@ -176,6 +176,10 @@
         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"

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Mon Jan 19 01:08:08 2009
@@ -12,6 +12,9 @@
 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

Modified: python/trunk/Modules/_fileio.c
==============================================================================
--- python/trunk/Modules/_fileio.c	(original)
+++ python/trunk/Modules/_fileio.c	Mon Jan 19 01:08:08 2009
@@ -119,6 +119,24 @@
 	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 @@
 					"Negative filedescriptor");
 			return -1;
 		}
+		if (check_fd(fd))
+			return -1;
 	}
 	else {
 		PyErr_Clear();


More information about the Python-checkins mailing list