From b587c54bbe21a87865fdb5ba9e67a4a287707bd5 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Sun, 12 Jul 2026 00:21:06 +0200 Subject: [PATCH] [3.14] gh-152132: Close stderr_copy in test_api.test_run (GH-153591) (#153596) gh-152132: Close stderr_copy in test_api.test_run (GH-153591) Previously, the test was leaking a file descriptor. (cherry picked from commit 0023d5b23df09f18425fea9b9c54a37ba050d5cf) Co-authored-by: Victor Stinner --- Lib/test/test_capi/test_run.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_capi/test_run.py b/Lib/test/test_capi/test_run.py index 14f9833f63a4..7796c589b557 100644 --- a/Lib/test/test_capi/test_run.py +++ b/Lib/test/test_capi/test_run.py @@ -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() -- 2.47.3