]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-152132: Close stderr_copy in test_api.test_run (#153591)
authorVictor Stinner <vstinner@python.org>
Sat, 11 Jul 2026 21:55:02 +0000 (23:55 +0200)
committerGitHub <noreply@github.com>
Sat, 11 Jul 2026 21:55:02 +0000 (21:55 +0000)
Previously, the test was leaking a file descriptor.

Lib/test/test_capi/test_run.py

index 14f9833f63a4fb8cf0e7e2b95d7c4f4e1077887a..7796c589b557c729e9070a1acb15304d40f55e81 100644 (file)
@@ -310,13 +310,15 @@ class CAPITest(unittest.TestCase):
             # supported". In this case, run the test without redirecting
             # stderr to a temporary file.
             self._check_run_interactive(run, encode_filename, use_loop)
-        else:
-            with tempfile.TemporaryFile() as tmp:
-                try:
-                    os.dup2(tmp.fileno(), STDERR_FD)
-                    self._check_run_interactive(run, encode_filename, use_loop)
-                finally:
-                    os.dup2(stderr_copy, STDERR_FD)
+            return
+
+        with tempfile.TemporaryFile() as tmp:
+            try:
+                os.dup2(tmp.fileno(), STDERR_FD)
+                self._check_run_interactive(run, encode_filename, use_loop)
+            finally:
+                os.dup2(stderr_copy, STDERR_FD)
+                os.close(stderr_copy)
 
     def test_run_interactiveone(self):
         # Test PyRun_InteractiveOne()