Skip to content

Add details for pickle serialization errors #122213

Closed

Description

@serhiy-storchaka

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

Activity

  1. added a commit that references this issue on Jul 24, 2024
  2. added a commit that references this issue on Sep 9, 2024
  3. added a commit that references this issue on Sep 10, 2024
  4. added a commit that references this issue on Feb 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixestype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions