Skip to content

Support pickling of super object #125767

Closed
Closed
@serhiy-storchaka

Description

@serhiy-storchaka

Feature or enhancement

As was noted in #125714 (comment), the super object is not pickleable. For example:

import pickle
class X: pass
s = super(X, X())
pickle.dumps(s)

Produces a traceback:

Traceback (most recent call last):
  File "<python-input-0>", line 4, in <module>
    pickle.dumps(s)
    ~~~~~~~~~~~~^^^
_pickle.PicklingError: first argument to __newobj__() must be <class 'super'>, not <class '__main__.X'>
when serializing super object

This is because the special methods like __reduce_ex__() are looked up in an instance and translated to a lookup in the underlying object.

>>> super(X, X()).__reduce_ex__(5)
(<function __newobj__ at 0x7fd9e8a0ad50>, (<class '__main__.X'>,), None, None, None)

This cannot be solved by implementing the __reduce_ex__() method in the super class, because the current behavior is expected when super() is used in the __reduce_ex__() implementation of some subclass. The super class likely should be registered in the global dispatch table.

There may be similar issue with shallow and deep copying.

Linked PRs

Metadata

Metadata

Labels

type-featureA feature request or enhancement

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions