Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
bpo-33237: Improve AttributeError message for partially initialized module.#6398
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
8684e59b072b9b770f2f58df64064c2dea386eb2c8b43863f9f37fba1bd96f99a78720d08f64dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import use | ||
| spam = 1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import source | ||
| source.spam |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Improved :exc:`AttributeError` message for partially initialized module. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -698,6 +698,27 @@ module_repr(PyModuleObject *m) | ||
| return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m); | ||
| } | ||
| /* Check if the "_initializing" attribute of the module spec is set to true. | ||
| Clear the exception and return 0 if spec is NULL. | ||
| */ | ||
| int | ||
| _PyModuleSpec_IsInitializing(PyObject *spec) | ||
| { | ||
| if (spec != NULL) { | ||
| _Py_IDENTIFIER(_initializing); | ||
| PyObject *value = _PyObject_GetAttrId(spec, &PyId__initializing); | ||
| if (value != NULL) { | ||
| int initializing = PyObject_IsTrue(value); | ||
| Py_DECREF(value); | ||
| if (initializing >= 0) { | ||
| return initializing; | ||
| } | ||
| } | ||
| } | ||
| PyErr_Clear(); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you always clear the current exception? What if the function is called with an exception set? Maybe start with a "assert(!PyErr_Occurred());". I prefer the old code which only clears the exception on error. MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not wrong to call this function with NULL and an exception set. Checking this here saves several lines of code and/or indentation level at caller places. The exception is cleared on error in most cases. The only exception from this rule is for Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, it makes sense. | ||
| return 0; | ||
| } | ||
| static PyObject* | ||
| module_getattro(PyModuleObject *m, PyObject *name) | ||
| { | ||
| @@ -717,8 +738,24 @@ module_getattro(PyModuleObject *m, PyObject *name) | ||
| _Py_IDENTIFIER(__name__); | ||
| mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__); | ||
| if (mod_name && PyUnicode_Check(mod_name)) { | ||
| PyErr_Format(PyExc_AttributeError, | ||
| "module '%U' has no attribute '%U'", mod_name, name); | ||
| _Py_IDENTIFIER(__spec__); | ||
| Py_INCREF(mod_name); | ||
| PyObject *spec = _PyDict_GetItemId(m->md_dict, &PyId___spec__); | ||
| Py_XINCREF(spec); | ||
| if (_PyModuleSpec_IsInitializing(spec)) { | ||
| PyErr_Format(PyExc_AttributeError, | ||
| "partially initialized " | ||
| "module '%U' has no attribute '%U' " | ||
| "(most likely due to a circular import)", | ||
| mod_name, name); | ||
| } | ||
| else { | ||
| PyErr_Format(PyExc_AttributeError, | ||
| "module '%U' has no attribute '%U'", | ||
| mod_name, name); | ||
| } | ||
| Py_XDECREF(spec); | ||
| Py_DECREF(mod_name); | ||
| return NULL; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.