feat: early interface with Python

This commit is contained in:
2025-03-13 15:38:51 -04:00
parent afe5eae96b
commit 96495b2a85
22 changed files with 623 additions and 6 deletions

21
src/python_utils.rs Normal file
View File

@@ -0,0 +1,21 @@
use pyo3::{exceptions::PyTypeError, prelude::*};
/// Create a GIL-independent reference (similar to [`Arc`](std::sync::Arc))
pub fn detach<T>(bound: &Bound<T>) -> Py<T> {
let py = bound.py();
bound.as_unbound().clone_ref(py)
}
pub fn validate_type_by_name(bound: &Bound<PyAny>, expected_type_name: &str) -> PyResult<()> {
let py_type = bound.get_type();
let type_name = py_type.name()?;
let type_name = type_name.to_str()?;
if type_name != expected_type_name {
let fully_qualified_type_name = py_type.fully_qualified_name()?;
let fully_qualified_type_name = fully_qualified_type_name.to_str()?;
return Err(PyTypeError::new_err(format!("expected an instance of {expected_type_name} but got an instance of {fully_qualified_type_name}")));
}
return Ok(());
}