[issue37489] pickling instance which inherited from Exception with keyword only parameter
liugang
report at bugs.python.org
Tue Jul 2 23:12:31 EDT 2019
New submission from liugang <liugang93 at 163.com>:
-- 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)
----------
components: Library (Lib)
messages: 347178
nosy: liugang93
priority: normal
severity: normal
status: open
title: pickling instance which inherited from Exception with keyword only parameter
type: behavior
versions: Python 3.5
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37489>
_______________________________________
More information about the Python-bugs-list
mailing list