mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
[3.11] gh-93741: Add private C API _PyImport_GetModuleAttrString() (GH-93742) (GH-93792)
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. (cherry picked from commit6fd4c8ec77
) (cherry picked from commitd42b3689f4
) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
e929dae42a
commit
968b238b5e
2 changed files with 34 additions and 0 deletions
|
@ -2294,6 +2294,37 @@ PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
|
|||
return PyImport_ExtendInittab(newtab);
|
||||
}
|
||||
|
||||
|
||||
PyObject *
|
||||
_PyImport_GetModuleAttr(PyObject *modname, PyObject *attrname)
|
||||
{
|
||||
PyObject *mod = PyImport_Import(modname);
|
||||
if (mod == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
PyObject *result = PyObject_GetAttr(mod, attrname);
|
||||
Py_DECREF(mod);
|
||||
return result;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_PyImport_GetModuleAttrString(const char *modname, const char *attrname)
|
||||
{
|
||||
PyObject *pmodname = PyUnicode_FromString(modname);
|
||||
if (pmodname == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
PyObject *pattrname = PyUnicode_FromString(attrname);
|
||||
if (pattrname == NULL) {
|
||||
Py_DECREF(pmodname);
|
||||
return NULL;
|
||||
}
|
||||
PyObject *result = _PyImport_GetModuleAttr(pmodname, pattrname);
|
||||
Py_DECREF(pattrname);
|
||||
Py_DECREF(pmodname);
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue