[Python-checkins] cpython (3.4): use with blocks to make sure files are closed

benjamin.peterson python-checkins at python.org
Sun May 4 02:19:08 CEST 2014


http://hg.python.org/cpython/rev/b8f5c8d07365
changeset:   90550:b8f5c8d07365
branch:      3.4
parent:      90548:57dacba9febf
user:        Benjamin Peterson <benjamin at python.org>
date:        Sat May 03 20:18:50 2014 -0400
summary:
  use with blocks to make sure files are closed

files:
  Lib/test/test_filecmp.py |  30 +++++++++++----------------
  1 files changed, 12 insertions(+), 18 deletions(-)


diff --git a/Lib/test/test_filecmp.py b/Lib/test/test_filecmp.py
--- a/Lib/test/test_filecmp.py
+++ b/Lib/test/test_filecmp.py
@@ -14,13 +14,11 @@
         self.name_diff = support.TESTFN + '-diff'
         data = 'Contents of file go here.\n'
         for name in [self.name, self.name_same, self.name_diff]:
-            output = open(name, 'w')
-            output.write(data)
-            output.close()
+            with open(name, 'w') as output:
+                output.write(data)
 
-        output = open(self.name_diff, 'a+')
-        output.write('An extra line.\n')
-        output.close()
+        with open(self.name_diff, 'a+') as output:
+            output.write('An extra line.\n')
         self.dir = tempfile.gettempdir()
 
     def tearDown(self):
@@ -71,13 +69,11 @@
                 fn = 'FiLe'     # Verify case-insensitive comparison
             else:
                 fn = 'file'
-            output = open(os.path.join(dir, fn), 'w')
-            output.write(data)
-            output.close()
+            with open(os.path.join(dir, fn), 'w') as output:
+                output.write(data)
 
-        output = open(os.path.join(self.dir_diff, 'file2'), 'w')
-        output.write('An extra file.\n')
-        output.close()
+        with open(os.path.join(self.dir_diff, 'file2'), 'w') as output:
+            output.write('An extra file.\n')
 
     def tearDown(self):
         for dir in (self.dir, self.dir_same, self.dir_diff):
@@ -104,9 +100,8 @@
                         "Comparing directory to same fails")
 
         # Add different file2
-        output = open(os.path.join(self.dir, 'file2'), 'w')
-        output.write('Different contents.\n')
-        output.close()
+        with open(os.path.join(self.dir, 'file2'), 'w') as output:
+            output.write('Different contents.\n')
 
         self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same,
                                      ['file', 'file2']) ==
@@ -178,9 +173,8 @@
         self._assert_report(d.report, expected_report)
 
         # Add different file2
-        output = open(os.path.join(self.dir_diff, 'file2'), 'w')
-        output.write('Different contents.\n')
-        output.close()
+        with open(os.path.join(self.dir_diff, 'file2'), 'w') as output:
+            output.write('Different contents.\n')
         d = filecmp.dircmp(self.dir, self.dir_diff)
         self.assertEqual(d.same_files, ['file'])
         self.assertEqual(d.diff_files, ['file2'])

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list