]> granicus.if.org Git - python/commitdiff
The error detection code in FileIO.close() could fail to reflect the `errno` value...
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 13 Mar 2009 22:33:17 +0000 (22:33 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 13 Mar 2009 22:33:17 +0000 (22:33 +0000)
Lib/test/test_fileio.py
Misc/NEWS
Modules/_fileio.c

index 498d3fc5a52a4f865fb5c34fd8406486e6501432..9f94053e47cef6f0b4aac57e853d40702678f037 100644 (file)
@@ -2,6 +2,7 @@
 
 import sys
 import os
+import errno
 import unittest
 from array import array
 from weakref import proxy
@@ -113,6 +114,20 @@ class AutoFileTests(unittest.TestCase):
         else:
             self.fail("Should have raised IOError")
 
+    def testErrnoOnClose(self):
+        # Test that the IOError's `errno` attribute is correctly set when
+        # close() fails. Here we first close the file descriptor ourselves so
+        # that close() fails with EBADF ('Bad file descriptor').
+        f = self.f
+        os.close(f.fileno())
+        self.f = None
+        try:
+            f.close()
+        except IOError as e:
+            self.assertEqual(e.errno, errno.EBADF)
+        else:
+            self.fail("Should have raised IOError")
+
 
 class OtherFileTests(unittest.TestCase):
 
index 4468df6f1c791c346f1ed571d00f370e14a8ad5d..21b4a9b993f904e059d20d1061bf56faac981825 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,9 @@ Core and Builtins
 Library
 -------
 
+- The error detection code in FileIO.close() could fail to reflect the `errno`
+  value, and report it as -1 instead.
+
 
 What's New in Python 3.1 alpha 1
 ================================
index fd35d69879d4150af47e9f0a785241f05cdf499b..32f679007aae3b998b8185b54e2526510950c3b7 100644 (file)
@@ -97,10 +97,8 @@ fileio_close(PyFileIOObject *self)
                Py_RETURN_NONE;
        }
        errno = internal_close(self);
-       if (errno < 0) {
-               PyErr_SetFromErrno(PyExc_IOError);
+       if (errno < 0)
                return NULL;
-       }
 
        return PyObject_CallMethod((PyObject*)&PyRawIOBase_Type,
                                   "close", "O", self);