diff options
Diffstat (limited to 'Lib/test/test_file.py')
-rw-r--r-- | Lib/test/test_file.py | 78 |
1 files changed, 48 insertions, 30 deletions
diff --git a/Lib/test/test_file.py b/Lib/test/test_file.py index dcfa265508d..234920d0733 100644 --- a/Lib/test/test_file.py +++ b/Lib/test/test_file.py @@ -11,14 +11,12 @@ class AutoFileTests(unittest.TestCase): # file tests for which a test file is automatically set up def setUp(self): - self.f = file(TESTFN, 'wb') + self.f = open(TESTFN, 'wb') def tearDown(self): - try: - if self.f: - self.f.close() - except IOError: - pass + if self.f: + self.f.close() + os.remove(TESTFN) def testWeakRefs(self): # verify weak references @@ -80,9 +78,11 @@ class AutoFileTests(unittest.TestCase): def testWritelinesNonString(self): # verify writelines with non-string object - class NonString: pass + class NonString: + pass - self.assertRaises(TypeError, self.f.writelines, [NonString(), NonString()]) + self.assertRaises(TypeError, self.f.writelines, + [NonString(), NonString()]) def testRepr(self): # verify repr works @@ -93,19 +93,21 @@ class AutoFileTests(unittest.TestCase): self.assertEquals(f.name, TESTFN) self.assert_(not f.isatty()) self.assert_(not f.closed) - + self.assertRaises(TypeError, f.readinto, "") f.close() self.assert_(f.closed) def testMethods(self): methods = ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto', - 'readline', 'readlines', 'seek', 'tell', 'truncate', 'write', - '__iter__'] + 'readline', 'readlines', 'seek', 'tell', 'truncate', + 'write', '__iter__'] if sys.platform.startswith('atheos'): methods.remove('truncate') - self.f.close() + # __exit__ should close the file + self.f.__exit__(None, None, None) + self.assert_(self.f.closed) for methodname in methods: method = getattr(self.f, methodname) @@ -113,6 +115,14 @@ class AutoFileTests(unittest.TestCase): self.assertRaises(ValueError, method) self.assertRaises(ValueError, self.f.writelines, []) + # file is closed, __exit__ shouldn't do anything + self.assertEquals(self.f.__exit__(None, None, None), None) + # it must also return None if an exception was given + try: + 1/0 + except: + self.assertEquals(self.f.__exit__(*sys.exc_info()), None) + class OtherFileTests(unittest.TestCase): @@ -120,7 +130,7 @@ class OtherFileTests(unittest.TestCase): # check invalid mode strings for mode in ("", "aU", "wU+"): try: - f = file(TESTFN, mode) + f = open(TESTFN, mode) except ValueError: pass else: @@ -142,6 +152,7 @@ class OtherFileTests(unittest.TestCase): f = open(unicode(TESTFN), "w") self.assert_(repr(f).startswith("<open file u'" + TESTFN)) f.close() + os.unlink(TESTFN) def testBadModeArgument(self): # verify that we get a sensible error message for bad mode argument @@ -182,11 +193,11 @@ class OtherFileTests(unittest.TestCase): def bug801631(): # SF bug <http://www.python.org/sf/801631> # "file.truncate fault on windows" - f = file(TESTFN, 'wb') + f = open(TESTFN, 'wb') f.write('12345678901') # 11 bytes f.close() - f = file(TESTFN,'rb+') + f = open(TESTFN,'rb+') data = f.read(5) if data != '12345': self.fail("Read on file opened for update failed %r" % data) @@ -208,14 +219,14 @@ class OtherFileTests(unittest.TestCase): os.unlink(TESTFN) def testIteration(self): - # Test the complex interaction when mixing file-iteration and the various - # read* methods. Ostensibly, the mixture could just be tested to work - # when it should work according to the Python language, instead of fail - # when it should fail according to the current CPython implementation. - # People don't always program Python the way they should, though, and the - # implemenation might change in subtle ways, so we explicitly test for - # errors, too; the test will just have to be updated when the - # implementation changes. + # Test the complex interaction when mixing file-iteration and the + # various read* methods. Ostensibly, the mixture could just be tested + # to work when it should work according to the Python language, + # instead of fail when it should fail according to the current CPython + # implementation. People don't always program Python the way they + # should, though, and the implemenation might change in subtle ways, + # so we explicitly test for errors, too; the test will just have to + # be updated when the implementation changes. dataoffset = 16384 filler = "ham\n" assert not dataoffset % len(filler), \ @@ -253,12 +264,13 @@ class OtherFileTests(unittest.TestCase): (methodname, args)) f.close() - # Test to see if harmless (by accident) mixing of read* and iteration - # still works. This depends on the size of the internal iteration - # buffer (currently 8192,) but we can test it in a flexible manner. - # Each line in the bag o' ham is 4 bytes ("h", "a", "m", "\n"), so - # 4096 lines of that should get us exactly on the buffer boundary for - # any power-of-2 buffersize between 4 and 16384 (inclusive). + # Test to see if harmless (by accident) mixing of read* and + # iteration still works. This depends on the size of the internal + # iteration buffer (currently 8192,) but we can test it in a + # flexible manner. Each line in the bag o' ham is 4 bytes + # ("h", "a", "m", "\n"), so 4096 lines of that should get us + # exactly on the buffer boundary for any power-of-2 buffersize + # between 4 and 16384 (inclusive). f = open(TESTFN, 'rb') for i in range(nchunks): f.next() @@ -319,7 +331,13 @@ class OtherFileTests(unittest.TestCase): def test_main(): - run_unittest(AutoFileTests, OtherFileTests) + # Historically, these tests have been sloppy about removing TESTFN. + # So get rid of it no matter what. + try: + run_unittest(AutoFileTests, OtherFileTests) + finally: + if os.path.exists(TESTFN): + os.unlink(TESTFN) if __name__ == '__main__': test_main() |