From 2a1e48fabcc696ade1aac9ac6c6c4a4fc980e365 Mon Sep 17 00:00:00 2001 From: don012rac3 Date: Sat, 5 Sep 2026 21:21:20 +0000 Subject: [PATCH] gh-157006: Clear PYTHONSTARTUP and other PYTHON* env vars in test_embed run_embedded_interpreter() now defaults to remove_python_envvars() when no explicit env is provided, preventing PYTHONSTARTUP (e.g., set by VS Code shell integration) from leaking into the embedded interpreter and causing test failures. Also fix test_init_run_main_startup_exitcode to use remove_python_envvars() instead of dict(os.environ) as the base environment. --- Lib/test/test_embed.py | 4 +++- .../next/Tests/2026-09-06-12-00-00.gh-issue-157006.Xk9mPq.rst | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Tests/2026-09-06-12-00-00.gh-issue-157006.Xk9mPq.rst diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py index 1ff600e30bf4cbd..ada9f4e29921805 100644 --- a/Lib/test/test_embed.py +++ b/Lib/test/test_embed.py @@ -137,6 +137,8 @@ def run_embedded_interpreter(self, *args, env=None, """Runs a test in the embedded interpreter""" cmd = [self.test_exe] cmd.extend(args) + if env is None: + env = remove_python_envvars() if env is not None and MS_WINDOWS: # Windows requires at least the SYSTEMROOT environment variable to # start Python. @@ -628,7 +630,7 @@ def test_init_run_main_startup_exitcode(self): with open(filename, 'x') as fp: fp.write(CODE_EXITCODE_123) - env = dict(os.environ) + env = remove_python_envvars() env['PYTHONSTARTUP'] = filename self.check_program_exitcode("test_init_run_main_interactive_exitcode", env=env, diff --git a/Misc/NEWS.d/next/Tests/2026-09-06-12-00-00.gh-issue-157006.Xk9mPq.rst b/Misc/NEWS.d/next/Tests/2026-09-06-12-00-00.gh-issue-157006.Xk9mPq.rst new file mode 100644 index 000000000000000..7e6b3db296dc41b --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-09-06-12-00-00.gh-issue-157006.Xk9mPq.rst @@ -0,0 +1,4 @@ +Fix ``test.test_embed`` to clear ``PYTHON*`` environment variables (including +``PYTHONSTARTUP``) before running embedded interpreter tests, preventing failures +when the test runner's environment has these variables set (e.g., by VS Code's +shell integration).