Hi all, I've found something weird with pdb and I don't understand it. I
want to define a function mydebugger() which starts the debugger in the
caller's frame. The following is copied from IDLE with Python 2.7.3
(I've since tried it with 3.3.0 and the same thing happens):
Python 2.7.3 (default, Feb 27 2014, 19:58:35)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> import pdb, sys
>>> def f(x):
mydebugger()
>>> def mydebugger():
frame = sys._getframe().f_back
pdb.Pdb().set_trace(frame)
>>> f(4)
--Return--
> (2)f()->None
(Pdb) x
4
This is what I expect: sys._getframe().f_back gives f's frame, so the
call to mydebugger() within f does approximately the same thing as if
I'd just called pdb.set_trace() instead. But when I add another
statement to mydebugger, this happens:
>>> def mydebugger():
frame = sys._getframe().f_back
pdb.Pdb().set_trace(frame)
print 'hmm'
>>> f(4)
--Call--
> /usr/lib/python2.7/idlelib/rpc.py(546)__getattr__()
-> def __getattr__(self, name):
(Pdb) x
*** NameError: name 'x' is not defined
(Pdb) w #Where am I?
(1)()
/usr/lib/python2.7/idlelib/run.py(97)main()
-> ret = method(*args, **kwargs)
/usr/lib/python2.7/idlelib/run.py(298)runcode()
-> exec code in self.locals
(1)()
(2)f()
(4)mydebugger()
> /usr/lib/python2.7/idlelib/rpc.py(546)__getattr__()
-> def __getattr__(self, name):
The same thing happens if I define
frame = sys._getframe().f_back.f_back
(i.e. the debugger starts in the same place) for example, though if I define
frame = sys._getframe()
then the debugger starts in mydebugger's frame as I would expect. Also,
whether it goes wrong depends on what the third line of mydebugger is;
some kinds of statement consistently cause the problem and others
consistently don't.
When I try the above simple code in the terminal rather than IDLE it
works like it should, but in the more complicated version where I first
noticed it it still goes wrong. Can anyone help?
--
https://mail.python.org/mailman/listinfo/python-list