[PATCH 05 of 10] run-tests: use a context manager for file I/O

Bryan O'Sullivan bos at serpentine.com
Tue Jan 12 16:50:08 CST 2016


# HG changeset patch
# User Bryan O'Sullivan <bryano at fb.com>
# Date 1452637797 28800
#      Tue Jan 12 14:29:57 2016 -0800
# Node ID 05e2519675079dfb6f343d5715541805c6e9be4b
# Parent  6525f45dd83f10295f6047a5c2a2648e8d45bbd6
run-tests: use a context manager for file I/O

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -1716,8 +1716,7 @@ class TextTestRunner(unittest.TextTestRu
                 if json is None:
                     raise ImportError("json module not installed")
                 jsonpath = os.path.join(self._runner._testdir, 'report.json')
-                fp = open(jsonpath, 'w')
-                try:
+                with open(jsonpath, 'w') as fp:
                     timesd = {}
                     for tdata in result.times:
                         test = tdata[0]
@@ -1741,8 +1740,6 @@ class TextTestRunner(unittest.TextTestRu
                             outcome[tc.name] = tres
                     jsonout = json.dumps(outcome, sort_keys=True, indent=4)
                     fp.writelines(("testreport =", jsonout))
-                finally:
-                    fp.close()
 
             self._runner._checkhglib('Tested')
 


More information about the Mercurial-devel mailing list