BdbQuit raised when debugging Python with pdb

前端 未结 5 1919
南笙
南笙 2020-12-14 00:22

Recently when adding the pdb debugger to my Python 2.7.10 code, I get this message:

Traceback (most recent call last):
  File "/Users/isaach         


        
5条回答
  •  臣服心动
    2020-12-14 00:35

    If you continue from the (pdb) prompt and allow your code to finish normally, I wouldn't expect output like the traceback you indicated, but if you quit pdb, with the quit command or ^D (EOF), a traceback like that occurs because there is nothing to catch the BdbQuit exception raised when the debugger quits. In bdb.py self.quitting gets set to True by the set_quit method (and by finally clauses in the various run methods). Dispatch methods called by trace_dispatch raise BdbQuit when self.quitting is True, and the typical except: clause for BdbQuit is a simple pass statement; pdb inherits all of that from gdb.

    In short, exception handling is used to disable the system trace function used by the debugger, when the debugger interaction finishes early.

    One way to avoid that traceback altogether is to use pdb differently. Rather than calling pdb.set_trace() from your code (and not handling BdbQuit at all), you can invoke your code within pdb (rather than vice versa), at which point the BdbQuit exception will be handled as intended by pdb. That will also allow you to choose breakpoint locations without modifying your code (using pdb's break command). Or you can mix the two approaches; run your code under pdb, pdb.set_trace() calls and all, and those calls will be breakpoints that you can remove only by modifying your code.

    You can invoke your code within pdb by using the pdb command with your script invocation as its command line arguments, or with python -m pdb.

提交回复
热议问题