-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
gh-141749: align exceptions raised by pickle.load with _pickle.load
#141754
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
base: main
Are you sure you want to change the base?
Conversation
This fix addresses error handling inconsistencies in CPython's pure Python `pickle.py` implementation to match the behavior of the C `_pickle` module. The changes make the pure Python implementation raise proper `UnpicklingError` exceptions for invalid pickle data instead of low-level `KeyError` and `IndexError` exceptions.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
| assert isinstance(key, bytes_types) | ||
| dispatch[key[0]](self) | ||
| try: | ||
| dispatch[key[0]](self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not catch the KeyError for the call. Store the dispatcher separately.
|
|
||
| unpickler = pickle._Unpickler | ||
| bad_stack_errors = (IndexError,) | ||
| bad_stack_errors = (pickle.UnpicklingError, IndexError) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need to catch IndexError?
| @@ -0,0 +1 @@ | |||
| Pure python pickle.py error handling is more consistent with the c implementation, raising UnpicklingError exceptions for invalid pickle data instead of KeyError or IndexError. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Pure python pickle.py error handling is more consistent with the c implementation, raising UnpicklingError exceptions for invalid pickle data instead of KeyError or IndexError. | |
| :mod:`pickle`: align exceptions raised by the pure Python implementation of :func:`pickle.load` | |
| with the C implementation. Previous cases raising :exc:`KeyError` or :exc:`IndexError` | |
| now raise :exc:`~pickle.UnpicklingError`. |
You can also add "Patch by [your name]" at the end.
pickle.load with _pickle.load
Disclaimer: I used Claude Code to help me make this change
This fix addresses error handling inconsistencies in CPython's pure Python
pickle.pyimplementation to match the behavior of the C_picklemodule. The changes make the pure Python implementation raise properUnpicklingErrorexceptions for invalid pickle data instead of low-levelKeyErrorandIndexErrorexceptions.UnpicklingErrorinstead ofKeyErrorUnpicklingError: could not find MARKinstead ofIndexError: pop from empty listNote: the C implementation (
_pickle) always raisesUnpicklingErrorfor all error conditions because it has explicit error checking. The pure Python implementation can't catch all cases without significant performance overhead, so there are still some cases where the pure Python implementation will raiseIndexError(e.g.,self.stack.pop()on empty stack,self.stack[-1]on empty stack)This approach:
load_get,load_bingetcatchKeyErrorfor memo access)What Gets Fixed
Before the fix:
After the fix:
Compatibility
UnpicklingErrorcontinues to workIndexErrorcontinues to work (for other stack operations)Impact
This fix benefits: