Standard library modules
Monty provides a fixed set of stdlib modules.
Other imports request host modules and raise ModuleNotFoundError if none is provided.
There is no sys.path or site-packages; sandboxed code cannot load additional modules itself.
os.path (also importable as posixpath) is the only submodule.
Every bundled import builds a fresh module object; there is no sys.modules cache.
Thus import math as a; import math as b leaves a is not b, and changes such as sys.argv.append(...) do not
survive the next import.
Assigning a bundled module’s attributes raises AttributeError, as in sys.x = 1.
Host modules are module objects too, bound afresh by every import.
The one exception is random’s module-level generator, which is session
state: a random.seed(...) is still in effect after a later import random,
in the next feed, and after a dump (see random.md).
| Module | See |
|---|---|
asyncio | asyncio.md |
base64 | base64.md |
binascii | base64.md |
collections | collections.md |
copy | copy.md |
dataclasses | dataclasses.md |
datetime | datetime.md |
functools | functools.md |
hashlib | hashlib.md |
itertools | itertools.md |
json | json.md |
math | math.md |
os | os.md |
os.path | os.md |
pathlib | pathlib.md |
posixpath | os.md |
random | random.md |
re | re.md |
sys | sys.md |
time | time.md |
typing | typing.md |
unicodedata | unicodedata.md |
collections is importable and exposes deque, Counter, defaultdict,
and namedtuple; OrderedDict, ChainMap, and the UserDict / UserList
/ UserString wrappers are missing (see collections.md).
A gc module exposing collect() / enable() / disable() is compiled
in only under the test-hooks Cargo feature, for Monty’s own test suite;
production sandboxes never see it.
Common modules that are not importable in Monty (non-exhaustive):
abc, argparse, array, bisect, contextlib, csv,
ctypes, decimal, enum, fractions,
heapq, hmac, http, inspect, io,
logging, multiprocessing, operator, pickle, queue,
socket, string, struct, subprocess, tempfile, threading,
traceback, unittest, urllib, uuid, warnings, weakref,
zipfile, zlib.
socket, subprocess, multiprocessing, threading and ctypes are
excluded because they would breach the sandbox. Others (enum, operator)
are unimplemented and may appear over time.
Some available modules cover only part of their CPython surface: functools
implements only reduce and partial, copy only copy() and deepcopy(),
time everything but tzset and the clock_* family, hashlib everything but file_digest() and scrypt(),
and collections only the four types above.
The absent names are missing from
the module namespace rather than stubbed, so they fail type checking as well as
raising AttributeError at runtime; see each module’s page for the specifics.
abc, enum, types, typing_extensions, _collections_abc and _typeshed support the vendored stubs, so their
imports pass type checking.
At runtime, the host must provide them or the import raises ModuleNotFoundError.
The worker never requests host stubs for vendored typeshed modules: a host-provided abc is checked against
typeshed’s abc declarations.
Unknown imports request the top-level name through the import OS call; __import__ is undefined.
Dotted imports such as import a.b.c request a, then read b and c as successive attributes.
Bundled modules that are not packages never request host submodules: import os.x raises
ModuleNotFoundError: No module named 'os.x'; 'os' is not a package.
The host’s answer becomes the imported module object, with these differences from CPython:
- Dunder attributes, including
m.__name__andm.__dict__, raiseAttributeError. - Every
importrequests the module again, including imports inside functions. Two imports bind distinct objects:import m as a; import m as bleavesa is not b. - Assigning an attribute raises
AttributeError, as on a bundled module; a function call runs the host’s current attribute, and the next import binds the module afresh. - Host exceptions are raised at the
import. - Host modules are packages: a missing submodule, or
import m.xfor an attributexthat is not a module, raisesModuleNotFoundError: No module named 'm.x'. from m import xreads the module’s attributes; missing names raiseImportError: cannot import name 'x' from 'm' (unknown location).- Python classes among a module’s attrs become host functions:
m.X()constructs on the host, butisinstance(v, m.X)raisesTypeError. Wrap a class inClassTypeto send it as a type. - Imports with more than 32 dotted components raise
SyntaxError;import a.b.chas three.
With no host to answer, monty file.py included, every unknown module raises ModuleNotFoundError.