|
1 | 1 | #include "parts.h" |
2 | 2 | #include "util.h" |
3 | 3 |
|
| 4 | +static PyObject * |
| 5 | +pyimport_lazyimportwithoutframe(PyObject *self, PyObject *name) |
| 6 | +{ |
| 7 | + PyObject *lazy_import = PyImport_ImportModuleAttrString("builtins", |
| 8 | + "__lazy_import__"); |
| 9 | + if (lazy_import == NULL) { |
| 10 | + return NULL; |
| 11 | + } |
| 12 | + |
| 13 | + // Simulate being called with no running Python frame (e.g. from a freshly |
| 14 | + // attached C thread), so that PyEval_GetGlobals() returns NULL. |
| 15 | + PyThreadState *tstate = PyThreadState_Get(); |
| 16 | + struct _PyInterpreterFrame *saved = tstate->current_frame; |
| 17 | + tstate->current_frame = NULL; |
| 18 | + PyObject *res = PyObject_CallOneArg(lazy_import, name); |
| 19 | + tstate->current_frame = saved; |
| 20 | + |
| 21 | + Py_DECREF(lazy_import); |
| 22 | + return res; |
| 23 | +} |
| 24 | + |
4 | 25 | // Test PyImport_ImportModuleAttr() |
5 | 26 | static PyObject * |
6 | 27 | pyimport_importmoduleattr(PyObject *self, PyObject *args) |
@@ -95,6 +116,7 @@ static PyMethodDef test_methods[] = { |
95 | 116 | {"PyImport_GetLazyImportsMode", pyimport_getlazyimportsmode, METH_NOARGS}, |
96 | 117 | {"PyImport_SetLazyImportsFilter", pyimport_setlazyimportsfilter, METH_VARARGS}, |
97 | 118 | {"PyImport_GetLazyImportsFilter", pyimport_getlazyimportsfilter, METH_NOARGS}, |
| 119 | + {"lazy_import_without_frame", pyimport_lazyimportwithoutframe, METH_O}, |
98 | 120 | {NULL}, |
99 | 121 | }; |
100 | 122 |
|
|
0 commit comments