This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author liugang93
Recipients liugang93
Date 2019-07-03.03:12:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1562123551.66.0.0575103279234.issue37489@roundup.psfhosted.org>
In-reply-to
Content
-- 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)
History
Date User Action Args
2019-07-03 03:12:31liugang93setrecipients: + liugang93
2019-07-03 03:12:31liugang93setmessageid: <1562123551.66.0.0575103279234.issue37489@roundup.psfhosted.org>
2019-07-03 03:12:31liugang93linkissue37489 messages
2019-07-03 03:12:31liugang93create