mpylab.tools.safe_exec moduleΒΆ

Restricted execution of a direct method call stored as text.

The helpers in this module support autosave instructions such as measurement.Measure(**parameters) without using eval() or exec(). Only a direct public method on an explicitly supplied scope object can be called. Arguments are limited to literals, containers, public attributes, names from the scope, and basic arithmetic.

This is a defence-in-depth measure for stored command strings. It does not make untrusted pickle files safe: Python pickle loading can execute code before these checks are reached.

mpylab.tools.safe_exec.safe_dynamic_call_blacklist(call_string: str, local_scope: Mapping[str, Any]) Any

Execute a restricted direct public method call.

Parameter names on CRITICAL_BLACKLIST and dunder parameter names are rejected. Values supplied through **parameters are passed through unchanged so measurement configurations can contain quantities, callbacks, and device-specific objects.

mpylab.tools.safe_exec.safe_dynamic_call_whitelist(call_string: str, local_scope: Mapping[str, Any], allowed_params: Set[str] | None = None) Any

Execute a restricted direct method call using a parameter allowlist.

When allowed_params is None, all non-critical parameter names are accepted. Any parsing, validation, lookup, or call error is wrapped in a RuntimeError with a stable Whitelist Execution Error prefix.