diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-06-08 20:24:19 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-08 20:24:19 +0300 |
commit | 23999158f962e31e955f3cd232b3745d523720cb (patch) | |
tree | ee60e651a526989be2fb18ee028a86826c198500 /Lib | |
parent | 02b4d67a38b0c17d87749eeefc51e91baef10857 (diff) | |
download | cpython-git-23999158f962e31e955f3cd232b3745d523720cb.tar.gz |
[2.7] bpo-33751: Fix test_file. (GH-7378) (GH-7445)
testModeStrings and testTruncateOnWindows were depended on
a file leaked in other tests.
Also improve cleaning up after tests.
(cherry picked from commit c2745d2d05546d76f655ab450eb23d1af39e0b1c)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_file.py | 168 |
1 files changed, 81 insertions, 87 deletions
diff --git a/Lib/test/test_file.py b/Lib/test/test_file.py index 1fd7910d15..57e640d336 100644 --- a/Lib/test/test_file.py +++ b/Lib/test/test_file.py @@ -12,7 +12,8 @@ from weakref import proxy import io import _pyio as pyio -from test.test_support import TESTFN, run_unittest +from test.support import TESTFN, run_unittest +from test import support from UserList import UserList class AutoFileTests(unittest.TestCase): @@ -24,7 +25,7 @@ class AutoFileTests(unittest.TestCase): def tearDown(self): if self.f: self.f.close() - os.remove(TESTFN) + support.unlink(TESTFN) def testWeakRefs(self): # verify weak references @@ -143,8 +144,12 @@ class PyAutoFileTests(AutoFileTests): class OtherFileTests(unittest.TestCase): + def tearDown(self): + support.unlink(TESTFN) + def testModeStrings(self): # check invalid mode strings + self.open(TESTFN, 'wb').close() for mode in ("", "aU", "wU+"): try: f = self.open(TESTFN, mode) @@ -191,7 +196,6 @@ class OtherFileTests(unittest.TestCase): # SF bug <http://www.python.org/sf/801631> # "file.truncate fault on windows" - os.unlink(TESTFN) f = self.open(TESTFN, 'wb') try: @@ -215,7 +219,6 @@ class OtherFileTests(unittest.TestCase): self.fail("File size after ftruncate wrong %d" % size) finally: f.close() - os.unlink(TESTFN) def testIteration(self): # Test the complex interaction when mixing file-iteration and the @@ -236,86 +239,83 @@ class OtherFileTests(unittest.TestCase): methods = [("readline", ()), ("read", ()), ("readlines", ()), ("readinto", (array("b", b" "*100),))] - try: - # Prepare the testfile - bag = self.open(TESTFN, "wb") - bag.write(filler * nchunks) - bag.writelines(testlines) - bag.close() - # Test for appropriate errors mixing read* and iteration - for methodname, args in methods: - f = self.open(TESTFN, 'rb') - if next(f) != filler: - self.fail, "Broken testfile" - meth = getattr(f, methodname) - meth(*args) # This simply shouldn't fail - 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). + # Prepare the testfile + bag = self.open(TESTFN, "wb") + bag.write(filler * nchunks) + bag.writelines(testlines) + bag.close() + # Test for appropriate errors mixing read* and iteration + for methodname, args in methods: f = self.open(TESTFN, 'rb') - for i in range(nchunks): - next(f) - testline = testlines.pop(0) - try: - line = f.readline() - except ValueError: - self.fail("readline() after next() with supposedly empty " - "iteration-buffer failed anyway") - if line != testline: - self.fail("readline() after next() with empty buffer " - "failed. Got %r, expected %r" % (line, testline)) - testline = testlines.pop(0) - buf = array("b", b"\x00" * len(testline)) + if next(f) != filler: + self.fail, "Broken testfile" + meth = getattr(f, methodname) + meth(*args) # This simply shouldn't fail + 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). + f = self.open(TESTFN, 'rb') + for i in range(nchunks): + next(f) + testline = testlines.pop(0) + try: + line = f.readline() + except ValueError: + self.fail("readline() after next() with supposedly empty " + "iteration-buffer failed anyway") + if line != testline: + self.fail("readline() after next() with empty buffer " + "failed. Got %r, expected %r" % (line, testline)) + testline = testlines.pop(0) + buf = array("b", b"\x00" * len(testline)) + try: + f.readinto(buf) + except ValueError: + self.fail("readinto() after next() with supposedly empty " + "iteration-buffer failed anyway") + line = buf.tostring() + if line != testline: + self.fail("readinto() after next() with empty buffer " + "failed. Got %r, expected %r" % (line, testline)) + + testline = testlines.pop(0) + try: + line = f.read(len(testline)) + except ValueError: + self.fail("read() after next() with supposedly empty " + "iteration-buffer failed anyway") + if line != testline: + self.fail("read() after next() with empty buffer " + "failed. Got %r, expected %r" % (line, testline)) + try: + lines = f.readlines() + except ValueError: + self.fail("readlines() after next() with supposedly empty " + "iteration-buffer failed anyway") + if lines != testlines: + self.fail("readlines() after next() with empty buffer " + "failed. Got %r, expected %r" % (line, testline)) + # Reading after iteration hit EOF shouldn't hurt either + f.close() + f = self.open(TESTFN, 'rb') + try: + for line in f: + pass try: + f.readline() f.readinto(buf) + f.read() + f.readlines() except ValueError: - self.fail("readinto() after next() with supposedly empty " - "iteration-buffer failed anyway") - line = buf.tostring() - if line != testline: - self.fail("readinto() after next() with empty buffer " - "failed. Got %r, expected %r" % (line, testline)) - - testline = testlines.pop(0) - try: - line = f.read(len(testline)) - except ValueError: - self.fail("read() after next() with supposedly empty " - "iteration-buffer failed anyway") - if line != testline: - self.fail("read() after next() with empty buffer " - "failed. Got %r, expected %r" % (line, testline)) - try: - lines = f.readlines() - except ValueError: - self.fail("readlines() after next() with supposedly empty " - "iteration-buffer failed anyway") - if lines != testlines: - self.fail("readlines() after next() with empty buffer " - "failed. Got %r, expected %r" % (line, testline)) - # Reading after iteration hit EOF shouldn't hurt either - f.close() - f = self.open(TESTFN, 'rb') - try: - for line in f: - pass - try: - f.readline() - f.readinto(buf) - f.read() - f.readlines() - except ValueError: - self.fail("read* failed after next() consumed file") - finally: - f.close() + self.fail("read* failed after next() consumed file") finally: - os.unlink(TESTFN) + f.close() class COtherFileTests(OtherFileTests): open = io.open @@ -325,14 +325,8 @@ class PyOtherFileTests(OtherFileTests): def test_main(): - # Historically, these tests have been sloppy about removing TESTFN. - # So get rid of it no matter what. - try: - run_unittest(CAutoFileTests, PyAutoFileTests, - COtherFileTests, PyOtherFileTests) - finally: - if os.path.exists(TESTFN): - os.unlink(TESTFN) + run_unittest(CAutoFileTests, PyAutoFileTests, + COtherFileTests, PyOtherFileTests) if __name__ == '__main__': test_main() |