Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set sys.last_value / sys.last_exc before entering the debugger #380

Merged
merged 1 commit into from
Nov 29, 2024

Conversation

saharan-deshaw
Copy link
Collaborator

@saharan-deshaw saharan-deshaw commented Nov 22, 2024

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 call saveframe to dump a specific frame.

saveframe relies on sys.last_value (or sys.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 (or sys.last_exc) is not set. If the user attempts to call saveframe 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 (or sys.last_exc) after an exception is caught and before entering the debugger. This ensures that users can directly enter the debugger using py, and successfully call the pyflyby.saveframe function.

Reviewer: rakh-deshaw
Request: PyInf#12047

**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 call `saveframe`
to dump a specific frame.

`saveframe` relies on `sys.last_value` (or `sys.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` (or `sys.last_exc`) is not set. If the user attempts to
call `saveframe` 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` (or `sys.last_exc`)
after an exception is caught and before entering the debugger. This ensures that users can
directly enter the debugger and successfully call the `pyflyby.saveframe` function.

Request: PyInf#12047
@saharan-deshaw
Copy link
Collaborator Author

cc @sac111gp

@saharan-deshaw saharan-deshaw merged commit 9268cdc into deshaw:master Nov 29, 2024
7 checks passed
saharan-deshaw pushed a commit that referenced this pull request Dec 3, 2024
Major changes:

- Bumped lint from 3.12 to 3.13 (#370)
- Bumped actions/checkout from 3 to 4 (#371)
- Bumped peaceiris/actions-gh-pages from 3 to 4 (#373)
- Bumped codecov/codecov-action from 2 to 4 (#374)
- Bump actions/setup-python from 3 to 5 (#372)
- Try to detect non-gloabl unused imports (#369)
- Set sys.last_value / sys.last_exc before entering the debugger (#380)
- Disable auto-import of lazy variables (#379)
- Bump codecov/codecov-action from 4 to 5 (#378)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants