Browse Source

[1828] Use with statement to auto-close file handles when done using them

Mukund Sivaraman 13 years ago
parent
commit
933d2b6f17

+ 12 - 18
src/bin/bind10/tests/bind10_test.py.in

@@ -1055,33 +1055,29 @@ class TestPIDFile(unittest.TestCase):
         # dump PID to the file, and confirm the content is correct
         dump_pid(self.pid_file)
         my_pid = os.getpid()
-        fd = open(self.pid_file, "r")
-        self.assertEqual(my_pid, int(fd.read()))
-        fd.close()
+        with open(self.pid_file, "r") as f:
+            self.assertEqual(my_pid, int(f.read()))
 
     def test_dump_pid(self):
         self.check_pid_file()
 
         # make sure any existing content will be removed
-        fd = open(self.pid_file, "w")
-        fd.write('dummy data\n')
-        fd.close()
+        with open(self.pid_file, "w") as f:
+            f.write('dummy data\n')
         self.check_pid_file()
 
     def test_unlink_pid_file_notexist(self):
         dummy_data = 'dummy_data\n'
 
-        fd = open(self.pid_file, "w")
-        fd.write(dummy_data)
-        fd.close()
+        with open(self.pid_file, "w") as f:
+            f.write(dummy_data)
 
         unlink_pid_file("no_such_pid_file")
 
         # the file specified for unlink_pid_file doesn't exist,
         # and the original content of the file should be intact.
-        fd = open(self.pid_file, "r")
-        self.assertEqual(dummy_data, fd.read())
-        fd.close()
+        with open(self.pid_file, "r") as f:
+            self.assertEqual(dummy_data, f.read())
 
     def test_dump_pid_with_none(self):
         # Check the behavior of dump_pid() and unlink_pid_file() with None.
@@ -1091,15 +1087,13 @@ class TestPIDFile(unittest.TestCase):
 
         dummy_data = 'dummy_data\n'
 
-        fd = open(self.pid_file, "w")
-        fd.write(dummy_data)
-        fd.close()
+        with open(self.pid_file, "w") as f:
+            f.write(dummy_data)
 
         unlink_pid_file(None)
 
-        fd = open(self.pid_file, "r")
-        self.assertEqual(dummy_data, fd.read())
-        fd.close()
+        with open(self.pid_file, "r") as f:
+            self.assertEqual(dummy_data, f.read())
 
     def test_dump_pid_failure(self):
         # the attempt to open file will fail, which should result in exception.

+ 4 - 6
src/bin/tests/process_rename_test.py.in

@@ -25,9 +25,8 @@ class TestRename(unittest.TestCase):
     def __scan(self, directory, script, fun):
         # Scan one script if it contains call to the renaming function
         filename = os.path.join(directory, script)
-        fd = open(filename)
-        data = ''.join(fd.readlines())
-        fd.close()
+        with open(filename) as f:
+            data = ''.join(f.readlines())
         prettyname = 'src' + filename[filename.rfind('../') + 2:]
         self.assertTrue(fun.search(data),
             "Didn't find a call to isc.util.process.rename in " + prettyname)
@@ -55,9 +54,8 @@ class TestRename(unittest.TestCase):
         # Find all Makefile and extract names of scripts
         for (d, _, fs) in os.walk('@top_builddir@'):
             if 'Makefile' in fs:
-                fd = open(os.path.join(d, "Makefile"))
-                makefile = ''.join(fd.readlines())
-                fd.close()
+                with open(os.path.join(d, "Makefile")) as f:
+                    makefile = ''.join(f.readlines())
                 for (var, _) in lines.findall(re.sub(excluded_lines, '',
                                                      makefile)):
                     for (script, _) in scripts.findall(var):

+ 8 - 9
src/lib/dns/python/tests/testutil.py

@@ -28,16 +28,15 @@ def read_wire_data(filename):
     data = bytes()
     for path in testdata_path.split(":"):
         try:
-            file = open(path + os.sep + filename, "r")
-            for line in file:
-                line = line.strip()
-                if line == "" or line.startswith("#"):
-                    pass
-                else:
-                    cur_data = bytes.fromhex(line)
-                    data += cur_data
+            with open(path + os.sep + filename, "r") as f:
+                for line in f:
+                    line = line.strip()
+                    if line == "" or line.startswith("#"):
+                        pass
+                    else:
+                        cur_data = bytes.fromhex(line)
+                        data += cur_data
 
-            file.close()
             return data
         except IOError:
             pass

+ 8 - 12
src/lib/python/isc/config/tests/module_spec_test.py

@@ -46,9 +46,8 @@ class TestModuleSpec(unittest.TestCase):
         self.spec1(dd)
 
     def test_open_file_obj(self):
-        file1 = open(self.spec_file("spec1.spec"))
-        dd = isc.config.module_spec_from_file(file1)
-        file1.close()
+        with open(self.spec_file("spec1.spec")) as file1:
+            dd = isc.config.module_spec_from_file(file1)
         self.spec1(dd)
 
     def test_open_bad_file_obj(self):
@@ -90,9 +89,8 @@ class TestModuleSpec(unittest.TestCase):
 
     def validate_data(self, specfile_name, datafile_name):
         dd = self.read_spec_file(specfile_name);
-        data_file = open(self.spec_file(datafile_name))
-        data_str = data_file.read()
-        data_file.close()
+        with open(self.spec_file(datafile_name)) as data_file:
+            data_str = data_file.read()
         data = isc.cc.data.parse_value_str(data_str)
         return dd.validate_config(True, data)
         
@@ -111,9 +109,8 @@ class TestModuleSpec(unittest.TestCase):
 
     def validate_command_params(self, specfile_name, datafile_name, cmd_name):
         dd = self.read_spec_file(specfile_name);
-        data_file = open(self.spec_file(datafile_name))
-        data_str = data_file.read()
-        data_file.close()
+        with open(self.spec_file(datafile_name)) as data_file:
+            data_str = data_file.read()
         params = isc.cc.data.parse_value_str(data_str)
         return dd.validate_command(cmd_name, params)
 
@@ -134,9 +131,8 @@ class TestModuleSpec(unittest.TestCase):
     def test_statistics_validation(self):
         def _validate_stat(specfile_name, datafile_name):
             dd = self.read_spec_file(specfile_name);
-            data_file = open(self.spec_file(datafile_name))
-            data_str = data_file.read()
-            data_file.close()
+            with open(self.spec_file(datafile_name)) as data_file:
+                data_str = data_file.read()
             data = isc.cc.data.parse_value_str(data_str)
             return dd.validate_statistics(True, data, [])
         self.assertFalse(self.read_spec_file("spec1.spec").validate_statistics(True, None, None));