inline-python 0.14.4

Inline Python code directly in your Rust code
Documentation
use crate::Context;
use pyo3::{Bound, PyObject, PyResult, Python, ffi, types::PyAny};

pub fn run_python_code<'p>(
    py: Python<'p>,
    context: &Context,
    bytecode: &[u8],
) -> PyResult<Bound<'p, PyAny>> {
    unsafe {
        let ptr = ffi::PyMarshal_ReadObjectFromString(
            bytecode.as_ptr() as *const _,
            bytecode.len() as isize,
        );
        let code = PyObject::from_owned_ptr_or_err(py, ptr)?;
        let result = ffi::PyEval_EvalCode(
            code.as_ptr(),
            context.globals.as_ptr(),
            std::ptr::null_mut(),
        );
        Bound::from_owned_ptr_or_err(py, result)
    }
}