Skip to main content

InterpreterBuilder

Struct InterpreterBuilder 

Source
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

Source

pub fn new() -> Self

Create a new interpreter configuration with default settings.

Source

pub fn settings(self, settings: Settings) -> Self

Set custom settings for the interpreter.

If called multiple times, only the last settings will be used.

Source

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();
Source

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();
Source

pub fn init_hook<F>(self, init: F) -> Self
where 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();
Source

pub fn add_frozen_modules<I>(self, frozen: I) -> Self
where I: IntoIterator<Item = (&'static str, FrozenModule)>,

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();
Source

pub fn build(self) -> Interpreter

Build the interpreter.

This consumes the configuration and returns a fully initialized Interpreter.

Source

pub fn interpreter(self) -> Interpreter

Alias for build() for compatibility with the interpreter() pattern.

Trait Implementations§

Source§

impl Default for InterpreterBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T, U> ExactFrom<T> for U
where U: TryFrom<T>,

Source§

fn exact_from(value: T) -> U

Source§

impl<T, U> ExactInto<U> for T
where U: ExactFrom<T>,

Source§

fn exact_into(self) -> U

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> OverflowingInto<U> for T
where U: OverflowingFrom<T>,

Source§

impl<T, U> RoundingInto<U> for T
where U: RoundingFrom<T>,

Source§

impl<T, U> SaturatingInto<U> for T
where U: SaturatingFrom<T>,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> WrappingInto<U> for T
where U: WrappingFrom<T>,

Source§

fn wrapping_into(self) -> U

Source§

impl<T> PyThreadingConstraint for T