gh-104066: Improve performance of hasattr for module objects (#104063)

This commit is contained in:
Itamar Ostricher 2023-05-04 07:50:26 -07:00 committed by GitHub
parent c9ecd3ee75
commit fdcb49c36b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 29 deletions

View file

@ -1085,6 +1085,17 @@ _PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
return 0;
}
}
else if (tp->tp_getattro == (getattrofunc)_Py_module_getattro) {
// optimization: suppress attribute error from module getattro method
*result = _Py_module_getattro_impl((PyModuleObject*)v, name, 1);
if (*result != NULL) {
return 1;
}
if (PyErr_Occurred()) {
return -1;
}
return 0;
}
else if (tp->tp_getattro != NULL) {
*result = (*tp->tp_getattro)(v, name);
}