Repository navigation
[mypyc] Fix exception chaining when adding a traceback entry fails - #22462
Open
Jahongir-Qurbonov wants to merge 2 commits into
Open
Jahongir-Qurbonov wants to merge 2 commits into
Jahongir-Qurbonov wants to merge 2 commits into
Conversation
On Python 3.12+, CPy_AddTraceback fetched the exception with PyErr_Fetch but passed only the type to _PyErr_ChainExceptions1 if creating the frame failed. The new exception then got the exception class as its context, the original exception was lost, and the value and traceback were leaked. Use PyErr_GetRaisedException/PyErr_SetRaisedException on 3.12+, so the exception instance is passed around.
…k-error-path # Conflicts: # mypyc/lib-rt/exc_ops.c # mypyc/test-data/run-exceptions.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
On Python 3.12+,
CPy_AddTracebacksaves the current exception withPyErr_Fetch(&exc, &val, &tb). If creating the code or frame object fails, it calls_PyErr_ChainExceptions1(exc), which expects an exception instance, butexcis the exception type. So:__context__, and printing the traceback fails (TypeError: print_exception(): Exception expected for value, type foundfrom the default hook,AttributeError: 'getset_descriptor' object has no attribute 'tb_frame'fromtraceback.print_exception);valandtbare leaked.This PR uses
PyErr_GetRaisedException/PyErr_SetRaisedExceptionon 3.12+, so the instance is passed around in both paths. Before 3.12 nothing changes.PyFrame_Newfailing usually needs an OOM, but it also fails if looking up"__builtins__"in the module globals raises. The new test uses that: it adds a key with the same hash as"__builtins__"whose__eq__raises (defined in an interpreted module, so that__eq__doesn't need a traceback entry itself). Then the exception chain is:__context__chainRuntimeError→RuntimeError→ValueErrorRuntimeError→<class 'RuntimeError'>RuntimeError→RuntimeError→ValueErrorThe test fails without the fix.
test_runandtest_externalpass.