Import host call
Merged
SA
2mo ago+6783+6783+0123456789012345678901234567890123456789-681-681-012345678901234567890123456789
Summary by cubic
Sandbox code can now import host-provided modules: importing a module the sandbox lacks suspends as the import OS call and binds the host's answer. A not_found answer raises ModuleNotFoundError naming the first missing component of a dotted import, and a future is refused.
Bindings
external_modules(Python) /externalModules(JS) on the checkout methods names the modules a session may import, each anExternalModulepairing the module's value with its optional.pyistub. The value is a mapping (any mapping in Python) whose public callables become host functions named<module>.<attr>, aClassInstance, aClassType(which crosses as the type it wraps), or a zero-argument callable (async underAsyncMonty/JS) returning one of those, run once per session at the module's first need.ExternalModuleis immutable: it copies and validates its attrs at construction and carries anid; the entries mapping stays live, read at each first import. Host modules cross as real sandboxmoduleobjects, sotype(tools)ismoduleand attribute and call errors raise in the sandbox, and a module exports back with theidit imported with. Passing a module as a value is refused on both sides.- Entries are validated when a feed first imports them: a CPython module entry is refused, any other entry raises
TypeError, and a stub name that is not an identifier or that the sandbox or its type checker already provides (now includinghashlib,posixpath, andgenericpath) is refused. Names and dict keys must be plainstridentifiers (keywords refused, non-XID names in JS), an ownundefinedentry is an invalid entry rather than a missing module, and the wire repr quotes a module's name. A throwing getter surfaces at the import, and private names find nothing.moduleson aClassInstanceentry and an entry nested inside itself are refused, as is a factory returning either. In JS a module entry must be an own property, and a plain value's ownthenis never run: it is boxed so calling it fails as not callable instead of being awaited. - Host module names are plain identifiers; a dotted import walks the host's answer one component at a time through a new
LoadSubmoduleopcode, capped at 32 components (SyntaxError).ExternalModule.modulesnests submodules; a missing component raisesModuleNotFoundErrornaming the full path, andfrom a.b importkeeps it on the frame. Repeated imports move to the end of the type-check prelude so the latest alias binds, and below a built-in module only a module is a submodule, soimport math.piraisesModuleNotFoundError.
Type checking and protocol
- A type-checked feed raises the
import_stubsOS call before it is checked, once per host module it imports that the session has not asked about; an ask naming a module twice is refused. The worker keeps the stubs it receives, carries them in the dump, and adopts the feed's cwd on arrival so a typing rejection after the ask keeps it. Checks resolve the imports from per-module stubs laid out as/<module>.pyi(a package for dotted paths); each committed import is re-injected once into later checks, and a reused worker never resolves a previous session's stubs. Diagnostics in the injected prelude are dropped, so a committedimport x # type: ignorere-emits without its comment. The JS binding refuses keywords and sandbox module names without needing a stub; reserved names split into the sandbox's own modules, refused as host module names, and the checker-only ones, refused only as stub names. - Host-backed class instances are now callable, suspending
__call__on their uuid; the Python and JS wrappers answer withTypeError: 'X' object is not callable.PROTOCOL_VERSIONbumps to 6 — a version 5 parent would answer neither new OS call — whileDUMP_VERSIONstays at 13:LoadAttrImportkeeps its one-operand shape with thefrom ... importmodule name carried on the call frame. ATypingErroranswering anything other than a feed that type-checks is a protocol violation.