You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python exception object is different after pickle.dumps and pickle.loads #89275
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2021-09-06.10:14:34.681>labels= ['type-bug', '3.9']
title='Python exception object is different after pickle.dumps and pickle.loads'updated_at=<Date2021-09-06.16:40:31.474>user='https://bugs.python.org/yonghengzero'
Hi, when I'm trying to serialize/deserialize python exception object through pickle, I found that deserialize result is not the same as the original object...
My python version is 3.9.1, working os: macOS Big Sur 11.4
Here is minimum reproducing code example:
importpickleclassExcA(Exception):
def__init__(self, want):
msg="missing "msg+=wantsuper().__init__(msg)
ExcA('bb') # this will output ExcA("missing bb"), which is goodpickle.loads(pickle.dumps(ExcA('bb'))) # this will output ExcA("missing missing bb"), which is different from `ExcA('bb')`
changed the title [-]Python exception object is different after pickle.loads and pickle.dumps[/-][+]Python exception object is different after pickle.dumps and pickle.loads[/+]on Sep 6, 2021
changed the title [-]Python exception object is different after pickle.loads and pickle.dumps[/-][+]Python exception object is different after pickle.dumps and pickle.loads[/+]on Sep 6, 2021
It would be nice if exception pickling was more friendly to subclasses. Perhaps, the default should be to store and restore all state including args and a __dict__ if any.
Why do exception return their own class as the callable though? Usually classes return __newobj__ as the callable in order for the instance to be created without calling the constructor (calling the class's __new__) and setting the original state (__dict__) afterwards.
Why does this change of behavior exist for exception instances in the first place?
Activity
yonghengzero commentedon Sep 6, 2021
Hi, when I'm trying to serialize/deserialize python exception object through pickle, I found that deserialize result is not the same as the original object...
My python version is 3.9.1, working os: macOS Big Sur 11.4
Here is minimum reproducing code example:
[-]Python exception object is different after pickle.loads and pickle.dumps[/-][+]Python exception object is different after pickle.dumps and pickle.loads[/+][-]Python exception object is different after pickle.loads and pickle.dumps[/-][+]Python exception object is different after pickle.dumps and pickle.loads[/+]iritkatriel commentedon Sep 6, 2021
The default __reduce__ method of Exception returns the arg you pass to the Exception constructor:
This is then pickled, and when unpickled the arg is passed to the ExcA constructor.
If you want to change this, you can implement __reduce__ on ExcA o return just the part you want it to return.
rhettinger commentedon Sep 6, 2021
Pickling customized subclasses can be tricky. The essential details are here: https://docs.python.org/3/library/pickle.html#object.\_\_reduce__
Here's some code to get you started.
rhettinger commentedon Sep 6, 2021
It would be nice if exception pickling was more friendly to subclasses. Perhaps, the default should be to store and restore all state including args and a __dict__ if any.
UltimateLobster commentedon Aug 24, 2022
Why do exception return their own class as the callable though? Usually classes return
__newobj__as the callable in order for the instance to be created without calling the constructor (calling the class's__new__) and setting the original state (__dict__) afterwards.Why does this change of behavior exist for exception instances in the first place?