python / cpython

The Python programming language
https://www.python.org
Other
62.32k stars 29.93k forks source link

test_pdb fails: Unexpected sys.path[0] behavior with PYTHONSAFEPATH set #123121

Open xncbf opened 3 weeks ago

xncbf commented 3 weeks ago

Bug report

Bug description:

Issue: The test_safe_path function in test_pdb.py is failing. When PYTHONSAFEPATH is set to '1', sys.path[0] still includes the current working directory, which is unexpected.

Steps to Reproduce:

  1. Run the Python test suite, specifically focusing on test_pdb.
  2. Observe that test_safe_path fails with the following error:
AssertionError: 'sys.path[0] is /Users/joon/projects/cpython/build/test_python_68610æ/tempcwd' unexpectedly found in '> /Users/joon/projects/cpython/build/test_python_68610æ/tempcwd/main.py(2)<module>()
-> import sys
(Pdb) sys.path[0] is /Users/joon/projects/cpython/build/test_python_68610æ/tempcwd/$PYTHONPATH
The program finished and will be restarted
> /Users/joon/projects/cpython/build/test_python_68610æ/tempcwd/main.py(2)<module>()
-> import sys
(Pdb) 
'

Expected Behavior: When PYTHONSAFEPATH is set to '1', sys.path[0] should not include the current working directory.

Actual Behavior: sys.path[0] includes the current working directory, and there's an unexpected $PYTHONPATH appended to the path.

Environment:

121724

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Eclips4 commented 3 weeks ago

cc @gaogaotiantian

gaogaotiantian commented 3 weeks ago

How did you run test_safe_path with PYTHONSAFEPATH = 1? It should be skipped (not that it's what I had in mind when I wrote the test).

    @unittest.skipIf(sys.flags.safe_path,
                     'PYTHONSAFEPATH changes default sys.path')
    def _run_pdb(self, pdb_args, commands,
                 expected_returncode=0,
                 extra_env=None):
xncbf commented 3 weeks ago
./python.exe -m test test_pdb
./python.exe -m test test_pdb --forever

The above command fails when executed after the build. No other preliminary work is needed.

gaogaotiantian commented 3 weeks ago

Are you able to repro this with

./python.exe -m test test_pdb -m test_safe_path

Can you copy/paste the full output? Is it because of mac that the test actually ran?