Title: gh-157006: Clear PYTHON* env vars in test_embed · Pull Request #157010 · python/cpython · GitHub
Open Graph Title: gh-157006: Clear PYTHON* env vars in test_embed · Pull Request #157010 · python/cpython
X Title: gh-157006: Clear PYTHON* env vars in test_embed · Pull Request #157010 · python/cpython
Description: 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. Issue: gh-157006
Open Graph Description: 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 be...
X Description: 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 tex...
Opengraph URL: https://github.com/python/cpython/pull/157010
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:dcc0942d-1c12-6be6-3e96-38a56ccd5341 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | D5B4:1D86C4:3E0A540:4ECD6E8:6B020FFA |
| html-safe-nonce | c1c386d370dfb07bf8df0007c8482da15ae5ff16bead8e82bab72e487170c194 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENUI0OjFEODZDNDozRTBBNTQwOjRFQ0Q2RTg6NkIwMjBGRkEiLCJ2aXNpdG9yX2lkIjoiMzI0NzA0OTMwMzk2NDkxMzY1OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8b22c12b3910cd0025565e45541788f1aea8b2591dfe01c0f66b4a5a65a7c093 |
| hovercard-subject-tag | pull_request:4453192335 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/157010/files |
| twitter:image | https://avatars.githubusercontent.com/u/10137?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/10137?s=400&v=4 |
| og:image:alt | 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 be... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width