Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd42b368

Browse files
[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 commit6fd4c8e)
1 parent02ff1cc commitd42b368

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

‎Include/cpython/import.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ struct _frozen {
4040
collection of frozen modules: */
4141

4242
PyAPI_DATA(conststruct_frozen*)PyImport_FrozenModules;
43+
44+
PyAPI_DATA(PyObject*)_PyImport_GetModuleAttr(PyObject*,PyObject*);
45+
PyAPI_DATA(PyObject*)_PyImport_GetModuleAttrString(constchar*,constchar*);

‎Python/import.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,6 +2632,37 @@ PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
26322632
returnPyImport_ExtendInittab(newtab);
26332633
}
26342634

2635+
2636+
PyObject*
2637+
_PyImport_GetModuleAttr(PyObject*modname,PyObject*attrname)
2638+
{
2639+
PyObject*mod=PyImport_Import(modname);
2640+
if (mod==NULL) {
2641+
returnNULL;
2642+
}
2643+
PyObject*result=PyObject_GetAttr(mod,attrname);
2644+
Py_DECREF(mod);
2645+
returnresult;
2646+
}
2647+
2648+
PyObject*
2649+
_PyImport_GetModuleAttrString(constchar*modname,constchar*attrname)
2650+
{
2651+
PyObject*pmodname=PyUnicode_FromString(modname);
2652+
if (pmodname==NULL) {
2653+
returnNULL;
2654+
}
2655+
PyObject*pattrname=PyUnicode_FromString(attrname);
2656+
if (pattrname==NULL) {
2657+
Py_DECREF(pmodname);
2658+
returnNULL;
2659+
}
2660+
PyObject*result=_PyImport_GetModuleAttr(pmodname,pattrname);
2661+
Py_DECREF(pattrname);
2662+
Py_DECREF(pmodname);
2663+
returnresult;
2664+
}
2665+
26352666
#ifdef__cplusplus
26362667
}
26372668
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp