diff --git a/Lib/test/test_difflib.py b/Lib/test/test_difflib.py index 8425a208866..c314b537667 100644 --- a/Lib/test/test_difflib.py +++ b/Lib/test/test_difflib.py @@ -135,14 +135,13 @@ class TestSFpatches(unittest.TestCase): k.make_table(f3.splitlines(True),t3.splitlines(True)), ]) actual = full.replace('','\n%s\n' % tables) - # temporarily uncomment next three lines to baseline this test - #f = open('test_difflib_expect.html','w') - #f.write(actual) - #f.close() - expect = open(findfile('test_difflib_expect.html')).read() + # temporarily uncomment next two lines to baseline this test + #with open('test_difflib_expect.html','w') as fp: + # fp.write(actual) - self.assertEqual(actual,expect) + with open(findfile('test_difflib_expect.html')) as fp: + self.assertEqual(actual, fp.read()) def test_recursion_limit(self): # Check if the problem described in patch #1413711 exists. diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 26ec953e034..37be2057571 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -175,7 +175,8 @@ class GetSourceBase(unittest.TestCase): def __init__(self, *args, **kwargs): unittest.TestCase.__init__(self, *args, **kwargs) - self.source = open(inspect.getsourcefile(self.fodderFile)).read() + with open(inspect.getsourcefile(self.fodderFile)) as fp: + self.source = fp.read() def sourcerange(self, top, bottom): lines = self.source.split("\n") diff --git a/Lib/test/test_univnewlines.py b/Lib/test/test_univnewlines.py index 39f55198b25..183ae759d91 100644 --- a/Lib/test/test_univnewlines.py +++ b/Lib/test/test_univnewlines.py @@ -37,12 +37,11 @@ class TestGenericUnivNewlines(unittest.TestCase): WRITEMODE = 'wb' def setUp(self): - fp = self.open(support.TESTFN, self.WRITEMODE) data = self.DATA if "b" in self.WRITEMODE: data = data.encode("ascii") - fp.write(data) - fp.close() + with self.open(support.TESTFN, self.WRITEMODE) as fp: + fp.write(data) def tearDown(self): try: @@ -51,35 +50,35 @@ class TestGenericUnivNewlines(unittest.TestCase): pass def test_read(self): - fp = self.open(support.TESTFN, self.READMODE) - data = fp.read() + with self.open(support.TESTFN, self.READMODE) as fp: + data = fp.read() self.assertEqual(data, DATA_LF) self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) def test_readlines(self): - fp = self.open(support.TESTFN, self.READMODE) - data = fp.readlines() + with self.open(support.TESTFN, self.READMODE) as fp: + data = fp.readlines() self.assertEqual(data, DATA_SPLIT) self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) def test_readline(self): - fp = self.open(support.TESTFN, self.READMODE) - data = [] - d = fp.readline() - while d: - data.append(d) + with self.open(support.TESTFN, self.READMODE) as fp: + data = [] d = fp.readline() + while d: + data.append(d) + d = fp.readline() self.assertEqual(data, DATA_SPLIT) self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) def test_seek(self): - fp = self.open(support.TESTFN, self.READMODE) - fp.readline() - pos = fp.tell() - data = fp.readlines() - self.assertEqual(data, DATA_SPLIT[1:]) - fp.seek(pos) - data = fp.readlines() + with self.open(support.TESTFN, self.READMODE) as fp: + fp.readline() + pos = fp.tell() + data = fp.readlines() + self.assertEqual(data, DATA_SPLIT[1:]) + fp.seek(pos) + data = fp.readlines() self.assertEqual(data, DATA_SPLIT[1:]) @@ -96,10 +95,10 @@ class TestCRLFNewlines(TestGenericUnivNewlines): DATA = DATA_CRLF def test_tell(self): - fp = self.open(support.TESTFN, self.READMODE) - self.assertEqual(repr(fp.newlines), repr(None)) - data = fp.readline() - pos = fp.tell() + with self.open(support.TESTFN, self.READMODE) as fp: + self.assertEqual(repr(fp.newlines), repr(None)) + data = fp.readline() + pos = fp.tell() self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) class TestMixedNewlines(TestGenericUnivNewlines):