1

Sometimes when I invoke ipdb, I know I want to be a frame above where the trace is set. I presume that's why the API exposes the frame parameter (as discussed in the documentation).

So here's the function:

import inspect, ipdb

def invoke_a_frame_up():
   ipdb.set_trace(frame=inspect.stack()[i][0])

I'm trying to figure out what i is in the function so that, when invoke_a_frame_up gets called, the trace is not inside invoke_a_frame_up.

Or, more aptly, I'm trying to figure out how to achieve this generally. I've tried every i for the depth of the stack and the trace seems to start at the same frame regardless, until i is greater than the depth, at which point I get IndexError('list index out of range',).

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
HaPsantran
  • 5,581
  • 6
  • 24
  • 39
  • Is there something wrong with just doing `up` command? – wim Jan 25 '18 at 21:23
  • @HaPsantran You should create your answer below, and mark it as accepted if it works for you. Do not edit your questions to include an answer. – Blue Jan 26 '18 at 15:29

1 Answers1

1
import inspect
import ipdb

def dbg_up():
    ipdb.set_trace(inspect.currentframe().f_back.f_back)

def foo():
    var = 'in foo'
    bar()

def bar():
    var = 'in bar'
    dbg_up()

foo()

Users of vanilla pdb: your interface is slightly different, like this:

pdb.Pdb().set_trace(inspect.currentframe().f_back.f_back)
wim
  • 338,267
  • 99
  • 616
  • 750
  • Thank you. Your example works for me too, but when I add quit() *after* the set_trace() call, I do not get the desired result anymore. I'll edit my question to further explain. – HaPsantran Jan 26 '18 at 15:22