Issue 36388: pdb: do_debug installs sys.settrace handler when used inside post_mortem (original) (raw)

It seems like the "debug" command is not properly handled with "post_mortem()".

It appears due to using sys.settrace(self.trace_dispatch) in the end of do_debug, although no tracing is installed with post_mortem in the first place.

More info:

Given the following test script:

def exc():
    raise Exception()


try:
    exc()
except Exception:
    import pdb
    pdb.post_mortem()

The behavior with just "quit" is fine:

% python3.8 t_pdb.py
> …/project/t_pdb.py(2)exc()
-> raise Exception()
(Pdb) q

But when using debug inside of it, it will stop at cmd.postcmd, and you have to use "continue" twice:

% python3.8 t_pdb.py
> …/project/t_pdb.py(2)exc()
-> raise Exception()
(Pdb) debug print(1)
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()
((Pdb)) c
1
LEAVING RECURSIVE DEBUGGER
> …/pyenv/3.8-dev/lib/python3.8/cmd.py(159)postcmd()
-> return stop
(Pdb) c
(Pdb) c

Also when using quit inside of the debug:

% python3.8 t_pdb.py
> …/project/t_pdb.py(2)exc()
-> raise Exception()
(Pdb) debug print(1)
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()
((Pdb)) q
LEAVING RECURSIVE DEBUGGER
> …/pyenv/3.8-dev/lib/python3.8/cmd.py(159)postcmd()
-> return stop
(Pdb) c
(Pdb) c

When using quit when at postcmd() it will even raise BdbQuit:

% python3.8 t_pdb.py
> …/project/t_pdb.py(2)exc()
-> raise Exception()
(Pdb) debug print(1)
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()
((Pdb)) q
LEAVING RECURSIVE DEBUGGER
> …/pyenv/3.8-dev/lib/python3.8/cmd.py(159)postcmd()
-> return stop
(Pdb) q
Traceback (most recent call last):
  File "t_pdb.py", line 6, in <module>
    exc()
  File "t_pdb.py", line 2, in exc
    raise Exception()
Exception

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "t_pdb.py", line 9, in <module>
    pdb.post_mortem()
  File "…/pyenv/3.8-dev/lib/python3.8/pdb.py", line 1626, in post_mortem
    p.interaction(None, t)
  File "…/pyenv/3.8-dev/lib/python3.8/pdb.py", line 352, in interaction
    self._cmdloop()
  File "…/pyenv/3.8-dev/lib/python3.8/pdb.py", line 321, in _cmdloop
    self.cmdloop()
  File "…/pyenv/3.8-dev/lib/python3.8/cmd.py", line 139, in cmdloop
    stop = self.postcmd(stop, line)
  File "…/pyenv/3.8-dev/lib/python3.8/cmd.py", line 159, in postcmd
    return stop
  File "…/pyenv/3.8-dev/lib/python3.8/cmd.py", line 159, in postcmd
    return stop
  File "…/pyenv/3.8-dev/lib/python3.8/bdb.py", line 88, in trace_dispatch
    return self.dispatch_line(frame)
  File "…/pyenv/3.8-dev/lib/python3.8/bdb.py", line 113, in dispatch_line
    if self.quitting: raise BdbQuit
bdb.BdbQuit