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

gh-99367: Do not mangle sys.path[0] in pdb if safe_path is set#111762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
iritkatriel merged 7 commits intopython:mainfromgaogaotiantian:pdb-safe-path
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletionsLib/pdb.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -142,8 +142,10 @@ def check(self):
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)
# Replace pdb's dir with script's dir in front of module search path
# if safe_path is not set, otherwise sys.path[0] is not pdb's dir
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
# if safe_path is not set, otherwise sys.path[0] is not pdb's dir
# if safe_path is not set

the part I removed seems a bit grammatically incorrect (and not really necessary) unless I'm missing something

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I was trying to comment the reason why we should not replacesys.path[0] - it was not obvious to me. We can either delete the comment or I can try to rephrase the whole thing to make more sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

ok, rephrase it then. I think it's too terse and not clear what that last part is talking about.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Is it better now?

if not getattr(sys.flags, 'safe_path', None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

issafe_path not guaranteed to exist? the test doessys.flags.safe_path.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I derived this from the original author, but from the source code it seems likesys.flags.safe_path should always exist after 3.11. I'll change it here, maybe together with the comment after we decide what to do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don’t remember exactly, but I probably added the check because I was not sure whether it’s okay to depend on ≥ 3.11. If you know that it is, go ahead and remove it.

sys.path[0] = os.path.dirname(self)

@property
def filename(self):
Expand Down
33 changes: 29 additions & 4 deletionsLib/test/test_pdb.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2493,15 +2493,21 @@ def tearDown(self):

@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)
Expand All@@ -2513,13 +2519,15 @@ def _run_pdb(self, pdb_args, commands, expected_returncode=0):
)
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"""
Expand DownExpand Up@@ -3104,6 +3112,23 @@ def test_issue42384_symlink(self):

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:
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Do not mangle ``sys.path[0]`` in :mod:`pdb` if safe_path is set

[8]ページ先頭

©2009-2025 Movatter.jp