{{ message }}
Closed
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

My test_embed was failing because VS Code's shell integration sets
PYTHONSTARTUP in the environment. The embedded interpreter picks it up
and runs the startup script, which prints extra text before the
expected "ok! Py_RunMain() returned 123", causing the assertion to
fail.
Detailed root cause analysis:
VS Code shell integration sets PYTHONSTARTUP to a script that prints
something like "Ctrl click to launch VS Code Native REPL". When
test_embed runs _testembed via run_embedded_interpreter(), the
method passes env=None to subprocess.Popen, which inherits the full
parent environment. The embedded interpreter then sees PYTHONSTARTUP
and executes the startup script before running the actual test code.
The startup script's output is prepended to stdout, so
check_program_exitcode() receives:
instead of the expected:
The assertion self.assertEqual(out.rstrip(), 'ok! Py_RunMain() returned 123')
then fails.
This only affects tests that go through run_embedded_interpreter()
without passing an explicit env. Tests that already call
remove_python_envvars() (e.g., the config snapshot tests around
line 878) are not affected.
There is also a secondary issue in test_init_run_main_startup_exitcode:
it builds its env from dict(os.environ), which copies the parent's
PYTHONSTARTUP before overriding it with the test's own startup file.
If the parent's PYTHONSTARTUP script produces output, it would also
pollute the test's expected output.
Changes:
run_embedded_interpreter(): when env is None, default to
remove_python_envvars() instead of inheriting os.environ. This
strips all PYTHON* variables (PYTHONSTARTUP, PYTHONPATH, PYTHONHOME,
etc.) that could affect the embedded interpreter's behavior. This is
consistent with how other tests in the same file already isolate
themselves from the parent environment.
test_init_run_main_startup_exitcode: changed dict(os.environ) to
remove_python_envvars() as the base env, then sets PYTHONSTARTUP
to the test's own startup file. This ensures the test is fully
isolated from whatever PYTHON* vars the runner happens to have.
Closes #157006
Note: this is a resubmission of #157008. The description on that PR
was a rough draft that I hadn't finished polishing when it was closed.
Note: AI tools were used only to assist in problem analysis. The fix
and code changes were determined and verified independently.