mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
__import__ is loaded at reification time
This commit is contained in:
parent
c5efb20d48
commit
0c246bc79d
4 changed files with 26 additions and 38 deletions
|
|
@ -13,7 +13,7 @@ PyAPI_DATA(PyTypeObject) PyLazyImport_Type;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PyObject_HEAD
|
PyObject_HEAD
|
||||||
PyObject *lz_import_func;
|
PyObject *lz_builtins;
|
||||||
PyObject *lz_from;
|
PyObject *lz_from;
|
||||||
PyObject *lz_attr;
|
PyObject *lz_attr;
|
||||||
/* Frame information for the original import location */
|
/* Frame information for the original import location */
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
#include "pycore_interpframe.h"
|
#include "pycore_interpframe.h"
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
_PyLazyImport_New(PyObject *import_func, PyObject *from, PyObject *attr)
|
_PyLazyImport_New(PyObject *builtins, PyObject *from, PyObject *attr)
|
||||||
{
|
{
|
||||||
PyLazyImportObject *m;
|
PyLazyImportObject *m;
|
||||||
if (!from || !PyUnicode_Check(from)) {
|
if (!from || !PyUnicode_Check(from)) {
|
||||||
|
|
@ -23,8 +23,8 @@ _PyLazyImport_New(PyObject *import_func, PyObject *from, PyObject *attr)
|
||||||
if (m == NULL) {
|
if (m == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
Py_XINCREF(import_func);
|
Py_XINCREF(builtins);
|
||||||
m->lz_import_func = import_func;
|
m->lz_builtins = builtins;
|
||||||
Py_INCREF(from);
|
Py_INCREF(from);
|
||||||
m->lz_from = from;
|
m->lz_from = from;
|
||||||
Py_XINCREF(attr);
|
Py_XINCREF(attr);
|
||||||
|
|
@ -52,7 +52,7 @@ static void
|
||||||
lazy_import_dealloc(PyLazyImportObject *m)
|
lazy_import_dealloc(PyLazyImportObject *m)
|
||||||
{
|
{
|
||||||
PyObject_GC_UnTrack(m);
|
PyObject_GC_UnTrack(m);
|
||||||
Py_XDECREF(m->lz_import_func);
|
Py_XDECREF(m->lz_builtins);
|
||||||
Py_XDECREF(m->lz_from);
|
Py_XDECREF(m->lz_from);
|
||||||
Py_XDECREF(m->lz_attr);
|
Py_XDECREF(m->lz_attr);
|
||||||
Py_XDECREF(m->lz_code);
|
Py_XDECREF(m->lz_code);
|
||||||
|
|
@ -88,7 +88,7 @@ lazy_import_repr(PyLazyImportObject *m)
|
||||||
static int
|
static int
|
||||||
lazy_import_traverse(PyLazyImportObject *m, visitproc visit, void *arg)
|
lazy_import_traverse(PyLazyImportObject *m, visitproc visit, void *arg)
|
||||||
{
|
{
|
||||||
Py_VISIT(m->lz_import_func);
|
Py_VISIT(m->lz_builtins);
|
||||||
Py_VISIT(m->lz_from);
|
Py_VISIT(m->lz_from);
|
||||||
Py_VISIT(m->lz_attr);
|
Py_VISIT(m->lz_attr);
|
||||||
Py_VISIT(m->lz_code);
|
Py_VISIT(m->lz_code);
|
||||||
|
|
@ -98,7 +98,7 @@ lazy_import_traverse(PyLazyImportObject *m, visitproc visit, void *arg)
|
||||||
static int
|
static int
|
||||||
lazy_import_clear(PyLazyImportObject *m)
|
lazy_import_clear(PyLazyImportObject *m)
|
||||||
{
|
{
|
||||||
Py_CLEAR(m->lz_import_func);
|
Py_CLEAR(m->lz_builtins);
|
||||||
Py_CLEAR(m->lz_from);
|
Py_CLEAR(m->lz_from);
|
||||||
Py_CLEAR(m->lz_attr);
|
Py_CLEAR(m->lz_attr);
|
||||||
Py_CLEAR(m->lz_code);
|
Py_CLEAR(m->lz_code);
|
||||||
|
|
|
||||||
|
|
@ -3089,14 +3089,6 @@ _PyEval_LazyImportName(PyThreadState *tstate, PyObject *builtins, PyObject *glob
|
||||||
return _PyEval_ImportName(tstate, builtins, globals, locals, name, fromlist, level);
|
return _PyEval_ImportName(tstate, builtins, globals, locals, name, fromlist, level);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *import_func;
|
|
||||||
if (PyMapping_GetOptionalItem(builtins, &_Py_ID(__import__), &import_func) < 0) {
|
|
||||||
goto error;
|
|
||||||
} else if (import_func == NULL) {
|
|
||||||
_PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
PyObject *lazy_import_func;
|
PyObject *lazy_import_func;
|
||||||
if (PyMapping_GetOptionalItem(builtins, &_Py_ID(__lazy_import__), &lazy_import_func) < 0) {
|
if (PyMapping_GetOptionalItem(builtins, &_Py_ID(__lazy_import__), &lazy_import_func) < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
|
|
@ -3116,16 +3108,15 @@ _PyEval_LazyImportName(PyThreadState *tstate, PyObject *builtins, PyObject *glob
|
||||||
}
|
}
|
||||||
|
|
||||||
res = _PyImport_LazyImportModuleLevelObject(
|
res = _PyImport_LazyImportModuleLevelObject(
|
||||||
tstate, name, import_func, globals, locals, fromlist, ilevel
|
tstate, name, builtins, globals, locals, fromlist, ilevel
|
||||||
);
|
);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject* args[6] = {name, globals, locals, fromlist, level, import_func};
|
PyObject* args[6] = {name, globals, locals, fromlist, level, builtins};
|
||||||
res = PyObject_Vectorcall(lazy_import_func, args, 6, NULL);
|
res = PyObject_Vectorcall(lazy_import_func, args, 6, NULL);
|
||||||
error:
|
error:
|
||||||
Py_XDECREF(lazy_import_func);
|
Py_XDECREF(lazy_import_func);
|
||||||
Py_XDECREF(import_func);
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -3323,7 +3314,7 @@ _PyEval_LazyImportFrom(PyThreadState *tstate, PyObject *v, PyObject *name)
|
||||||
if (d->lz_attr != NULL) {
|
if (d->lz_attr != NULL) {
|
||||||
if (PyUnicode_Check(d->lz_attr)) {
|
if (PyUnicode_Check(d->lz_attr)) {
|
||||||
PyObject *from = PyUnicode_FromFormat("%U.%U", d->lz_from, d->lz_attr);
|
PyObject *from = PyUnicode_FromFormat("%U.%U", d->lz_from, d->lz_attr);
|
||||||
ret = _PyLazyImport_New(d->lz_import_func, from, name);
|
ret = _PyLazyImport_New(d->lz_builtins, from, name);
|
||||||
Py_DECREF(from);
|
Py_DECREF(from);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
@ -3331,12 +3322,12 @@ _PyEval_LazyImportFrom(PyThreadState *tstate, PyObject *v, PyObject *name)
|
||||||
Py_ssize_t dot = PyUnicode_FindChar(d->lz_from, '.', 0, PyUnicode_GET_LENGTH(d->lz_from), 1);
|
Py_ssize_t dot = PyUnicode_FindChar(d->lz_from, '.', 0, PyUnicode_GET_LENGTH(d->lz_from), 1);
|
||||||
if (dot >= 0) {
|
if (dot >= 0) {
|
||||||
PyObject *from = PyUnicode_Substring(d->lz_from, 0, dot);
|
PyObject *from = PyUnicode_Substring(d->lz_from, 0, dot);
|
||||||
ret = _PyLazyImport_New(d->lz_import_func, from, name);
|
ret = _PyLazyImport_New(d->lz_builtins, from, name);
|
||||||
Py_DECREF(from);
|
Py_DECREF(from);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ret = _PyLazyImport_New(d->lz_import_func, d->lz_from, name);
|
ret = _PyLazyImport_New(d->lz_builtins, d->lz_from, name);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3766,9 +3766,13 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
|
||||||
|
|
||||||
PyObject *globals = PyEval_GetGlobals();
|
PyObject *globals = PyEval_GetGlobals();
|
||||||
|
|
||||||
|
PyObject *import_func;
|
||||||
|
if (PyMapping_GetOptionalItem(lz->lz_builtins, &_Py_ID(__import__), &import_func) < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
if (full) {
|
if (full) {
|
||||||
obj = _PyEval_ImportNameWithImport(tstate,
|
obj = _PyEval_ImportNameWithImport(tstate,
|
||||||
lz->lz_import_func,
|
import_func,
|
||||||
globals,
|
globals,
|
||||||
globals,
|
globals,
|
||||||
lz->lz_from,
|
lz->lz_from,
|
||||||
|
|
@ -3777,10 +3781,11 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
|
||||||
} else {
|
} else {
|
||||||
PyObject *name = PyUnicode_Substring(lz->lz_from, 0, dot);
|
PyObject *name = PyUnicode_Substring(lz->lz_from, 0, dot);
|
||||||
if (name == NULL) {
|
if (name == NULL) {
|
||||||
|
Py_DECREF(import_func);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
obj = _PyEval_ImportNameWithImport(tstate,
|
obj = _PyEval_ImportNameWithImport(tstate,
|
||||||
lz->lz_import_func,
|
import_func,
|
||||||
globals,
|
globals,
|
||||||
globals,
|
globals,
|
||||||
name,
|
name,
|
||||||
|
|
@ -3788,7 +3793,7 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
|
||||||
_PyLong_GetZero());
|
_PyLong_GetZero());
|
||||||
Py_DECREF(name);
|
Py_DECREF(name);
|
||||||
}
|
}
|
||||||
|
Py_DECREF(import_func);
|
||||||
if (obj == NULL) {
|
if (obj == NULL) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
@ -4113,7 +4118,7 @@ get_mod_dict(PyObject *module)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
register_lazy_on_parent(PyThreadState *tstate, PyObject *name, PyObject *import_func)
|
register_lazy_on_parent(PyThreadState *tstate, PyObject *name, PyObject *builtins)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
PyObject *parent = NULL;
|
PyObject *parent = NULL;
|
||||||
|
|
@ -4183,7 +4188,7 @@ register_lazy_on_parent(PyThreadState *tstate, PyObject *name, PyObject *import_
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
if (PyDict_CheckExact(parent_dict) && !PyDict_Contains(parent_dict, child)) {
|
if (PyDict_CheckExact(parent_dict) && !PyDict_Contains(parent_dict, child)) {
|
||||||
PyObject *lazy_module_attr = _PyLazyImport_New(import_func, parent, child);
|
PyObject *lazy_module_attr = _PyLazyImport_New(builtins, parent, child);
|
||||||
if (lazy_module_attr == NULL) {
|
if (lazy_module_attr == NULL) {
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
@ -4211,7 +4216,7 @@ register_lazy_on_parent(PyThreadState *tstate, PyObject *name, PyObject *import_
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
_PyImport_LazyImportModuleLevelObject(PyThreadState *tstate,
|
_PyImport_LazyImportModuleLevelObject(PyThreadState *tstate,
|
||||||
PyObject *name, PyObject *import_func,
|
PyObject *name, PyObject *builtins,
|
||||||
PyObject *globals, PyObject *locals,
|
PyObject *globals, PyObject *locals,
|
||||||
PyObject *fromlist, int level)
|
PyObject *fromlist, int level)
|
||||||
{
|
{
|
||||||
|
|
@ -4253,8 +4258,8 @@ _PyImport_LazyImportModuleLevelObject(PyThreadState *tstate,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *res = _PyLazyImport_New(import_func, abs_name, fromlist);
|
PyObject *res = _PyLazyImport_New(builtins, abs_name, fromlist);
|
||||||
if (register_lazy_on_parent(tstate, abs_name, import_func) < 0) {
|
if (register_lazy_on_parent(tstate, abs_name, builtins) < 0) {
|
||||||
Py_DECREF(res);
|
Py_DECREF(res);
|
||||||
res = NULL;
|
res = NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -5302,15 +5307,7 @@ _imp__set_lazy_attributes_impl(PyObject *module, PyObject *child_module,
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
PyObject *builtins = _PyEval_GetBuiltins(tstate);
|
PyObject *builtins = _PyEval_GetBuiltins(tstate);
|
||||||
PyObject *import_func;
|
PyObject *lazy_module_attr = _PyLazyImport_New(builtins, name, attr_name);
|
||||||
if (PyMapping_GetOptionalItem(builtins, &_Py_ID(__import__), &import_func) < 0) {
|
|
||||||
goto error;
|
|
||||||
} else if (import_func == NULL) {
|
|
||||||
_PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
PyObject *lazy_module_attr = _PyLazyImport_New(import_func, name, attr_name);
|
|
||||||
if (lazy_module_attr == NULL) {
|
if (lazy_module_attr == NULL) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue