gh-122311: Improve and unify pickle errors (GH-122771)

* Raise PicklingError instead of UnicodeEncodeError, ValueError
  and AttributeError in both implementations.
* Chain the original exception to the pickle-specific one as __context__.
* Include the error message of ImportError and some AttributeError in
  the PicklingError error message.
* Unify error messages between Python and C implementations.
* Refer to documented __reduce__ and __newobj__ callables instead of
  internal methods (e.g. save_reduce()) or pickle opcodes (e.g. NEWOBJ).
* Include more details in error messages (what expected, what got).
* Avoid including a potentially long repr of an arbitrary object in
  error messages.
This commit is contained in:
Serhiy Storchaka 2024-09-09 15:04:51 +03:00 committed by GitHub
parent 32bc2d6141
commit b2a8c38bb2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 249 additions and 223 deletions

View file

@ -0,0 +1,5 @@
Improve errors in the :mod:`pickle` module. :exc:`~pickle.PicklingError` is
now raised more often instead of :exc:`UnicodeEncodeError`,
:exc:`ValueError` and :exc:`AttributeError`, and the original exception is
chained to it. Improve and unify error messages in Python and C
implementations.