Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.4k
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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
3fe225d
Do not mangle sys.path[0] if safe_path is set
gaogaotiantian1ec6724
📜🤖 Added by blurb_it.
blurb-it[bot]9a8919e
Rephrase the comments and simplified the logic
gaogaotiantianc5fabe6
Update Lib/pdb.py
gaogaotiantiand73c2da
Add whatsnew entry
gaogaotiantianc810afc
Update Doc/whatsnew/3.13.rst
gaogaotiantiand40d3db
Update phrasing
gaogaotiantianFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletionsDoc/whatsnew/3.13.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
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 = {**env, 'PYTHONIOENCODING': 'utf-8'} | ||
) as proc: | ||
stdout, stderr = proc.communicate(str.encode(commands)) | ||
stdout = stdout and bytes.decode(stdout) | ||
@@ -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, | ||
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, extra_env) | ||
def run_pdb_module(self, script, commands): | ||
"""Runs the script code as part of a module""" | ||
@@ -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) | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
def test_issue42383(self): | ||
with os_helper.temp_cwd() as cwd: | ||
with open('foo.py', 'w') as f: | ||
1 change: 1 addition & 0 deletionsMisc/NEWS.d/next/Library/2023-11-05-20-09-27.gh-issue-99367.HLaWKo.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Do not mangle ``sys.path[0]`` in :mod:`pdb` if safe_path is set |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.