Skip to content

Commit 076300d

Browse files
gh-126944: Show explicit errors when required arguments of pdb commands are missing (#130240)
1 parent 427dd10 commit 076300d

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

Lib/pdb.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,9 @@ def do_enable(self, arg):
12861286
Enables the breakpoints given as a space separated list of
12871287
breakpoint numbers.
12881288
"""
1289+
if not arg:
1290+
self._print_invalid_arg(arg)
1291+
return
12891292
args = arg.split()
12901293
for i in args:
12911294
try:
@@ -1307,6 +1310,9 @@ def do_disable(self, arg):
13071310
breakpoint, it remains in the list of breakpoints and can be
13081311
(re-)enabled.
13091312
"""
1313+
if not arg:
1314+
self._print_invalid_arg(arg)
1315+
return
13101316
args = arg.split()
13111317
for i in args:
13121318
try:
@@ -1327,6 +1333,9 @@ def do_condition(self, arg):
13271333
condition is absent, any existing condition is removed; i.e.,
13281334
the breakpoint is made unconditional.
13291335
"""
1336+
if not arg:
1337+
self._print_invalid_arg(arg)
1338+
return
13301339
args = arg.split(' ', 1)
13311340
try:
13321341
cond = args[1]
@@ -1360,6 +1369,9 @@ def do_ignore(self, arg):
13601369
and the breakpoint is not disabled and any associated
13611370
condition evaluates to true.
13621371
"""
1372+
if not arg:
1373+
self._print_invalid_arg(arg)
1374+
return
13631375
args = arg.split()
13641376
if not args:
13651377
self.error('Breakpoint number expected')
@@ -1690,6 +1702,9 @@ def do_jump(self, arg):
16901702
instance it is not possible to jump into the middle of a
16911703
for loop or out of a finally clause.
16921704
"""
1705+
if not arg:
1706+
self._print_invalid_arg(arg)
1707+
return
16931708
if self.curindex + 1 != len(self.stack):
16941709
self.error('You can only jump within the bottom frame')
16951710
return
@@ -1715,6 +1730,9 @@ def do_debug(self, arg):
17151730
argument (which is an arbitrary expression or statement to be
17161731
executed in the current environment).
17171732
"""
1733+
if not arg:
1734+
self._print_invalid_arg(arg)
1735+
return
17181736
sys.settrace(None)
17191737
globals = self.curframe.f_globals
17201738
locals = self.curframe.f_locals
@@ -1840,13 +1858,19 @@ def do_p(self, arg):
18401858
18411859
Print the value of the expression.
18421860
"""
1861+
if not arg:
1862+
self._print_invalid_arg(arg)
1863+
return
18431864
self._msg_val_func(arg, repr)
18441865

18451866
def do_pp(self, arg):
18461867
"""pp expression
18471868
18481869
Pretty-print the value of the expression.
18491870
"""
1871+
if not arg:
1872+
self._print_invalid_arg(arg)
1873+
return
18501874
self._msg_val_func(arg, pprint.pformat)
18511875

18521876
complete_print = _complete_expression
@@ -1935,6 +1959,9 @@ def do_source(self, arg):
19351959
19361960
Try to get source code for the given object and display it.
19371961
"""
1962+
if not arg:
1963+
self._print_invalid_arg(arg)
1964+
return
19381965
try:
19391966
obj = self._getval(arg)
19401967
except:
@@ -1974,6 +2001,9 @@ def do_whatis(self, arg):
19742001
19752002
Print the type of the argument.
19762003
"""
2004+
if not arg:
2005+
self._print_invalid_arg(arg)
2006+
return
19772007
try:
19782008
value = self._getval(arg)
19792009
except:
@@ -2318,7 +2348,10 @@ def _help_message_from_doc(self, doc, usage_only=False):
23182348
def _print_invalid_arg(self, arg):
23192349
"""Return the usage string for a function."""
23202350

2321-
self.error(f"Invalid argument: {arg}")
2351+
if not arg:
2352+
self.error("Argument is required for this command")
2353+
else:
2354+
self.error(f"Invalid argument: {arg}")
23222355

23232356
# Yes it's a bit hacky. Get the caller name, get the method based on
23242357
# that name, and get the docstring from that method.

Lib/test/test_pdb.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1758,10 +1758,12 @@ def test_pdb_invalid_arg():
17581758
>>> def test_function():
17591759
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
17601760
1761-
>>> with PdbTestInput([
1761+
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
17621762
... 'a = 3',
17631763
... 'll 4',
17641764
... 'step 1',
1765+
... 'p',
1766+
... 'enable ',
17651767
... 'continue'
17661768
... ]):
17671769
... test_function()
@@ -1776,6 +1778,12 @@ def test_pdb_invalid_arg():
17761778
(Pdb) step 1
17771779
*** Invalid argument: 1
17781780
Usage: s(tep)
1781+
(Pdb) p
1782+
*** Argument is required for this command
1783+
Usage: p expression
1784+
(Pdb) enable
1785+
*** Argument is required for this command
1786+
Usage: enable bpnumber [bpnumber ...]
17791787
(Pdb) continue
17801788
"""
17811789

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Show explicit errors when required arguments of :mod:`pdb` commands are missing

0 commit comments

Comments
 (0)