diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2009-03-13 22:33:17 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2009-03-13 22:33:17 (GMT) |
commit | 0ae29cf6176d3f80c1845cf23716a708acbe598b (patch) | |
tree | f6f6463670ede6c7e4ab0c15867bfe6c0764e3d8 | |
parent | 652e7076fee59d92d19a0d6e326b9069a2aa09e4 (diff) | |
download | cpython-0ae29cf6176d3f80c1845cf23716a708acbe598b.zip cpython-0ae29cf6176d3f80c1845cf23716a708acbe598b.tar.gz cpython-0ae29cf6176d3f80c1845cf23716a708acbe598b.tar.bz2 |
The error detection code in FileIO.close() could fail to reflect the `errno` value, and report it as -1 instead.
-rw-r--r-- | Lib/test/test_fileio.py | 15 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Modules/_fileio.c | 4 |
3 files changed, 19 insertions, 3 deletions
diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index 498d3fc..9f94053 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -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): @@ -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 ================================ diff --git a/Modules/_fileio.c b/Modules/_fileio.c index fd35d69..32f6790 100644 --- a/Modules/_fileio.c +++ b/Modules/_fileio.c @@ -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); |