using breakpoints in a normal interactive session

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dan.gass@gmail.com

    #1

    using breakpoints in a normal interactive session

    Is there a way to temporarily halt execution of a script (without using
    a debugger) and have it put you in an interactive session where you
    have access to the locals? And possibly resume? For example:
    [color=blue][color=green][color=darkred]
    >>> def a():[/color][/color][/color]
    .... x = 1
    .... magic_breakpoin t()
    .... y = 1
    .... print "got here"
    ....[color=blue][color=green][color=darkred]
    >>> a()[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<stdin>", line 3, in a
    File "<stdin>", line 2, in magic_breakpoin t[color=blue][color=green][color=darkred]
    >>> x[/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> y[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'y' is not defined[color=blue][color=green][color=darkred]
    >>> magic_resume()[/color][/color][/color]
    got here[color=blue][color=green][color=darkred]
    >>> x[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'x' is not defined

  • R. Bernstein

    #2
    Re: using breakpoints in a normal interactive session

    dan.gass@gmail. com writes:
    [color=blue]
    > Is there a way to temporarily halt execution of a script (without using
    > a debugger) and have it put you in an interactive session where you
    > have access to the locals?[/color]

    Here's what I was able to do using the Extended Python debugger.
    http://bashdb.sourceforge.net/pydb/. I'm sure there's a similar (if
    not even simpler) way to do this in the stock debugger; but I'll let
    others suggest that ;-)

    First add this routine:

    def call_debugger() :
    from pydbdisp import Display, DisplayNode
    import pydb, inspect, sys
    try:
    raise Exception
    except:
    frame=inspect.c urrentframe()
    p = pydb.Pdb()
    p.reset()
    p.display = Display()
    p._program_sys_ argv = list(sys.argv)
    p.interaction(f rame, sys.exc_traceba ck)

    And then call it from your program as you indicated below for
    "magic_breakpoi nt()". For "magic_resume() " just issue "quit"
    "continue" or give a termanal EOF.

    That the above routine is so long suggests some initialization
    probably should be moved around. And in a future release (if there is
    one), I'll consider adding something like the above routine.


    And possibly resume? For example:[color=blue]
    >[color=green][color=darkred]
    > >>> def a():[/color][/color]
    > ... x = 1
    > ... magic_breakpoin t()
    > ... y = 1
    > ... print "got here"
    > ...[color=green][color=darkred]
    > >>> a()[/color][/color]
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > File "<stdin>", line 3, in a
    > File "<stdin>", line 2, in magic_breakpoin t[color=green][color=darkred]
    > >>> x[/color][/color]
    > 1[color=green][color=darkred]
    > >>> y[/color][/color]
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > NameError: name 'y' is not defined[color=green][color=darkred]
    > >>> magic_resume()[/color][/color]
    > got here[color=green][color=darkred]
    > >>> x[/color][/color]
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > NameError: name 'x' is not defined[/color]

    Comment

    • Carl Friedrich Bolz

      #3
      Re: using breakpoints in a normal interactive session

      dan.gass@gmail. com wrote:[color=blue]
      > Is there a way to temporarily halt execution of a script (without using
      > a debugger) and have it put you in an interactive session where you
      > have access to the locals? And possibly resume? For example:
      >
      >[color=green][color=darkred]
      >>>>def a():[/color][/color]
      >
      > ... x = 1
      > ... magic_breakpoin t()
      > ... y = 1
      > ... print "got here"
      > ...
      >[color=green][color=darkred]
      >>>>a()[/color][/color]
      >
      > Traceback (most recent call last):
      > File "<stdin>", line 1, in ?
      > File "<stdin>", line 3, in a
      > File "<stdin>", line 2, in magic_breakpoin t
      >[color=green][color=darkred]
      >>>>x[/color][/color]
      >
      > 1
      >[color=green][color=darkred]
      >>>>y[/color][/color]
      >
      > Traceback (most recent call last):
      > File "<stdin>", line 1, in ?
      > NameError: name 'y' is not defined
      >[color=green][color=darkred]
      >>>>magic_resum e()[/color][/color]
      >
      > got here
      >[color=green][color=darkred]
      >>>>x[/color][/color]
      >
      > Traceback (most recent call last):
      > File "<stdin>", line 1, in ?
      > NameError: name 'x' is not defined
      >[/color]

      you can use the standard-library code module for that: instead of
      magic_breakpoin t() just call code.interact(l ocal=locals()). The
      magic_resume() would be a regular Ctrl-D (or Ctrl-Z Enter under
      windows). You can also package this nicely into a convenient function,
      see for example the third example on the following page:



      Cheers,

      Carl Friedrich Bolz

      Comment

      • R. Bernstein

        #4
        Re: using breakpoints in a normal interactive session

        In revising pydb the code and documentation for the routine originally
        described, I learn that the pdb equivalent (sort of) is called
        set_trace().

        However set_trace() will terminate the program when you quit the
        debugger, so I've retained this routine and made a couple of
        corrections -- in particular to support a restart and make "show args"
        work. The changes are in pydb's CVS. Lacking a better name, the
        routine is called "debugger".

        There is one other difference between set_trace() and debugger(). In
        set_trace you stop at the statement following set_trace(), With
        debugger() the call trace shows you in debugger and you may need to
        switch to the next most-recent call frame to get info about the
        program being debugged.

        A downside of the debugger() approach is that debug session
        information can't be saved between calls: each call is a new instance
        of the debugger and when it is left via "quit" the instance is
        destroyed. (In the case of pydb.set_trace( ) the issue never comes up
        because the program is terminated on exit.)

        rocky@panix.com (R. Bernstein) writes:
        [color=blue]
        > Here's what I was able to do using the Extended Python debugger.
        > http://bashdb.sourceforge.net/pydb/. ....[/color]

        Comment

        • dan.gass@gmail.com

          #5
          Re: using breakpoints in a normal interactive session

          Carl -- Perfect! That is exactly what I want. I hoped it would be
          that easy. Thanks for taking the time to post the solution.

          Comment

          Working...