Skip to content

Add details for pickle serialization errors #122213

Closed
@serhiy-storchaka

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixestype-featureA feature request or enhancement

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions