8000 [3.8] bpo-42384: pdb: correctly populate sys.path[0] (GH-23338) by hexagonrecursion · Pull Request #24320 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.8] bpo-42384: pdb: correctly populate sys.path[0] (GH-23338) #24320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1686,8 +1686,9 @@ def main():

sys.argv[:] = args # Hide "pdb.py" and pdb options from argument list

# Replace pdb's dir with script's dir in front of module search path.
if not run_as_module:
mainpyfile = os.path.realpath(mainpyfile)
# Replace pdb's dir with script's dir in front of module search path.
sys.path[0] = os.path.dirname(mainpyfile)

# Note on saving/restoring sys.argv: it's a good idea when sys.argv was
Expand Down
42 changes: 42 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,48 @@ def test_errors_in_command(self):
'(Pdb) ',
])


def test_issue42384(self):
'''When running `python foo.py` sys.path[0] is an absolute path. `python -m pdb foo.py` should behave the same'''
script = textwrap.dedent("""
import sys
print('sys.path[0] is', sys.path[0])
""")
commands = 'c\nq'

with support.temp_cwd() as cwd:
expected = f'(Pdb) sys.path[0] is {os.path.realpath(cwd)}'

stdout, stderr = self.run_pdb_script(script, commands)

self.assertEqual(stdout.split('\n')[2].rstrip('\r'), expected)

@support.skip_unless_symlink
def test_issue42384_symlink(self):
'''When running `python foo.py` sys.path[0] resolves symlinks. `python -m pdb foo.py` should behave the same'''
script = textwrap.dedent("""
import sys
print('sys.path[0] is', sys.path[0])
""")
commands = 'c\nq'

with support.temp_cwd() as cwd:
cwd = os.path.realpath(cwd)
dir_one = os.path.join(cwd, 'dir_one')
dir_two = os.path.join(cwd, 'dir_two')
expected = f'(Pdb) sys.path[0] is {dir_one}'

os.mkdir(dir_one)
with open(os.path.join(dir_one, 'foo.py'), 'w') as f:
f.write(script)
os.mkdir(dir_two)
os.symlink(os.path.join(dir_one, 'foo.py'), os.path.join(dir_two, 'foo.py'))

stdout, stderr = self._run_pdb([os.path.join('dir_two', 'foo.py')], commands)

self.assertEqual(stdout.split('\n')[2].rstrip('\r'), expected)


def load_tests(*args):
from test import test_pdb
suites = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make pdb populate sys.path[0] exactly the same as regular python execution.
0