Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pickling is losing some fields on exceptions #103333

Closed
csm10495 opened this issue Apr 7, 2023 · 4 comments
Closed

Pickling is losing some fields on exceptions #103333

csm10495 opened this issue Apr 7, 2023 · 4 comments
Labels
3.12 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error type-feature A feature request or enhancement

Comments

@csm10495
Copy link
Contributor

csm10495 commented Apr 7, 2023

Bug report

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.

Linked PRs

@csm10495 csm10495 added the type-bug An unexpected behavior, bug, or error label Apr 7, 2023
@csm10495
Copy link
Contributor Author

csm10495 commented 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
Copy link
Member

sobolevn commented Apr 7, 2023

It looks very related to #73652
If you agree, let's move there :)

@csm10495
Copy link
Contributor Author

csm10495 commented 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.

@arhadthedev arhadthedev added 3.11 only security fixes 3.12 bugs and security fixes stdlib Python modules in the Lib dir labels Apr 21, 2023
gpshead added a commit that referenced this issue May 12, 2023
* Pickle the `name` and `args` attributes of AttributeError when present.

Co-authored-by: Gregory P. Smith <greg@krypto.org>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
@gpshead gpshead added type-feature A feature request or enhancement and removed 3.11 only security fixes labels May 12, 2023
@gpshead
Copy link
Member

gpshead commented 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.

@gpshead gpshead closed this as completed May 12, 2023
gpshead pushed a commit that referenced this issue May 13, 2023
* Fix the reference leak introduced by #103333

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error type-feature A feature request or enhancement
Projects
Status: Done
Development

No branches or pull requests

4 participants