summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTian Gao <gaogaotiantian@hotmail.com>2023-11-27 23:11:40 (GMT)
committerGitHub <noreply@github.com>2023-11-27 23:11:40 (GMT)
commitb90a5cf11cdb69e60aed7be732e80113bca7bbe4 (patch)
tree2b6c903d11ec948baf80ba163f01978189a72b5b
parent8f71b349de1ff2b11223ff7a8241c62a5a932339 (diff)
downloadcpython-b90a5cf11cdb69e60aed7be732e80113bca7bbe4.zip
cpython-b90a5cf11cdb69e60aed7be732e80113bca7bbe4.tar.gz
cpython-b90a5cf11cdb69e60aed7be732e80113bca7bbe4.tar.bz2
gh-99367: Do not mangle sys.path[0] in pdb if safe_path is set (#111762)
Co-authored-by: Christian Walther <cwalther@users.noreply.github.com>
-rw-r--r--Doc/whatsnew/3.13.rst5
-rwxr-xr-xLib/pdb.py6
-rw-r--r--Lib/test/test_pdb.py33
-rw-r--r--Misc/NEWS.d/next/Library/2023-11-05-20-09-27.gh-issue-99367.HLaWKo.rst1
4 files changed, 39 insertions, 6 deletions
diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst
index dad49f4..bf6a70f 100644
--- a/Doc/whatsnew/3.13.rst
+++ b/Doc/whatsnew/3.13.rst
@@ -285,6 +285,11 @@ pdb
identified and executed.
(Contributed by Tian Gao in :gh:`108464`.)
+* ``sys.path[0]`` will no longer be replaced by the directory of the script
+ being debugged when ``sys.flags.safe_path`` is set (via the :option:`-P`
+ command line option or :envvar:`PYTHONSAFEPATH` environment variable).
+ (Contributed by Tian Gao and Christian Walther in :gh:`111762`.)
+
sqlite3
-------
diff --git a/Lib/pdb.py b/Lib/pdb.py
index ed78d74..9d12418 100755
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -142,8 +142,10 @@ class _ScriptTarget(str):
print('Error:', self.orig, 'is a directory')
sys.exit(1)
- # Replace pdb's dir with script's dir in front of module search path.
- sys.path[0] = os.path.dirname(self)
+ # If safe_path(-P) is not set, sys.path[0] is the directory
+ # of pdb, and we should replace it with the directory of the script
+ if not sys.flags.safe_path:
+ sys.path[0] = os.path.dirname(self)
@property
def filename(self):
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index 67a4053..2a279ca 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -2520,15 +2520,21 @@ class PdbTestCase(unittest.TestCase):
@unittest.skipIf(sys.flags.safe_path,
'PYTHONSAFEPATH changes default sys.path')
- def _run_pdb(self, pdb_args, commands, expected_returncode=0):
+ def _run_pdb(self, pdb_args, commands,
+ expected_returncode=0,
+ extra_env=None):
self.addCleanup(os_helper.rmtree, '__pycache__')
cmd = [sys.executable, '-m', 'pdb'] + pdb_args
+ if extra_env is not None:
+ env = os.environ | extra_env
+ else:
+ env = os.environ
with subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT,
- env = {**os.environ, 'PYTHONIOENCODING': 'utf-8'}
+ env = {**env, 'PYTHONIOENCODING': 'utf-8'}
) as proc:
stdout, stderr = proc.communicate(str.encode(commands))
stdout = stdout and bytes.decode(stdout)
@@ -2540,13 +2546,15 @@ class PdbTestCase(unittest.TestCase):
)
return stdout, stderr
- def run_pdb_script(self, script, commands, expected_returncode=0):
+ def run_pdb_script(self, script, commands,
+ expected_returncode=0,
+ extra_env=None):
"""Run 'script' lines with pdb and the pdb 'commands'."""
filename = 'main.py'
with open(filename, 'w') as f:
f.write(textwrap.dedent(script))
self.addCleanup(os_helper.unlink, filename)
- return self._run_pdb([filename], commands, expected_returncode)
+ return self._run_pdb([filename], commands, expected_returncode, extra_env)
def run_pdb_module(self, script, commands):
"""Runs the script code as part of a module"""
@@ -3131,6 +3139,23 @@ def bœr():
self.assertEqual(stdout.split('\n')[2].rstrip('\r'), expected)
+ def test_safe_path(self):
+ """ With safe_path set, pdb should not mangle sys.path[0]"""
+
+ script = textwrap.dedent("""
+ import sys
+ import random
+ print('sys.path[0] is', sys.path[0])
+ """)
+ commands = 'c\n'
+
+
+ with os_helper.temp_cwd() as cwd:
+ stdout, _ = self.run_pdb_script(script, commands, extra_env={'PYTHONSAFEPATH': '1'})
+
+ unexpected = f'sys.path[0] is {os.path.realpath(cwd)}'
+ self.assertNotIn(unexpected, stdout)
+
def test_issue42383(self):
with os_helper.temp_cwd() as cwd:
with open('foo.py', 'w') as f:
diff --git a/Misc/NEWS.d/next/Library/2023-11-05-20-09-27.gh-issue-99367.HLaWKo.rst b/Misc/NEWS.d/next/Library/2023-11-05-20-09-27.gh-issue-99367.HLaWKo.rst
new file mode 100644
index 0000000..0920da2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-11-05-20-09-27.gh-issue-99367.HLaWKo.rst
@@ -0,0 +1 @@
+Do not mangle ``sys.path[0]`` in :mod:`pdb` if safe_path is set