Struct Context

Source
pub struct Context { /* private fields */ }
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§

Source§

impl Context

Source

pub fn new() -> Self

Create a new context for running Python code.

This function panics if it fails to create the context.

Source

pub fn globals<'p>(&self) -> &Py<PyDict>

Get the globals as dictionary.

Source

pub fn get<T: for<'p> FromPyObject<'p>>(&self, name: &str) -> T

Retrieve a global variable from the context.

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

Source

pub fn set<T: for<'p> IntoPyObject<'p>>(&self, name: &str, value: T)

Set a global variable in the context.

This function panics if the conversion fails.

Source

pub fn add_wrapped( &self, wrapper: &impl Fn(Python<'_>) -> PyResult<Bound<'_, PyCFunction>>, )

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

pub fn run<F: FnOnce(&Bound<'_, PyDict>)>(&self, code: PythonBlock<F>)

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 panics if the Python code fails.

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> 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, 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> Ungil for T
where T: Send,