Skip to content

Commit 0449ffe

Browse files
authored
gh-104301: Allow leading whitespace in disambiguated pdb statements (#104342)
1 parent 27419a7 commit 0449ffe

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

Doc/library/pdb.rst

+11-3
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,17 @@ can be overridden by the local file.
602602

603603
Execute the (one-line) *statement* in the context of the current stack frame.
604604
The exclamation point can be omitted unless the first word of the statement
605-
resembles a debugger command. To set a global variable, you can prefix the
606-
assignment command with a :keyword:`global` statement on the same line,
607-
e.g.::
605+
resembles a debugger command, e.g.:
606+
607+
.. code-block:: none
608+
609+
(Pdb) ! n=42
610+
(Pdb)
611+
612+
To set a global variable, you can prefix the assignment command with a
613+
:keyword:`global` statement on the same line, e.g.:
614+
615+
.. code-block:: none
608616
609617
(Pdb) global list_options; list_options = ['-l']
610618
(Pdb)

Lib/pdb.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def displayhook(self, obj):
440440
self.message(repr(obj))
441441

442442
def default(self, line):
443-
if line[:1] == '!': line = line[1:]
443+
if line[:1] == '!': line = line[1:].strip()
444444
locals = self.curframe_locals
445445
globals = self.curframe.f_globals
446446
try:
@@ -1642,9 +1642,12 @@ def help_exec(self):
16421642
16431643
Execute the (one-line) statement in the context of the current
16441644
stack frame. The exclamation point can be omitted unless the
1645-
first word of the statement resembles a debugger command. To
1646-
assign to a global variable you must always prefix the command
1647-
with a 'global' command, e.g.:
1645+
first word of the statement resembles a debugger command, e.g.:
1646+
(Pdb) ! n=42
1647+
(Pdb)
1648+
1649+
To assign to a global variable you must always prefix the command with
1650+
a 'global' command, e.g.:
16481651
(Pdb) global list_options; list_options = ['-l']
16491652
(Pdb)
16501653
"""

Lib/pydoc_data/topics.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -5283,11 +5283,14 @@
52835283
'current\n'
52845284
' stack frame. The exclamation point can be omitted unless the '
52855285
'first\n'
5286-
' word of the statement resembles a debugger command. To set '
5287-
'a\n'
5288-
' global variable, you can prefix the assignment command with '
5289-
'a\n'
5290-
' "global" statement on the same line, e.g.:\n'
5286+
' word of the statement resembles a debugger command, e.g.:'
5287+
'\n'
5288+
' (Pdb) ! n=42\n'
5289+
' (Pdb)\n'
5290+
'\n'
5291+
' To set a global variable, you can prefix the assignment command '
5292+
' with \n'
5293+
' a "global" statement on the same line, e.g.:\n'
52915294
'\n'
52925295
" (Pdb) global list_options; list_options = ['-l']\n"
52935296
' (Pdb)\n'

Lib/test/test_pdb.py

+23
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,29 @@ def test_pdb_issue_gh_101517():
17981798
(Pdb) continue
17991799
"""
18001800

1801+
def test_pdb_ambiguous_statements():
1802+
"""See GH-104301
1803+
1804+
Make sure that ambiguous statements prefixed by '!' are properly disambiguated
1805+
1806+
>>> with PdbTestInput([
1807+
... '! n = 42', # disambiguated statement: reassign the name n
1808+
... 'n', # advance the debugger into the print()
1809+
... 'continue'
1810+
... ]):
1811+
... n = -1
1812+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
1813+
... print(f"The value of n is {n}")
1814+
> <doctest test.test_pdb.test_pdb_ambiguous_statements[0]>(8)<module>()
1815+
-> print(f"The value of n is {n}")
1816+
(Pdb) ! n = 42
1817+
(Pdb) n
1818+
The value of n is 42
1819+
> <doctest test.test_pdb.test_pdb_ambiguous_statements[0]>(1)<module>()
1820+
-> with PdbTestInput([
1821+
(Pdb) continue
1822+
"""
1823+
18011824

18021825
@support.requires_subprocess()
18031826
class PdbTestCase(unittest.TestCase):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow leading whitespace in disambiguated statements in :mod:`pdb`.

0 commit comments

Comments
 (0)