BIND 10 trac1828, updated. 933d2b6f17ab8a634f392e94747074d2515fceb0 [1828] Use with statement to auto-close file handles when done using them

BIND 10 source code commits bind10-changes at lists.isc.org
Sun Jun 10 19:35:54 UTC 2012


The branch, trac1828 has been updated
       via  933d2b6f17ab8a634f392e94747074d2515fceb0 (commit)
      from  aef9cc2d584e6eda16792f126a6c018e61fd5d5e (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 933d2b6f17ab8a634f392e94747074d2515fceb0
Author: Mukund Sivaraman <muks at isc.org>
Date:   Mon Jun 11 00:02:20 2012 +0530

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

-----------------------------------------------------------------------

Summary of changes:
 src/bin/bind10/tests/bind10_test.py.in             |   30 ++++++++------------
 src/bin/tests/process_rename_test.py.in            |   10 +++----
 src/lib/dns/python/tests/testutil.py               |   17 ++++++-----
 .../python/isc/config/tests/module_spec_test.py    |   20 ++++++-------
 4 files changed, 32 insertions(+), 45 deletions(-)

-----------------------------------------------------------------------
diff --git a/src/bin/bind10/tests/bind10_test.py.in b/src/bin/bind10/tests/bind10_test.py.in
index 80ee226..fd4fa41 100644
--- a/src/bin/bind10/tests/bind10_test.py.in
+++ b/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.
diff --git a/src/bin/tests/process_rename_test.py.in b/src/bin/tests/process_rename_test.py.in
index ced698e..1156b29 100644
--- a/src/bin/tests/process_rename_test.py.in
+++ b/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):
diff --git a/src/lib/dns/python/tests/testutil.py b/src/lib/dns/python/tests/testutil.py
index 7acd02a..6a1397f 100644
--- a/src/lib/dns/python/tests/testutil.py
+++ b/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
diff --git a/src/lib/python/isc/config/tests/module_spec_test.py b/src/lib/python/isc/config/tests/module_spec_test.py
index f50e6f0..bb2bcda 100644
--- a/src/lib/python/isc/config/tests/module_spec_test.py
+++ b/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));



More information about the bind10-changes mailing list