summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrey Bienkowski <hexagonrecursion@gmail.com>2021-01-22 01:19:51 +0000
committerGitHub <noreply@github.com>2021-01-21 17:19:51 -0800
commit8603dfb4219989644601f6ede75b57e82648cd4e (patch)
tree87933525918ab55cae1a36fc322c9536e3a5afac
parentbpo-31904: setup.py: fix cross-compilation on VxWorks (GH-24191) (diff)
downloadcpython-8603dfb4219989644601f6ede75b57e82648cd4e.tar.gz
cpython-8603dfb4219989644601f6ede75b57e82648cd4e.tar.bz2
cpython-8603dfb4219989644601f6ede75b57e82648cd4e.zip
bpo-42384: pdb: correctly populate sys.path[0] (GH-23338)
Automerge-Triggered-By: GH:gvanrossum
-rwxr-xr-xLib/pdb.py3
-rw-r--r--Lib/test/test_pdb.py42
-rw-r--r--Misc/NEWS.d/next/Library/2020-11-17-14-32-39.bpo-42384.1ZnQSn.rst1
3 files changed, 45 insertions, 1 deletions
diff --git a/Lib/pdb.py b/Lib/pdb.py
index d7d95715945..7a5192cbadc 100755
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -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
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index 93b61dcfbcd..0a6f186d4be 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -1662,6 +1662,48 @@ def bœr():
'(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 os_helper.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)
+
+ @os_helper.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 os_helper.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 = [
diff --git a/Misc/NEWS.d/next/Library/2020-11-17-14-32-39.bpo-42384.1ZnQSn.rst b/Misc/NEWS.d/next/Library/2020-11-17-14-32-39.bpo-42384.1ZnQSn.rst
new file mode 100644
index 00000000000..ae990162fba
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-11-17-14-32-39.bpo-42384.1ZnQSn.rst
@@ -0,0 +1 @@
+Make pdb populate sys.path[0] exactly the same as regular python execution.