]> granicus.if.org Git - python/commitdiff
regrtest: close the new stdout and restore the original stdout at exit
authorVictor Stinner <victor.stinner@haypocalc.com>
Wed, 5 Jan 2011 03:54:26 +0000 (03:54 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Wed, 5 Jan 2011 03:54:26 +0000 (03:54 +0000)
Fix a ResourceWarning(unclosed file).

Lib/test/regrtest.py

index 1649865ecc48a6da31fcf56c357885373899b446..92d1597c62ed13e4ac372480a593ed226eb2a881 100755 (executable)
@@ -743,10 +743,19 @@ def replace_stdout():
     if os.name == "nt":
         # Replace sys.stdout breaks the stdout newlines on Windows: issue #8533
         return
+
+    import atexit
+
     stdout = sys.stdout
     sys.stdout = open(stdout.fileno(), 'w',
         encoding=stdout.encoding,
-        errors="backslashreplace")
+        errors="backslashreplace",
+        closefd=False)
+
+    def restore_stdout():
+        sys.stdout.close()
+        sys.stdout = stdout
+    atexit.register(restore_stdout)
 
 def runtest(test, verbose, quiet,
             huntrleaks=False, debug=False, use_resources=None):