-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
gh-103333: Pickle the keyword attributes of AttributeError #103352
Conversation
Hello! |
Sure, I'll write tests, update blurb once I know that this approach is likely to be accepted :) |
It won't be a rare case when the object is not picklable right? Won't that make a picklable Exception non-picklable with this change? |
In theory yeah, if the object isn't picklable it would not work well here. Though this format follows in line with how other exceptions are made to be picklable (like OSError). Is there a way to somehow check if the obj, etc. are picklable and set them in that case (and if not, just leave as None (as it is now))? |
I wonder if there should be some sort of pickle mode that pickles all it can and for the fields it couldn't pickle: set to |
I think |
The original issue came from a user using AttributeError via multiprocessing and the .name attribute was lost via the pickle. .. maybe a middle ground is to just serialize .name and postpone the conversation on .obj? |
Having an Exception that loses attributes after pickling is definitely better than having one that is not picklable sometimes. Supporting pickling the |
Updated PR to not pickle obj. Also updated a test to hit this change as well. |
CC @serhiy-storchaka re pickle. |
@iritkatriel @serhiy-storchaka any chance for a merge or feedback? |
@iritkatriel @serhiy-storchaka please provide feedback and/merge. Thanks. |
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
I left some comments, but I don't feel confident judging what changes to something that gets pickled are valid compatibility wise as I generally avoid use of pickle outside of tightly controlled scenarios. Will there be any compatibility issue with an AttributeError pickled by this PR being loaded in a Python process running an older Python version without this PR? |
@gpshead I updated based off your feedback. In terms of pickle-ability it works out well:
Then in Python 3.11 I try to use the dumped pickle bytes:
Then back in 3.12.0a7, using that pickle data works, albeit without the name field (which makes sense since the old Python doesn't pickle the field):
Does this seem ok for merge now? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP-7 nits
Co-authored-by: Erlend E. Aasland <[email protected]>
Fixed nits. Thanks! |
There is a refleak now in this test, could it be this PR?
|
See #104454 |
gh-103333: Pickle the keyword attributes of AttributeError
This should fix the case where pickling/unpickling an AttributeError loses the name/obj fields.
If this looks ok, I'd be happy to add a unit test, blurb, etc.