Skip to content

Add details for pickle serialization errors #122213

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

Closed
serhiy-storchaka opened this issue Jul 24, 2024 · 0 comments
Closed

Add details for pickle serialization errors #122213

serhiy-storchaka opened this issue Jul 24, 2024 · 0 comments
Labels
3.14 new features, bugs and security fixes type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Jul 24, 2024

Feature or enhancement

When pickling a complex object, or a graph of objects, it is difficult to locate the source of error. At best you get the type of the unpickleable object at the bottom level, but you cannot know the part of what object or data structure it is.

The proposed PR adds notes to the raised exception which allow to identify the source of the error. For example:

>>> import pickle
>>> pickle.dumps([{'a': 1, 'b': 2}, {'a': 3, 'b': pickle}])
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    pickle.dumps([{'a': 1, 'b': 2}, {'a': 3, 'b': pickle}])
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: cannot pickle 'module' object
when serializing dict item 'b'
when serializing list item 1
>>> class A: pass
... 
>>> a = A()
>>> a.x = pickle
>>> pickle.dumps(a)
Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    pickle.dumps(a)
    ~~~~~~~~~~~~^^^
TypeError: cannot pickle 'module' object
when serializing dict item 'x'
when serializing A state

See also similar issue #122163 for JSON.

Linked PRs

@serhiy-storchaka serhiy-storchaka added type-feature A feature request or enhancement 3.14 new features, bugs and security fixes labels Jul 24, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jul 24, 2024
This allows to identify the source of the error.
serhiy-storchaka added a commit that referenced this issue Sep 9, 2024
This allows to identify the source of the error.
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.14 new features, bugs and security fixes type-feature A feature request or enhancement
Projects
Status: Done
Development

No branches or pull requests

1 participant