use std::ffi::CString;
use std::path::{Path, PathBuf};
use pyo3::prelude::*;
use pyo3::types::PyModule;
#[derive(Debug, Clone, PartialEq)]
pub enum PyBlockError {
Load {
path: PathBuf,
message: String,
},
MissingFunction {
path: PathBuf,
name: &'static str,
message: String,
},
Exception {
path: PathBuf,
function: &'static str,
message: String,
},
}
impl std::fmt::Display for PyBlockError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PyBlockError::Load { path, message } => {
write!(
f,
"failed to load Python block {}: {message}",
path.display()
)
}
PyBlockError::MissingFunction {
path,
name,
message,
} => write!(
f,
"Python block {} is missing required function `{name}`: {message}",
path.display()
),
PyBlockError::Exception {
path,
function,
message,
} => write!(
f,
"Python block {} raised in `{function}`: {message}",
path.display()
),
}
}
}
impl std::error::Error for PyBlockError {}
#[derive(Debug, Clone, Copy)]
pub enum PyInput<'a> {
Scalar(f64),
Vector(&'a [f64]),
}
fn py_traceback(_py: Python<'_>, err: &PyErr) -> String {
err.to_string()
}
fn build_args<'py>(
py: Python<'py>,
inputs: &[PyInput],
) -> PyResult<pyo3::Bound<'py, pyo3::types::PyList>> {
use numpy::PyArray1;
use pyo3::types::PyList;
let list = PyList::empty(py);
for input in inputs {
match input {
PyInput::Scalar(x) => list.append(*x)?,
PyInput::Vector(xs) => list.append(PyArray1::from_slice(py, xs))?,
}
}
Ok(list)
}
fn extract_outputs(result: pyo3::Bound<'_, PyAny>, out_len: usize) -> PyResult<Vec<f64>> {
if out_len <= 1 {
let v: f64 = result.extract()?;
return Ok(vec![v]);
}
extract_vector(result)
}
fn extract_vector(result: pyo3::Bound<'_, PyAny>) -> PyResult<Vec<f64>> {
let seq = result.try_iter()?;
let mut out = Vec::new();
for item in seq {
out.push(item?.extract::<f64>()?);
}
Ok(out)
}
#[derive(Default)]
pub struct PyBlockRegistry {
sources: std::collections::BTreeMap<PathBuf, String>,
}
impl PyBlockRegistry {
pub fn new() -> Self {
Self::default()
}
fn load_source(&mut self, path: &Path) -> Result<String, PyBlockError> {
if let Some(source) = self.sources.get(path) {
return Ok(source.clone());
}
let source = std::fs::read_to_string(path).map_err(|e| PyBlockError::Load {
path: path.to_path_buf(),
message: e.to_string(),
})?;
self.sources.insert(path.to_path_buf(), source.clone());
Ok(source)
}
pub fn instantiate(&mut self, path: &Path) -> Result<PyBlockInstance, PyBlockError> {
let source = self.load_source(path)?;
PyBlockInstance::new(path, &source, false)
}
pub fn instantiate_xc(&mut self, path: &Path) -> Result<PyBlockInstance, PyBlockError> {
let source = self.load_source(path)?;
PyBlockInstance::new(path, &source, true)
}
}
#[derive(Debug)]
pub struct PyBlockInstance {
path: PathBuf,
source: String,
state: Py<PyAny>,
output_fn: Option<Py<PyAny>>,
derivative_fn: Option<Py<PyAny>>,
output_xc_fn: Option<Py<PyAny>>,
update_fn: Option<Py<PyAny>>,
next_sample_hit_fn: Option<Py<PyAny>>,
}
impl PyBlockInstance {
fn new(path: &Path, source: &str, want_xc: bool) -> Result<Self, PyBlockError> {
Python::attach(|py| {
let module = exec_fresh_module(py, path, source).map_err(|e| PyBlockError::Load {
path: path.to_path_buf(),
message: py_traceback(py, &e),
})?;
let get = |name: &'static str| -> Option<Py<PyAny>> {
module.getattr(name).ok().map(|f| f.unbind())
};
let start_fn = get("start").ok_or_else(|| PyBlockError::MissingFunction {
path: path.to_path_buf(),
name: "start",
message: "not found".to_string(),
})?;
let output_fn = get("output");
let derivative_fn = get("derivative");
let output_xc_fn = get("output_xc");
let update_fn = get("update");
let next_sample_hit_fn = get("next_sample_hit");
if want_xc {
if derivative_fn.is_none() {
return Err(PyBlockError::MissingFunction {
path: path.to_path_buf(),
name: "derivative",
message: "required because this block declares xc_count > 0".to_string(),
});
}
if output_xc_fn.is_none() {
return Err(PyBlockError::MissingFunction {
path: path.to_path_buf(),
name: "output_xc",
message: "required because this block declares xc_count > 0".to_string(),
});
}
} else if output_fn.is_none() {
return Err(PyBlockError::MissingFunction {
path: path.to_path_buf(),
name: "output",
message: "not found (or this block only implements the xc contract -- see \
PyBlockRegistry::instantiate_xc)"
.to_string(),
});
}
let state = start_fn
.bind(py)
.call0()
.map_err(|e| PyBlockError::Exception {
path: path.to_path_buf(),
function: "start",
message: py_traceback(py, &e),
})?
.unbind();
Ok(PyBlockInstance {
path: path.to_path_buf(),
source: source.to_string(),
state,
output_fn,
derivative_fn,
output_xc_fn,
update_fn,
next_sample_hit_fn,
})
})
}
pub fn call(
&mut self,
t: f64,
dt: f64,
inputs: &[PyInput],
out_len: usize,
) -> Result<Vec<f64>, PyBlockError> {
if self.output_fn.is_none() {
panic!(
"call requires an instance created via PyBlockRegistry::instantiate (which \
already requires `output` up front -- this should be unreachable)"
);
}
Python::attach(|py| {
let output_fn = self.output_fn.as_ref().unwrap().clone_ref(py);
let args = build_args(py, inputs).map_err(|e| self.exception(py, "output", e))?;
let result = output_fn
.bind(py)
.call1((self.state.bind(py), t, dt, args))
.map_err(|e| self.exception(py, "output", e))?;
extract_outputs(result, out_len).map_err(|e| self.exception(py, "output", e))
})
}
pub fn call_xc(
&mut self,
t: f64,
dt: f64,
inputs: &[PyInput],
xc: &[f64],
out_len: usize,
) -> Result<Vec<f64>, PyBlockError> {
if self.output_xc_fn.is_none() {
panic!(
"call_xc requires an instance created via PyBlockRegistry::instantiate_xc \
(which already requires `output_xc` up front -- this should be unreachable)"
);
}
Python::attach(|py| {
let output_xc_fn = self.output_xc_fn.as_ref().unwrap().clone_ref(py);
let args = build_args(py, inputs).map_err(|e| self.exception(py, "output_xc", e))?;
let xc_arr = numpy::PyArray1::from_slice(py, xc);
let result = output_xc_fn
.bind(py)
.call1((self.state.bind(py), t, dt, args, xc_arr))
.map_err(|e| self.exception(py, "output_xc", e))?;
extract_outputs(result, out_len).map_err(|e| self.exception(py, "output_xc", e))
})
}
pub fn update(&mut self, t: f64, dt: f64, inputs: &[PyInput]) -> Result<(), PyBlockError> {
if self.update_fn.is_none() {
return Ok(());
}
Python::attach(|py| {
let update_fn = self.update_fn.as_ref().unwrap().clone_ref(py);
let args = build_args(py, inputs).map_err(|e| self.exception(py, "update", e))?;
update_fn
.bind(py)
.call1((self.state.bind(py), t, dt, args))
.map_err(|e| self.exception(py, "update", e))?;
Ok(())
})
}
pub fn update_xc(
&mut self,
t: f64,
dt: f64,
inputs: &[PyInput],
xc: &[f64],
) -> Result<(), PyBlockError> {
if self.update_fn.is_none() {
return Ok(());
}
Python::attach(|py| {
let update_fn = self.update_fn.as_ref().unwrap().clone_ref(py);
let args = build_args(py, inputs).map_err(|e| self.exception(py, "update", e))?;
let xc_arr = numpy::PyArray1::from_slice(py, xc);
update_fn
.bind(py)
.call1((self.state.bind(py), t, dt, args, xc_arr))
.map_err(|e| self.exception(py, "update", e))?;
Ok(())
})
}
pub fn supports_next_sample_hit(&self) -> bool {
self.next_sample_hit_fn.is_some()
}
pub fn next_sample_hit(
&self,
t: f64,
dt: f64,
inputs: &[PyInput],
) -> Result<f64, PyBlockError> {
if self.next_sample_hit_fn.is_none() {
panic!(
"next_sample_hit requires a .py file defining next_sample_hit (required for \
ts=variable — this should be unreachable, since that's checked up front)"
);
}
Python::attach(|py| {
let f = self.next_sample_hit_fn.as_ref().unwrap().clone_ref(py);
let args =
build_args(py, inputs).map_err(|e| self.exception(py, "next_sample_hit", e))?;
let result = f
.bind(py)
.call1((self.state.bind(py), t, dt, args))
.map_err(|e| self.exception(py, "next_sample_hit", e))?;
result
.extract::<f64>()
.map_err(|e| self.exception(py, "next_sample_hit", e))
})
}
pub fn next_sample_hit_xc(
&self,
t: f64,
dt: f64,
inputs: &[PyInput],
xc: &[f64],
) -> Result<f64, PyBlockError> {
if self.next_sample_hit_fn.is_none() {
panic!(
"next_sample_hit_xc requires a .py file defining next_sample_hit (required for \
ts=variable — this should be unreachable, since that's checked up front)"
);
}
Python::attach(|py| {
let f = self.next_sample_hit_fn.as_ref().unwrap().clone_ref(py);
let args =
build_args(py, inputs).map_err(|e| self.exception(py, "next_sample_hit", e))?;
let xc_arr = numpy::PyArray1::from_slice(py, xc);
let result = f
.bind(py)
.call1((self.state.bind(py), t, dt, args, xc_arr))
.map_err(|e| self.exception(py, "next_sample_hit", e))?;
result
.extract::<f64>()
.map_err(|e| self.exception(py, "next_sample_hit", e))
})
}
pub fn derivative(
&self,
t: f64,
inputs: &[PyInput],
xc: &[f64],
) -> Result<Vec<f64>, PyBlockError> {
if self.derivative_fn.is_none() {
panic!(
"derivative requires an instance created via PyBlockRegistry::instantiate_xc \
(which already requires `derivative` up front -- this should be unreachable)"
);
}
Python::attach(|py| {
let derivative_fn = self.derivative_fn.as_ref().unwrap().clone_ref(py);
let args = build_args(py, inputs).map_err(|e| self.exception(py, "derivative", e))?;
let xc_arr = numpy::PyArray1::from_slice(py, xc);
let result = derivative_fn
.bind(py)
.call1((self.state.bind(py), t, args, xc_arr))
.map_err(|e| self.exception(py, "derivative", e))?;
extract_vector(result).map_err(|e| self.exception(py, "derivative", e))
})
}
pub fn rk4_step_xc(
&self,
xc: &[f64],
t: f64,
inputs: &[PyInput],
dt: f64,
) -> Result<Vec<f64>, PyBlockError> {
let add = |a: &[f64], b: &[f64], scale: f64| -> Vec<f64> {
a.iter().zip(b).map(|(ai, bi)| ai + scale * bi).collect()
};
let k1 = self.derivative(t, inputs, xc)?;
let k2 = self.derivative(t, inputs, &add(xc, &k1, dt / 2.0))?;
let k3 = self.derivative(t, inputs, &add(xc, &k2, dt / 2.0))?;
let k4 = self.derivative(t, inputs, &add(xc, &k3, dt))?;
Ok((0..xc.len())
.map(|i| xc[i] + (dt / 6.0) * (k1[i] + 2.0 * k2[i] + 2.0 * k3[i] + k4[i]))
.collect())
}
pub fn try_clone(&self) -> Result<PyBlockInstance, PyBlockError> {
Python::attach(|py| {
let module = exec_fresh_module(py, &self.path, &self.source).map_err(|e| {
PyBlockError::Load {
path: self.path.clone(),
message: py_traceback(py, &e),
}
})?;
let get = |name: &'static str| -> Option<Py<PyAny>> {
module.getattr(name).ok().map(|f| f.unbind())
};
let copy_mod = py
.import("copy")
.map_err(|e| self.exception(py, "try_clone", e))?;
let cloned_state = copy_mod
.call_method1("deepcopy", (self.state.bind(py),))
.map_err(|e| self.exception(py, "try_clone", e))?
.unbind();
Ok(PyBlockInstance {
path: self.path.clone(),
source: self.source.clone(),
state: cloned_state,
output_fn: get("output"),
derivative_fn: get("derivative"),
output_xc_fn: get("output_xc"),
update_fn: get("update"),
next_sample_hit_fn: get("next_sample_hit"),
})
})
}
pub fn pickle_state(&self) -> Result<Vec<u8>, PyBlockError> {
Python::attach(|py| {
let pickle = py
.import("pickle")
.map_err(|e| self.exception(py, "pickle_state", e))?;
let bytes = pickle
.call_method1("dumps", (self.state.bind(py),))
.map_err(|e| self.exception(py, "pickle_state", e))?;
bytes
.extract::<Vec<u8>>()
.map_err(|e| self.exception(py, "pickle_state", e))
})
}
pub fn restore_state(&mut self, bytes: &[u8]) -> Result<(), PyBlockError> {
Python::attach(|py| {
let pickle = py
.import("pickle")
.map_err(|e| self.exception(py, "restore_state", e))?;
let state = pickle
.call_method1("loads", (pyo3::types::PyBytes::new(py, bytes),))
.map_err(|e| self.exception(py, "restore_state", e))?;
self.state = state.unbind();
Ok(())
})
}
fn exception(&self, py: Python<'_>, function: &'static str, err: PyErr) -> PyBlockError {
PyBlockError::Exception {
path: self.path.clone(),
function,
message: py_traceback(py, &err),
}
}
}
impl Clone for PyBlockInstance {
fn clone(&self) -> Self {
self.try_clone()
.unwrap_or_else(|e| panic!("cannot clone this PyBlockInstance: {e}"))
}
}
fn exec_fresh_module<'py>(
py: Python<'py>,
path: &Path,
source: &str,
) -> PyResult<pyo3::Bound<'py, PyModule>> {
let code = CString::new(source)?;
let filename = CString::new(path.to_string_lossy().as_bytes())?;
let module_name = CString::new(
path.file_stem()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or_else(|| "pyblock".to_string()),
)?;
PyModule::from_code(py, &code, &filename, &module_name)
}
mod pure_function {
use std::path::{Path, PathBuf};
use pyo3::prelude::*;
use pyo3::types::PyTuple;
use super::{exec_fresh_module, extract_outputs, py_traceback, PyBlockError, PyInput};
fn build_positional_args<'py>(
py: Python<'py>,
inputs: &[PyInput],
) -> PyResult<pyo3::Bound<'py, PyTuple>> {
use numpy::PyArray1;
let mut items: Vec<Py<PyAny>> = Vec::with_capacity(inputs.len());
for input in inputs {
let obj: Py<PyAny> = match input {
PyInput::Scalar(x) => x.into_pyobject(py)?.into_any().unbind(),
PyInput::Vector(xs) => PyArray1::from_slice(py, xs).into_any().unbind(),
};
items.push(obj);
}
PyTuple::new(py, items)
}
#[derive(Default)]
pub struct PyFunctionRegistry {
sources: std::collections::BTreeMap<PathBuf, String>,
}
impl PyFunctionRegistry {
pub fn new() -> Self {
Self::default()
}
fn load_source(&mut self, path: &Path) -> Result<String, PyBlockError> {
if let Some(source) = self.sources.get(path) {
return Ok(source.clone());
}
let source = std::fs::read_to_string(path).map_err(|e| PyBlockError::Load {
path: path.to_path_buf(),
message: e.to_string(),
})?;
self.sources.insert(path.to_path_buf(), source.clone());
Ok(source)
}
pub fn instantiate(
&mut self,
path: &Path,
function_name: &str,
) -> Result<PyFunctionInstance, PyBlockError> {
let source = self.load_source(path)?;
PyFunctionInstance::new(path, &source, function_name)
}
}
#[derive(Debug)]
pub struct PyFunctionInstance {
path: PathBuf,
function_name: String,
function: Py<PyAny>,
}
impl PyFunctionInstance {
fn new(path: &Path, source: &str, function_name: &str) -> Result<Self, PyBlockError> {
Python::attach(|py| {
let module =
exec_fresh_module(py, path, source).map_err(|e| PyBlockError::Load {
path: path.to_path_buf(),
message: py_traceback(py, &e),
})?;
let function = module
.getattr(function_name)
.map_err(|_| PyBlockError::MissingFunction {
path: path.to_path_buf(),
name: "function",
message: format!("`{function_name}` not found"),
})?
.unbind();
Ok(PyFunctionInstance {
path: path.to_path_buf(),
function_name: function_name.to_string(),
function,
})
})
}
pub fn call(&self, inputs: &[PyInput], out_len: usize) -> Result<Vec<f64>, PyBlockError> {
Python::attach(|py| {
let args = build_positional_args(py, inputs).map_err(|e| self.exception(py, e))?;
let result = self
.function
.bind(py)
.call1(args)
.map_err(|e| self.exception(py, e))?;
extract_outputs(result, out_len).map_err(|e| self.exception(py, e))
})
}
fn exception(&self, py: Python<'_>, err: PyErr) -> PyBlockError {
PyBlockError::Exception {
path: self.path.clone(),
function: "call",
message: format!("in `{}`: {}", self.function_name, py_traceback(py, &err)),
}
}
}
impl Clone for PyFunctionInstance {
fn clone(&self) -> Self {
Python::attach(|py| PyFunctionInstance {
path: self.path.clone(),
function_name: self.function_name.clone(),
function: self.function.clone_ref(py),
})
}
}
}
pub use pure_function::{PyFunctionInstance, PyFunctionRegistry};