[Python-checkins] r68767 - in python/branches/py3k: Lib/test/test_fileio.py Modules/_fileio.c

benjamin.peterson python-checkins at python.org
Mon Jan 19 16:11:52 CET 2009


Author: benjamin.peterson
Date: Mon Jan 19 16:11:51 2009
New Revision: 68767

Log:
Merged revisions 68755 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r68755 | benjamin.peterson | 2009-01-18 18:08:08 -0600 (Sun, 18 Jan 2009) | 1 line
  
  raise an OSError for invalid fds #4991
........


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Lib/test/test_fileio.py
   python/branches/py3k/Modules/_fileio.c

Modified: python/branches/py3k/Lib/test/test_fileio.py
==============================================================================
--- python/branches/py3k/Lib/test/test_fileio.py	(original)
+++ python/branches/py3k/Lib/test/test_fileio.py	Mon Jan 19 16:11:51 2009
@@ -175,6 +175,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/branches/py3k/Modules/_fileio.c
==============================================================================
--- python/branches/py3k/Modules/_fileio.c	(original)
+++ python/branches/py3k/Modules/_fileio.c	Mon Jan 19 16:11:51 2009
@@ -138,6 +138,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)
@@ -170,6 +188,8 @@
 					"Negative filedescriptor");
 			return -1;
 		}
+		if (check_fd(fd))
+			return -1;
 	}
 	else {
 		PyErr_Clear();


More information about the Python-checkins mailing list