KEMBAR78
gh-95754: Better AttributeError on partially initialised module by hauntsaninja · Pull Request #112577 · python/cpython · GitHub
Skip to content

Conversation

@hauntsaninja
Copy link
Contributor

@hauntsaninja hauntsaninja commented Dec 1, 2023

if (suppress != 1) {
if (_PyModuleSpec_IsInitializing(spec)) {
PyErr_Format(PyExc_AttributeError,
origin = PyObject_GetAttr(spec, &_Py_ID(origin));
Copy link
Member

Choose a reason for hiding this comment

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

PyObject_GetAttr() can set not only AttributeError. If it sets a different error, it should not be overridden by AttributeError.

I suggest to use PyObject_GetOptionalAttr().

Copy link
Member

Choose a reason for hiding this comment

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

Why not chain the original error to a new exception (raise from)?

Copy link
Member

Choose a reason for hiding this comment

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

Or maybe this is a use case for adding a note to the original exception and letting it propagate on (if we don't want to change the type of the exception being raised).

@hauntsaninja
Copy link
Contributor Author

Thank you for the reviews!

@serhiy-storchaka
Copy link
Member

I fixed some errors, please resolve conflicts.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

@hauntsaninja hauntsaninja merged commit 61e8184 into python:main Dec 21, 2023
@hauntsaninja hauntsaninja deleted the gh95754 branch December 21, 2023 21:24
@hugovk
Copy link
Member

hugovk commented Dec 21, 2023

Would be great to mention this nice quality-of-life improvement with an example in What's New, similar to:

https://docs.python.org/3/whatsnew/3.12.html#improved-error-messages

ryan-duve pushed a commit to ryan-duve/cpython that referenced this pull request Dec 26, 2023
kulikjak pushed a commit to kulikjak/cpython that referenced this pull request Jan 22, 2024
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
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.

4 participants