[][src]Struct inline_python::Context

pub struct Context { /* fields omitted */ }

An execution context for Python code.

If you pass a manually created context to the python!{} macro, you can share it across invocations. This will keep all global variables and imports intact between macro invocations.

let c = inline_python::Context::new();
python! {
  #![context = &c]
  foo = 5
}
python! {
  #![context = &c]
  assert foo == 5
}

You may also use it to inspect global variables after the execution of the Python code. Note that you need to acquire the GIL in order to access those globals:

use inline_python::{pyo3, python};
let context = inline_python::Context::new();
python! {
  #![context = &context]
  foo = 5
}

let gil = pyo3::Python::acquire_gil();
let py  = gil.python();
let foo: Option<i32> = context.get_global(py, "foo").unwrap();
assert_eq!(foo, Some(5));

Methods

impl Context[src]

pub fn new() -> Self[src]

Create a new context for running python code.

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

This function panics if it fails to create the context. See Context::new_checked for a version that returns a result.

pub fn new_checked() -> PyResult<Self>[src]

Create a new context for running python code.

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

pub fn new_with_gil(py: Python) -> PyResult<Self>[src]

Create a new context for running Python code.

You must acquire the GIL to call this function.

pub fn globals<'p>(&self, py: Python<'p>) -> &'p PyDict[src]

Get the globals as dictionary.

pub fn get_global<'p, T: FromPyObject<'p>>(
    self,
    py: Python<'p>,
    name: &str
) -> PyResult<Option<T>>
[src]

Retrieve a global variable from the context.

Auto Trait Implementations

impl Send for Context

impl Sync for Context

Blanket Implementations

impl<T> From for T[src]

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

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

impl<T, U> TryInto 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<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> FromPy for T[src]

impl<T, U> IntoPy for T where
    U: FromPy<T>, 
[src]