pdb:完成后跳过重启

pdb: skip restart when finished

python -m pdb -c "c" script.py

出现问题时进入调试模式。从 doc 中,我发现选项 -c "c" (Python 3.2+) 使我无需点击 c + Enter 每次在程序启动时。

然而,当程序正常结束时,它输出 The program finished and will be restarted 和 我仍然需要点击 q + Enter 来退出程序。 还有办法跳过这个吗?

您可以在一个序列中为 -c 添加多个命令。

方法 1:只有在没有遇到错误时才退出

如果没有遇到错误,您可以再输入一个命令q 跳出 pdb 模式。但是,如果遇到错误,它将进入调试模式,您必须继续点击 c 并继续前进。

python -mpdb -c "c" -c "q" script.py

没有遇到错误(立即退出!)-

(base) $ python -mpdb -c "c" -c "q" script.py
The program finished and will be restarted
(base) $ 

遇到错误(进入调试模式!)-

(base) $ python -mpdb -c "c" -c "q" script.py
Traceback (most recent call last):
  File "/anaconda3/lib/python3.7/pdb.py", line 1701, in main
    pdb._runscript(mainpyfile)
  File "/anaconda3/lib/python3.7/pdb.py", line 1570, in _runscript
    self.run(statement)
  File "/anaconda3/lib/python3.7/bdb.py", line 585, in run
    exec(cmd, globals, locals)
  File "<string>", line 1, in <module>
  File "/Projects/Random/script.py", line 6, in <module>
    """
ModuleNotFoundError: No module named 'thispackagedoesntexist'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
Post mortem debugger finished. The script.py will be restarted
> /Projects/Random/script.py(6)<module>()
-> """
(Pdb) 

方法二:不管有无错误都退出

您可以使用 echo "q" 并通过以下方式使用 | 将其传递给下一个 (pdb) 命令。这将 运行 第二个命令一次,并立即获取 echo "q" 的输出以退出 -

echo "q" | python -mpdb -c "c" script.py

这会在程序完成后点击 q 运行在调试模式下运行脚本。调试遇到(或未遇到)错误后自动退出。

没有遇到错误(立即退出!)-

(base) $ echo "q" | python -mpdb -c "c" script.py
The program finished and will be restarted
> /Projects/Random/script.py(6)<module>()
-> """
(Pdb) (base) $ 

遇到错误(立即退出!)-

(base) $ echo "q" | python -mpdb -c "c" script.py
Traceback (most recent call last):
  File "/anaconda3/lib/python3.7/pdb.py", line 1701, in main
    pdb._runscript(mainpyfile)
  File "/anaconda3/lib/python3.7/pdb.py", line 1570, in _runscript
    self.run(statement)
  File "/anaconda3/lib/python3.7/bdb.py", line 585, in run
    exec(cmd, globals, locals)
  File "<string>", line 1, in <module>
  File /Projects/Random/script.py", line 6, in <module>
    """
ModuleNotFoundError: No module named 'thispackagedoesntexist'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /Projects/Random/script.py(6)<module>()
-> """
(Pdb) Post mortem debugger finished. The script.py will be restarted
> /Projects/Random/script.py(6)<module>()
-> """
(Pdb) 
(base) $ 

这是您可以与 pdb 一起使用的命令列表 -

(Pdb) help

Documented commands (type help <topic>):
========================================
EOF    c          d        h         list      q        rv       undisplay
a      cl         debug    help      ll        quit     s        unt
alias  clear      disable  ignore    longlist  r        source   until
args   commands   display  interact  n         restart  step     up
b      condition  down     j         next      return   tbreak   w
break  cont       enable   jump      p         retval   u        whatis
bt     continue   exit     l         pp        run      unalias  where

您可以简单地使用 -- python3 -mpdb -c "q" -c "c" example.py

Link