Reputation: 9093
I know I can use PDB to trace through a module, and that I can execute an arbitrary command from pdb by prepending it with a ! (e.g. !"foo".upper()
).
Is there some way to combine these functionalities to trace through an arbitrary command executed in the current context? E.g. something like step !"foo".upper()"
that would let me step through the upper
method, and then return to the earlier context?
Upvotes: 3
Views: 448
Reputation: 15548
Use pdb.runcall
(Pdb) pdb.runcall(func, *args, **kwds) # e.g. pdb.runcall(myfunc, arg1, arg2)
and step through it by (n or s) or set a breakpoint into it
(Pdb) b my_module.py:123 # b ([file:]lineno | function) [, condition]
and run to the breakpoint by pressing "c".
You can not trace builtin functions like str.upper that you used in your example.
EDIT: You asked also for the current context:
You can evaluate and debug an expression with a user defined function in the current context. Example:
pdb.runeval("[myfunc(x) for x in range(3)]", globals(), locals())
# or with ... some_module.globals())
The parameters globals(), locals()
are important because without them the expression will be evaluated in the __main__
module context, or directly in the current module context if used without locals(), but not in the current global and local context, as it is usual with !expression
.
Upvotes: 3