python / cpython

The Python programming language
https://www.python.org
Other
63.12k stars 30.22k forks source link

pdb: do_debug installs sys.settrace handler when used inside post_mortem #80569

Open 1f7c169c-a6ad-45ae-b614-0d40d962c776 opened 5 years ago

1f7c169c-a6ad-45ae-b614-0d40d962c776 commented 5 years ago
BPO 36388
Nosy @blueyed
PRs
  • python/cpython#12479
  • python/cpython#23202
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['library', '3.9'] title = 'pdb: do_debug installs sys.settrace handler when used inside post_mortem' updated_at = user = 'https://github.com/blueyed' ``` bugs.python.org fields: ```python activity = actor = 'blueyed' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'blueyed' dependencies = [] files = [] hgrepos = [] issue_num = 36388 keywords = ['patch'] message_count = 1.0 messages = ['338531'] nosy_count = 1.0 nosy_names = ['blueyed'] pr_nums = ['12479', '23202'] priority = 'normal' resolution = None stage = 'patch review' status = 'open' superseder = None type = None url = 'https://bugs.python.org/issue36388' versions = ['Python 3.9'] ```

    1f7c169c-a6ad-45ae-b614-0d40d962c776 commented 5 years ago

    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