8000 gh-122311: Improve and unify pickle errors by serhiy-storchaka · Pull Request #122771 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-122311: Improve and unify pickle errors #122771

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

Merged
merged 2 commits into from
Sep 9, 2024

Conversation

serhiy-storchaka
Copy link
Member
@serhiy-storchaka serhiy-storchaka commented Aug 7, 2024
  • Raise PicklingError instead of UnicodeEncodeError, ValueError and AttributeError in both implementations.
  • Chain the original exception to the pickle-specific one as __context__.
  • Include the error message of ImportError and some AttributeError in the PicklingError error message.
  • Unify error messages between Python and C implementations.
  • Refer to documented __reduce__ and __newobj__ callables instead of internal methods (e.g. save_reduce()) or pickle opcodes (e.g. NEWOBJ).
  • Include more details in error messages (what expected, what got).
  • Avoid including a potentially long repr of an arbitrary object in error messages.

* Raise PicklingError instead of UnicodeEncodeError, ValueError
  and AttributeError in both implementations.
* Chain the original exception to the pickle-specific one as __context__.
* Include the error message of ImportError and some AttributeError in
  the PicklingError error message.
* Unify error messages between Python and C implementations.
* Refer to documented __reduce__ and __newobj__ callables instead of
  internal methods (e.g. save_reduce()) or pickle opcodes (e.g. NEWOBJ).
* Include more details in error messages (what expected, what got).
* Avoid including a potentially long repr of an arbitrary object in
  error messages.

try:
__import__(module_name, level=0)
module = sys.modules[module_name]
except (ImportError, ValueError, KeyError) as exc:
raise PicklingError(f"Can't pickle {obj!r}: {exc!s}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do from None or from exc here (and in the other cases below?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, removing from None was intentional. Error here is not normal. The original exception and its traceback can add additional context.

We use from None if the details of the original exception are not important. For example when it is a result of using EAFP instead of LBYL. But in this case we expect that the object can be found by name, and if this fails, we want to know gore details.

@serhiy-storchaka serhiy-storchaka requested a review from pitrou August 7, 2024 18:04
@serhiy-storchaka
Copy link
Member Author

If there are no objections, I am planning to marge this PR in few days.

@serhiy-storchaka serhiy-storchaka merged commit b2a8c38 into python:main Sep 9, 2024
37 checks passed
@serhiy-storchaka serhiy-storchaka deleted the pickle-errors branch September 9, 2024 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0