Use with open(): ... to ensure file objects get closed

pull/920/head
Alex Willmer 2 years ago
parent 65809a6f0f
commit 1287d58a54

@ -652,7 +652,8 @@ def read_path(path):
"""
Fetch the contents of a filesystem `path` as bytes.
"""
return open(path, 'rb').read()
with open(path, 'rb') as f:
return f.read()
def set_file_owner(path, owner, group=None, fd=None):

@ -129,7 +129,8 @@ class PutDataTest(ConnectionMixin, testlib.TestCase):
self.conn.put_data(path, contents)
self.wait_for_completion()
self.assertEqual(contents, open(path, 'rb').read())
with open(path, 'rb') as f:
self.assertEqual(contents, f.read())
os.unlink(path)
def test_mode(self):
@ -163,17 +164,18 @@ class PutFileTest(ConnectionMixin, testlib.TestCase):
path = tempfile.mktemp(prefix='mitotest')
self.conn.put_file(in_path=__file__, out_path=path)
self.wait_for_completion()
self.assertEqual(open(path, 'rb').read(),
open(__file__, 'rb').read())
with open(path, 'rb') as path_f:
with open(__file__, 'rb') as __file__f:
self.assertEqual(path_f.read(), __file__f.read())
os.unlink(path)
def test_out_path_big(self):
path = tempfile.mktemp(prefix='mitotest')
self.conn.put_file(in_path=self.big_path, out_path=path)
self.wait_for_completion()
self.assertEqual(open(path, 'rb').read(),
open(self.big_path, 'rb').read())
with open(path, 'rb') as path_f:
with open(self.big_path, 'rb') as big_path_f:
self.assertEqual(path_f.read(), big_path_f.read())
#self._compare_times_modes(path, __file__)
os.unlink(path)

@ -84,7 +84,8 @@ class IsGoodTempDirTest(unittest.TestCase):
fp.write('derp')
self.assertTrue(os.path.isfile(bleh))
self.assertFalse(self.func(bleh))
self.assertEqual(open(bleh).read(), 'derp')
with open(bleh) as fp:
self.assertEqual(fp.read(), 'derp')
@unittest.skipIf(
os.geteuid() == 0, 'writes by root ignore directory permissions')

@ -12,7 +12,8 @@ class MyError(Exception):
def get_sentinel_value():
# Some proof we're even talking to the mitogen-test Docker image
return open('/etc/sentinel', 'rb').read().decode()
with open('/etc/sentinel', 'rb') as f:
return f.read().decode()
def add(x, y):

@ -22,5 +22,6 @@ class ScanCodeImportsTest(testlib.TestCase):
def test_simple(self):
source_path = inspect.getsourcefile(ScanCodeImportsTest)
co = compile(open(source_path).read(), source_path, 'exec')
with open(source_path) as f:
co = compile(f.read(), source_path, 'exec')
self.assertEqual(list(self.func(co)), self.SIMPLE_EXPECT)

@ -120,7 +120,8 @@ class SysModulesMethodTest(testlib.TestCase):
self.assertEqual(path, __main__.__file__)
# linecache adds a line ending to the final line if one is missing.
actual_src = open(path, 'rb').read()
with open(path, 'rb') as f:
actual_src = f.read()
if actual_src[-1:] != b('\n'):
actual_src += b('\n')
@ -167,7 +168,8 @@ class GetModuleViaParentEnumerationTest(testlib.TestCase):
modpath = os.path.join(MODS_DIR, 'pkg_like_plumbum/colors.py')
self.assertEqual(path, modpath)
self.assertEqual(src, open(modpath, 'rb').read())
with open(modpath, 'rb') as f:
self.assertEqual(src, f.read())
self.assertFalse(is_pkg)
def test_ansible_module_utils_distro_succeeds(self):
@ -184,7 +186,8 @@ class GetModuleViaParentEnumerationTest(testlib.TestCase):
modpath = os.path.join(MODS_DIR,
'pkg_like_ansible/module_utils/distro/__init__.py')
self.assertEqual(path, modpath)
self.assertEqual(src, open(modpath, 'rb').read())
with open(modpath, 'rb') as f:
self.assertEqual(src, f.read())
self.assertEqual(is_pkg, True)
# ensure we can resolve a child of the subpackage.
@ -194,7 +197,8 @@ class GetModuleViaParentEnumerationTest(testlib.TestCase):
modpath = os.path.join(MODS_DIR,
'pkg_like_ansible/module_utils/distro/_distro.py')
self.assertEqual(path, modpath)
self.assertEqual(src, open(modpath, 'rb').read())
with open(modpath, 'rb') as f:
self.assertEqual(src, f.read())
self.assertEqual(is_pkg, False)
def test_ansible_module_utils_system_distro_succeeds(self):
@ -212,7 +216,8 @@ class GetModuleViaParentEnumerationTest(testlib.TestCase):
modpath = os.path.join(MODS_DIR,
'pkg_like_ansible/module_utils/sys_distro/__init__.py')
self.assertEqual(path, modpath)
self.assertEqual(src, open(modpath, 'rb').read())
with open(modpath, 'rb') as f:
self.assertEqual(src, f.read())
self.assertEqual(is_pkg, True)
# ensure we can resolve a child of the subpackage.
@ -222,7 +227,8 @@ class GetModuleViaParentEnumerationTest(testlib.TestCase):
modpath = os.path.join(MODS_DIR,
'pkg_like_ansible/module_utils/sys_distro/_distro.py')
self.assertEqual(path, modpath)
self.assertEqual(src, open(modpath, 'rb').read())
with open(modpath, 'rb') as f:
self.assertEqual(src, f.read())
self.assertEqual(is_pkg, False)

Loading…
Cancel
Save