Index: Lib/test/test_os.py =================================================================== --- Lib/test/test_os.py (revisione 85425) +++ Lib/test/test_os.py (copia locale) @@ -106,7 +106,8 @@ self.assertFalse(os.path.exists(name), "file already exists for temporary file") # make sure we can create the file - open(name, "w") + with open(name, "w") as _: + pass self.files.append(name) def test_tempnam(self): @@ -163,6 +164,9 @@ # dummy file and proceed with the test as normal. fp.close() os.remove(name) + finally: + if not fp.closed: + fp.close() fp = os.tmpfile() fp.write("foobar") @@ -199,9 +203,16 @@ self.check_tempfile(name) def fdopen_helper(self, *args): - fd = os.open(support.TESTFN, os.O_RDONLY) - fp2 = os.fdopen(fd, *args) - fp2.close() + fd = None + fp2 = None + try: + fd = os.open(support.TESTFN, os.O_RDONLY) + fp2 = os.fdopen(fd, *args) + finally: + if fd is not None: + fd.close() + if fp2 is not None: + fp2.close() def test_fdopen(self): self.fdopen_helper() @@ -213,9 +224,8 @@ def setUp(self): os.mkdir(support.TESTFN) self.fname = os.path.join(support.TESTFN, "f1") - f = open(self.fname, 'wb') - f.write(b"ABC") - f.close() + with open(self.fname, 'wb') as f: + f.write(b"ABC") def tearDown(self): os.unlink(self.fname) @@ -533,9 +543,8 @@ os.makedirs(sub2_path) os.makedirs(t2_path) for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path: - f = open(path, "w") - f.write("I'm " + path + " and proud of it. Blame test_os.\n") - f.close() + with open(path, "w") as f: + f.write("I'm " + path + " and proud of it. Blame test_os.\n") if support.can_symlink(): os.symlink(os.path.abspath(t2_path), link_path) sub2_tree = (sub2_path, ["link"], ["tmp3"]) @@ -638,12 +647,10 @@ class DevNullTests(unittest.TestCase): def test_devnull(self): - f = open(os.devnull, 'w') - f.write('hello') - f.close() - f = open(os.devnull, 'r') - self.assertEqual(f.read(), '') - f.close() + with open(os.devnull, 'w') as f: + f.write('hello') + with open(os.devnull, 'r') as f: + self.assertEqual(f.read(), '') class URandomTests(unittest.TestCase): def test_urandom(self):