gh-154002: Fix exception chaining in pickle _Unpickler._instantiate#154003
Open
fedonman wants to merge 2 commits into
Open
gh-154002: Fix exception chaining in pickle _Unpickler._instantiate#154003fedonman wants to merge 2 commits into
_Unpickler._instantiate#154003fedonman wants to merge 2 commits into
Conversation
…iate When a class constructor raised TypeError during old-style (INST/OBJ) unpickling, the pure-Python unpickler did `raise TypeError(msg, err.__traceback__)`, which stored the traceback object in the exception's args and performed no real chaining (__cause__ stayed None). Use `raise TypeError(msg) from err` instead. The C implementation is unaffected; it lets the original error propagate.
Contributor
Author
|
@zware part of EuroPython sprint. |
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.
When unpickling an old-style instance (the INST and OBJ opcodes) and the class constructor raises
TypeError, the pure-Pythonpickle._Unpickler._instantiatedid:Passing
err.__traceback__as a second positional argument put the traceback object into the exception'sargs, so it leaked intostr()and there was no real chaining (__cause__stayedNone). This changes it to chain properly and drop the traceback argument:It is a leftover from the Python 2 way of carrying a traceback. It was
sys.exc_info()[2]until gh-102799 mechanically turned it intoerr.__traceback__without noticing the value was being passed as a constructor argument. The C_pickleunpickler does not wrap the error at all, so it is unaffected.I added a regression test that loads an INST pickle whose constructor raises
TypeErrorand checks that no traceback object ends up inargsand that the original error is chained. It runs against both unpickler implementations and fails without the fix.Fixes #154002.
_Unpickler._instantiatemangles a constructor TypeError (traceback in args, no chaining) #154002