Set sys.last_value / sys.last_exc before entering the debugger #380
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background:
In commit 6eae532, we introduced a new utility in pyflyby named
saveframe
. This utility is designed to save error stack frames upon an exception. Users can also enter a debugger after an exception (using ipdb.pm()) and callsaveframe
to dump a specific frame.saveframe
relies onsys.last_value
(orsys.last_exc
in Python 3.12) to retrieve the last exception object raised. These attributes are automatically set by Python after an uncaught exception occurs.Issue:
When a user executes a script or command using
py <some_script_or_command>
or employs the@debug_on_exception
decorator, the exception is caught, and the debugger is invoked. Consequently,sys.last_value
(orsys.last_exc
) is not set. If the user attempts to callsaveframe
within the debugger, it fails because the necessary exception information is unavailable.Solution:
In this commit, we modified the code to explicitly set
sys.last_value
(orsys.last_exc
) after an exception is caught and before entering the debugger. This ensures that users can directly enter the debugger usingpy
, and successfully call thepyflyby.saveframe
function.Request: PyInf#12047