Skip to content

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.

ClassInstance

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'})},
)

Attributes

id

Unique id for the value.

Type: UUID Default: field(default_factory=uuid4)

class_type

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

Methods

convert_value
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.

Returns

Any

ClassType

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')\},
)

Attributes

value

The type/class to send.

Type: type[Any]

id

Unique id for the type.

If unset an ID will be reused or generated.

Type: UUID | None Default: None

init

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

instance_eager_attrs

Policy applied to constructed instances (see ClassInstance).

Type: Policy Default: None

instance_lazy_attrs

Policy applied to constructed instances (see ClassInstance).

Type: Policy Default: None

instance_allowed_methods

Policy applied to constructed instances (see ClassInstance).

Type: Policy Default: None

Methods

get_eager_attrs
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.

Returns

dict[str, Any]

get_type_hints
def get_type_hints() -> Mapping[str, Any]

The class’s type hints, returned by typing.get_type_hints() on the class in the sandbox. Called each time the sandbox asks; an exception (NameError for an unresolvable forward reference) is raised there.

Override to return different hints in any mapping, or {} to hide them.

Returns

Mapping[str, Any]

call_method
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.

Returns

Any

method_allowed
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.

Returns

bool

construct
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.

Returns

ClassInstance

instance_wrapper
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.

Returns

ClassInstance

MontyClassProxy

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.

== and hash() follow the sandbox class where the host can replay it: a @dataclass compares (and, when frozen, hashes) its fields. Sandbox code never runs on the host, so a user-defined __eq__ is replaced by id comparison, as is any rule for a proxy whose attributes hold a host object (whose own __eq__ / __hash__ would run). The hash follows ==: the compared fields, or the id (even for a user-defined __hash__). A class the sandbox cannot hash raises TypeError from hash(), and proxies nested over 1000 deep through their fields raise RecursionError.

Attributes

name

Class name of the instance (e.g. 'Point').

Type: str

id

Identity of the instance, the id the sandbox resolves it by when passed back.

Type: uuid.UUID

is_dataclass

Whether the instance was a dataclass on the side that produced it.

Type: bool

attributes

A copy of the instance’s attributes as a plain dict; editing it changes nothing.

Type: dict[str, Any]

MontyClassTypeProxy

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. Proxies compare and hash by id.

Attributes

name

Name of the class (e.g. 'Point').

Type: str

id

Identity of the class, the id the sandbox resolves it by when passed back.

Type: uuid.UUID

is_dataclass

Whether the class is a dataclass on the side that produced it.

Type: bool

attributes

A copy of the eager class attrs that crossed with the class, as a plain dict; editing it changes nothing.

Type: dict[str, Any]

MontyNamedTuple

Bases: ]

A namedtuple returned by the sandbox: a tuple whose fields are also attributes.

It supports the instance API of collections.namedtuple (p.x, _fields, _asdict(), _replace(), _make()), but every sandbox namedtuple type shares this one class, so type(p) is MontyNamedTuple and the sandbox’s type name is p._type_name, such as 'Point' or 'os.stat_result'.

MontyStdTypeProxy

Read-only proxy for a builtin function, or a type object outside the data-type allowlist, returned from the sandbox: open, type(print), functools.partial. Only the name crosses, so the host never holds a live callable built from sandbox output. Passed back in, it is the builtin again.

Attributes

kind

'function' for a builtin function, 'type' for a type object.

Type: Literal[‘function’, ‘type’]

name

The name the sandbox renders the builtin as ('open', 'functools.partial').

Type: str