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
Pickling is losing some fields on exceptions #103333
Say we have an AttributeError with some fields on it (like name). When we pickle and unpickle it, we can see that .name (at least) is lost (and set to None). Optimally those fields should still be in-tact on the unpickled instance.
C:\Users\csm10495\Desktop>python
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> a = AttributeError("test text", name="test name", obj="test obj")
>>> a.name
'test name'
>>> pickle.loads(pickle.dumps(a)).name
>>> pickle.loads(pickle.dumps(a))
AttributeError('test text')
Your environment
C:\Users\csm10495\Desktop>python --version
Python 3.11.0
C:\Users\csm10495\Desktop>ver
Microsoft Windows [Version 10.0.19045.2364]
C:\Users\csm10495\Desktop>
Same thing seems to happen on 3.10.7 and 3.11.3 as well.
Why don't values like name/obj appear in __dict__ for exceptions? I see that if that were the case, BaseException's default reduce should have worked out here.
I think its the same overall problem, but i'm not really fit to come up with the be-all-end-all solution to the problem for all exceptions and their fields. (Which is where that issues seems to point).
For now I'd like to just try to fix this for AttributeError (if that's ok)... i've raised #103352 if you could check it.
Thanks for the contribution, merged! I'm considering this a feature and doing it for 3.12 only as it can be seen as an API enhancement otherwise so doing this mid-cycle in 3.11 unless it were a regression from past releases doesn't feel right.
@sobolevn is right that there is a larger all Exceptions issue that could be dealt with, but this is still a meaningful improvement in the interim.
Activity
csm10495 commentedon Apr 7, 2023
Why don't values like name/obj appear in
__dict__for exceptions? I see that if that were the case,BaseException's default reduce should have worked out here.sobolevn commentedon Apr 7, 2023
It looks very related to #73652
If you agree, let's move there :)
csm10495 commentedon Apr 7, 2023
I think its the same overall problem, but i'm not really fit to come up with the be-all-end-all solution to the problem for all exceptions and their fields. (Which is where that issues seems to point).
For now I'd like to just try to fix this for AttributeError (if that's ok)... i've raised #103352 if you could check it.
gh-103333: Pickle the keyword attributes of AttributeError (#103352)
gpshead commentedon May 12, 2023
Thanks for the contribution, merged! I'm considering this a feature and doing it for 3.12 only as it can be seen as an API enhancement otherwise so doing this mid-cycle in 3.11 unless it were a regression from past releases doesn't feel right.
@sobolevn is right that there is a larger all Exceptions issue that could be dealt with, but this is still a meaningful improvement in the interim.
gh-104454: Fix refleak in AttributeError_reduce (#104455)