Irit Katriel <[email protected]> added the comment:
This isn't really an issue with exceptions, it's just that __reduce__ goes out
of sync with the object's constructor signature. Here's a simple example of the
same:
class Base:
def __init__(self, msg):
self.msg = msg
def __reduce__(self):
return type(self), (self.msg,)
class Derived(Base):
def __init__(self, a, b):
super().__init__(f'{a}|{b}')
x = Derived('a', 'b')
assert x.msg == 'a|b'
y = pickle.dumps(x, -1)
z = pickle.loads(y)
-------------------------------------------
Output:
Traceback (most recent call last):
File "/Users/iritkatriel/src/cpython/zz.py", line 22, in <module>
z = pickle.loads(y)
^^^^^^^^^^^^^^^
TypeError: Derived.__init__() missing 1 required positional argument: 'b'
If I define __reduce__ on Derived to return an arg tuple of the right length
for its __init__, it works:
class Derived(Base):
def __init__(self, a, b):
super().__init__(f'{a}|{b}')
def __reduce__(self):
return type(self), tuple(self.msg.split('|'))
But note that this is not something we could make the base class do, it has no
way of finding out what the semantics of the derived class constructor args are.
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue32696>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com