Support `from m import *`
from m import * used to be rejected at parse time. It now binds m.__all__, or every name of m not starting with _, for bundled, host and file modules, at module level, in REPL feeds and in exec() snippets.
In a function or class body it raises CPython's SyntaxError: import * only allowed at module level.
How it works
The names are only known at runtime, so the compiler emits a loop driven by one new opcode, ImportStar, with a u8 mode:
LoadModule m (+ LoadSubmodule ...) [m]
ImportStar NAMES; GetIter [m, iter]
loop: ForIter end [m, iter, name]
ImportStar LOAD [m, iter, name, value] (may suspend)
ImportStar BIND; Jump loop [m, iter]
end: Pop []
LOADis separate fromBINDso a property such asos.environcan suspend for the host and resume.BINDstores into the frame's locals dict, its globals dict, or a module global slot.
A slot-global binding interns the name at runtime and appends a slot, asexec()already does, so later feeds,exec()snippets and functions see it.- A module containing a star import no longer substitutes builtins at module scope, since the import may rebind them (
from math import *makespowreturn afloat). __all__is read as CPython reads it, by index from 0 untilIndexError, so any sequence works (range,deque,Counter, a class with__getitem__).
Mappings and other values raisePySequence_GetItem'sTypeError; that check is now shared withbisect.- Errors name the module by the
__name__in its namespace.
This also fixes the generalmodule 'x' has no attribute 'y'error for a file module that rebinds__name__. - Bundled modules whose CPython counterpart defines
__all__now have one: their public names, lessbase64'sMAXBINSIZEandMAXLINESIZE.
To set it in one place, each module'screate_modulenow returns itsModuleandStandardLib::createallocates it.
No heap/ changes and no unsafe.
The opcode fingerprint in dump_format.rs is updated without a DUMP_VERSION bump, following #1173.
Dumps from before this change load as before: their modules simply have no __all__.
Other changes
ParseError::NotSupportedis removed; the wildcard rejection was its only user.repl_rejected_snippet_locationsusedfrom math import *as its rejected snippet; it now usestype Alias = int.- The traceback test harness now honours
mount-fs, so file-module error cases can run on CPython.
Remaining differences
Documented under "Star imports" in limitations/modules.md:
- A bundled module's
__all__lists only what Monty implements, in Monty's order, and is always alist. __all__is read in full before the first name is bound; this shows only through a__getitem__with side effects, or when an earlier binding fails.- Binding past 65536 module globals raises
SyntaxError.
Tests
import__star.py(replacesimport__star_error.py),import__star_host.py,import__star_bundled.py, and three traceback cases.repl_star_import_binds_names_for_later_feedschecks names bound in one feed are visible in the next, across a dump round trip.- On the final commit:
make test-cases(1518 passed), theimport__cases undermemory-model-checksandref-count-return,make lint-rs,make lint-py, mdformat. cargo nextest run -p monty -p monty-type-checking -p monty-typespassed (1236) before the bundled__all__change; it was not re-run after it.
Follow-ups
- Once this merges, the vendored
statcan drop0002-stat-no-accelerator.patch.
The candidate scan showsbisectandstatnow load from CPython's source;posixpath,pathlibandtypesmove on to other blockers. - Bundled modules have no
__name__. - An exception passing through a non-matching
exceptreports theexceptbody's line instead of the raising line.
Summary by cubic
from m import * used to be rejected at parse time; it now binds m.__all__, or every name of m not starting with _, for bundled, host, and file modules, at module level, in REPL feeds, and in exec() snippets. In a function or class body it raises CPython's SyntaxError: import * only allowed at module level.
Notable changes
- A module with a star import no longer substitutes builtins at module scope, since the import may rebind them (
powafterfrom math import *). - Bundled modules whose CPython counterpart defines
__all__now have one, listing only the names Monty implements. - Attribute errors on modules now use the module's
__name__, which also fixes the message for file modules that rebind it. - Names bound by a star import are interned at runtime, so later REPL feeds and
exec()snippets see them. - Reading
__all__is subject to the feed's time limit and growth checks, so an endless__all__times out. - The dump version is now 16; older dumps load as before, their modules simply having no
__all__. - Remaining differences from CPython are documented under "Star imports" in
docs/limitations/modules.md.