Skip to content

Commit afe03da

Browse files
committed
Refactor to reduce nesting
1 parent 66f3964 commit afe03da

File tree

1 file changed

+53
-53
lines changed

1 file changed

+53
-53
lines changed

Objects/moduleobject.c

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -819,68 +819,68 @@ _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
819819
Py_DECREF(getattr);
820820
return result;
821821
}
822+
823+
if (suppress == 1) {
824+
return NULL;
825+
}
822826
if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__name__), &mod_name) < 0) {
823827
return NULL;
824828
}
825-
if (mod_name && PyUnicode_Check(mod_name)) {
826-
PyObject *spec;
827-
if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__spec__), &spec) < 0) {
829+
if (!mod_name || !PyUnicode_Check(mod_name)) {
830+
Py_XDECREF(mod_name);
831+
PyErr_Format(PyExc_AttributeError,
832+
"module has no attribute '%U'", name);
833+
return NULL;
834+
}
835+
PyObject *spec;
836+
if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__spec__), &spec) < 0) {
837+
Py_DECREF(mod_name);
838+
return NULL;
839+
}
840+
int rc = _PyModuleSpec_IsInitializing(spec);
841+
if (rc > 0) {
842+
int valid_spec = PyObject_GetOptionalAttr(spec, &_Py_ID(origin), &origin);
843+
if (valid_spec == -1) {
844+
Py_XDECREF(spec);
828845
Py_DECREF(mod_name);
829846
return NULL;
830847
}
831-
if (suppress != 1) {
832-
int rc = _PyModuleSpec_IsInitializing(spec);
833-
if (rc > 0) {
834-
int valid_spec = PyObject_GetOptionalAttr(spec, &_Py_ID(origin), &origin);
835-
if (valid_spec == -1) {
836-
Py_XDECREF(spec);
837-
Py_DECREF(mod_name);
838-
return NULL;
839-
}
840-
if (valid_spec == 1 && !PyUnicode_Check(origin)) {
841-
valid_spec = 0;
842-
Py_DECREF(origin);
843-
}
844-
if (valid_spec == 1) {
845-
PyErr_Format(PyExc_AttributeError,
846-
"partially initialized "
847-
"module '%U' from '%U' has no attribute '%U' "
848-
"(most likely due to a circular import)",
849-
mod_name, origin, name);
850-
Py_DECREF(origin);
851-
}
852-
else {
853-
PyErr_Format(PyExc_AttributeError,
854-
"partially initialized "
855-
"module '%U' has no attribute '%U' "
856-
"(most likely due to a circular import)",
857-
mod_name, name);
858-
}
859-
}
860-
else if (rc == 0) {
861-
rc = _PyModuleSpec_IsUninitializedSubmodule(spec, name);
862-
if (rc > 0) {
863-
PyErr_Format(PyExc_AttributeError,
864-
"cannot access submodule '%U' of module '%U' "
865-
"(most likely due to a circular import)",
866-
name, mod_name);
867-
}
868-
else if (rc == 0) {
869-
PyErr_Format(PyExc_AttributeError,
870-
"module '%U' has no attribute '%U'",
871-
mod_name, name);
872-
}
873-
}
848+
if (valid_spec == 1 && !PyUnicode_Check(origin)) {
849+
valid_spec = 0;
850+
Py_DECREF(origin);
851+
}
852+
if (valid_spec == 1) {
853+
PyErr_Format(PyExc_AttributeError,
854+
"partially initialized "
855+
"module '%U' from '%U' has no attribute '%U' "
856+
"(most likely due to a circular import)",
857+
mod_name, origin, name);
858+
Py_DECREF(origin);
859+
}
860+
else {
861+
PyErr_Format(PyExc_AttributeError,
862+
"partially initialized "
863+
"module '%U' has no attribute '%U' "
864+
"(most likely due to a circular import)",
865+
mod_name, name);
874866
}
875-
Py_XDECREF(spec);
876-
Py_DECREF(mod_name);
877-
return NULL;
878867
}
879-
Py_XDECREF(mod_name);
880-
if (suppress != 1) {
881-
PyErr_Format(PyExc_AttributeError,
882-
"module has no attribute '%U'", name);
868+
else if (rc == 0) {
869+
rc = _PyModuleSpec_IsUninitializedSubmodule(spec, name);
870+
if (rc > 0) {
871+
PyErr_Format(PyExc_AttributeError,
872+
"cannot access submodule '%U' of module '%U' "
873+
"(most likely due to a circular import)",
874+
name, mod_name);
875+
}
876+
else if (rc == 0) {
877+
PyErr_Format(PyExc_AttributeError,
878+
"module '%U' has no attribute '%U'",
879+
mod_name, name);
880+
}
883881
}
882+
Py_XDECREF(spec);
883+
Py_DECREF(mod_name);
884884
return NULL;
885885
}
886886

0 commit comments

Comments
 (0)