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

antoine.pitrou python-checkins at python.org
Fri Mar 13 23:33:17 CET 2009


Author: antoine.pitrou
Date: Fri Mar 13 23:33:17 2009
New Revision: 70349

Log:
The error detection code in FileIO.close() could fail to reflect the `errno` value, and report it as -1 instead.




Modified:
   python/branches/py3k/Lib/test/test_fileio.py
   python/branches/py3k/Misc/NEWS
   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	Fri Mar 13 23:33:17 2009
@@ -2,6 +2,7 @@
 
 import sys
 import os
+import errno
 import unittest
 from array import array
 from weakref import proxy
@@ -113,6 +114,20 @@
         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):
 

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Fri Mar 13 23:33:17 2009
@@ -18,6 +18,9 @@
 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
 ================================

Modified: python/branches/py3k/Modules/_fileio.c
==============================================================================
--- python/branches/py3k/Modules/_fileio.c	(original)
+++ python/branches/py3k/Modules/_fileio.c	Fri Mar 13 23:33:17 2009
@@ -97,10 +97,8 @@
 		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);


More information about the Python-checkins mailing list