pub struct Interpreter {
pub global_state: PyRc<PyGlobalState>,
/* private fields */
}Expand description
One isolated Python interpreter in the process (≈ CPython PyInterpreterState + main tstate).
Historically RustPython exposed a single process-level Interpreter. For PEP 734
(multiple interpreters / subinterpreters) this type is now the owned handle for
one interpreter. Use Interpreter::create_subinterpreter to create additional
isolated interpreters that share the process-wide type context but not modules or
PyGlobalState.
§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>",
).map_err(|err| err.into_pyexception(vm, Some(source))).unwrap();
vm.run_code_obj(code_obj, scope).unwrap();
});Fields§
§global_state: PyRc<PyGlobalState>Implementations§
Source§impl Interpreter
impl Interpreter
Sourcepub fn builder(settings: Settings) -> InterpreterBuilder
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();Sourcepub fn without_stdlib(settings: Settings) -> Self
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.
Sourcepub fn with_init<F>(settings: Settings, init: F) -> Selfwhere
F: FnOnce(&mut VirtualMachine),
pub fn with_init<F>(settings: Settings, init: F) -> Selfwhere
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.
Sourcepub fn id(&self) -> i64
pub fn id(&self) -> i64
Process-global interpreter id (main is super::MAIN_INTERPRETER_ID).
Sourcepub fn whence(&self) -> InterpreterWhence
pub fn whence(&self) -> InterpreterWhence
Where this interpreter was created.
Sourcepub fn is_main(&self) -> bool
pub fn is_main(&self) -> bool
Whether this is a top-level interpreter rather than a subinterpreter.
Every top-level interpreter answers true; for the process main, use
Interpreter::is_process_main.
Sourcepub fn is_process_main(&self) -> bool
pub fn is_process_main(&self) -> bool
Whether this is the PEP 734 process main interpreter (get_main()).
Unlike Interpreter::is_main, which is set for every top-level
interpreter, this is true for only the single first-registered main.
Sourcepub fn create_subinterpreter(&self) -> Self
pub fn create_subinterpreter(&self) -> Self
Create an isolated subinterpreter sharing this interpreter’s type context
(Context) and module definitions, but with its own sys.modules,
builtins module instance, thread registry, and stop-the-world state.
May be called while the parent is entered (matching CPython, where
_interpreters.create() runs under the main interpreter). When the
calling thread is currently attached to a VM, that attachment is
temporarily saved so the subinterpreter can bootstrap as an outermost
enter (correct thread-slot / stop-the-world state).
Sourcepub fn create_subinterpreter_from_vm(
parent: &VirtualMachine,
config: InterpreterConfig,
) -> Result<Self, &'static str>
pub fn create_subinterpreter_from_vm( parent: &VirtualMachine, config: InterpreterConfig, ) -> Result<Self, &'static str>
Create a subinterpreter from a parent VM (the currently entered one).
Sourcepub fn create_subinterpreter_with_config(
&self,
config: InterpreterConfig,
) -> Result<Self, &'static str>
pub fn create_subinterpreter_with_config( &self, config: InterpreterConfig, ) -> Result<Self, &'static str>
Create a subinterpreter with an explicit PEP 734 / PyInterpreterConfig.
Sourcepub fn enter<F, R>(&self, f: F) -> Rwhere
F: FnOnce(&VirtualMachine) -> R,
pub fn enter<F, R>(&self, f: F) -> Rwhere
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.
Sourcepub fn enter_and_expect<F, R>(&self, f: F, msg: &str) -> R
pub fn enter_and_expect<F, R>(&self, f: F, msg: &str) -> 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.
Sourcepub fn run<F>(self, f: F) -> u32
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.
Sourcepub fn finalize(self, exc: Option<PyBaseExceptionRef>) -> u32
pub fn finalize(self, exc: Option<PyBaseExceptionRef>) -> u32
Finalize vm and turns an exception to exit code.
Finalization steps (matching Py_FinalizeEx):
- Flush stdout and stderr.
- Handle exit exception and turn it to exit code.
- Call threading._shutdown() to join non-daemon threads.
- Run atexit exit functions.
- Set finalizing flag and hang remaining daemon threads.
- Forced GC collection pass (collect cycles while builtins are available).
- Module finalization (finalize_modules).
- Clear interpreter-owned cross-interpreter data.
- Final stdout/stderr flush.
Note that calling finalize is not necessary by purpose though.
Auto Trait Implementations§
impl !Freeze for Interpreter
impl !RefUnwindSafe for Interpreter
impl !Send for Interpreter
impl !Sync for Interpreter
impl !UnwindSafe for Interpreter
impl Unpin for Interpreter
impl UnsafeUnpin for Interpreter
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
impl<T> Helper for T
Source§impl<T, U> ImaginaryInto<U> for Twhere
U: ImaginaryFrom<T>,
impl<T, U> ImaginaryInto<U> for Twhere
U: ImaginaryFrom<T>,
fn imaginary_into(self) -> U
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