Skip to content

Commit a8ebc87

Browse files
committed
Rename lz_submodule to lz_dotted_as and trim the comments
The flag means "bind the whole dotted name, not the root", which the old name did not say, and import.c already has unrelated lazy_pending_submodules machinery to be confused with.
1 parent 9194e0b commit a8ebc87

5 files changed

Lines changed: 13 additions & 17 deletions

File tree

‎Include/internal/pycore_lazyimportobject.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ typedef struct {
2222
// Frame information for the original import location.
2323
PyCodeObject *lz_code; // Code object where the lazy import was created.
2424
int lz_instr_offset; // Instruction offset where the lazy import was created.
25-
int lz_submodule; // True for `import a.b as c`, which binds `a.b`
26-
// where `import a.b` binds `a`.
25+
int lz_dotted_as; // `import a.b as c`, which binds `a.b` not `a`.
2726
} PyLazyImportObject;
2827

2928

3029
PyAPI_FUNC(PyObject *) _PyLazyImport_GetName(PyObject *lazy_import);
3130
PyAPI_FUNC(PyObject *) _PyLazyImport_New(
3231
struct _PyInterpreterFrame *frame, PyObject *import_func, PyObject *from,
33-
PyObject *attr, int submodule);
32+
PyObject *attr, int dotted_as);
3433

3534
#ifdef __cplusplus
3635
}

‎Lib/test/test_lazy_import/__init__.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,7 @@ def test_non_package_lazily_imported(self):
725725

726726
def test_non_package_lazily_imported_as(self):
727727
"""A dotted lazy import as raises when the name is not a module."""
728-
# gh-157757: the eager statement raises ModuleNotFoundError, so the
729-
# lazy one raises it at first use rather than binding math.pi.
728+
# gh-157757: the eager statement raises, so the lazy one raises too.
730729
code = textwrap.dedent("""
731730
lazy import math.pi as pi
732731
@@ -2219,9 +2218,8 @@ def test_import_after_variable_wins(self):
22192218

22202219
def test_lazy_import_as_wins_over_variable(self):
22212220
"""A dotted lazy import as imports the submodule the variable hides."""
2222-
# gh-157757: the eager statement imports pkg.b, which rebinds pkg.b to
2223-
# the module, so the lazy one must import it too rather than read the
2224-
# variable off pkg.
2221+
# gh-157757: importing pkg.b rebinds pkg.b from the variable to the
2222+
# module, eagerly and lazily alike.
22252223
code = textwrap.dedent("""
22262224
import sys
22272225
import test.test_lazy_import.data.pkg as pkg

‎Objects/lazyimportobject.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
PyObject *
1414
_PyLazyImport_New(_PyInterpreterFrame *frame, PyObject *builtins,
15-
PyObject *name, PyObject *fromlist, int submodule)
15+
PyObject *name, PyObject *fromlist, int dotted_as)
1616
{
1717
PyLazyImportObject *m;
1818
if (!name || !PyUnicode_Check(name)) {
@@ -34,7 +34,7 @@ _PyLazyImport_New(_PyInterpreterFrame *frame, PyObject *builtins,
3434
m->lz_builtins = Py_XNewRef(builtins);
3535
m->lz_from = Py_NewRef(name);
3636
m->lz_attr = Py_XNewRef(fromlist);
37-
m->lz_submodule = submodule;
37+
m->lz_dotted_as = dotted_as;
3838

3939
// Capture frame information for the original import location.
4040
m->lz_code = NULL;

‎Python/ceval.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3335,9 +3335,9 @@ _PyEval_LazyImportFrom(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObje
33353335
PyLazyImportObject *d = (PyLazyImportObject *)v;
33363336

33373337
if (d->lz_attr == NULL) {
3338-
// `import a.b as x` binds `a.b`, not an attribute of `a` (gh-157757).
3339-
if (d->lz_submodule) {
3340-
return Py_NewRef(v); // a later component of the same name
3338+
// `import a.b as x` binds the module `a.b`, not an attribute of `a`.
3339+
if (d->lz_dotted_as) {
3340+
return Py_NewRef(v);
33413341
}
33423342
return _PyLazyImport_New(frame, d->lz_builtins, d->lz_from, NULL, 1);
33433343
}
@@ -3362,7 +3362,7 @@ _PyEval_LazyImportFrom(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObje
33623362
Py_DECREF(mod);
33633363
}
33643364

3365-
assert(!PyUnicode_Check(d->lz_attr)); // a fromlist, not a taken name
3365+
assert(!PyUnicode_Check(d->lz_attr)); // a fromlist
33663366
return _PyLazyImport_New(frame, d->lz_builtins, d->lz_from, name, 0);
33673367
}
33683368

‎Python/import.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3898,8 +3898,7 @@ _PyImport_ResolveName(PyThreadState *tstate, PyObject *name,
38983898
return resolve_name(tstate, name, globals, level);
38993899
}
39003900

3901-
// Repeat IMPORT_FROM over the components of `name` after the first, taking
3902-
// `a.b.c` off the `a` that importing "a.b.c" returns.
3901+
// Take `a.b.c` off the `a` that importing "a.b.c" returns, as IMPORT_FROM does.
39033902
static PyObject *
39043903
import_from_dotted_name(PyThreadState *tstate, PyObject *mod, PyObject *name)
39053904
{
@@ -4010,7 +4009,7 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
40104009
goto error;
40114010
}
40124011

4013-
if (lz->lz_submodule) {
4012+
if (lz->lz_dotted_as) {
40144013
PyObject *top = obj;
40154014
obj = import_from_dotted_name(tstate, top, lz->lz_from);
40164015
Py_DECREF(top);

0 commit comments

Comments
 (0)