Message347178
-- code 1
import pickle
class MyException():
def __init__(self, desc, *, item):
super().__init__()
self.desc = desc
self.item = item
def __getnewargs_ex__(self):
print('called in {}.__getnewargs_ex__'.format(self.__class__.__name__))
return (self.desc,), self.__dict__
e = MyException('testing', item='cpu')
s = pickle.dumps(e, protocol=-1)
x = pickle.loads(s)
-- code 2
import pickle
class MyException(Exception):
def __init__(self, desc, *, item):
super().__init__()
self.desc = desc
self.item = item
def __getnewargs_ex__(self):
print('called in {}.__getnewargs_ex__'.format(self.__class__.__name__))
return (self.desc,), self.__dict__
e = MyException('testing', item='cpu')
s = pickle.dumps(e, protocol=-1)
x = pickle.loads(s)
in code 1, the class is inherted from object, __getnewargs_ex__ is called and the returned args, kwargs are passed to __new__/__init__ to construct object when pickling.
in code 2, the class is inherted from Exception, __getnewargs_ex__ is not called, and rasie an exception of "TypeError: __init__() missing 1 required positional argument: 'desc'"
I think this is not python issue, it should be the right behavior of Exception. I want to known why it is, and how to implement the logic in code 2 (passing keyword only parameter to __new__/__init__ when pickling for class inherted from Exception) |
|
Date |
User |
Action |
Args |
2019-07-03 03:12:31 | liugang93 | set | recipients:
+ liugang93 |
2019-07-03 03:12:31 | liugang93 | set | messageid: <1562123551.66.0.0575103279234.issue37489@roundup.psfhosted.org> |
2019-07-03 03:12:31 | liugang93 | link | issue37489 messages |
2019-07-03 03:12:31 | liugang93 | create | |
|