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.

classification
Title: pickling instance which inherited from Exception with keyword only parameter
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: pickling and repr of exceptions with kwargs
View: 27015
Assigned To: Nosy List: alexandre.vassalotti, iritkatriel, liugang93, liyang1025@gmail.com, pitrou
Priority: normal Keywords:

Created on 2019-07-03 03:12 by liugang93, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg347178 - (view) Author: liugang (liugang93) Date: 2019-07-03 03:12
-- 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)
msg347519 - (view) Author: liyang (liyang1025@gmail.com) Date: 2019-07-09 05:19
yesterday i have test two case, first i find the code 1 not execute the __init__ when execute loads. second the code 1 and code 2 dumps result s is not equal,because the dumps is the buildin function ,so i can't find more message.but if the code2 only have keyword parameter the code2 is not raise exception. i want to debug the interpreter that can get the reason.
msg409840 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-06 12:59
> I want to known why it is

It's because Exception implements __reduce__ and that takes precedence over __getnewargs_ex__. You can see that with this example:

""""
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__

    def __reduce__(self):
        print('called in {}.__reduce__'.format(self.__class__.__name__))
        return MyException, (self.desc, self.item),

e = MyException('testing', item='cpu')
s = pickle.dumps(e, protocol=-1)

x = pickle.loads(s)
""""

Output: called in MyException.__reduce__
msg409844 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-06 13:22
> I want to known [...] how to implement the logic in code 2 (passing keyword only parameter to __new__/__init__ when pickling for class inherted from Exception)


This is currently a problem, see issue27015.
History
Date User Action Args
2022-04-11 14:59:17adminsetgithub: 81670
2022-01-06 13:22:02iritkatrielsetstatus: open -> closed
superseder: pickling and repr of exceptions with kwargs
messages: + msg409844

resolution: duplicate
stage: resolved
2022-01-06 12:59:34iritkatrielsetnosy: + iritkatriel
messages: + msg409840
2019-07-09 05:19:37liyang1025@gmail.comsetnosy: + liyang1025@gmail.com
messages: + msg347519
2019-07-03 19:04:52liugang93setnosy: + alexandre.vassalotti
2019-07-03 07:11:57SilentGhostsetnosy: + pitrou

versions: + Python 3.7, - Python 3.5
2019-07-03 03:12:31liugang93create