pub struct InterpreterBuilder {
pub ctx: PyRc<Context>,
/* private fields */
}Expand description
Configuration builder for constructing an Interpreter.
This is the preferred way to configure and create an interpreter with custom modules.
Modules must be registered before the interpreter is built,
similar to CPython’s PyImport_AppendInittab which must be called before Py_Initialize.
§Example
use rustpython_vm::Interpreter;
let builder = Interpreter::builder(Default::default());
// In practice, add stdlib: builder.add_native_modules(&stdlib_module_defs(&builder.ctx))
let interp = builder.build();Fields§
§ctx: PyRc<Context>Implementations§
Source§impl InterpreterBuilder
impl InterpreterBuilder
Sourcepub fn settings(self, settings: Settings) -> Self
pub fn settings(self, settings: Settings) -> Self
Set custom settings for the interpreter.
If called multiple times, only the last settings will be used.
Sourcepub fn add_native_module(self, def: &'static PyModuleDef) -> Self
pub fn add_native_module(self, def: &'static PyModuleDef) -> Self
Add a single native module definition.
§Example
use rustpython_vm::{Interpreter, builtins::PyModuleDef};
let builder = Interpreter::builder(Default::default());
// Note: In practice, use module_def from your #[pymodule]
// let def = mymodule::module_def(&builder.ctx);
// let interp = builder.add_native_module(def).build();
let interp = builder.build();Sourcepub fn add_native_modules(self, defs: &[&'static PyModuleDef]) -> Self
pub fn add_native_modules(self, defs: &[&'static PyModuleDef]) -> Self
Add multiple native module definitions.
§Example
use rustpython_vm::Interpreter;
let builder = Interpreter::builder(Default::default());
// In practice, use module_defs from rustpython_stdlib:
// let defs = rustpython_stdlib::stdlib_module_defs(&builder.ctx);
// let interp = builder.add_native_modules(&defs).build();
let interp = builder.build();Sourcepub fn init_hook<F>(self, init: F) -> Selfwhere
F: FnOnce(&mut VirtualMachine) + 'static,
pub fn init_hook<F>(self, init: F) -> Selfwhere
F: FnOnce(&mut VirtualMachine) + 'static,
Add a custom initialization hook.
Hooks are executed in the order they are added during interpreter creation. This function will be called after modules are registered but before the VM is initialized, allowing for additional customization.
§Example
use rustpython_vm::Interpreter;
let interp = Interpreter::builder(Default::default())
.init_hook(|vm| {
// Custom initialization
})
.build();Sourcepub fn add_frozen_modules<I>(self, frozen: I) -> Self
pub fn add_frozen_modules<I>(self, frozen: I) -> Self
Add frozen modules to the interpreter.
Frozen modules are Python modules compiled into the binary. This method accepts any iterator of (name, FrozenModule) pairs.
§Example
use rustpython_vm::Interpreter;
let interp = Interpreter::builder(Default::default())
// In practice: .add_frozen_modules(rustpython_pylib::FROZEN_STDLIB)
.build();Sourcepub fn build(self) -> Interpreter
pub fn build(self) -> Interpreter
Build the interpreter.
This consumes the configuration and returns a fully initialized Interpreter.
Sourcepub fn interpreter(self) -> Interpreter
pub fn interpreter(self) -> Interpreter
Alias for build() for compatibility with the interpreter() pattern.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for InterpreterBuilder
impl !RefUnwindSafe for InterpreterBuilder
impl !Send for InterpreterBuilder
impl !Sync for InterpreterBuilder
impl Unpin for InterpreterBuilder
impl UnsafeUnpin for InterpreterBuilder
impl !UnwindSafe for InterpreterBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more