python - how to use the post_mortem method of pdb? -
i trying understand how use pdb.post_mortem() method.
for given file
# expdb.py import pdb import trace def hello(): = 6 * 9 b = 7 ** 2 c = * b d = 4 / 0 print(c) tracer = trace.trace() command prompt
''' # first try λ python -i expdb.py >>> pdb.post_mortem() traceback (most recent call last): file "<stdin>", line 1, in <module> file "c:\program files\anaconda3\lib\pdb.py", line 1590, in post_mortem raise valueerror("a valid traceback must passed if no " valueerror: valid traceback must passed if no exception being handled ''' ''' # second try λ python -i expdb.py >>> pdb.post_mortem(traceback=tracer.run('hello()') ) --- modulename: trace, funcname: _unsettrace trace.py(77): sys.settrace(none) traceback (most recent call last): file "<stdin>", line 1, in <module> file "c:\program files\anaconda3\lib\trace.py", line 500, in run self.runctx(cmd, dict, dict) file "c:\program files\anaconda3\lib\trace.py", line 508, in runctx exec(cmd, globals, locals) file "<string>", line 1, in <module> file "expdb.py", line 8, in hello d = 4 / 0 zerodivisionerror: division 0 >>>
the post_mortem method wants traceback object, not trace object. traceback objects can acquired sys.exec_info()[2] inside of except block, or can call pdb.post_mortem() no arguments directly (in except block).
but either way, must catch exception before can debug it.
Comments
Post a Comment