Support `from m import *`

Merged
R3
20d ago
+1515-242

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                             []
  • LOAD is separate from BIND so a property such as os.environ can suspend for the host and resume.
  • BIND stores 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, as exec() 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 * makes pow return a float).
  • __all__ is read as CPython reads it, by index from 0 until IndexError, so any sequence works (range, deque, Counter, a class with __getitem__).
    Mappings and other values raise PySequence_GetItem's TypeError; that check is now shared with bisect.
  • Errors name the module by the __name__ in its namespace.
    This also fixes the general module '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, less base64's MAXBINSIZE and MAXLINESIZE.
    To set it in one place, each module's create_module now returns its Module and StandardLib::create allocates 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::NotSupported is removed; the wildcard rejection was its only user.
  • repl_rejected_snippet_locations used from math import * as its rejected snippet; it now uses type 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 a list.
  • __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 (replaces import__star_error.py), import__star_host.py, import__star_bundled.py, and three traceback cases.
  • repl_star_import_binds_names_for_later_feeds checks names bound in one feed are visible in the next, across a dump round trip.
  • On the final commit: make test-cases (1518 passed), the import__ cases under memory-model-checks and ref-count-return, make lint-rs, make lint-py, mdformat.
  • cargo nextest run -p monty -p monty-type-checking -p monty-types passed (1236) before the bundled __all__ change; it was not re-run after it.

Follow-ups

  • Once this merges, the vendored stat can drop 0002-stat-no-accelerator.patch.
    The candidate scan shows bisect and stat now load from CPython's source; posixpath, pathlib and types move on to other blockers.
  • Bundled modules have no __name__.
  • An exception passing through a non-matching except reports the except body'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 (pow after from 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.