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

This commit is contained in:
Antoine Pitrou 2009-03-13 22:33:17 +00:00
parent 652e7076fe
commit 0ae29cf617
3 changed files with 19 additions and 3 deletions

View 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):