mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
Move eager check for from imports into import from
This commit is contained in:
parent
7d07ae13d7
commit
c63198cba4
5 changed files with 37 additions and 9 deletions
|
|
@ -2778,6 +2778,19 @@ def test_lazy_import_pkg(self):
|
||||||
self.assertTrue("test.test_import.data.lazy_imports.pkg" in sys.modules)
|
self.assertTrue("test.test_import.data.lazy_imports.pkg" in sys.modules)
|
||||||
self.assertTrue("test.test_import.data.lazy_imports.pkg.bar" in sys.modules)
|
self.assertTrue("test.test_import.data.lazy_imports.pkg.bar" in sys.modules)
|
||||||
|
|
||||||
|
def test_lazy_import_pkg_cross_import(self):
|
||||||
|
try:
|
||||||
|
import test.test_import.data.lazy_imports.pkg.c
|
||||||
|
except ImportError as e:
|
||||||
|
self.fail('lazy import failed')
|
||||||
|
|
||||||
|
self.assertTrue("test.test_import.data.lazy_imports.pkg" in sys.modules)
|
||||||
|
self.assertTrue("test.test_import.data.lazy_imports.pkg.c" in sys.modules)
|
||||||
|
self.assertFalse("test.test_import.data.lazy_imports.pkg.b" in sys.modules)
|
||||||
|
|
||||||
|
g = test.test_import.data.lazy_imports.pkg.c.get_globals()
|
||||||
|
self.assertEqual(type(g["x"]), int)
|
||||||
|
self.assertEqual(type(g["b"]), importlib.lazy_import)
|
||||||
|
|
||||||
class TestSinglePhaseSnapshot(ModuleSnapshot):
|
class TestSinglePhaseSnapshot(ModuleSnapshot):
|
||||||
"""A representation of a single-phase init module for testing.
|
"""A representation of a single-phase init module for testing.
|
||||||
|
|
|
||||||
3
Lib/test/test_import/data/lazy_imports/pkg/b.py
Normal file
3
Lib/test/test_import/data/lazy_imports/pkg/b.py
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
def foo():
|
||||||
|
return 'foo'
|
||||||
|
|
||||||
4
Lib/test/test_import/data/lazy_imports/pkg/c.py
Normal file
4
Lib/test/test_import/data/lazy_imports/pkg/c.py
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
lazy from . import b, x
|
||||||
|
|
||||||
|
def get_globals():
|
||||||
|
return globals()
|
||||||
|
|
@ -3304,6 +3304,22 @@ _PyEval_LazyImportFrom(PyThreadState *tstate, PyObject *v, PyObject *name)
|
||||||
assert(name && PyUnicode_Check(name));
|
assert(name && PyUnicode_Check(name));
|
||||||
PyObject *ret;
|
PyObject *ret;
|
||||||
PyLazyImportObject *d = (PyLazyImportObject *)v;
|
PyLazyImportObject *d = (PyLazyImportObject *)v;
|
||||||
|
PyObject *mod = PyImport_GetModule(d->lz_from);
|
||||||
|
if (mod != NULL) {
|
||||||
|
// Check if the module already has the attribute, if so, resolve it eagerly.
|
||||||
|
if (PyModule_Check(mod)) {
|
||||||
|
PyObject *mod_dict = PyModule_GetDict(mod);
|
||||||
|
if (mod_dict != NULL) {
|
||||||
|
if (PyDict_GetItemRef(mod_dict, name, &ret) < 0) {
|
||||||
|
return NULL;
|
||||||
|
} else if (ret != NULL) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Py_DECREF(mod);
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
|
||||||
|
|
@ -4221,16 +4221,8 @@ _PyImport_LazyImportModuleLevelObject(PyThreadState *tstate,
|
||||||
}
|
}
|
||||||
|
|
||||||
PyInterpreterState *interp = tstate->interp;
|
PyInterpreterState *interp = tstate->interp;
|
||||||
_PyInterpreterFrame *frame = _PyEval_GetFrame();
|
|
||||||
assert(frame->f_globals == frame->f_locals); // should only be called in global scope
|
assert(frame->f_globals == frame->f_locals); // should only be called in global scope
|
||||||
|
|
||||||
PyObject *mod = PyImport_GetModule(abs_name);
|
|
||||||
bool already_exists = mod != NULL;
|
|
||||||
Py_XDECREF(mod);
|
|
||||||
if (mod != NULL) {
|
|
||||||
return PyImport_ImportModuleLevelObject(name, globals, locals, fromlist, level);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the filter disables the lazy import
|
// Check if the filter disables the lazy import
|
||||||
PyObject *filter = LAZY_IMPORTS_FILTER(interp);
|
PyObject *filter = LAZY_IMPORTS_FILTER(interp);
|
||||||
if (filter != NULL) {
|
if (filter != NULL) {
|
||||||
|
|
@ -4262,7 +4254,7 @@ _PyImport_LazyImportModuleLevelObject(PyThreadState *tstate,
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *res = _PyLazyImport_New(import_func, abs_name, fromlist);
|
PyObject *res = _PyLazyImport_New(import_func, abs_name, fromlist);
|
||||||
if (!already_exists && register_lazy_on_parent(tstate, abs_name, import_func) < 0) {
|
if (register_lazy_on_parent(tstate, abs_name, import_func) < 0) {
|
||||||
Py_DECREF(res);
|
Py_DECREF(res);
|
||||||
res = NULL;
|
res = NULL;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue