Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-90095: Make .pdbrc work properly and add some reasonable tests#110496
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 our terms of service and privacy 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 from all commits
Commits
Show all changes
11 commits Select commit Hold shift + click to select a range
fdd36d2 Make .pdbrc work properly and add some reasonable tests
gaogaotiantian 3f3cd7d Add comment test
gaogaotiantian 33225ee Fix homesave issue
gaogaotiantian 2e1bc86 📜🤖 Added by blurb_it.
blurb-it[bot] 78f953a Merge branch 'main' into pdb-rc-commands
gaogaotiantian 6761844 Merge branch 'main' into pdb-rc-commands
gaogaotiantian e66eb76 Revert emplty line change
gaogaotiantian f07e770 Fix a bare return
gaogaotiantian 0b1c0e8 Clean up logic for remove_home
gaogaotiantian 75d1361 Check homesave to restore HOME
gaogaotiantian a5187a5 Merge branch 'main' into pdb-rc-commands
iritkatriel File 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
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 |
|---|---|---|
| @@ -2618,13 +2618,29 @@ def _run_pdb(self, pdb_args, commands, | ||
| def run_pdb_script(self, script, commands, | ||
| expected_returncode=0, | ||
| extra_env=None): | ||
| extra_env=None, | ||
| pdbrc=None, | ||
| remove_home=False): | ||
| """Run 'script' lines with pdb and the pdb 'commands'.""" | ||
| filename = 'main.py' | ||
| with open(filename, 'w') as f: | ||
| f.write(textwrap.dedent(script)) | ||
| if pdbrc is not None: | ||
| with open('.pdbrc', 'w') as f: | ||
| f.write(textwrap.dedent(pdbrc)) | ||
| self.addCleanup(os_helper.unlink, '.pdbrc') | ||
| self.addCleanup(os_helper.unlink, filename) | ||
| return self._run_pdb([filename], commands, expected_returncode, extra_env) | ||
| homesave = None | ||
gaogaotiantian marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| if remove_home: | ||
| homesave = os.environ.pop('HOME', None) | ||
| try: | ||
| stdout, stderr = self._run_pdb([filename], commands, expected_returncode, extra_env) | ||
| finally: | ||
| if homesave is not None: | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| os.environ['HOME'] = homesave | ||
| return stdout, stderr | ||
| def run_pdb_module(self, script, commands): | ||
| """Runs the script code as part of a module""" | ||
| @@ -2904,37 +2920,80 @@ def test_issue26053(self): | ||
| self.assertRegex(res, "Restarting .* with arguments:\na b c") | ||
| self.assertRegex(res, "Restarting .* with arguments:\nd e f") | ||
| def test_readrc_kwarg(self): | ||
| def test_pdbrc_basic(self): | ||
| script = textwrap.dedent(""" | ||
| import pdb; pdb.Pdb(readrc=False).set_trace() | ||
| a = 1 | ||
| b = 2 | ||
| """) | ||
| print('hello') | ||
| pdbrc = textwrap.dedent(""" | ||
| # Comments should be fine | ||
| n | ||
| p f"{a+8=}" | ||
| """) | ||
| save_home = os.environ.pop('HOME', None) | ||
| try: | ||
| with os_helper.temp_cwd(): | ||
| with open('.pdbrc', 'w') as f: | ||
| f.write("invalid\n") | ||
| with open('main.py', 'w') as f: | ||
| f.write(script) | ||
| cmd = [sys.executable, 'main.py'] | ||
| proc = subprocess.Popen( | ||
| cmd, | ||
| stdout=subprocess.PIPE, | ||
| stdin=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| ) | ||
| with proc: | ||
| stdout, stderr = proc.communicate(b'q\n') | ||
| self.assertNotIn(b"NameError: name 'invalid' is not defined", | ||
| stdout) | ||
| stdout, stderr = self.run_pdb_script(script, 'q\n', pdbrc=pdbrc, remove_home=True) | ||
| self.assertIn("a+8=9", stdout) | ||
| finally: | ||
| if save_home is not None: | ||
| os.environ['HOME'] = save_home | ||
| def test_pdbrc_alias(self): | ||
| script = textwrap.dedent(""" | ||
| class A: | ||
| def __init__(self): | ||
| self.attr = 1 | ||
| a = A() | ||
| b = 2 | ||
| """) | ||
| pdbrc = textwrap.dedent(""" | ||
| alias pi for k in %1.__dict__.keys(): print(f"%1.{k} ={%1.__dict__[k]}") | ||
| until 6 | ||
| pi a | ||
| """) | ||
| stdout, stderr = self.run_pdb_script(script, 'q\n', pdbrc=pdbrc, remove_home=True) | ||
| self.assertIn("a.attr = 1", stdout) | ||
| def test_pdbrc_semicolon(self): | ||
| script = textwrap.dedent(""" | ||
| class A: | ||
| def __init__(self): | ||
| self.attr = 1 | ||
| a = A() | ||
| b = 2 | ||
| """) | ||
| pdbrc = textwrap.dedent(""" | ||
| b 5;c;n | ||
| """) | ||
| stdout, stderr = self.run_pdb_script(script, 'q\n', pdbrc=pdbrc, remove_home=True) | ||
| self.assertIn("-> b = 2", stdout) | ||
| def test_pdbrc_commands(self): | ||
| script = textwrap.dedent(""" | ||
| class A: | ||
| def __init__(self): | ||
| self.attr = 1 | ||
| a = A() | ||
| b = 2 | ||
| """) | ||
| pdbrc = textwrap.dedent(""" | ||
| b 6 | ||
| commands 1 ; p a; end | ||
| c | ||
| """) | ||
| stdout, stderr = self.run_pdb_script(script, 'q\n', pdbrc=pdbrc, remove_home=True) | ||
| self.assertIn("<__main__.A object at", stdout) | ||
| def test_readrc_kwarg(self): | ||
| script = textwrap.dedent(""" | ||
| print('hello') | ||
| """) | ||
| stdout, stderr = self.run_pdb_script(script, 'q\n', pdbrc='invalid', remove_home=True) | ||
| self.assertIn("NameError: name 'invalid' is not defined", stdout) | ||
| def test_readrc_homedir(self): | ||
| save_home = os.environ.pop("HOME", None) | ||
| @@ -2949,40 +3008,6 @@ def test_readrc_homedir(self): | ||
| if save_home is not None: | ||
| os.environ["HOME"] = save_home | ||
| def test_read_pdbrc_with_ascii_encoding(self): | ||
| script = textwrap.dedent(""" | ||
| import pdb; pdb.Pdb().set_trace() | ||
| print('hello') | ||
| """) | ||
| save_home = os.environ.pop('HOME', None) | ||
| try: | ||
| with os_helper.temp_cwd(): | ||
| with open('.pdbrc', 'w', encoding='utf-8') as f: | ||
| f.write("Fran\u00E7ais") | ||
| with open('main.py', 'w', encoding='utf-8') as f: | ||
| f.write(script) | ||
| cmd = [sys.executable, 'main.py'] | ||
| env ={'PYTHONIOENCODING': 'ascii'} | ||
| if sys.platform == 'win32': | ||
| env['PYTHONLEGACYWINDOWSSTDIO'] = 'non-empty-string' | ||
| proc = subprocess.Popen( | ||
| cmd, | ||
| stdout=subprocess.PIPE, | ||
| stdin=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| env={**os.environ, **env} | ||
| ) | ||
| with proc: | ||
| stdout, stderr = proc.communicate(b'c\n') | ||
| self.assertIn(b"UnicodeEncodeError: \'ascii\' codec can\'t encode character " | ||
| b"\'\\xe7\' in position 21: ordinal not in range(128)", stderr) | ||
| finally: | ||
| if save_home is not None: | ||
| os.environ['HOME'] = save_home | ||
| def test_header(self): | ||
| stdout = StringIO() | ||
| header = 'Nobody expects... blah, blah, blah' | ||
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Library/2023-10-07-06-15-13.gh-issue-90095.gWn1ka.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 @@ | ||
| Make .pdbrc and -c work with any valid pdb commands. |
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.
Uh oh!
There was an error while loading. Please reload this page.