Search code examples
pythonpdbipdb

How to restart ipdb while in debugging?


My main goal is to restart ipdb, which should continue on the beginning on the code and catch on the first break-point.

# code.py

val = 10
print(f"Hello_World {val}")
__import__('ipdb').set_trace(context=5)
print("Goodbye_World")

run:

$ ./code.py
Hello_World 10
> /home/alper/eBlocBroker/doo.py(4)<module>()
      1 val = 10
      2 print(f"Hello_World {val}")
      3 __import__('ipdb').set_trace(context=5)
----> 4 print("Goodbye_World")   // right away catched here 

ipdb>

[Q] At this point, I updated my code setting val into 11. Now, I want to restart the ipdb session (without closing it) and it should be catched on the same break point, is it possible?

  • If I run as python3 -m ipdb code.py it does not start from the breakpoint instead it starts from top of the file, which I don't want.

If I type: restart I get following error:

Traceback (most recent call last):
  File "doo.py", line 4, in <module>
    print("Goodbye_World")
  File "doo.py", line 4, in <module>
    print("Goodbye_World")
  File "/usr/lib/python3.7/bdb.py", line 88, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/lib/python3.7/bdb.py", line 112, in dispatch_line
    self.user_line(frame)
  File "/usr/lib/python3.7/pdb.py", line 259, in user_line
    self.interaction(frame, None)
  File "/home/alper/venv/lib/python3.7/site-packages/IPython/core/debugger.py", line 305, in interaction
    OldPdb.interaction(self, frame, traceback)
  File "/usr/lib/python3.7/pdb.py", line 350, in interaction
    self._cmdloop()
  File "/usr/lib/python3.7/pdb.py", line 319, in _cmdloop
    self.cmdloop()
  File "/home/alper/venv/lib/python3.7/site-packages/IPython/terminal/debugger.py", line 126, in cmdloop
    stop = self.onecmd(line)
  File "/usr/lib/python3.7/pdb.py", line 416, in onecmd
    return cmd.Cmd.onecmd(self, line)
  File "/usr/lib/python3.7/cmd.py", line 217, in onecmd
    return func(arg)
  File "/usr/lib/python3.7/pdb.py", line 1026, in do_run
    raise Restart
pdb.Restart

If you suspect this is an IPython 7.16.1 bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at [email protected]

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    %config Application.verbose_crash=True

Solution

    • Ctrl-d Ctril-c to break the loop.
    • Ctrl-d to restart the session
    #!/bin/bash
    
    control_c()
    {
        printf goodbye
        # kill -SIGINT $(jobs -p)
        exit #$
    }
    
    trap control_c SIGINT
    
    while true
    do
        ./Driver.py
        # sleep 10 &
        wait
        printf "\n"
        # loop infinitely
    done