More proper closing of files

This commit is contained in:
Antoine Pitrou 2010-10-14 22:11:44 +00:00
parent 73315e9200
commit 92f60ed82a
7 changed files with 82 additions and 57 deletions

View file

@ -285,7 +285,8 @@ class TestShutil(unittest.TestCase):
if hasattr(os, "link"):
os.link(src, dst)
self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
self.assertEqual(open(src,'r').read(), 'cheddar')
with open(src, 'r') as f:
self.assertEqual(f.read(), 'cheddar')
os.remove(dst)
# Using `src` here would mean we end up with a symlink pointing
@ -293,7 +294,8 @@ class TestShutil(unittest.TestCase):
# TESTFN/cheese.
os.symlink('cheese', dst)
self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
self.assertEqual(open(src,'r').read(), 'cheddar')
with open(src, 'r') as f:
self.assertEqual(f.read(), 'cheddar')
os.remove(dst)
finally:
try:
@ -690,9 +692,11 @@ class TestMove(unittest.TestCase):
pass
def _check_move_file(self, src, dst, real_dst):
contents = open(src, "rb").read()
with open(src, "rb") as f:
contents = f.read()
shutil.move(src, dst)
self.assertEqual(contents, open(real_dst, "rb").read())
with open(real_dst, "rb") as f:
self.assertEqual(contents, f.read())
self.assertFalse(os.path.exists(src))
def _check_move_dir(self, src, dst, real_dst):