mirror of
https://github.com/python/cpython.git
synced 2025-11-03 23:21:29 +00:00
bpo-33751: Fix test_file. (GH-7378)
testModeStrings and testTruncateOnWindows were depended on a file leaked in other tests. Also improve cleaning up after tests.
This commit is contained in:
parent
ac1ee1bada
commit
c2745d2d05
1 changed files with 79 additions and 85 deletions
|
|
@ -8,6 +8,7 @@
|
||||||
import _pyio as pyio
|
import _pyio as pyio
|
||||||
|
|
||||||
from test.support import TESTFN
|
from test.support import TESTFN
|
||||||
|
from test import support
|
||||||
from collections import UserList
|
from collections import UserList
|
||||||
|
|
||||||
class AutoFileTests:
|
class AutoFileTests:
|
||||||
|
|
@ -19,7 +20,7 @@ def setUp(self):
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
if self.f:
|
if self.f:
|
||||||
self.f.close()
|
self.f.close()
|
||||||
os.remove(TESTFN)
|
support.unlink(TESTFN)
|
||||||
|
|
||||||
def testWeakRefs(self):
|
def testWeakRefs(self):
|
||||||
# verify weak references
|
# verify weak references
|
||||||
|
|
@ -137,8 +138,12 @@ class PyAutoFileTests(AutoFileTests, unittest.TestCase):
|
||||||
|
|
||||||
class OtherFileTests:
|
class OtherFileTests:
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
support.unlink(TESTFN)
|
||||||
|
|
||||||
def testModeStrings(self):
|
def testModeStrings(self):
|
||||||
# check invalid mode strings
|
# check invalid mode strings
|
||||||
|
self.open(TESTFN, 'wb').close()
|
||||||
for mode in ("", "aU", "wU+", "U+", "+U", "rU+"):
|
for mode in ("", "aU", "wU+", "U+", "+U", "rU+"):
|
||||||
try:
|
try:
|
||||||
f = self.open(TESTFN, mode)
|
f = self.open(TESTFN, mode)
|
||||||
|
|
@ -185,7 +190,6 @@ def testTruncateOnWindows(self):
|
||||||
# SF bug <http://www.python.org/sf/801631>
|
# SF bug <http://www.python.org/sf/801631>
|
||||||
# "file.truncate fault on windows"
|
# "file.truncate fault on windows"
|
||||||
|
|
||||||
os.unlink(TESTFN)
|
|
||||||
f = self.open(TESTFN, 'wb')
|
f = self.open(TESTFN, 'wb')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -209,7 +213,6 @@ def testTruncateOnWindows(self):
|
||||||
self.fail("File size after ftruncate wrong %d" % size)
|
self.fail("File size after ftruncate wrong %d" % size)
|
||||||
finally:
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
os.unlink(TESTFN)
|
|
||||||
|
|
||||||
def testIteration(self):
|
def testIteration(self):
|
||||||
# Test the complex interaction when mixing file-iteration and the
|
# Test the complex interaction when mixing file-iteration and the
|
||||||
|
|
@ -230,7 +233,6 @@ def testIteration(self):
|
||||||
methods = [("readline", ()), ("read", ()), ("readlines", ()),
|
methods = [("readline", ()), ("read", ()), ("readlines", ()),
|
||||||
("readinto", (array("b", b" "*100),))]
|
("readinto", (array("b", b" "*100),))]
|
||||||
|
|
||||||
try:
|
|
||||||
# Prepare the testfile
|
# Prepare the testfile
|
||||||
bag = self.open(TESTFN, "wb")
|
bag = self.open(TESTFN, "wb")
|
||||||
bag.write(filler * nchunks)
|
bag.write(filler * nchunks)
|
||||||
|
|
@ -309,8 +311,6 @@ def testIteration(self):
|
||||||
self.fail("read* failed after next() consumed file")
|
self.fail("read* failed after next() consumed file")
|
||||||
finally:
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
finally:
|
|
||||||
os.unlink(TESTFN)
|
|
||||||
|
|
||||||
class COtherFileTests(OtherFileTests, unittest.TestCase):
|
class COtherFileTests(OtherFileTests, unittest.TestCase):
|
||||||
open = io.open
|
open = io.open
|
||||||
|
|
@ -319,11 +319,5 @@ class PyOtherFileTests(OtherFileTests, unittest.TestCase):
|
||||||
open = staticmethod(pyio.open)
|
open = staticmethod(pyio.open)
|
||||||
|
|
||||||
|
|
||||||
def tearDownModule():
|
|
||||||
# Historically, these tests have been sloppy about removing TESTFN.
|
|
||||||
# So get rid of it no matter what.
|
|
||||||
if os.path.exists(TESTFN):
|
|
||||||
os.unlink(TESTFN)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue