summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-06-16 07:19:29 (GMT)
committerGitHub <noreply@github.com>2022-06-16 07:19:29 (GMT)
commit968b238b5e2c8d9e9050b05ff6741544a66c104d (patch)
tree7e249a02058cf1d561f9d19efb6ef387395e2e4f /Python
parente929dae42ad6aaf55e5cce6339c1ef97c01c8f7b (diff)
downloadcpython-968b238b5e2c8d9e9050b05ff6741544a66c104d.zip
cpython-968b238b5e2c8d9e9050b05ff6741544a66c104d.tar.gz
cpython-968b238b5e2c8d9e9050b05ff6741544a66c104d.tar.bz2
[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 commit 6fd4c8ec7740523bb81191c013118d9d6959bc9d) (cherry picked from commit d42b3689f4a14694f5b1ff75c155141102aa2557) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Python')
-rw-r--r--Python/import.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/Python/import.c b/Python/import.c
index acfe969..58d1117 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -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