Struct inline_python::Context[][src]

pub struct Context { /* fields omitted */ }
Expand description

An execution context for Python code.

This can be used to keep all global variables and imports intact between macro invocations:

let c = Context::new();

c.run(python! {
  foo = 5
});

c.run(python! {
  assert foo == 5
});

You may also use it to inspect global variables after the execution of the Python code, or set global variables before running:

let c = Context::new();

c.set("x", 13);

c.run(python! {
  foo = x + 2
});

assert_eq!(c.get::<i32>("foo"), 15);

Implementations

Create a new context for running Python code.

This function temporarily acquires the GIL. If you already have the GIL, you can use Context::new_with_gil instead.

This function panics if it fails to create the context.

Create a new context for running Python code.

You must acquire the GIL to call this function.

This function panics if it fails to create the context.

Get the globals as dictionary.

Retrieve a global variable from the context.

This function temporarily acquires the GIL. If you already have the GIL, you can use Context::get_with_gil instead.

This function panics if the variable doesn’t exist, or the conversion fails.

Retrieve a global variable from the context.

This function panics if the variable doesn’t exist, or the conversion fails.

Set a global variable in the context.

This function temporarily acquires the GIL. If you already have the GIL, you can use Context::set_with_gil instead.

This function panics if the conversion fails.

Set a global variable in the context.

This function panics if the conversion fails.

Add a wrapped #[pyfunction] or #[pymodule] using its own __name__.

Use this with pyo3::wrap_pyfunction or pyo3::wrap_pymodule.

use pyo3::{prelude::*, wrap_pyfunction};

#[pyfunction]
fn get_five() -> i32 {
    5
}

fn main() {
    let c = Context::new();

    c.add_wrapped(wrap_pyfunction!(get_five));

    c.run(python! {
        assert get_five() == 5
    });
}

This function temporarily acquires the GIL. If you already have the GIL, you can use Context::add_wrapped_with_gil instead.

Add a wrapped #[pyfunction] or #[pymodule] using its own __name__.

See Context::add_wrapped.

Run Python code using this context.

This function should be called using the python!{} macro:

let c = Context::new();

c.run(python!{
    print("Hello World")
});

This function temporarily acquires the GIL. If you already have the GIL, you can use Context::run_with_gil instead.

This function panics if the Python code fails.

Run Python code using this context.

This function should be called using the python!{} macro, just like Context::run.

This function panics if the Python code fails.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.