[][src]Struct pyembed::MainPythonInterpreter

pub struct MainPythonInterpreter<'a> {
    pub config: PythonConfig,
    // some fields omitted
}

Manages an embedded Python interpreter.

Warning: Python interpreters have global state. There should only be a single instance of this type per process.

Instances must only be constructed through MainPythonInterpreter::new().

This type and its various functionality is a glorified wrapper around the Python C API. But there's a lot of added functionality on top of what the C API provides.

Both the low-level python3-sys and higher-level cpython crates are used.

Fields

config: PythonConfig

Methods

impl<'a> MainPythonInterpreter<'a>[src]

pub fn new(
    config: PythonConfig
) -> Result<MainPythonInterpreter<'a>, &'static str>
[src]

Construct a Python interpreter from a configuration.

The Python interpreter is initialized as a side-effect. The GIL is held.

pub fn release_gil(&mut self)[src]

Ensure the Python GIL is released.

pub fn acquire_gil(&mut self) -> Python<'a>[src]

Ensure the Python GIL is acquired, returning a handle on the interpreter.

pub fn run(&mut self) -> PyResult<PyObject>[src]

Runs the interpreter with the default code execution settings.

The crate was built with settings that configure what should be executed by default. Those settings will be loaded and executed.

pub fn run_and_handle_error(&mut self) -> PythonRunResult[src]

Runs the interpreter and handles any exception that was raised.

pub fn run_as_main(&mut self) -> i32[src]

Calls run() and resolves a suitable exit code.

pub fn run_module_as_main(&mut self, name: &str) -> PyResult<PyObject>[src]

Runs a Python module as the main module.

Returns the execution result of the module code.

The interpreter is automatically initialized if needed.

pub fn run_repl(&mut self) -> PyResult<PyObject>[src]

Start and run a Python REPL.

This emulates what CPython's main.c does.

The interpreter is automatically initialized if needed.

pub fn run_code(&mut self, code: &str) -> PyResult<PyObject>[src]

Runs Python code provided by a string.

This is similar to what python -c <code> would do.

The interpreter is automatically initialized if needed.

pub fn run_file(&mut self, filename: &CStr) -> PyResult<PyObject>[src]

Runs Python code in a filesystem path.

pub fn print_err(&mut self, err: PyErr)[src]

Print a Python error.

Under the hood this calls PyErr_PrintEx(), which may call Py_Exit() and may write to stderr.

Trait Implementations

impl<'a> Drop for MainPythonInterpreter<'a>[src]

Auto Trait Implementations

impl<'a> !RefUnwindSafe for MainPythonInterpreter<'a>

impl<'a> !Send for MainPythonInterpreter<'a>

impl<'a> !Sync for MainPythonInterpreter<'a>

impl<'a> Unpin for MainPythonInterpreter<'a>

impl<'a> !UnwindSafe for MainPythonInterpreter<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,