Skip to content
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

Support pickling of super object #125767

Closed
serhiy-storchaka opened this issue Oct 21, 2024 · 0 comments
Closed

Support pickling of super object #125767

serhiy-storchaka opened this issue Oct 21, 2024 · 0 comments
Assignees
Labels
type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Oct 21, 2024

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

@serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label Oct 21, 2024
@serhiy-storchaka serhiy-storchaka self-assigned this Oct 21, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Oct 21, 2024
Previously, copying a super object returned a copy of the instance
invoking super(). Pickling a super object could pickle the instance
invoking super() or fail, depending on its type and protocol.

Now deep copying returns a new super object and pickling pickles the super
object. Shallow copying returns the same super object.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Oct 21, 2024
Previously, copying a super object returned a copy of the instance
invoking super(). Pickling a super object could pickle the instance
invoking super() or fail, depending on its type and protocol.

Now deep copying returns a new super object and pickling pickles the super
object. Shallow copying returns the same super object.
serhiy-storchaka added a commit that referenced this issue Oct 21, 2024
Previously, copying a super object returned a copy of the instance
invoking super(). Pickling a super object could pickle the instance
invoking super() or fail, depending on its type and protocol.

Now deep copying returns a new super object and pickling pickles the super
object. Shallow copying returns the same super object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
Status: Done
Development

No branches or pull requests

1 participant