◐ Shell
clean mode source ↗

gh-106670: Allow Pdb to move between chained exceptions by Carreau · Pull Request #106676 · python/cpython

gaogaotiantian

Carreau

gaogaotiantian

iritkatriel

iritkatriel

iritkatriel

iritkatriel

iritkatriel

iritkatriel

iritkatriel

iritkatriel

iritkatriel

Carreau

This lets Pdb receive an exception, instead of a traceback, and when
this is the case and the exception are chained, the new `exceptions` command
allows to both list (no arguments) and move between the chained exceptions.

That is to say if you have something like

    def out():
        try:
            middle()                                # B
        except Exception as e:
            raise ValueError("foo(): bar failed")   # A

    def middle():
        try:
            return inner(0)                         # D
        except Exception as e:
            raise ValueError("Middle fail") from e  # C

    def inner(x):
        1 / x                                       # E

Only A was reachable after calling `out()` and doing post mortem debug.

With this all A-E points are reachable with a combination of up/down,
and ``exception <number>``.

This also change the default behavior of ``pdb.pm()``, as well as
`python -m pdb <script.py>` to receive `sys.last_exc` so that chained
exception navigation is enabled.

We do follow the logic of the ``traceback`` module and handle the
``_context__`` and ``__cause__`` in the same way. That is to say, we try
``__cause__`` first, and if not present chain with ``__context__``. In
the same vein, if we encounter an exception that has
``__suppress_context__`` (like when ``raise ... from None``), we do stop
walking the chain.

Some implementation notes:

 - We do handle cycle in exceptions
 - cleanup of references to tracebacks are not cleared in ``forget()``, as
   ``setup()`` and ``forget()`` are both for setting a single
   exception.
 - We do not handle sub-exceptions of exception groups.

Closes pythongh-106670
Also move the release of the list of exception to a context manager
for security

iritkatriel

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>

iritkatriel

AlexWaygood

AlexWaygood

@Carreau

auto-merge was automatically disabled

August 28, 2023 18:00

Head branch was pushed to by a user without write access

Carreau added a commit to Carreau/ipython that referenced this pull request

Aug 29, 2023

Carreau added a commit to Carreau/ipython that referenced this pull request

Aug 29, 2023

Carreau added a commit to ipython/ipython that referenced this pull request

Aug 29, 2023

Carreau added a commit to Carreau/cpython that referenced this pull request

Sep 4, 2023
The introduction of chained exception in pythongh-106676 would lead to

    File .../Lib/pdb.py", line 298, in setup
      self.curframe = self.stack[self.curindex][0]
                    ~~~~~~~~~~^^^^^^^^^^^^^^^
    IndexError: list index out of range

This fixes that by filtering exceptions that that do not have a stack.
Update tests to not use stack-less exceptions when testing another
feature, and add an explicit test on how we handle stackless exceptions.

Carreau added a commit to Carreau/cpython that referenced this pull request

Sep 4, 2023
The introduction of chained exception in pythongh-106676 would lead to

    File .../Lib/pdb.py", line 298, in setup
      self.curframe = self.stack[self.curindex][0]
                    ~~~~~~~~~~^^^^^^^^^^^^^^^
    IndexError: list index out of range

This fixes that by filtering exceptions that that do not have a stack.
Update tests to not use stack-less exceptions when testing another
feature, and add an explicit test on how we handle stackless exceptions.

Carreau added a commit to Carreau/cpython that referenced this pull request

Sep 4, 2023
The introduction of chained exception in pythongh-106676 would lead to

    File .../Lib/pdb.py", line 298, in setup
      self.curframe = self.stack[self.curindex][0]
                    ~~~~~~~~~~^^^^^^^^^^^^^^^
    IndexError: list index out of range

This fixes that by filtering exceptions that that do not have a stack.
Update tests to not use stack-less exceptions when testing another
feature, and add an explicit test on how we handle stackless exceptions.

Carreau added a commit to Carreau/cpython that referenced this pull request

Sep 4, 2023
The introduction of chained exception in pythongh-106676 would sometime lead to

    File .../Lib/pdb.py", line 298, in setup
      self.curframe = self.stack[self.curindex][0]
                    ~~~~~~~~~~^^^^^^^^^^^^^^^
    IndexError: list index out of range

This fixes that by filtering exceptions that that do not have a
stack/traceback. Update tests to not use stack-less exceptions when
testing another feature, and add an explicit test on how we handle
stackless exceptions.