Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Lib/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1322,12 +1322,18 @@ def load(self):
if not key:
raise EOFError
assert isinstance(key, bytes_types)
dispatch[key[0]](self)
try:
dispatch[key[0]](self)
Copy link
Member

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.

except KeyError:
raise UnpicklingError(
f"invalid load key, '\\x{key[0]:02x}'.")
except _Stop as stopinst:
return stopinst.value

# Return a list of items pushed in the stack after last MARK instruction.
def pop_mark(self):
if not self.metastack:
raise UnpicklingError("could not find MARK")
items = self.stack
self.stack = self.metastack.pop()
self.append = self.stack.append
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class PyPickleTests(AbstractPickleModuleTests, unittest.TestCase):
class PyUnpicklerTests(AbstractUnpickleTests, unittest.TestCase):

unpickler = pickle._Unpickler
bad_stack_errors = (IndexError,)
bad_stack_errors = (pickle.UnpicklingError, IndexError)
Copy link
Member

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?

truncated_errors = (pickle.UnpicklingError, EOFError,
AttributeError, ValueError,
struct.error, IndexError, ImportError)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Loading