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


Groups > comp.lang.python > #73557

Passing a frame to pdb.Pdb.set_trace

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Rotwang <sg552@hotmail.co.uk>
Newsgroups comp.lang.python
Subject Passing a frame to pdb.Pdb.set_trace
Date Tue, 24 Jun 2014 20:10:46 +0100
Organization A noiseless patient Spider
Lines 73
Message-ID <locifn$rh2$1@dont-email.me> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Tue, 24 Jun 2014 19:10:47 +0000 (UTC)
Injection-Info mx05.eternal-september.org; posting-host="cf9f28f6639b0d937e20ee4b3a33608e"; logging-data="28194"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/KzW4sI6Mw3OSc/SIIpYaK"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
Cancel-Lock sha1:mOjieDVgKWkWdiVkIZXhUysvj2Y=
Xref csiph.com comp.lang.python:73557

Show key headers only | View raw


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?

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web