diff --git a/ansible_mitogen/target.py b/ansible_mitogen/target.py index 7b10e163..a24955d2 100644 --- a/ansible_mitogen/target.py +++ b/ansible_mitogen/target.py @@ -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): diff --git a/tests/ansible/tests/connection_test.py b/tests/ansible/tests/connection_test.py index afe0a215..377b5d24 100644 --- a/tests/ansible/tests/connection_test.py +++ b/tests/ansible/tests/connection_test.py @@ -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) diff --git a/tests/ansible/tests/target_test.py b/tests/ansible/tests/target_test.py index 918bd1d7..4b40a113 100644 --- a/tests/ansible/tests/target_test.py +++ b/tests/ansible/tests/target_test.py @@ -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') diff --git a/tests/data/plain_old_module.py b/tests/data/plain_old_module.py index 608f27a5..7239f76a 100755 --- a/tests/data/plain_old_module.py +++ b/tests/data/plain_old_module.py @@ -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): diff --git a/tests/master_test.py b/tests/master_test.py index bcf68cb2..98acdf6e 100644 --- a/tests/master_test.py +++ b/tests/master_test.py @@ -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) diff --git a/tests/module_finder_test.py b/tests/module_finder_test.py index 6e28d8fa..64059853 100644 --- a/tests/module_finder_test.py +++ b/tests/module_finder_test.py @@ -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)