Skip to content

Commit 48244f1

Browse files
committed
gh-157757: Preserve empty fromlists for custom import hooks
1 parent 5f48508 commit 48244f1

4 files changed

Lines changed: 40 additions & 9 deletions

File tree

‎Lib/test/test_lazy_import/__init__.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,36 @@ def test_empty_fromlist_placeholder_matches_no_fromlist(self):
22932293
""")
22942294
assert_python_ok("-c", code)
22952295

2296+
def test_empty_fromlist_preserved_for_custom_import(self):
2297+
code = textwrap.dedent("""
2298+
import builtins
2299+
import types
2300+
2301+
value = object()
2302+
module = types.SimpleNamespace(dom=value)
2303+
placeholder = [__lazy_import__("xml.dom", fromlist=())]
2304+
default_import = builtins.__import__
2305+
default_lazy_import = builtins.__lazy_import__
2306+
calls = []
2307+
2308+
def import_hook(name, globals, locals, fromlist, level):
2309+
assert name == "xml.dom", name
2310+
assert fromlist == (), fromlist
2311+
calls.append(fromlist)
2312+
return module
2313+
2314+
builtins.__import__ = import_hook
2315+
assert placeholder[0].resolve() is module
2316+
builtins.__lazy_import__ = lambda *args: placeholder[0]
2317+
lazy import fake.dom as dom
2318+
assert dom is value
2319+
builtins.__import__ = default_import
2320+
builtins.__lazy_import__ = default_lazy_import
2321+
2322+
assert calls == [(), ()], calls
2323+
""")
2324+
assert_python_ok("-c", code)
2325+
22962326
def test_dotted_as_replays_lookups_on_dotted_placeholder(self):
22972327
"""A dotted lazy import as replays its names on the hook's package."""
22982328
# importlib.metadata has a `metadata` attribute of its own, which the

‎Objects/lazyimportobject.c‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ _PyLazyImport_New(_PyInterpreterFrame *frame, PyObject *builtins, PyObject *name
2626
"lazy_import: fromlist must be None, a string, or a tuple");
2727
return NULL;
2828
}
29-
else if (PyTuple_Check(fromlist) && PyTuple_GET_SIZE(fromlist) == 0) {
30-
// __import__("a.b", fromlist=()) returns `a`, as fromlist=None does.
31-
fromlist = NULL;
32-
}
3329
m = PyObject_GC_New(PyLazyImportObject, &PyLazyImport_Type);
3430
if (m == NULL) {
3531
return NULL;
@@ -121,7 +117,8 @@ lazy_import_path(PyLazyImportObject *m)
121117
Py_DECREF(base);
122118
return res;
123119
}
124-
if (m->lz_attr != NULL) {
120+
if (m->lz_attr != NULL &&
121+
(!PyTuple_Check(m->lz_attr) || PyTuple_GET_SIZE(m->lz_attr) > 0)) {
125122
return Py_NewRef(m->lz_from);
126123
}
127124
// __import__("a.b") returns the top-level package `a`.
@@ -143,7 +140,8 @@ lazy_import_name(PyLazyImportObject *m)
143140
if (PyLazyImport_CheckExact(m->lz_from)) {
144141
return lazy_import_path(m);
145142
}
146-
if (m->lz_attr != NULL) {
143+
if (m->lz_attr != NULL &&
144+
(!PyTuple_Check(m->lz_attr) || PyTuple_GET_SIZE(m->lz_attr) > 0)) {
147145
return PyUnicode_FromFormat("%U...", m->lz_from);
148146
}
149147
return Py_NewRef(m->lz_from);

‎Python/ceval.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3336,7 +3336,8 @@ _PyEval_LazyImportFrom(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObje
33363336
PyObject *mod = NULL;
33373337
// Only `from a import b` can take b off an already imported a;
33383338
// `import a.b as c` has to import a.b first.
3339-
if (d->lz_attr != NULL && PyTuple_Check(d->lz_attr)) {
3339+
if (d->lz_attr != NULL && PyTuple_Check(d->lz_attr) &&
3340+
PyTuple_GET_SIZE(d->lz_attr) > 0) {
33403341
mod = PyImport_GetModule(d->lz_from);
33413342
}
33423343
if (mod != NULL) {

‎Python/import.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3984,8 +3984,10 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
39843984

39853985
if (root->lz_attr != NULL) {
39863986
// `from a import b, c`: import only the name being resolved.
3987-
fromlist = first ? PyTuple_Pack(1, first->lz_attr)
3988-
: Py_NewRef(root->lz_attr);
3987+
// Keep an empty tuple intact for custom __import__ hooks.
3988+
fromlist = first && PyTuple_GET_SIZE(root->lz_attr) > 0
3989+
? PyTuple_Pack(1, first->lz_attr)
3990+
: Py_NewRef(root->lz_attr);
39893991
if (fromlist == NULL) {
39903992
goto error;
39913993
}

0 commit comments

Comments
 (0)