Skip to main content

Interpreter

Struct Interpreter 

Source
pub struct Interpreter {
    pub global_state: PyRc<PyGlobalState>,
    /* private fields */
}
Expand description

The general interface for the VM

§Examples

Runs a simple embedded hello world program.

use rustpython_vm::Interpreter;
use rustpython_vm::compiler::Mode;
Interpreter::without_stdlib(Default::default()).enter(|vm| {
    let scope = vm.new_scope_with_builtins();
    let source = r#"print("Hello World!")"#;
    let code_obj = vm.compile(
            source,
            Mode::Exec,
            "<embedded>".to_owned(),
    ).map_err(|err| vm.new_syntax_error(&err, Some(source))).unwrap();
    vm.run_code_obj(code_obj, scope).unwrap();
});

Fields§

§global_state: PyRc<PyGlobalState>

Implementations§

Source§

impl Interpreter

Source

pub fn builder(settings: Settings) -> InterpreterBuilder

Create a new interpreter configuration builder.

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

pub fn without_stdlib(settings: Settings) -> Self

This is a bare unit to build up an interpreter without the standard library. To create an interpreter with the standard library with the rustpython crate, use rustpython::InterpreterBuilder. To create an interpreter without the rustpython crate, but only with rustpython-vm, try to build one from the source code of InterpreterBuilder. It will not be a one-liner but it also will not be too hard.

Source

pub fn with_init<F>(settings: Settings, init: F) -> Self
where F: FnOnce(&mut VirtualMachine),

Create with initialize function taking mutable vm reference.

Note: This is a legacy API. To add stdlib, use Interpreter::builder() instead.

Source

pub fn enter<F, R>(&self, f: F) -> R
where F: FnOnce(&VirtualMachine) -> R,

Run a function with the main virtual machine and return a PyResult of the result.

To enter vm context multiple times or to avoid buffer/exception management, this function is preferred. enter is lightweight and it returns a python object in PyResult. You can stop or continue the execution multiple times by calling enter.

To finalize the vm once all desired enters are called, calling finalize will be helpful.

See also Interpreter::run for managed way to run the interpreter.

Source

pub fn enter_and_expect<F, R>(&self, f: F, msg: &str) -> R
where F: FnOnce(&VirtualMachine) -> PyResult<R>,

Run Interpreter::enter and call VirtualMachine::expect_pyresult for the result.

This function is useful when you want to expect a result from the function, but also print useful panic information when exception raised.

See also Interpreter::enter and VirtualMachine::expect_pyresult for more information.

Source

pub fn run<F>(self, f: F) -> u32

Run a function with the main virtual machine and return exit code.

To enter vm context only once and safely terminate the vm, this function is preferred. Unlike Interpreter::enter, run calls finalize and returns exit code. You will not be able to obtain Python exception in this way.

See Interpreter::finalize for the finalization steps. See also Interpreter::enter for pure function call to obtain Python exception.

Source

pub fn finalize(self, exc: Option<PyBaseExceptionRef>) -> u32

Finalize vm and turns an exception to exit code.

Finalization steps (matching Py_FinalizeEx):

  1. Flush stdout and stderr.
  2. Handle exit exception and turn it to exit code.
  3. Call threading._shutdown() to join non-daemon threads.
  4. Run atexit exit functions.
  5. Set finalizing flag (suppresses unraisable exceptions from del).
  6. Forced GC collection pass (collect cycles while builtins are available).
  7. Module finalization (finalize_modules).
  8. Final stdout/stderr flush.

Note that calling finalize is not necessary by purpose though.

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