Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73557 > unrolled thread

Passing a frame to pdb.Pdb.set_trace

Started byRotwang <sg552@hotmail.co.uk>
First post2014-06-24 20:10 +0100
Last post2014-06-25 21:59 +0100
Articles 2 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Passing a frame to pdb.Pdb.set_trace Rotwang <sg552@hotmail.co.uk> - 2014-06-24 20:10 +0100
    Re: Passing a frame to pdb.Pdb.set_trace Rotwang <sg552@hotmail.co.uk> - 2014-06-25 21:59 +0100

#73557 — Passing a frame to pdb.Pdb.set_trace

FromRotwang <sg552@hotmail.co.uk>
Date2014-06-24 20:10 +0100
SubjectPassing a frame to pdb.Pdb.set_trace
Message-ID<locifn$rh2$1@dont-email.me>
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?

[toc] | [next] | [standalone]


#73591

FromRotwang <sg552@hotmail.co.uk>
Date2014-06-25 21:59 +0100
Message-ID<lofd7b$r3s$1@dont-email.me>
In reply to#73557
On 24/06/2014 20:10, Rotwang wrote:
> 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):
>
> [...]

Never mind, I figured out what's going on thanks to an excellent article 
by Ned Batchelder explaining the internals of pdb:

http://nedbatchelder.com/text/trace-function.html

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web