gh-141749: align exceptions raised by pickle.load with _pickle.load#141754
gh-141749: align exceptions raised by pickle.load with _pickle.load#141754djoume wants to merge 1 commit intopython:mainfrom
pickle.load with _pickle.load#141754Conversation
|
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 |
Lib/pickle.py
Outdated
| assert isinstance(key, bytes_types) | ||
| dispatch[key[0]](self) | ||
| try: | ||
| dispatch[key[0]](self) |
There was a problem hiding this comment.
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.
Do we still need to catch IndexError?
There was a problem hiding this comment.
Yes, IndexError is still needed. This PR only fixes specific cases (invalid opcodes, missing MARK). Many other operations like self.stack[-1], self.stack.pop() can still raise IndexError on an empty stack during normal unpickling operations.
| @@ -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.
| 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
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.
0c0376f to
ca85362
Compare
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: