10000 [3.9] bpo-42384: pdb: correctly populate sys.path[0] (GH-23338) (#24321) · python/cpython@f2df795 · GitHub
[go: up one dir, main page]

Skip to content

Commit f2df795

Browse files
[3.9] bpo-42384: pdb: correctly populate sys.path[0] (GH-23338) (#24321)
1 parent 26af2fa commit f2df795

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

Lib/pdb.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1686,8 +1686,9 @@ def main():
16861686

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

1689-
# Replace pdb's dir with script's dir in front of module search path.
16901689
< 10000 span class=pl-k>if not run_as_module:
1690+
mainpyfile = os.path.realpath(mainpyfile)
1691+
# Replace pdb's dir with script's dir in front of module search path.
16911692
sys.path[0] = os.path.dirname(mainpyfile)
16921693

16931694
# Note on saving/restoring sys.argv: it's a good idea when sys.argv was

Lib/test/test_pdb.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,48 @@ def test_errors_in_command(self):
16611661
'(Pdb) ',
16621662
])
16631663

1664+
1665+
def test_issue42384(self):
1666+
'''When running `python foo.py` sys.path[0] is an absolute path. `python -m pdb foo.py` should behave the same'''
1667+
script = textwrap.dedent("""
1668+
import sys
1669+
print('sys.path[0] is', sys.path[0])
1670+
""")
1671+
commands = 'c\nq'
1672+
1673+
with support.temp_cwd() as cwd:
1674+
expected = f'(Pdb) sys.path[0] is {os.path.realpath(cwd)}'
1675+
1676+
stdout, stderr = self.run_pdb_script(script, commands)
1677+
1678+
self.assertEqual(stdout.split('\n')[2].rstrip('\r'), expected)
1679+
1680+
@support.skip_unless_symlink
1681+
def test_issue42384_symlink(self):
1682+
'''When running `python foo.py` sys.path[0] resolves symlinks. `python -m pdb foo.py` should behave the same'''
1683+
script = textwrap.dedent("""
1684+
import sys
1685+
print('sys.path[0] is', sys.path[0])
1686+
""")
1687+
commands = 'c\nq'
1688+
1689+
with support.temp_cwd() as cwd:
1690+
cwd = os.path.realpath(cwd)
1691+
dir_one = os.path.join(cwd, 'dir_one')
1692+
dir_two = os.path.join(cwd, 'dir_two')
1693+
expected = f'(Pdb) sys.path[0] i B524 s {dir_one}'
1694+
1695+
os.mkdir(dir_one)
1696+
with open(os.path.join(dir_one, 'foo.py'), 'w') as f:
1697+
f.write(script)
1698+
os.mkdir(dir_two)
1699+
os.symlink(os.path.join(dir_one, 'foo.py'), os.path.join(dir_two, 'foo.py'))
1700+
1701+
stdout, stderr = self._run_pdb([os.path.join('dir_two', 'foo.py')], commands)
1702+
1703+
self.assertEqual(stdout.split('\n')[2].rstrip('\r'), expected)
1704+
1705+
16641706
def load_tests(*args):
16651707
from test import test_pdb
16661708
suites = [
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make pdb populate sys.path[0] exactly the same as regular python execution.

0 commit comments

Comments
 (0)
0