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

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


# HG changeset patch
# User Bryan O'Sullivan <bryano at fb.com>
# Date 1452638950 28800
#      Tue Jan 12 14:49:10 2016 -0800
# Node ID e9a844767dfabda6625b15d1f2e6231aefa13e2c
# Parent  070233a783acc9011d3a3b18dc2e162e3a846c3f
run-tests: use a context manager for file I/O in TextTestRunner

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -1681,8 +1681,7 @@ class TextTestRunner(unittest.TextTestRu
                 self.stream.writeln('Errored %s: %s' % (test.name, msg))
 
             if self._runner.options.xunit:
-                xuf = open(self._runner.options.xunit, 'wb')
-                try:
+                with open(self._runner.options.xunit, 'wb') as xuf:
                     timesd = dict((t[0], t[3]) for t in result.times)
                     doc = minidom.Document()
                     s = doc.createElement('testsuite')
@@ -1709,8 +1708,6 @@ class TextTestRunner(unittest.TextTestRu
                         t.appendChild(cd)
                         s.appendChild(t)
                     xuf.write(doc.toprettyxml(indent='  ', encoding='utf-8'))
-                finally:
-                    xuf.close()
 
             if self._runner.options.json:
                 if json is None:


More information about the Mercurial-devel mailing list