gh-93741: Add private C API _PyImport_GetModuleAttrString() (GH-93742)

It combines PyImport_ImportModule() and PyObject_GetAttrString()
and saves 4-6 lines of code on every use.

Add also _PyImport_GetModuleAttr() which takes Python strings as arguments.
This commit is contained in:
Serhiy Storchaka 2022-06-14 07:15:26 +03:00 committed by GitHub
parent 7b2064b4b9
commit 6fd4c8ec77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 114 additions and 248 deletions

View file

@ -526,18 +526,12 @@ _Py_add_one_to_index_C(int nd, Py_ssize_t *index, const Py_ssize_t *shape)
Py_ssize_t
PyBuffer_SizeFromFormat(const char *format)
{
PyObject *structmodule = NULL;
PyObject *calcsize = NULL;
PyObject *res = NULL;
PyObject *fmt = NULL;
Py_ssize_t itemsize = -1;
structmodule = PyImport_ImportModule("struct");
if (structmodule == NULL) {
return itemsize;
}
calcsize = PyObject_GetAttrString(structmodule, "calcsize");
calcsize = _PyImport_GetModuleAttrString("struct", "calcsize");
if (calcsize == NULL) {
goto done;
}
@ -558,7 +552,6 @@ PyBuffer_SizeFromFormat(const char *format)
}
done:
Py_DECREF(structmodule);
Py_XDECREF(calcsize);
Py_XDECREF(fmt);
Py_XDECREF(res);