Re: pdb.run/runcall return values?

Guido.van.Rossum@cwi.nl
Fri, 17 Feb 1995 11:17:46 +0100

> Is there a direct way to get the result of a string or
> function call run under pdb?
>
> I want to be able to route embedded calls to the debugger
> if a switch is set, but return the result to the caller,
> so it can continue. I can do it now, by:
>
> - to run a string expression 'str':
> if debugging
> pdb.run("_res = <str>")
> res = getattr(main, "_res")
> else
> res = run_string(<str>,...)
>
> - to run a func(*args):
> predefine a dummy function:
> def runit(func, args):
> global _res
> _res = apply(func, args)
> if debugging
> pdb.runcall(dummy.runit, func, args)
> res = getattr(dummy, "_res")
> else
> res = call_object(func, args)
>
> but this seems a roundabout way of doing it. Is there an
> easy way to do this (subclassing Pdb,...)?

It is an easy change to bdb.Pdb.runcall and pdb.runcall to let runcall
return the result of the function call, and I'll fix this in 1.2 (I
just never thought of that).

Changing run() to return the value of the expression is not really
possible without changing the interface in an incompatible way, since
it currently accepts statement syntax.

I suppose I could add a function and method runeval() which is like
run() but evaluates an expression. You can also fake this using the
modified runcall(): runcall(eval, "...").

Here's the patch for runcall:

===================================================================
RCS file: /ufs/guido/CVSROOT/python/Lib/bdb.py,v
retrieving revision 1.12
diff -u -1 -r1.12 bdb.py
--- 1.12 1995/02/03 12:50:04
+++ bdb.py 1995/02/17 10:11:54
@@ -294,2 +294,3 @@
sys.settrace(self.trace_dispatch)
+ res = None
try:
@@ -296,3 +297,3 @@
try:
- apply(func, args)
+ res = apply(func, args)
except BdbQuit:
@@ -302,2 +303,3 @@
sys.settrace(None)
+ return res

===================================================================
RCS file: /ufs/guido/CVSROOT/python/Lib/pdb.py,v
retrieving revision 1.20
diff -u -1 -r1.20 pdb.py
--- 1.20 1995/02/03 12:50:02
+++ pdb.py 1995/02/17 10:11:07
@@ -438,3 +438,3 @@
def runcall(*args):
- apply(Pdb().runcall, args)
+ return apply(Pdb().runcall, args)

===================================================================

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>