Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb90a5cf

Browse files
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>
1 parent8f71b34 commitb90a5cf

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

‎Doc/whatsnew/3.13.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ pdb
285285
identified and executed.
286286
(Contributed by Tian Gao in:gh:`108464`.)
287287

288+
* ``sys.path[0]`` will no longer be replaced by the directory of the script
289+
being debugged when ``sys.flags.safe_path`` is set (via the:option:`-P`
290+
command line option or:envvar:`PYTHONSAFEPATH` environment variable).
291+
(Contributed by Tian Gao and Christian Walther in:gh:`111762`.)
292+
288293
sqlite3
289294
-------
290295

‎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+
# If safe_path(-P) is not set, sys.path[0] is the directory
146+
# of pdb, and we should replace it with the directory of the script
147+
ifnotsys.flags.safe_path:
148+
sys.path[0]=os.path.dirname(self)
147149

148150
@property
149151
deffilename(self):

‎Lib/test/test_pdb.py

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

25212521
@unittest.skipIf(sys.flags.safe_path,
25222522
'PYTHONSAFEPATH changes default sys.path')
2523-
def_run_pdb(self,pdb_args,commands,expected_returncode=0):
2523+
def_run_pdb(self,pdb_args,commands,
2524+
expected_returncode=0,
2525+
extra_env=None):
25242526
self.addCleanup(os_helper.rmtree,'__pycache__')
25252527
cmd= [sys.executable,'-m','pdb']+pdb_args
2528+
ifextra_envisnotNone:
2529+
env=os.environ|extra_env
2530+
else:
2531+
env=os.environ
25262532
withsubprocess.Popen(
25272533
cmd,
25282534
stdout=subprocess.PIPE,
25292535
stdin=subprocess.PIPE,
25302536
stderr=subprocess.STDOUT,
2531-
env= {**os.environ,'PYTHONIOENCODING':'utf-8'}
2537+
env= {**env,'PYTHONIOENCODING':'utf-8'}
25322538
)asproc:
25332539
stdout,stderr=proc.communicate(str.encode(commands))
25342540
stdout=stdoutandbytes.decode(stdout)
@@ -2540,13 +2546,15 @@ def _run_pdb(self, pdb_args, commands, expected_returncode=0):
25402546
)
25412547
returnstdout,stderr
25422548

2543-
defrun_pdb_script(self,script,commands,expected_returncode=0):
2549+
defrun_pdb_script(self,script,commands,
2550+
expected_returncode=0,
2551+
extra_env=None):
25442552
"""Run 'script' lines with pdb and the pdb 'commands'."""
25452553
filename='main.py'
25462554
withopen(filename,'w')asf:
25472555
f.write(textwrap.dedent(script))
25482556
self.addCleanup(os_helper.unlink,filename)
2549-
returnself._run_pdb([filename],commands,expected_returncode)
2557+
returnself._run_pdb([filename],commands,expected_returncode,extra_env)
25502558

25512559
defrun_pdb_module(self,script,commands):
25522560
"""Runs the script code as part of a module"""
@@ -3131,6 +3139,23 @@ def test_issue42384_symlink(self):
31313139

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

3142+
deftest_safe_path(self):
3143+
""" With safe_path set, pdb should not mangle sys.path[0]"""
3144+
3145+
script=textwrap.dedent("""
3146+
import sys
3147+
import random
3148+
print('sys.path[0] is', sys.path[0])
3149+
""")
3150+
commands='c\n'
3151+
3152+
3153+
withos_helper.temp_cwd()ascwd:
3154+
stdout,_=self.run_pdb_script(script,commands,extra_env={'PYTHONSAFEPATH':'1'})
3155+
3156+
unexpected=f'sys.path[0] is{os.path.realpath(cwd)}'
3157+
self.assertNotIn(unexpected,stdout)
3158+
31343159
deftest_issue42383(self):
31353160
withos_helper.temp_cwd()ascwd:
31363161
withopen('foo.py','w')asf:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Do not mangle ``sys.path[0]`` in:mod:`pdb` if safe_path is set

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp