如何在调试时重启 ipdb?

How to restart ipdb while in debugging?

我的主要目标是重新启动 ipdb,它应该在代码的开头继续并抓住第一个断点。

# code.py

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

运行:

$ ./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] 此时,我将我的代码设置 val 更新为 11。现在,我想重新启动 ipdb 会话(没有关闭它)并且它应该在同一个断点上被捕获,这可能吗?

如果我输入:restart 我会收到以下错误:

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 ipython-dev@python.org

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
  • Ctrl-d Ctril-c 打破循环。
  • Ctrl-d 重新启动会话
#!/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