Host Objects
The policy wrappers that put a host object, or a host class, in front of the sandbox, and the read-only proxies the sandbox hands back for instances the host has no original object for. See host objects for the concepts.
Bases: BaseWrapper
Policy wrapper exposing a host class instance to the Monty sandbox.
Example:
session.feed_run(
'assert user.greeting() == "hi Samuel"',
inputs={'user': ClassInstance(user, eager_attrs='all', allowed_methods={'greeting'})},
)
Unique id for the value.
Type: UUID Default: field(default_factory=uuid4)
The ClassType wrapper for the value’s type.
Defaults to ClassType(type(value)); pass one to carry a pinned id or
eager class attrs with the instance. Its eager class attrs are sent on
every crossing of the instance, so type(x) in the sandbox sees them.
Type: ClassType | None Default: None
def convert_value(name: str, value: Any) -> Any
Defers to the class wrapper’s hook, so a ClassType subclass that
redacts or wraps values covers the instances it constructs and any
instance sent with it as class_type; override here to differ.
Bases: BaseWrapper
Policy wrapper exposing a host class to the Monty sandbox.
ClassInstance’s sibling, applied to the class object itself: eager_attrs
sends class constants with the type, lazy_attrs serves them on demand,
and allowed_methods exposes classmethods/staticmethods.
With init=True, sandbox code may call the class; the construction
arrives as a __call__ method call, runs host-side, and the result
crosses back wrapped in a ClassInstance carrying the instance_*
policies.
Example:
session.feed_run(
'p = Point(1, 2)
assert p.x == 1',
inputs={'Point': ClassType(Point, init=True, instance_eager_attrs='all')},
)
The type/class to send.
Unique id for the type.
If unset an ID will be reused or generated.
Type: UUID | None Default: None
Whether sandbox code may instantiate the class.
Purely a host-side policy: it never crosses the wire, and construct
checks it on every request.
Type: bool Default: False
Policy applied to constructed instances (see ClassInstance).
Type: Policy Default: None
Policy applied to constructed instances (see ClassInstance).
Type: Policy Default: None
Policy applied to constructed instances (see ClassInstance).
Type: Policy Default: None
def get_eager_attrs() -> dict[str, Any]
Class-object variant of eager attrs: 'all' sends public
non-callable entries of the class __dict__ (class constants),
skipping methods and descriptors; an explicit list reads exactly
those names. Called when the class crosses as a value and for every
crossing of one of its instances.
def call_method(name: str, args: tuple[Any, ...], kwargs: dict[str, Any]) -> Any
Routes __call__ (construction) to construct; every other name
is a classmethod/staticmethod call gated by allowed_methods.
def method_allowed(name: str) -> bool
Only classmethods and staticmethods are callable on the class, under
every policy: an instance method reached through the class would take
an arbitrary sandbox value as self.
def construct(args: tuple[Any, ...], kwargs: dict[str, Any]) -> ClassInstance
Constructs an instance for the sandbox, re-checking the init policy.
Returns the instance wrapped with the instance_* policies and this
wrapper’s convert_value, so it registers and crosses back like any
host-sent ClassInstance.
def instance_wrapper(instance: Any) -> ClassInstance
Wraps a constructed instance with the instance_* policies.
The instance carries this wrapper as its class_type, so its class
keeps this wrapper’s id (an explicit one included) and eager class
attrs. A constructor returning an instance of another class (a
__new__ override) gets that class’s default ClassType instead,
since ClassInstance rejects a mismatched class_type.
Override to customize how constructed instances are exposed.
Read-only proxy for a class instance the host has no original object for:
a sandbox-defined instance, or a host-sent one after a session restore (the
instance store never survives load_session / load_snapshot). It keeps the
instance’s id, so passed back in it resolves to the live sandbox object
(attributes not applied; a freed one raises) or, after a restore, re-enters
as a host-backed copy built from attributes.
Class name of the instance (e.g. 'Point').
Type: str
Identity of the instance, the id the sandbox resolves it by when passed back.
Type: uuid.UUID
Whether the instance was a dataclass on the side that produced it.
Type: bool
The instance’s attributes as a plain dict.
Read-only proxy for a host class the session has no class object for: a
ClassType, or type(x) of a ClassInstance, returned after a session
restore. It keeps the class id, so passed back in it is the same sandbox
type object.
Name of the class (e.g. 'Point').
Type: str
Identity of the class, the id the sandbox resolves it by when passed back.
Type: uuid.UUID
Whether the class is a dataclass on the side that produced it.
Type: bool
The eager class attrs that crossed with the class, as a plain dict.