]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 68767,68769-68770 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Mon, 19 Jan 2009 15:35:29 +0000 (15:35 +0000)
committerBenjamin Peterson <benjamin@python.org>
Mon, 19 Jan 2009 15:35:29 +0000 (15:35 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r68767 | benjamin.peterson | 2009-01-19 09:11:51 -0600 (Mon, 19 Jan 2009) | 9 lines

  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
  ........
................
  r68769 | benjamin.peterson | 2009-01-19 09:15:02 -0600 (Mon, 19 Jan 2009) | 1 line

  reenable the invalid fd test for fdopen
................
  r68770 | benjamin.peterson | 2009-01-19 09:19:46 -0600 (Mon, 19 Jan 2009) | 1 line

  fix compiler warning
................

Lib/test/test_fileio.py
Misc/NEWS
Modules/_fileio.c

index 817103ed9973dc299f6e0c605e3b5882211211cc..69aa3fd23c0f359b43bc014f13059f2a775077e0 100644 (file)
@@ -175,6 +175,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 43ab22426f3f6f7bf799d0d7c294530f2daad50e..c9235783299d4689b9c6b79b6329e3975dc48dc7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 3.0.1?
 Core and Builtins
 -----------------
 
+- Issue #4991: os.fdopen now raises an OSError for invalid file descriptors.
+
 - Issue #4838: When a module is deallocated, free the memory backing the
   optional module state data.
 
index 8579e4e60bdb0568e0e36519bffe974a89e7ea5d..22b473c513905dab6c678f1d0c8ebb829ff89917 100644 (file)
@@ -60,7 +60,7 @@ static int
 internal_close(PyFileIOObject *self)
 {
        int err = 0;
-       int save_errno;
+       int save_errno = 0;
        if (self->fd >= 0) {
                int fd = self->fd;
                self->fd = -1;
@@ -138,6 +138,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)
@@ -170,6 +188,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
                                        "Negative filedescriptor");
                        return -1;
                }
+               if (check_fd(fd))
+                       return -1;
        }
        else {
                PyErr_Clear();