From c889443808507937d1a41fdfef90374ce9e285b7 Mon Sep 17 00:00:00 2001 From: James Gerity Date: Tue, 9 May 2023 18:41:42 -0400 Subject: [PATCH 1/8] Allow leading whitespace in disambiguated pdb statements --- Lib/pdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index b3dc5a455e56b9..2f6ba48c57983e 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -440,7 +440,7 @@ def displayhook(self, obj): self.message(repr(obj)) def default(self, line): - if line[:1] == '!': line = line[1:] + if line[:1] == '!': line = line[1:].lstrip() locals = self.curframe_locals globals = self.curframe.f_globals try: From 34c84e28b595739dced6657f7d4e17b11a5a237d Mon Sep 17 00:00:00 2001 From: James Gerity Date: Tue, 9 May 2023 18:41:52 -0400 Subject: [PATCH 2/8] Add example of disambiguated statement to pdb docs --- Doc/library/pdb.rst | 14 +++++++++++--- Lib/pdb.py | 9 ++++++--- Lib/pydoc_data/topics.py | 13 ++++++++----- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index 74bffef5562ae1..ef52370bff8058 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -602,9 +602,17 @@ can be overridden by the local file. Execute the (one-line) *statement* in the context of the current stack frame. The exclamation point can be omitted unless the first word of the statement - resembles a debugger command. To set a global variable, you can prefix the - assignment command with a :keyword:`global` statement on the same line, - e.g.:: + resembles a debugger command, e.g.: + + .. code-block:: none + + (Pdb) ! n=42 + (Pdb) + + To set a global variable, you can prefix the assignment command with a + :keyword:`global` statement on the same line, e.g.: + + .. code-block:: none (Pdb) global list_options; list_options = ['-l'] (Pdb) diff --git a/Lib/pdb.py b/Lib/pdb.py index 2f6ba48c57983e..a286e662f0de84 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1642,9 +1642,12 @@ def help_exec(self): Execute the (one-line) statement in the context of the current stack frame. The exclamation point can be omitted unless the - first word of the statement resembles a debugger command. To - assign to a global variable you must always prefix the command - with a 'global' command, e.g.: + first word of the statement resembles a debugger command, e.g.: + (Pdb) ! n=42 + (Pdb) + + To assign to a global variable you must always prefix the command with + a 'global' command, e.g.: (Pdb) global list_options; list_options = ['-l'] (Pdb) """ diff --git a/Lib/pydoc_data/topics.py b/Lib/pydoc_data/topics.py index 1babb5ce9476c9..3aaaee67fa35a6 100644 --- a/Lib/pydoc_data/topics.py +++ b/Lib/pydoc_data/topics.py @@ -5283,11 +5283,14 @@ 'current\n' ' stack frame. The exclamation point can be omitted unless the ' 'first\n' - ' word of the statement resembles a debugger command. To set ' - 'a\n' - ' global variable, you can prefix the assignment command with ' - 'a\n' - ' "global" statement on the same line, e.g.:\n' + ' word of the statement resembles a debugger command, e.g.:' + '\n' + ' (Pdb) ! n=42\n' + ' (Pdb)\n' + '\n' + ' To set a global variable, you can prefix the assignment command ' + ' with \n' + ' a "global" statement on the same line, e.g.:\n' '\n' " (Pdb) global list_options; list_options = ['-l']\n" ' (Pdb)\n' From 3cf0d8aa5baf6abc9b931c5a9acb280437f9f908 Mon Sep 17 00:00:00 2001 From: James Gerity Date: Tue, 9 May 2023 18:46:36 -0400 Subject: [PATCH 3/8] Add NEWS entry --- .../next/Library/2023-05-09-18-46-24.gh-issue-104301.gNnbId.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-05-09-18-46-24.gh-issue-104301.gNnbId.rst diff --git a/Misc/NEWS.d/next/Library/2023-05-09-18-46-24.gh-issue-104301.gNnbId.rst b/Misc/NEWS.d/next/Library/2023-05-09-18-46-24.gh-issue-104301.gNnbId.rst new file mode 100644 index 00000000000000..a44ad765e62458 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-05-09-18-46-24.gh-issue-104301.gNnbId.rst @@ -0,0 +1 @@ +Allow leading whitespace in disambiguated statements in :mod:`pdb`. From 3968695feb3b9dc6ffa6a95c1ee8652f941a922d Mon Sep 17 00:00:00 2001 From: James Gerity Date: Wed, 10 May 2023 18:33:23 -0400 Subject: [PATCH 4/8] Use strip() instead of lstrip() --- Lib/pdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index a286e662f0de84..6b6feac1ddead1 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -440,7 +440,7 @@ def displayhook(self, obj): self.message(repr(obj)) def default(self, line): - if line[:1] == '!': line = line[1:].lstrip() + if line[:1] == '!': line = line[1:].strip() locals = self.curframe_locals globals = self.curframe.f_globals try: From ebf179d3ef1ce2dc4dfb89d8cdb4a1a3f34335d0 Mon Sep 17 00:00:00 2001 From: James Gerity Date: Thu, 11 May 2023 11:02:28 -0400 Subject: [PATCH 5/8] Add test for disambiguated statements in pdb --- Lib/test/test_pdb.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 482c92dbf1f6a0..dfaff74588d4f7 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1798,6 +1798,29 @@ def test_pdb_issue_gh_101517(): (Pdb) continue """ +def test_pdb_issue_gh_104301(): + """See GH-104301 + + Make sure that ambiguous statements prefixed by '!' are properly disambiguated + + >>> with PdbTestInput([ + ... '! n = 42', # disambiguated statement: reassign the name n + ... 'n', # advance the debugger into the print() + ... 'continue' + ... ]): + ... n = -1 + ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() + ... print(n) + > (8)() + -> print(n) + (Pdb) ! n = 42 + (Pdb) n + 42 + > (1)() + -> with PdbTestInput([ + (Pdb) continue + """ + @support.requires_subprocess() class PdbTestCase(unittest.TestCase): From ac6e0e39d21ff720d60c61fbe673b78265735a05 Mon Sep 17 00:00:00 2001 From: James Gerity Date: Thu, 11 May 2023 11:22:24 -0400 Subject: [PATCH 6/8] Use more explicit print() --- Lib/test/test_pdb.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index dfaff74588d4f7..bf8b0ae054f35b 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1810,12 +1810,12 @@ def test_pdb_issue_gh_104301(): ... ]): ... n = -1 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() - ... print(n) + ... print(f"The value of n is {n}") > (8)() - -> print(n) + -> print(f"The value of n is {n}") (Pdb) ! n = 42 (Pdb) n - 42 + The value of n is 42 > (1)() -> with PdbTestInput([ (Pdb) continue From 778a4ef9560914084e13d23aac309e65f8663e8c Mon Sep 17 00:00:00 2001 From: James Gerity Date: Thu, 11 May 2023 11:22:58 -0400 Subject: [PATCH 7/8] Use better name for test Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Lib/test/test_pdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index bf8b0ae054f35b..43e77143e1af73 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1798,7 +1798,7 @@ def test_pdb_issue_gh_101517(): (Pdb) continue """ -def test_pdb_issue_gh_104301(): +def test_pdb_ambiguous_statements(): """See GH-104301 Make sure that ambiguous statements prefixed by '!' are properly disambiguated From 5f6aecb16028029384bc09eb7ab3c669a117dd6b Mon Sep 17 00:00:00 2001 From: James Gerity Date: Thu, 11 May 2023 12:18:39 -0400 Subject: [PATCH 8/8] Use correct name in doctest expected output --- Lib/test/test_pdb.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 43e77143e1af73..037673dd0ea802 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1811,12 +1811,12 @@ def test_pdb_ambiguous_statements(): ... n = -1 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() ... print(f"The value of n is {n}") - > (8)() + > (8)() -> print(f"The value of n is {n}") (Pdb) ! n = 42 (Pdb) n The value of n is 42 - > (1)() + > (1)() -> with PdbTestInput([ (Pdb) continue """