Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3fe225d

Browse files
Do not mangle sys.path[0] if safe_path is set
1 parent ba8aa1f commit 3fe225d

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

Lib/pdb.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ def check(self):
142142
print('Error:', self.orig, 'is a directory')
143143
sys.exit(1)
144144

145-
# Replace pdb's dir with script's dir in front of module search path.
146-
sys.path[0] = os.path.dirname(self)
145+
# Replace pdb's dir with script's dir in front of module search path
146+
# if safe_path is not set, otherwise sys.path[0] is not pdb's dir
147+
if not getattr(sys.flags, 'safe_path', None):
148+
sys.path[0] = os.path.dirname(self)
147149

148150
@property
149151
def filename(self):

Lib/test/test_pdb.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,15 +2493,21 @@ def tearDown(self):
24932493

24942494
@unittest.skipIf(sys.flags.safe_path,
24952495
'PYTHONSAFEPATH changes default sys.path')
2496-
def _run_pdb(self, pdb_args, commands, expected_returncode=0):
2496+
def _run_pdb(self, pdb_args, commands,
2497+
expected_returncode=0,
2498+
extra_env=None):
24972499
self.addCleanup(os_helper.rmtree, '__pycache__')
24982500
cmd = [sys.executable, '-m', 'pdb'] + pdb_args
2501+
if extra_env is not None:
2502+
env = os.environ | extra_env
2503+
else:
2504+
env = os.environ
24992505
with subprocess.Popen(
25002506
cmd,
25012507
stdout=subprocess.PIPE,
25022508
stdin=subprocess.PIPE,
25032509
stderr=subprocess.STDOUT,
2504-
env = {**os.environ, 'PYTHONIOENCODING': 'utf-8'}
2510+
env = {**env, 'PYTHONIOENCODING': 'utf-8'}
25052511
) as proc:
25062512
stdout, stderr = proc.communicate(str.encode(commands))
25072513
stdout = stdout and bytes.decode(stdout)
@@ -2513,13 +2519,15 @@ def _run_pdb(self, pdb_args, commands, expected_returncode=0):
25132519
)
25142520
return stdout, stderr
25152521

2516-
def run_pdb_script(self, script, commands, expected_returncode=0):
2522+
def run_pdb_script(self, script, commands,
2523+
expected_returncode=0,
2524+
extra_env=None):
25172525
"""Run 'script' lines with pdb and the pdb 'commands'."""
25182526
filename = 'main.py'
25192527
with open(filename, 'w') as f:
25202528
f.write(textwrap.dedent(script))
25212529
self.addCleanup(os_helper.unlink, filename)
2522-
return self._run_pdb([filename], commands, expected_returncode)
2530+
return self._run_pdb([filename], commands, expected_returncode, extra_env)
25232531

25242532
def run_pdb_module(self, script, commands):
25252533
"""Runs the script code as part of a module"""
@@ -3104,6 +3112,23 @@ def test_issue42384_symlink(self):
31043112

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

3115+
def test_safe_path(self):
3116+
""" With safe_path set, pdb should not mangle sys.path[0]"""
3117+
3118+
script = textwrap.dedent("""
3119+
import sys
3120+
import random
3121+
print('sys.path[0] is', sys.path[0])
3122+
""")
3123+
commands = 'c\n'
3124+
3125+
3126+
with os_helper.temp_cwd() as cwd:
3127+
stdout, _ = self.run_pdb_script(script, commands, extra_env={'PYTHONSAFEPATH': '1'})
3128+
3129+
unexpected = f'sys.path[0] is {os.path.realpath(cwd)}'
3130+
self.assertNotIn(unexpected, stdout)
3131+
31073132
def test_issue42383(self):
31083133
with os_helper.temp_cwd() as cwd:
31093134
with open('foo.py', 'w') as f:

0 commit comments

Comments
 (0)