Skip to content

gh-157006: Clear PYTHON* env vars in test_embed - #157010

Closed
ghost wants to merge 1 commit into
mainfrom
unknown repository
Closed

gh-157006: Clear PYTHON* env vars in test_embed#157010
ghost wants to merge 1 commit into
mainfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Sep 5, 2026

Copy link
Copy Markdown

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:

"Ctrl click to launch VS Code Native REPL\nok! Py_RunMain() returned 123"

instead of the expected:

"ok! Py_RunMain() returned 123"

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.

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.
@bedevere-app bedevere-app Bot added tests Tests in the Lib/test dir awaiting review labels Sep 5, 2026
@python python blocked ghost Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test_embed may fail if PYTHONSTARTUP set

1 participant