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

@ -48,7 +48,7 @@ module _imp
PyStatus
_PyImportZip_Init(PyThreadState *tstate)
{
PyObject *path_hooks, *zipimport;
PyObject *path_hooks;
int err = 0;
path_hooks = PySys_GetObject("path_hooks");
@ -63,32 +63,22 @@ _PyImportZip_Init(PyThreadState *tstate)
PySys_WriteStderr("# installing zipimport hook\n");
}
zipimport = PyImport_ImportModule("zipimport");
if (zipimport == NULL) {
_PyErr_Clear(tstate); /* No zip import module -- okay */
PyObject *zipimporter = _PyImport_GetModuleAttrString("zipimport", "zipimporter");
if (zipimporter == NULL) {
_PyErr_Clear(tstate); /* No zipimporter object -- okay */
if (verbose) {
PySys_WriteStderr("# can't import zipimport\n");
PySys_WriteStderr("# can't import zipimport.zipimporter\n");
}
}
else {
PyObject *zipimporter = PyObject_GetAttr(zipimport, &_Py_ID(zipimporter));
Py_DECREF(zipimport);
if (zipimporter == NULL) {
_PyErr_Clear(tstate); /* No zipimporter object -- okay */
if (verbose) {
PySys_WriteStderr("# can't import zipimport.zipimporter\n");
}
/* sys.path_hooks.insert(0, zipimporter) */
err = PyList_Insert(path_hooks, 0, zipimporter);
Py_DECREF(zipimporter);
if (err < 0) {
goto error;
}
else {
/* sys.path_hooks.insert(0, zipimporter) */
err = PyList_Insert(path_hooks, 0, zipimporter);
Py_DECREF(zipimporter);
if (err < 0) {
goto error;
}
if (verbose) {
PySys_WriteStderr("# installed zipimport hook\n");
}
if (verbose) {
PySys_WriteStderr("# installed zipimport hook\n");
}
}
@ -2632,6 +2622,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

View file

@ -2342,19 +2342,15 @@ error:
static PyStatus
init_set_builtins_open(void)
{
PyObject *iomod = NULL, *wrapper;
PyObject *wrapper;
PyObject *bimod = NULL;
PyStatus res = _PyStatus_OK();
if (!(iomod = PyImport_ImportModule("io"))) {
goto error;
}
if (!(bimod = PyImport_ImportModule("builtins"))) {
goto error;
}
if (!(wrapper = PyObject_GetAttrString(iomod, "open"))) {
if (!(wrapper = _PyImport_GetModuleAttrString("io", "open"))) {
goto error;
}
@ -2371,7 +2367,6 @@ error:
done:
Py_XDECREF(bimod);
Py_XDECREF(iomod);
return res;
}