Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 46b9afc

Browse files
committed
#1472251: remove addition of "\n" to code given to pdb.run[eval](), the bug in exec() that made this necessary has been fixed. Also document that you can give code objects to run() and runeval(), and add some tests to test_pdb.
1 parent 44f8bf9 commit 46b9afc

3 files changed

Lines changed: 59 additions & 19 deletions

File tree

Doc/library/pdb.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,21 @@ slightly different way:
8585

8686
.. function:: run(statement, globals=None, locals=None)
8787

88-
Execute the *statement* (given as a string) under debugger control. The
89-
debugger prompt appears before any code is executed; you can set breakpoints
90-
and type :pdbcmd:`continue`, or you can step through the statement using
91-
:pdbcmd:`step` or :pdbcmd:`next` (all these commands are explained below).
92-
The optional *globals* and *locals* arguments specify the environment in
93-
which the code is executed; by default the dictionary of the module
94-
:mod:`__main__` is used. (See the explanation of the built-in :func:`exec`
95-
or :func:`eval` functions.)
88+
Execute the *statement* (given as a string or a code object) under debugger
89+
control. The debugger prompt appears before any code is executed; you can
90+
set breakpoints and type :pdbcmd:`continue`, or you can step through the
91+
statement using :pdbcmd:`step` or :pdbcmd:`next` (all these commands are
92+
explained below). The optional *globals* and *locals* arguments specify the
93+
environment in which the code is executed; by default the dictionary of the
94+
module :mod:`__main__` is used. (See the explanation of the built-in
95+
:func:`exec` or :func:`eval` functions.)
9696

9797

9898
.. function:: runeval(expression, globals=None, locals=None)
9999

100-
Evaluate the *expression* (given as a string) under debugger control. When
101-
:func:`runeval` returns, it returns the value of the expression. Otherwise
102-
this function is similar to :func:`run`.
100+
Evaluate the *expression* (given as a string or a code object) under debugger
101+
control. When :func:`runeval` returns, it returns the value of the
102+
expression. Otherwise this function is similar to :func:`run`.
103103

104104

105105
.. function:: runcall(function, *args, **kwds)

Lib/bdb.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,9 @@ def format_stack_entry(self, frame_lineno, lprefix=': '):
364364
if line: s = s + lprefix + line.strip()
365365
return s
366366

367-
# The following two methods can be called by clients to use
368-
# a debugger to debug a statement, given as a string.
367+
# The following methods can be called by clients to use
368+
# a debugger to debug a statement or an expression.
369+
# Both can be given as a string, or a code object.
369370

370371
def run(self, cmd, globals=None, locals=None):
371372
if globals is None:
@@ -375,8 +376,6 @@ def run(self, cmd, globals=None, locals=None):
375376
locals = globals
376377
self.reset()
377378
sys.settrace(self.trace_dispatch)
378-
if not isinstance(cmd, types.CodeType):
379-
cmd = cmd+'\n'
380379
try:
381380
exec(cmd, globals, locals)
382381
except BdbQuit:
@@ -393,8 +392,6 @@ def runeval(self, expr, globals=None, locals=None):
393392
locals = globals
394393
self.reset()
395394
sys.settrace(self.trace_dispatch)
396-
if not isinstance(expr, types.CodeType):
397-
expr = expr+'\n'
398395
try:
399396
return eval(expr, globals, locals)
400397
except BdbQuit:

Lib/test/test_pdb.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# A test suite for pdb; at the moment, this only validates skipping of
2-
# specified test modules (RFE #5142).
1+
# A test suite for pdb; not very comprehensive at the moment.
32

43
import imp
54
import sys
@@ -123,6 +122,50 @@ def test_pdb_skip_modules_with_callback():
123122
"""
124123

125124

125+
def pdb_invoke(method, arg):
126+
"""Run pdb.method(arg)."""
127+
import pdb; getattr(pdb, method)(arg)
128+
129+
130+
def test_pdb_run_with_incorrect_argument():
131+
"""Testing run and runeval with incorrect first argument.
132+
133+
>>> pti = PdbTestInput(['continue',])
134+
>>> with pti:
135+
... pdb_invoke('run', lambda x: x)
136+
Traceback (most recent call last):
137+
TypeError: exec() arg 1 must be a string, bytes or code object
138+
139+
>>> with pti:
140+
... pdb_invoke('runeval', lambda x: x)
141+
Traceback (most recent call last):
142+
TypeError: eval() arg 1 must be a string, bytes or code object
143+
"""
144+
145+
146+
def test_pdb_run_with_code_object():
147+
"""Testing run and runeval with code object as a first argument.
148+
149+
>>> with PdbTestInput(['step','x', 'continue']):
150+
... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
151+
> <string>(1)<module>()
152+
(Pdb) step
153+
--Return--
154+
> <string>(1)<module>()->None
155+
(Pdb) x
156+
1
157+
(Pdb) continue
158+
159+
>>> with PdbTestInput(['x', 'continue']):
160+
... x=0
161+
... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
162+
> <string>(1)<module>()->None
163+
(Pdb) x
164+
1
165+
(Pdb) continue
166+
"""
167+
168+
126169
def test_main():
127170
from test import test_pdb
128171
support.run_doctest(test_pdb, verbosity=True)

0 commit comments

Comments
 (0)