use std::path::PathBuf;
use std::sync::{Mutex, OnceLock};
use pyo3::prelude::*;
use pyo3::types::{
PyBool, PyByteArray, PyBytes, PyDict, PyFloat, PyFrozenSet, PyInt, PyList, PySet, PyString,
PyTuple,
};
use pyo3::IntoPyObjectExt;
use crate::host::{with_host, PyHost, PyObj};
use fusevm::Value;
static TABLE: OnceLock<Mutex<Vec<Py<PyAny>>>> = OnceLock::new();
fn table() -> &'static Mutex<Vec<Py<PyAny>>> {
TABLE.get_or_init(|| Mutex::new(Vec::new()))
}
static MODULE_HANDLES: OnceLock<Mutex<rustc_hash::FxHashMap<String, u32>>> = OnceLock::new();
fn module_handles() -> &'static Mutex<rustc_hash::FxHashMap<String, u32>> {
MODULE_HANDLES.get_or_init(|| Mutex::new(rustc_hash::FxHashMap::default()))
}
static TYPE_HANDLES: OnceLock<Mutex<rustc_hash::FxHashMap<usize, u32>>> = OnceLock::new();
fn type_handles() -> &'static Mutex<rustc_hash::FxHashMap<usize, u32>> {
TYPE_HANDLES.get_or_init(|| Mutex::new(rustc_hash::FxHashMap::default()))
}
fn resolve_home() -> Option<PathBuf> {
if let Some(p) = std::env::var_os("PYTHONRS_STDLIB") {
return Some(PathBuf::from(p));
}
if let Ok(exe) = std::env::current_exe() {
for candidate in [Some(exe.clone()), std::fs::canonicalize(&exe).ok()]
.into_iter()
.flatten()
{
if let Some(prefix) = candidate.parent().and_then(|d| d.parent()) {
if has_stdlib(prefix) {
return Some(prefix.to_path_buf());
}
}
}
}
if let Some(home) = dirs::home_dir() {
let prefix = home.join(".pythonrs");
if has_stdlib(&prefix) {
return Some(prefix);
}
}
if let Some(p) = std::env::var_os("PYTHONHOME") {
let prefix = PathBuf::from(p);
if has_stdlib(&prefix) {
return Some(prefix);
}
}
None
}
fn has_stdlib(prefix: &std::path::Path) -> bool {
stdlib_dir(prefix).is_some()
}
fn stdlib_dir(prefix: &std::path::Path) -> Option<PathBuf> {
let entries = std::fs::read_dir(prefix.join("lib")).ok()?;
entries.flatten().map(|e| e.path()).find(|p| {
p.file_name()
.and_then(|n| n.to_str())
.is_some_and(|n| n.starts_with("python3."))
&& p.join("encodings/__init__.py").is_file()
&& p.join("os.py").is_file()
})
}
fn bridge_unavailable(name: &str) -> String {
format!("ModuleNotFoundError: No module named '{name}'")
}
#[cfg(unix)]
fn linked_libpython_prefixes() -> Vec<PathBuf> {
use std::ffi::{CStr, CString, OsStr};
use std::os::unix::ffi::OsStrExt;
let Ok(sym) = CString::new("Py_IsInitialized") else {
return Vec::new();
};
let path = unsafe {
let addr = libc::dlsym(libc::RTLD_DEFAULT, sym.as_ptr());
if addr.is_null() {
return Vec::new();
}
let mut info: libc::Dl_info = std::mem::zeroed();
if libc::dladdr(addr, &mut info) == 0 || info.dli_fname.is_null() {
return Vec::new();
}
PathBuf::from(OsStr::from_bytes(CStr::from_ptr(info.dli_fname).to_bytes()))
};
let Some(libdir) = path.parent() else {
return Vec::new();
};
let mut out = vec![libdir.to_path_buf()];
if let Some(prefix) = libdir.parent() {
out.push(prefix.to_path_buf());
}
out
}
#[cfg(not(unix))]
fn linked_libpython_prefixes() -> Vec<PathBuf> {
Vec::new()
}
static BRIDGE_USABLE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true);
static INTERPRETER_STARTED: std::sync::atomic::AtomicBool =
std::sync::atomic::AtomicBool::new(false);
pub fn init() -> bool {
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| {
if let Some(p) = std::env::var_os("PYTHONRS_STDLIB") {
if !has_stdlib(std::path::Path::new(&p)) {
BRIDGE_USABLE.store(false, std::sync::atomic::Ordering::Relaxed);
return;
}
}
match resolve_home() {
Some(home) => std::env::set_var("PYTHONHOME", home),
None => {
if std::env::var_os("PYTHONHOME").is_some() {
std::env::remove_var("PYTHONHOME");
}
let linked = linked_libpython_prefixes();
if !linked.is_empty() && !linked.iter().any(|p| has_stdlib(p)) {
BRIDGE_USABLE.store(false, std::sync::atomic::Ordering::Relaxed);
return;
}
}
}
pyo3::prepare_freethreaded_python();
INTERPRETER_STARTED.store(true, std::sync::atomic::Ordering::Relaxed);
line_buffer_std_streams();
});
BRIDGE_USABLE.load(std::sync::atomic::Ordering::Relaxed)
}
fn line_buffer_std_streams() {
Python::with_gil(|py| {
let sys = match py.import("sys") {
Ok(m) => m,
Err(_) => return,
};
for stream in ["stdout", "stderr"] {
if let Ok(s) = sys.getattr(stream) {
let kw = PyDict::new(py);
if kw.set_item("line_buffering", true).is_ok() {
let _ = s.call_method("reconfigure", (), Some(&kw));
}
}
}
});
}
pub fn run_module(modname: &str, args: &[String]) -> i32 {
if !init() {
eprintln!("python: {}", bridge_unavailable("<module>"));
return 1;
}
Python::with_gil(|py| {
let sys = match py.import("sys") {
Ok(m) => m,
Err(e) => {
eprintln!("python: {e}");
return 1;
}
};
let argv = PyList::empty(py);
let _ = argv.append(modname);
for a in args {
let _ = argv.append(a);
}
if let Err(e) = sys.setattr("argv", argv) {
eprintln!("python: {e}");
return 1;
}
let runpy = match py.import("runpy") {
Ok(m) => m,
Err(e) => {
eprintln!("python: {e}");
return 1;
}
};
let code = match runpy.call_method1("_run_module_as_main", (modname, true)) {
Ok(_) => 0,
Err(e) => {
if e.is_instance_of::<pyo3::exceptions::PySystemExit>(py) {
system_exit_code(py, &e)
} else {
e.print(py);
1
}
}
};
for stream in ["stdout", "stderr"] {
if let Ok(s) = sys.getattr(stream) {
let _ = s.call_method0("flush");
}
}
code
})
}
fn system_exit_code(py: Python, e: &PyErr) -> i32 {
match e.value(py).getattr("code") {
Ok(code) if code.is_none() => 0,
Ok(code) => {
if let Ok(n) = code.extract::<i32>() {
n
} else {
eprintln!("{}", code.str().map(|s| s.to_string()).unwrap_or_default());
1
}
}
Err(_) => 0,
}
}
thread_local! {
static STORES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
fn store(obj: Py<PyAny>) -> u32 {
STORES.with(|n| n.set(n.get() + 1));
let mut t = table().lock().expect("ffi table poisoned");
t.push(obj);
(t.len() - 1) as u32
}
pub fn table_len() -> usize {
table().lock().map(|t| t.len()).unwrap_or(0)
}
pub fn table_stores_on_thread() -> usize {
STORES.with(|n| n.get())
}
fn fetch<'py>(py: Python<'py>, id: u32) -> Result<Bound<'py, PyAny>, String> {
let t = table().lock().expect("ffi table poisoned");
match t.get(id as usize) {
Some(obj) => Ok(obj.clone_ref(py).into_bound(py)),
None => Err(format!("ffi: invalid foreign handle {id}")),
}
}
fn pyerr_class_bases(py: Python, err: &PyErr) -> Option<(String, Vec<String>)> {
let ty = err.get_type(py);
let class = ty.name().ok()?.to_string();
let mut bases: Vec<String> = Vec::new();
if let Ok(mro) = ty.getattr("__mro__") {
if let Ok(seq) = mro.try_iter() {
for c in seq.flatten() {
if let Ok(n) = c.getattr("__name__").and_then(|n| n.extract::<String>()) {
bases.push(n);
}
}
}
}
(!bases.is_empty()).then_some((class, bases))
}
fn pyerr_to_error(py: Python, err: &PyErr) -> String {
if let Some((class, bases)) = pyerr_class_bases(py, err) {
with_host(|h| {
h.foreign_exc_bases.insert(class, bases);
});
}
let line = err.to_string();
with_host(|h| record_foreign_exc(h, py, err, &line));
line
}
fn record_foreign_exc(host: &mut PyHost, py: Python, err: &PyErr, line: &str) {
host.foreign_exc = None;
let value = err.value(py);
let Ok(args) = value.getattr("args").and_then(|a| a.try_iter()) else {
return;
};
let items: Vec<Bound<PyAny>> = args.flatten().collect();
let Ok(args) = items
.iter()
.map(|a| py_to_value(host, py, a))
.collect::<Result<Vec<Value>, String>>()
else {
return;
};
let mut attrs: Vec<(String, Value)> = Vec::new();
if let Some(d) = value
.getattr("__dict__")
.ok()
.and_then(|d| d.downcast_into::<PyDict>().ok())
{
for (k, v) in d.iter() {
let (Ok(name), Ok(val)) = (k.extract::<String>(), py_to_value(host, py, &v)) else {
continue;
};
attrs.push((name, val));
}
attrs.sort_by(|a, b| a.0.cmp(&b.0));
}
host.foreign_exc = Some(crate::host::ForeignExc {
line: line.to_string(),
args,
attrs,
});
}
fn pyerr_to_error_h(host: &mut PyHost, py: Python, err: &PyErr) -> String {
if let Some((class, bases)) = pyerr_class_bases(py, err) {
host.foreign_exc_bases.insert(class, bases);
}
let line = err.to_string();
record_foreign_exc(host, py, err, &line);
line
}
fn pending_search_paths() -> &'static Mutex<Vec<String>> {
static P: OnceLock<Mutex<Vec<String>>> = OnceLock::new();
P.get_or_init(|| Mutex::new(Vec::new()))
}
pub fn queue_search_paths(paths: Vec<String>) {
if let Ok(mut q) = pending_search_paths().lock() {
*q = paths;
}
}
fn apply_pending_search_paths() {
let paths = match pending_search_paths().lock() {
Ok(mut q) if !q.is_empty() => std::mem::take(&mut *q),
_ => return,
};
Python::with_gil(|py| {
let Ok(sys) = py.import("sys") else { return };
let Ok(path) = sys.getattr("path") else {
return;
};
let Ok(path) = path.downcast_into::<PyList>() else {
return;
};
let have: Vec<String> = path
.iter()
.filter_map(|e| e.extract::<String>().ok())
.collect();
for p in paths.iter().rev() {
if !have.iter().any(|h| h == p) {
let _ = path.insert(0, p.as_str());
}
}
});
}
fn pending_argv() -> &'static Mutex<Option<Vec<String>>> {
static A: OnceLock<Mutex<Option<Vec<String>>>> = OnceLock::new();
A.get_or_init(|| Mutex::new(None))
}
pub fn queue_argv(argv: Vec<String>) {
if let Ok(mut q) = pending_argv().lock() {
*q = Some(argv);
}
}
fn apply_pending_argv() {
let argv = match pending_argv().lock() {
Ok(mut q) => match q.take() {
Some(a) => a,
None => return,
},
Err(_) => return,
};
Python::with_gil(|py| {
let Ok(sys) = py.import("sys") else { return };
let list = PyList::empty(py);
for a in &argv {
let _ = list.append(a.as_str());
}
let _ = sys.setattr("argv", list);
});
}
pub fn flush_open_files() {
if !INTERPRETER_STARTED.load(std::sync::atomic::Ordering::Relaxed) {
return;
}
Python::with_gil(|py| {
if let (Ok(gc), Ok(io_mod)) = (py.import("gc"), py.import("io")) {
if let (Ok(objects), Ok(base)) =
(gc.call_method0("get_objects"), io_mod.getattr("IOBase"))
{
if let Ok(objects) = objects.downcast_into::<PyList>() {
for obj in objects.iter() {
let Ok(true) = obj.is_instance(&base) else {
continue;
};
if !matches!(
obj.getattr("closed").and_then(|c| c.extract::<bool>()),
Ok(false)
) {
continue;
}
if !matches!(
obj.call_method0("writable")
.and_then(|w| w.extract::<bool>()),
Ok(true)
) {
continue;
}
let _ = obj.call_method0("flush");
}
}
}
}
if let Ok(sys) = py.import("sys") {
for stream in ["stdout", "stderr"] {
if let Ok(s) = sys.getattr(stream) {
let _ = s.call_method0("flush");
}
}
}
});
}
pub fn import(name: &str) -> Result<u32, String> {
if INTERPRETER_STARTED.load(std::sync::atomic::Ordering::Relaxed) {
apply_pending_argv();
}
if let Some(id) = module_handles()
.lock()
.ok()
.and_then(|m| m.get(name).copied())
{
return Ok(id);
}
if !init() {
return Err(bridge_unavailable(name));
}
apply_pending_search_paths();
apply_pending_argv();
Python::with_gil(|py| match py.import(name) {
Ok(module) => {
let id = store(module.into_any().unbind());
if let Ok(mut m) = module_handles().lock() {
m.insert(name.to_string(), id);
}
Ok(id)
}
Err(e) => Err(e.to_string()),
})
}
pub fn same_object(a: u32, b: u32) -> bool {
Python::with_gil(|py| match (fetch(py, a), fetch(py, b)) {
(Ok(x), Ok(y)) => x.is(&y),
_ => false,
})
}
pub fn foreign_eq(a: u32, b: u32) -> bool {
Python::with_gil(|py| match (fetch(py, a), fetch(py, b)) {
(Ok(x), Ok(y)) => x.eq(&y).unwrap_or(false),
_ => false,
})
}
pub enum Prim<'a> {
Int(i64),
Float(f64),
Str(&'a str),
}
pub fn foreign_eq_prim(fid: u32, prim: Prim) -> bool {
Python::with_gil(|py| {
let Ok(x) = fetch(py, fid) else { return false };
let other: Bound<PyAny> = match prim {
Prim::Int(n) => match n.into_pyobject(py) {
Ok(o) => o.into_any(),
Err(_) => return false,
},
Prim::Float(f) => match f.into_pyobject(py) {
Ok(o) => o.into_any(),
Err(_) => return false,
},
Prim::Str(s) => match s.into_pyobject(py) {
Ok(o) => o.into_any(),
Err(_) => return false,
},
};
x.eq(&other).unwrap_or(false)
})
}
#[cfg(feature = "stdlib-ffi")]
pub fn foreign_numeric_key(fid: u32) -> Option<Value> {
Python::with_gil(|py| {
let obj = fetch(py, fid).ok()?;
let builtins = py.import("builtins").ok()?;
if let Ok(as_int) = builtins.getattr("int").ok()?.call1((&obj,)) {
if obj.eq(&as_int).unwrap_or(false) {
return with_host(|h| py_to_value(h, py, &as_int)).ok();
}
}
if let Ok(as_float) = builtins.getattr("float").ok()?.call1((&obj,)) {
if obj.eq(&as_float).unwrap_or(false) {
return as_float.extract::<f64>().ok().map(Value::Float);
}
}
None
})
}
pub fn foreign_cmp(a: u32, b: u32) -> Result<std::cmp::Ordering, String> {
Python::with_gil(|py| match (fetch(py, a), fetch(py, b)) {
(Ok(x), Ok(y)) => x.compare(&y).map_err(|e| e.to_string()),
_ => Err("ffi: invalid foreign handle".into()),
})
}
pub fn foreign_hash(id: u32) -> Result<i64, String> {
Python::with_gil(|py| match fetch(py, id) {
Ok(x) => x.hash().map(|h| h as i64).map_err(|e| e.to_string()),
Err(e) => Err(e),
})
}
pub fn build_foreign_class(
name: &str,
bases: &[Value],
members: &[(String, Value)],
) -> Result<Value, String> {
if !init() {
return Err(bridge_unavailable(name));
}
Python::with_gil(|py| {
let (bases_tuple, members_dict): (Bound<PyAny>, Bound<PyAny>) =
with_host(|h| -> Result<_, String> {
let base_objs: Vec<Bound<PyAny>> = bases
.iter()
.map(|b| value_to_py(h, py, b))
.collect::<Result<_, _>>()?;
let bases_tuple = PyTuple::new(py, &base_objs).map_err(|e| e.to_string())?;
let members_dict = PyDict::new(py);
for (k, v) in members {
let pv = value_to_py(h, py, v)?;
members_dict
.set_item(k.as_str(), pv)
.map_err(|e| e.to_string())?;
}
Ok((bases_tuple.into_any(), members_dict.into_any()))
})?;
let helper = make_class_helper(py)?;
let cls = helper
.call1((name, bases_tuple, members_dict))
.map_err(|e| e.to_string())?;
let id = store(cls.unbind());
Ok(with_host(|h| h.alloc(PyObj::Foreign(id))))
})
}
fn make_class_helper(py: Python) -> Result<Bound<PyAny>, String> {
static MAKE_CLASS: OnceLock<Py<PyAny>> = OnceLock::new();
if let Some(f) = MAKE_CLASS.get() {
return Ok(f.bind(py).clone());
}
let code = cr#"
import types
def _make(name, bases, members):
def body(ns):
for k in members:
ns[k] = members[k]
return types.new_class(name, tuple(bases), {}, body)
"#;
let module = PyModule::from_code(py, code, c"_pyrs_class.py", c"_pyrs_class")
.map_err(|e| e.to_string())?;
let f = module.getattr("_make").map_err(|e| e.to_string())?;
let _ = MAKE_CLASS.set(f.clone().unbind());
Ok(f)
}
fn value_to_py<'py>(
host: &PyHost,
py: Python<'py>,
v: &Value,
) -> Result<Bound<'py, PyAny>, String> {
let conv = |b: Result<Bound<'py, PyAny>, PyErr>| b.map_err(|e| e.to_string());
match v {
Value::Undef => Ok(py.None().into_bound(py)),
Value::Bool(b) => conv(b.into_bound_py_any(py)),
Value::Int(n) => conv(n.into_bound_py_any(py)),
Value::Float(f) => conv(f.into_bound_py_any(py)),
Value::Str(s) => conv(s.as_str().into_bound_py_any(py)),
Value::Obj(_) => match host.get(v) {
Some(PyObj::Str(s)) => conv(s.as_str().into_bound_py_any(py)),
Some(PyObj::Bytes(b)) => Ok(PyBytes::new(py, b).into_any()),
Some(PyObj::Bytearray(b)) => Ok(PyByteArray::new(py, b).into_any()),
Some(PyObj::BigInt(b)) => {
let int_ctor = py
.import("builtins")
.and_then(|m| m.getattr("int"))
.map_err(|e| e.to_string())?;
int_ctor
.call1((b.to_str_radix(16), 16))
.map_err(|e| e.to_string())
}
Some(PyObj::List(items)) => {
let elems = marshal_seq(host, py, items)?;
Ok(PyList::new(py, elems)
.map_err(|e| e.to_string())?
.into_any())
}
Some(PyObj::Tuple(items)) => {
let elems = marshal_seq(host, py, items)?;
Ok(PyTuple::new(py, elems)
.map_err(|e| e.to_string())?
.into_any())
}
Some(PyObj::Set(s)) => {
let elems = marshal_seq(host, py, &s.values().cloned().collect::<Vec<_>>())?;
Ok(PySet::new(py, &elems)
.map_err(|e| e.to_string())?
.into_any())
}
Some(PyObj::Frozenset(s)) => {
let elems = marshal_seq(host, py, &s.values().cloned().collect::<Vec<_>>())?;
Ok(PyFrozenSet::new(py, &elems)
.map_err(|e| e.to_string())?
.into_any())
}
Some(PyObj::Range { start, stop, step }) => {
let range = py
.import("builtins")
.and_then(|m| m.getattr("range"))
.map_err(|e| e.to_string())?;
range
.call1((*start, *stop, *step))
.map_err(|e| e.to_string())
}
Some(PyObj::Complex(re, im)) => {
let cplx = py
.import("builtins")
.and_then(|m| m.getattr("complex"))
.map_err(|e| e.to_string())?;
cplx.call1((*re, *im)).map_err(|e| e.to_string())
}
Some(PyObj::Deque { items, maxlen }) => {
let elems = marshal_seq(host, py, &items.iter().cloned().collect::<Vec<_>>())?;
let pylist = PyList::new(py, elems).map_err(|e| e.to_string())?;
let deque = py
.import("collections")
.and_then(|m| m.getattr("deque"))
.map_err(|e| e.to_string())?;
match maxlen {
Some(n) => deque.call1((pylist, *n)),
None => deque.call1((pylist,)),
}
.map_err(|e| e.to_string())
}
Some(PyObj::Dict(d)) => {
let dict = PyDict::new(py);
for (k, val) in d.values() {
let pk = value_to_py(host, py, k)?;
let pv = value_to_py(host, py, val)?;
dict.set_item(pk, pv).map_err(|e| e.to_string())?;
}
Ok(dict.into_any())
}
Some(PyObj::Ellipsis) => Ok(py.Ellipsis().into_bound(py)),
Some(PyObj::Foreign(id)) => fetch(py, *id),
Some(
PyObj::Generator { .. }
| PyObj::Iter(_)
| PyObj::Zip { .. }
| PyObj::MapObj { .. }
| PyObj::FilterObj { .. }
| PyObj::EnumerateObj { .. }
| PyObj::CallIter { .. },
) => {
let it = PyrsIterator { target: v.clone() };
Py::new(py, it)
.map(|p| p.into_any().into_bound(py))
.map_err(|e| e.to_string())
}
Some(PyObj::Builtin(name))
if !name.contains('.')
&& py
.import("builtins")
.and_then(|m| m.getattr(name.as_str()))
.is_ok() =>
{
py.import("builtins")
.and_then(|m| m.getattr(name.as_str()))
.map_err(|e| e.to_string())
}
Some(
PyObj::Func(_)
| PyObj::Builtin(_)
| PyObj::BoundMethod { .. }
| PyObj::Partial { .. }
| PyObj::LruCache { .. }
| PyObj::StaticMethod(_)
| PyObj::ClassMethod(_),
) => {
let cb = PyrsCallable {
target: v.clone(),
doc: None,
module: None,
};
Py::new(py, cb)
.map(|p| p.into_any().into_bound(py))
.map_err(|e| e.to_string())
}
Some(PyObj::Class(cname)) => {
let members: Vec<(String, Value)> = host
.classes
.get(cname)
.map(|c| {
c.ns.iter()
.map(|(k, val)| (k.clone(), val.clone()))
.collect()
})
.unwrap_or_default();
let ns_dict = PyDict::new(py);
for (k, val) in &members {
let pv = value_to_py(host, py, val)?;
ns_dict
.set_item(k.as_str(), pv)
.map_err(|e| e.to_string())?;
}
if !ns_dict.contains("__module__").unwrap_or(false) {
let _ = ns_dict.set_item("__module__", "__main__");
}
let _ = ns_dict.set_item("__qualname__", cname.as_str());
let obj_base = py
.import("builtins")
.and_then(|m| m.getattr("object"))
.map_err(|e| e.to_string())?;
let bases = PyTuple::new(py, &[obj_base]).map_err(|e| e.to_string())?;
let helper = make_class_helper(py)?;
helper
.call1((cname.as_str(), bases, ns_dict))
.map_err(|e| e.to_string())
}
Some(PyObj::Exception { class, args }) => {
let ctor = py
.import("builtins")
.and_then(|m| m.getattr(class.as_str()))
.map_err(|e| e.to_string())?;
let pargs = marshal_seq(host, py, args)?;
let tup = PyTuple::new(py, pargs).map_err(|e| e.to_string())?;
ctor.call1(tup).map_err(|e| e.to_string())
}
Some(PyObj::File { .. }) => {
let proxy = PyrsFile { target: v.clone() };
Py::new(py, proxy)
.map(|p| p.into_any().into_bound(py))
.map_err(|e| e.to_string())
}
Some(PyObj::Instance(i)) => {
let proxy = PyrsInstance { target: v.clone() };
if crate::builtins::instance_has(host, i, "__index__") {
return Py::new(py, (PyrsIndexInstance, proxy))
.map(|p| p.into_any().into_bound(py))
.map_err(|e| e.to_string());
}
Py::new(py, proxy)
.map(|p| p.into_any().into_bound(py))
.map_err(|e| e.to_string())
}
Some(PyObj::Slice { lo, hi, step }) => {
let (lo, hi, step) = (lo.clone(), hi.clone(), step.clone());
let bound = |b: &Value| match b {
Value::Undef => Ok(py.None().into_bound(py)),
other => value_to_py(host, py, other),
};
let a = bound(&lo)?;
let b = bound(&hi)?;
let c = bound(&step)?;
py.import("builtins")
.and_then(|m| m.getattr("slice"))
.and_then(|f| f.call1((a, b, c)))
.map_err(|e| e.to_string())
}
_ => Err(crate::host::type_error(&format!(
"cannot pass '{}' to a CPython stdlib call",
host.type_name(v)
))),
},
_ => Err(crate::host::type_error(
"unsupported value for CPython call",
)),
}
}
fn marshal_seq<'py>(
host: &PyHost,
py: Python<'py>,
items: &[Value],
) -> Result<Vec<Bound<'py, PyAny>>, String> {
items.iter().map(|it| value_to_py(host, py, it)).collect()
}
fn big_from_py(obj: &Bound<PyAny>) -> Result<num_bigint::BigInt, String> {
let hex = obj
.call_method1("__format__", ("x",))
.and_then(|s| s.extract::<String>())
.map_err(|e| e.to_string())?;
let (neg, digits) = match hex.strip_prefix('-') {
Some(d) => (true, d),
None => (false, hex.as_str()),
};
let b = num_bigint::BigInt::parse_bytes(digits.as_bytes(), 16)
.ok_or_else(|| format!("ffi: cannot marshal int 0x{hex}"))?;
Ok(if neg { -b } else { b })
}
fn py_to_value(host: &mut PyHost, py: Python, obj: &Bound<PyAny>) -> Result<Value, String> {
if obj.is_none() {
return Ok(Value::Undef);
}
if obj.is(&py.Ellipsis()) {
return Ok(host.alloc(PyObj::Ellipsis));
}
if obj.is_exact_instance_of::<PyBool>() {
return Ok(Value::Bool(
obj.extract::<bool>().map_err(|e| e.to_string())?,
));
}
if obj.is_exact_instance_of::<PyInt>() {
return Ok(match obj.extract::<i64>() {
Ok(n) => Value::Int(n),
Err(_) => host.alloc(PyObj::BigInt(big_from_py(obj)?)),
});
}
if obj.is_exact_instance_of::<PyFloat>() {
return Ok(Value::Float(
obj.extract::<f64>().map_err(|e| e.to_string())?,
));
}
if obj.is_exact_instance_of::<pyo3::types::PyString>() {
return Ok(host.new_str(obj.extract::<String>().map_err(|e| e.to_string())?));
}
if obj.is_exact_instance_of::<PyBytes>() {
let b = obj.downcast::<PyBytes>().map_err(|e| e.to_string())?;
return Ok(host.alloc(PyObj::Bytes(b.as_bytes().to_vec())));
}
if obj.is_exact_instance_of::<PyList>() {
let list = obj.downcast::<PyList>().map_err(|e| e.to_string())?;
let items = unmarshal_seq(host, py, list.iter())?;
return Ok(host.new_list(items));
}
if obj.is_exact_instance_of::<PyTuple>() {
let tup = obj.downcast::<PyTuple>().map_err(|e| e.to_string())?;
let items = unmarshal_seq(host, py, tup.iter())?;
return Ok(host.new_tuple(items));
}
if obj.is_exact_instance_of::<PyDict>() {
let dict = obj.downcast::<PyDict>().map_err(|e| e.to_string())?;
let mut map = indexmap::IndexMap::new();
for (k, v) in dict.iter() {
let kv = py_to_value(host, py, &k)?;
let vv = py_to_value(host, py, &v)?;
let key = host.to_key(&kv)?;
map.insert(key, (kv, vv));
}
return Ok(host.new_dict(map));
}
if obj.is_exact_instance_of::<PySet>() || obj.is_exact_instance_of::<PyFrozenSet>() {
let mut map = indexmap::IndexMap::new();
for it in obj.try_iter().map_err(|e| e.to_string())? {
let iv = py_to_value(host, py, &it.map_err(|e| e.to_string())?)?;
let key = host.to_key(&iv)?;
map.insert(key, iv);
}
return Ok(host.new_set(map));
}
if let Some(v) = unwrap_proxy(obj) {
return Ok(v);
}
Ok(host.alloc(PyObj::Foreign(store(obj.clone().unbind()))))
}
fn unwrap_proxy(obj: &Bound<PyAny>) -> Option<Value> {
if let Ok(c) = obj.downcast::<PyrsCallable>() {
return Some(c.borrow().target.clone());
}
if let Ok(c) = obj.downcast::<PyrsIterator>() {
return Some(c.borrow().target.clone());
}
if let Ok(c) = obj.downcast::<PyrsInstance>() {
return Some(c.borrow().target.clone());
}
if let Ok(c) = obj.downcast::<PyrsFile>() {
return Some(c.borrow().target.clone());
}
None
}
fn unmarshal_seq<'py, I>(host: &mut PyHost, py: Python, items: I) -> Result<Vec<Value>, String>
where
I: Iterator<Item = Bound<'py, PyAny>>,
{
items.map(|it| py_to_value(host, py, &it)).collect()
}
fn reference_to_value(host: &mut PyHost, py: Python, obj: &Bound<PyAny>) -> Result<Value, String> {
if obj.is_exact_instance_of::<PyList>()
|| obj.is_exact_instance_of::<PyDict>()
|| obj.is_exact_instance_of::<PySet>()
{
return Ok(host.alloc(PyObj::Foreign(store(obj.clone().unbind()))));
}
py_to_value(host, py, obj)
}
pub fn get_attr(host: &mut PyHost, id: u32, name: &str) -> Result<Value, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let attr = obj
.getattr(name)
.map_err(|e| pyerr_to_error_h(host, py, &e))?;
reference_to_value(host, py, &attr)
})
}
pub fn dir_names(id: u32) -> Vec<String> {
Python::with_gil(|py| {
let Ok(obj) = fetch(py, id) else {
return Vec::new();
};
obj.dir()
.ok()
.map(|names| {
names
.iter()
.filter_map(|n| n.extract::<String>().ok())
.collect()
})
.unwrap_or_default()
})
}
pub fn set_attr(host: &mut PyHost, id: u32, name: &str, value: &Value) -> Result<(), String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let v = value_to_py(host, py, value)?;
obj.setattr(name, v)
.map_err(|e| pyerr_to_error_h(host, py, &e))
})
}
pub fn call(id: u32, args: Vec<Value>, kwargs: Vec<(String, Value)>) -> Result<Value, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
invoke_bound(py, &obj, &args, &kwargs)
})
}
pub fn call_method(
id: u32,
name: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let method = obj.getattr(name).map_err(|e| pyerr_to_error(py, &e))?;
invoke_bound(py, &method, &args, &kwargs)
})
}
fn invoke_bound(
py: Python,
callable: &Bound<PyAny>,
args: &[Value],
kwargs: &[(String, Value)],
) -> Result<Value, String> {
let (arg_tuple, kw) = with_host(|h| build_call_args(h, py, args, kwargs))?;
let result = callable
.call(&arg_tuple, kw.as_ref())
.map_err(|e| pyerr_to_error(py, &e))?;
with_host(|h| {
writeback_mutated_args(h, py, args, &arg_tuple);
py_to_value(h, py, &result)
})
}
#[derive(Clone, Copy)]
enum MutKind {
List,
Bytearray,
Deque(Option<usize>),
}
fn writeback_mutated_args(
host: &mut PyHost,
py: Python,
args: &[Value],
arg_tuple: &Bound<PyTuple>,
) {
for (i, orig) in args.iter().enumerate() {
let kind = match host.get(orig) {
Some(PyObj::List(_)) => MutKind::List,
Some(PyObj::Bytearray(_)) => MutKind::Bytearray,
Some(PyObj::Deque { maxlen, .. }) => MutKind::Deque(*maxlen),
_ => continue,
};
let Ok(cpy) = arg_tuple.get_item(i) else {
continue;
};
if let Some(obj) = rebuild_mutable(host, py, &cpy, kind) {
if let Some(slot) = host.get_mut(orig) {
*slot = obj;
}
}
}
}
fn rebuild_mutable(
host: &mut PyHost,
py: Python,
cpy: &Bound<PyAny>,
kind: MutKind,
) -> Option<PyObj> {
match kind {
MutKind::Bytearray => {
let ba = cpy.downcast::<PyByteArray>().ok()?;
Some(PyObj::Bytearray(ba.to_vec()))
}
MutKind::List => {
let items = pure_seq(host, py, cpy)?;
Some(PyObj::List(items))
}
MutKind::Deque(maxlen) => {
let items = pure_seq(host, py, cpy)?;
Some(PyObj::Deque {
items: items.into_iter().collect(),
maxlen,
})
}
}
}
fn pure_seq(host: &mut PyHost, py: Python, cpy: &Bound<PyAny>) -> Option<Vec<Value>> {
let it = cpy.try_iter().ok()?;
let mut out = Vec::new();
for item in it {
out.push(pure_value(host, py, &item.ok()?)?);
}
Some(out)
}
fn pure_value(host: &mut PyHost, py: Python, obj: &Bound<PyAny>) -> Option<Value> {
if obj.is_none() {
return Some(Value::Undef);
}
if obj.is_exact_instance_of::<PyBool>() {
return obj.extract::<bool>().ok().map(Value::Bool);
}
if obj.is_exact_instance_of::<PyInt>() {
return match obj.extract::<i64>() {
Ok(n) => Some(Value::Int(n)),
Err(_) => big_from_py(obj).ok().map(|b| host.alloc(PyObj::BigInt(b))),
};
}
if obj.is_exact_instance_of::<PyFloat>() {
return obj.extract::<f64>().ok().map(Value::Float);
}
if obj.is_exact_instance_of::<PyString>() {
return obj.extract::<String>().ok().map(|s| host.new_str(s));
}
if obj.is_exact_instance_of::<PyBytes>() {
let b = obj.downcast::<PyBytes>().ok()?;
return Some(host.alloc(PyObj::Bytes(b.as_bytes().to_vec())));
}
if obj.is_exact_instance_of::<PyByteArray>() {
let b = obj.downcast::<PyByteArray>().ok()?;
return Some(host.alloc(PyObj::Bytearray(b.to_vec())));
}
if obj.is_exact_instance_of::<PyList>() {
let items = pure_seq(host, py, obj)?;
return Some(host.new_list(items));
}
if obj.is_exact_instance_of::<PyTuple>() {
let items = pure_seq(host, py, obj)?;
return Some(host.new_tuple(items));
}
None
}
#[allow(clippy::type_complexity)]
fn build_call_args<'py>(
host: &PyHost,
py: Python<'py>,
args: &[Value],
kwargs: &[(String, Value)],
) -> Result<(Bound<'py, PyTuple>, Option<Bound<'py, PyDict>>), String> {
let py_args = marshal_seq(host, py, args)?;
let arg_tuple = PyTuple::new(py, py_args).map_err(|e| e.to_string())?;
let kw = if kwargs.is_empty() {
None
} else {
let d = PyDict::new(py);
for (k, v) in kwargs {
let pv = value_to_py(host, py, v)?;
d.set_item(k.as_str(), pv).map_err(|e| e.to_string())?;
}
Some(d)
};
Ok((arg_tuple, kw))
}
#[pyclass(dict)]
struct PyrsCallable {
target: Value,
doc: Option<Py<PyAny>>,
module: Option<Py<PyAny>>,
}
#[pymethods]
impl PyrsCallable {
#[getter(__doc__)]
fn get_doc(&self, py: Python) -> PyResult<Py<PyAny>> {
self.shadowed_dunder(py, self.doc.as_ref(), "__doc__")
}
#[setter(__doc__)]
fn set_doc(&mut self, value: Py<PyAny>) {
self.doc = Some(value);
}
#[getter(__module__)]
fn get_module(&self, py: Python) -> PyResult<Py<PyAny>> {
self.shadowed_dunder(py, self.module.as_ref(), "__module__")
}
#[setter(__module__)]
fn set_module(&mut self, value: Py<PyAny>) {
self.module = Some(value);
}
fn __getattr__(&self, py: Python, name: String) -> PyResult<Py<PyAny>> {
match with_host(|h| h.get_attr(&self.target, &name)) {
Ok(v) => with_host(|h| value_to_py(h, py, &v))
.map(|b| b.unbind())
.map_err(pyo3::exceptions::PyRuntimeError::new_err),
Err(e) => Err(pyo3::exceptions::PyAttributeError::new_err(e)),
}
}
fn __setattr__(&self, py: Python, name: String, value: Bound<PyAny>) -> PyResult<()> {
let v = with_host(|h| py_to_value(h, py, &value))
.map_err(pyo3::exceptions::PyRuntimeError::new_err)?;
with_host(|h| h.set_attr(&self.target, &name, v))
.map_err(pyo3::exceptions::PyAttributeError::new_err)
}
fn __get__<'py>(
slf: Bound<'py, Self>,
py: Python<'py>,
obj: Option<Bound<'py, PyAny>>,
_owner: Option<Bound<'py, PyAny>>,
) -> PyResult<Bound<'py, PyAny>> {
match obj {
Some(instance) if !instance.is_none() => py
.import("types")?
.getattr("MethodType")?
.call1((slf, instance)),
_ => Ok(slf.into_any()),
}
}
#[pyo3(signature = (*args, **kwargs))]
fn __call__(
&self,
py: Python,
args: &Bound<PyTuple>,
kwargs: Option<&Bound<PyDict>>,
) -> PyResult<Py<PyAny>> {
let to_pyerr = |e: String| pyo3::exceptions::PyRuntimeError::new_err(e);
let rs_args: Vec<Value> = with_host(|h| {
args.iter()
.map(|a| py_to_value(h, py, &a))
.collect::<Result<_, _>>()
})
.map_err(to_pyerr)?;
let rs_kwargs: Vec<(String, Value)> = match kwargs {
None => Vec::new(),
Some(d) => with_host(|h| {
d.iter()
.map(|(k, v)| {
let key = k.str().map_err(|e| e.to_string())?.to_string();
Ok((key, py_to_value(h, py, &v)?))
})
.collect::<Result<_, String>>()
})
.map_err(to_pyerr)?,
};
let result = crate::host::invoke(&self.target, rs_args, rs_kwargs).map_err(call_err)?;
with_host(|h| value_to_py(h, py, &result))
.map(|b| b.unbind())
.map_err(to_pyerr)
}
}
impl PyrsCallable {
fn shadowed_dunder(
&self,
py: Python,
override_value: Option<&Py<PyAny>>,
name: &str,
) -> PyResult<Py<PyAny>> {
if let Some(v) = override_value {
return Ok(v.clone_ref(py));
}
match with_host(|h| h.get_attr(&self.target, name)) {
Ok(v) => with_host(|h| value_to_py(h, py, &v))
.map(|b| b.unbind())
.map_err(pyo3::exceptions::PyRuntimeError::new_err),
Err(_) => Ok(py.None()),
}
}
}
#[pyclass]
struct PyrsIterator {
target: Value,
}
#[pymethods]
impl PyrsIterator {
fn __iter__(slf: PyRef<Self>) -> PyRef<Self> {
slf
}
fn __next__(&self, py: Python) -> PyResult<Option<Py<PyAny>>> {
match crate::host::iter_step(&self.target).map_err(rs_err_typed)? {
None => Ok(None),
Some(v) => with_host(|h| value_to_py(h, py, &v))
.map(|b| Some(b.unbind()))
.map_err(rs_err),
}
}
#[pyo3(signature = (value = None))]
fn send(&self, py: Python, value: Option<Bound<'_, PyAny>>) -> PyResult<Py<PyAny>> {
self.require_generator("send")?;
let sent = match value {
None => Value::Undef,
Some(v) => with_host(|h| py_to_value(h, py, &v)).map_err(rs_err)?,
};
if !crate::host::gen_started(&self.target) && !matches!(sent, Value::Undef) {
return Err(pyo3::exceptions::PyTypeError::new_err(
"can't send non-None value to a just-started generator",
));
}
self.finish_step(py, crate::host::gen_resume(&self.target, sent))
}
#[pyo3(signature = (*args))]
fn throw(&self, py: Python, args: &Bound<'_, PyTuple>) -> PyResult<Py<PyAny>> {
self.require_generator("throw")?;
let exc = normalize_throw_args(py, args)?;
let exc_v = pyexc_to_value(py, &exc)?;
self.finish_step(py, crate::host::gen_throw(&self.target, exc_v))
}
fn close(&self) -> PyResult<()> {
self.require_generator("close")?;
if with_host(|h| h.close_unstarted_gen(&self.target)) {
return Ok(());
}
let ge = with_host(|h| {
h.alloc(PyObj::Exception {
class: "GeneratorExit".into(),
args: vec![],
})
});
match crate::host::gen_throw(&self.target, ge) {
Ok(Some(_)) => {
clear_host_error();
Err(pyo3::exceptions::PyRuntimeError::new_err(
"generator ignored GeneratorExit",
))
}
Ok(None) => Ok(()),
Err(e) if e.contains("GeneratorExit") || e.contains("StopIteration") => {
clear_host_error();
Ok(())
}
Err(e) => Err(self.body_err(e)),
}
}
}
impl PyrsIterator {
fn body_err(&self, e: String) -> pyo3::PyErr {
let err = crate::host::gen_pending_exc(&self.target)
.as_ref()
.and_then(exc_value_to_pyerr)
.unwrap_or_else(|| rs_err_typed(e));
clear_host_error();
err
}
fn require_generator(&self, name: &str) -> PyResult<()> {
if with_host(|h| matches!(h.get(&self.target), Some(PyObj::Generator { .. }))) {
return Ok(());
}
Err(pyo3::exceptions::PyAttributeError::new_err(format!(
"'{}' object has no attribute '{name}'",
with_host(|h| h.type_name(&self.target))
)))
}
fn finish_step(&self, py: Python, step: Result<Option<Value>, String>) -> PyResult<Py<PyAny>> {
match step {
Ok(Some(v)) => with_host(|h| value_to_py(h, py, &v))
.map(|b| b.unbind())
.map_err(rs_err),
Ok(None) => {
let ret = crate::host::coro_return_value(&self.target);
let arg = match ret {
Value::Undef => None,
v => Some(with_host(|h| value_to_py(h, py, &v)).map_err(rs_err)?),
};
Err(match arg {
Some(a) => pyo3::exceptions::PyStopIteration::new_err((a.unbind(),)),
None => pyo3::exceptions::PyStopIteration::new_err(()),
})
}
Err(e) => Err(self.body_err(e)),
}
}
}
fn normalize_throw_args<'py>(
py: Python<'py>,
args: &Bound<'py, PyTuple>,
) -> PyResult<Bound<'py, PyAny>> {
let first = args.get_item(0).map_err(|_| {
pyo3::exceptions::PyTypeError::new_err("throw() takes at least 1 argument (0 given)")
})?;
let base_exc = py.get_type::<pyo3::exceptions::PyBaseException>();
let is_exc_class = first
.downcast::<pyo3::types::PyType>()
.ok()
.map(|t| t.is_subclass(&base_exc).unwrap_or(false))
.unwrap_or(false);
if is_exc_class {
let second = args.get_item(1).ok().filter(|v| !v.is_none());
return match second {
Some(v) if v.is_instance(&first)? => Ok(v),
Some(v) if v.is_instance_of::<PyTuple>() => {
first.call1(v.downcast::<PyTuple>()?.clone())
}
Some(v) => first.call1((v,)),
None => first.call0(),
};
}
if !first.is_instance(&base_exc)? {
return Err(pyo3::exceptions::PyTypeError::new_err(
"exceptions must derive from BaseException",
));
}
Ok(first)
}
fn pyexc_to_value(py: Python, exc: &Bound<PyAny>) -> PyResult<Value> {
let ty = exc.get_type();
let class = ty
.name()
.map(|n| n.to_string())
.unwrap_or_else(|_| "Exception".into());
let mut bases: Vec<String> = Vec::new();
if let Ok(mro) = ty.getattr("__mro__") {
if let Ok(seq) = mro.try_iter() {
for c in seq.flatten() {
if let Ok(n) = c.getattr("__name__").and_then(|n| n.extract::<String>()) {
bases.push(n);
}
}
}
}
let args: Vec<Bound<PyAny>> = match exc.getattr("args").and_then(|a| a.try_iter()) {
Ok(it) => it.flatten().collect(),
Err(_) => Vec::new(),
};
with_host(|h| {
if !bases.is_empty() {
h.foreign_exc_bases.insert(class.clone(), bases);
}
let args = args
.iter()
.map(|a| py_to_value(h, py, a))
.collect::<Result<Vec<_>, _>>()?;
Ok::<Value, String>(h.alloc(PyObj::Exception { class, args }))
})
.map_err(rs_err)
}
fn exc_value_to_pyerr(v: &Value) -> Option<pyo3::PyErr> {
let (class, args) = with_host(|h| match h.get(v) {
Some(PyObj::Exception { class, args }) => Some((class.clone(), args.clone())),
_ => None,
})?;
Python::with_gil(|py| {
let ty = py
.import("builtins")
.and_then(|m| m.getattr(&*class))
.ok()?;
let is_exc = ty
.downcast::<pyo3::types::PyType>()
.ok()?
.is_subclass(&py.get_type::<pyo3::exceptions::PyBaseException>())
.ok()?;
if !is_exc {
return None;
}
let pargs = with_host(|h| marshal_seq(h, py, &args)).ok()?;
let tup = PyTuple::new(py, pargs).ok()?;
ty.call1(tup).ok().map(pyo3::PyErr::from_value)
})
}
fn call_err(e: String) -> pyo3::PyErr {
let pending = with_host(|h| h.exc.clone());
let class = pending.as_ref().and_then(|v| {
with_host(|h| match h.get(v) {
Some(PyObj::Exception { class, .. }) => Some(class.clone()),
_ => None,
})
});
let from_value = match class {
Some(c) if e == c || e.starts_with(&format!("{c}: ")) => {
pending.as_ref().and_then(exc_value_to_pyerr)
}
_ => None,
};
let err = from_value.unwrap_or_else(|| rs_err_typed(e));
clear_host_error();
err
}
fn clear_host_error() {
with_host(|h| {
h.error = None;
h.exc = None;
});
}
#[pyclass(subclass)]
struct PyrsInstance {
target: Value,
}
#[pymethods]
impl PyrsInstance {
fn __getattr__(&self, py: Python, name: String) -> PyResult<Py<PyAny>> {
match with_host(|h| h.get_attr(&self.target, &name)) {
Ok(v) => with_host(|h| value_to_py(h, py, &v))
.map(|b| b.unbind())
.map_err(pyo3::exceptions::PyRuntimeError::new_err),
Err(e) => Err(pyo3::exceptions::PyAttributeError::new_err(e)),
}
}
fn __getitem__(&self, py: Python, key: Bound<PyAny>) -> PyResult<Py<PyAny>> {
let to_pyerr = |e: String| pyo3::exceptions::PyRuntimeError::new_err(e);
let key_v = with_host(|h| py_to_value(h, py, &key)).map_err(to_pyerr)?;
let r = crate::host::call_method(&self.target, "__getitem__", vec![key_v], vec![])
.map_err(call_err)?;
with_host(|h| value_to_py(h, py, &r))
.map(|b| b.unbind())
.map_err(to_pyerr)
}
fn __richcmp__(
&self,
py: Python,
other: Bound<PyAny>,
op: pyo3::pyclass::CompareOp,
) -> PyResult<Py<PyAny>> {
use fusevm::NumOp;
let to_pyerr = |e: String| pyo3::exceptions::PyRuntimeError::new_err(e);
let other_v = with_host(|h| py_to_value(h, py, &other)).map_err(to_pyerr)?;
let numop = match op {
pyo3::pyclass::CompareOp::Lt => NumOp::Lt,
pyo3::pyclass::CompareOp::Le => NumOp::Le,
pyo3::pyclass::CompareOp::Eq => NumOp::Eq,
pyo3::pyclass::CompareOp::Ne => NumOp::Ne,
pyo3::pyclass::CompareOp::Gt => NumOp::Gt,
pyo3::pyclass::CompareOp::Ge => NumOp::Ge,
};
let r = crate::builtins::numeric_hook(numop, &self.target, &other_v).map_err(to_pyerr)?;
with_host(|h| value_to_py(h, py, &r))
.map(|b| b.unbind())
.map_err(to_pyerr)
}
fn __hash__(&self) -> PyResult<isize> {
with_host(|h| h.to_key(&self.target))
.map(|k| crate::builtins::hash_key(&k) as isize)
.map_err(pyo3::exceptions::PyTypeError::new_err)
}
fn __repr__(&self) -> PyResult<String> {
crate::builtins::py_repr(&self.target).map_err(pyo3::exceptions::PyRuntimeError::new_err)
}
fn __str__(&self) -> PyResult<String> {
crate::builtins::py_str(&self.target).map_err(pyo3::exceptions::PyRuntimeError::new_err)
}
}
#[pyclass(extends = PyrsInstance)]
struct PyrsIndexInstance;
#[pymethods]
impl PyrsIndexInstance {
fn __index__(slf: PyRef<Self>, py: Python) -> PyResult<Py<PyAny>> {
let target = slf.as_super().target.clone();
let r = crate::builtins::index_dunder(&target)
.map_err(call_err)?
.ok_or_else(|| {
let tn = with_host(|h| h.type_name(&target));
pyo3::exceptions::PyTypeError::new_err(format!(
"'{tn}' object cannot be interpreted as an integer"
))
})?;
with_host(|h| value_to_py(h, py, &r))
.map(|b| b.unbind())
.map_err(rs_err)
}
}
#[pyclass]
struct PyrsFile {
target: Value,
}
impl PyrsFile {
fn call(&self, py: Python, name: &str, args: Vec<Value>) -> PyResult<Py<PyAny>> {
let r = crate::host::call_method(&self.target, name, args, vec![]).map_err(call_err)?;
with_host(|h| value_to_py(h, py, &r))
.map(|b| b.unbind())
.map_err(rs_err)
}
}
#[pymethods]
impl PyrsFile {
#[pyo3(signature = (size=None))]
fn read(&self, py: Python, size: Option<i64>) -> PyResult<Py<PyAny>> {
self.call(py, "read", vec![size.map_or(Value::Undef, Value::Int)])
}
fn readline(&self, py: Python) -> PyResult<Py<PyAny>> {
self.call(py, "readline", vec![])
}
fn readlines(&self, py: Python) -> PyResult<Py<PyAny>> {
self.call(py, "readlines", vec![])
}
fn write(&self, py: Python, data: Bound<PyAny>) -> PyResult<Py<PyAny>> {
let v = with_host(|h| py_to_value(h, py, &data)).map_err(rs_err)?;
self.call(py, "write", vec![v])
}
fn writelines(&self, py: Python, lines: Bound<PyAny>) -> PyResult<Py<PyAny>> {
let v = with_host(|h| py_to_value(h, py, &lines)).map_err(rs_err)?;
self.call(py, "writelines", vec![v])
}
fn flush(&self, py: Python) -> PyResult<Py<PyAny>> {
self.call(py, "flush", vec![])
}
fn close(&self, py: Python) -> PyResult<Py<PyAny>> {
self.call(py, "close", vec![])
}
fn tell(&self, py: Python) -> PyResult<Py<PyAny>> {
self.call(py, "tell", vec![])
}
#[pyo3(signature = (offset, whence=0))]
fn seek(&self, py: Python, offset: i64, whence: i64) -> PyResult<Py<PyAny>> {
self.call(py, "seek", vec![Value::Int(offset), Value::Int(whence)])
}
fn fileno(&self, py: Python) -> PyResult<Py<PyAny>> {
self.call(py, "fileno", vec![])
}
fn readable(&self, py: Python) -> PyResult<Py<PyAny>> {
self.call(py, "readable", vec![])
}
fn writable(&self, py: Python) -> PyResult<Py<PyAny>> {
self.call(py, "writable", vec![])
}
fn seekable(&self, py: Python) -> PyResult<Py<PyAny>> {
self.call(py, "seekable", vec![])
}
fn isatty(&self, py: Python) -> PyResult<Py<PyAny>> {
self.call(py, "isatty", vec![])
}
#[getter]
fn name(&self, py: Python) -> PyResult<Py<PyAny>> {
self.attr(py, "name")
}
#[getter]
fn mode(&self, py: Python) -> PyResult<Py<PyAny>> {
self.attr(py, "mode")
}
#[getter]
fn closed(&self, py: Python) -> PyResult<Py<PyAny>> {
self.attr(py, "closed")
}
#[getter]
fn encoding(&self, py: Python) -> PyResult<Py<PyAny>> {
self.attr(py, "encoding")
}
#[getter]
fn newlines(&self, py: Python) -> PyResult<Py<PyAny>> {
self.attr(py, "newlines")
}
fn __iter__(slf: PyRef<Self>) -> PyRef<Self> {
slf
}
fn __next__(&self, py: Python) -> PyResult<Option<Py<PyAny>>> {
let line =
crate::host::call_method(&self.target, "readline", vec![], vec![]).map_err(rs_err)?;
let text = with_host(|h| h.str_of(&line));
if text.is_empty() {
return Ok(None);
}
with_host(|h| value_to_py(h, py, &line))
.map(|b| Some(b.unbind()))
.map_err(rs_err)
}
fn __enter__(slf: Py<Self>) -> Py<Self> {
slf
}
#[pyo3(signature = (*_args))]
fn __exit__(&self, py: Python, _args: &Bound<PyTuple>) -> PyResult<bool> {
self.call(py, "close", vec![])?;
Ok(false)
}
fn __repr__(&self) -> PyResult<String> {
crate::builtins::py_repr(&self.target).map_err(rs_err)
}
}
impl PyrsFile {
fn attr(&self, py: Python, name: &str) -> PyResult<Py<PyAny>> {
let v = with_host(|h| h.get_attr(&self.target, name)).map_err(rs_err)?;
with_host(|h| value_to_py(h, py, &v))
.map(|b| b.unbind())
.map_err(rs_err)
}
}
fn rs_err(e: String) -> pyo3::PyErr {
pyo3::exceptions::PyRuntimeError::new_err(e)
}
fn rs_err_typed(e: String) -> pyo3::PyErr {
let (class, msg) = match e.split_once(": ") {
Some((c, m)) => (c, m),
None => (e.as_str(), ""),
};
if class.is_empty() || !class.chars().all(|c| c.is_alphanumeric() || c == '_') {
return rs_err(e);
}
Python::with_gil(|py| {
let Ok(ty) = py.import("builtins").and_then(|m| m.getattr(class)) else {
return rs_err(e.clone());
};
let is_exc = ty
.downcast::<pyo3::types::PyType>()
.ok()
.map(|t| {
t.is_subclass(&py.get_type::<pyo3::exceptions::PyBaseException>())
.unwrap_or(false)
})
.unwrap_or(false);
if !is_exc {
return rs_err(e.clone());
}
let built = if msg.is_empty() {
ty.call0()
} else {
ty.call1((msg,))
};
match built {
Ok(obj) => pyo3::PyErr::from_value(obj),
Err(_) => rs_err(e.clone()),
}
})
}
pub fn get_item(host: &mut PyHost, id: u32, idx: &Value) -> Result<Value, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let key = value_to_py(host, py, idx)?;
let item = obj
.get_item(key)
.map_err(|e| pyerr_to_error_h(host, py, &e))?;
reference_to_value(host, py, &item)
})
}
pub fn get_item_cb(id: u32, idx: &Value) -> Result<Value, String> {
Python::with_gil(|py| {
let key = with_host(|h| value_to_py(h, py, idx))?;
let obj = fetch(py, id)?;
let item = obj.get_item(key).map_err(|e| pyerr_to_error(py, &e))?;
with_host(|h| reference_to_value(h, py, &item))
})
}
pub fn set_item(host: &mut PyHost, id: u32, idx: &Value, val: &Value) -> Result<(), String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let key = value_to_py(host, py, idx)?;
let v = value_to_py(host, py, val)?;
obj.set_item(key, v)
.map_err(|e| pyerr_to_error_h(host, py, &e))
})
}
pub fn del_item(host: &mut PyHost, id: u32, idx: &Value) -> Result<(), String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let key = value_to_py(host, py, idx)?;
obj.del_item(key)
.map_err(|e| pyerr_to_error_h(host, py, &e))
})
}
pub fn make_iter(host: &mut PyHost, id: u32) -> Result<Value, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let it = obj.try_iter().map_err(|e| e.to_string())?;
Ok(host.alloc(PyObj::Foreign(store(it.into_any().unbind()))))
})
}
pub fn make_iter_cb(id: u32) -> Result<Value, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let it = obj.try_iter().map_err(|e| e.to_string())?;
let handle = it.into_any().unbind();
Ok(with_host(|h| h.alloc(PyObj::Foreign(store(handle)))))
})
}
pub fn is_iterator(id: u32) -> bool {
Python::with_gil(|py| fetch(py, id).is_ok_and(|obj| obj.hasattr("__next__").unwrap_or(false)))
}
pub fn iter_next(host: &mut PyHost, id: u32) -> Result<Option<Value>, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let mut it = obj.try_iter().map_err(|e| e.to_string())?;
match it.next() {
None => Ok(None),
Some(Ok(item)) => Ok(Some(py_to_value(host, py, &item)?)),
Some(Err(e)) => Err(e.to_string()),
}
})
}
pub fn iter_next_cb(id: u32) -> Result<Option<Value>, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let mut it = obj.try_iter().map_err(|e| e.to_string())?;
match it.next() {
None => Ok(None),
Some(Ok(item)) => Ok(Some(with_host(|h| py_to_value(h, py, &item))?)),
Some(Err(e)) => Err(e.to_string()),
}
})
}
pub fn contains(host: &mut PyHost, id: u32, item: &Value) -> Result<bool, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let needle = value_to_py(host, py, item)?;
obj.contains(needle).map_err(|e| e.to_string())
})
}
pub fn contains_cb(id: u32, item: &Value) -> Result<bool, String> {
Python::with_gil(|py| {
let needle = with_host(|h| value_to_py(h, py, item))?;
let obj = fetch(py, id)?;
obj.contains(needle).map_err(|e| e.to_string())
})
}
pub fn binary_op(host: &mut PyHost, func: &str, a: &Value, b: &Value) -> Result<Value, String> {
Python::with_gil(|py| {
let pa = value_to_py(host, py, a)?;
let pb = value_to_py(host, py, b)?;
let op = py
.import("operator")
.and_then(|m| m.getattr(func))
.map_err(|e| e.to_string())?;
let res = op.call1((pa, pb)).map_err(|e| e.to_string())?;
py_to_value(host, py, &res)
})
}
pub fn to_float(id: u32) -> Result<f64, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let f = py
.import("builtins")
.and_then(|b| b.getattr("float"))
.and_then(|f| f.call1((obj,)))
.map_err(|e| e.to_string())?;
f.extract::<f64>().map_err(|e| e.to_string())
})
}
pub fn isinstance_foreign(host: &mut PyHost, v: &Value, cls_id: u32) -> Result<bool, String> {
Python::with_gil(|py| {
let obj = value_to_py(host, py, v)?;
let cls = fetch(py, cls_id)?;
obj.is_instance(&cls).map_err(|e| e.to_string())
})
}
pub fn foreign_isinstance_of_builtin(fid: u32, type_name: &str) -> bool {
Python::with_gil(|py| {
let (Ok(obj), Ok(builtins)) = (fetch(py, fid), py.import("builtins")) else {
return false;
};
match builtins.getattr(type_name) {
Ok(ty) => obj.is_instance(&ty).unwrap_or(false),
Err(_) => false,
}
})
}
pub fn to_int(host: &mut PyHost, id: u32) -> Result<Value, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let i = py
.import("builtins")
.and_then(|b| b.getattr("int"))
.and_then(|f| f.call1((obj,)))
.map_err(|e| e.to_string())?;
py_to_value(host, py, &i)
})
}
pub fn to_int_cb(id: u32) -> Result<Value, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let i = py
.import("builtins")
.and_then(|b| b.getattr("int"))
.and_then(|f| f.call1((obj,)))
.map_err(|e| e.to_string())?;
with_host(|h| py_to_value(h, py, &i))
})
}
pub fn binary_op_cb(func: &str, a: &Value, b: &Value) -> Result<Value, String> {
Python::with_gil(|py| {
let (pa, pb) = with_host(|h| -> Result<_, String> {
Ok((value_to_py(h, py, a)?, value_to_py(h, py, b)?))
})?;
let op = py
.import("operator")
.and_then(|m| m.getattr(func))
.map_err(|e| e.to_string())?;
let res = op.call1((pa, pb)).map_err(|e| e.to_string())?;
with_host(|h| py_to_value(h, py, &res))
})
}
pub fn unary_op(host: &mut PyHost, func: &str, v: &Value) -> Result<Value, String> {
Python::with_gil(|py| {
let pv = value_to_py(host, py, v)?;
let op = py
.import("operator")
.and_then(|m| m.getattr(func))
.map_err(|e| e.to_string())?;
let res = op.call1((pv,)).map_err(|e| e.to_string())?;
py_to_value(host, py, &res)
})
}
pub fn unary_op_cb(func: &str, v: &Value) -> Result<Value, String> {
Python::with_gil(|py| {
let pv = with_host(|h| value_to_py(h, py, v))?;
let op = py
.import("operator")
.and_then(|m| m.getattr(func))
.map_err(|e| e.to_string())?;
let res = op.call1((pv,)).map_err(|e| e.to_string())?;
with_host(|h| py_to_value(h, py, &res))
})
}
pub fn len(id: u32) -> Result<usize, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
obj.len().map_err(|e| e.to_string())
})
}
pub fn str_of(id: u32) -> String {
Python::with_gil(
|py| match fetch(py, id).and_then(|o| o.str().map_err(|e| e.to_string())) {
Ok(s) => s.to_string(),
Err(e) => e,
},
)
}
pub fn repr_of(id: u32) -> String {
Python::with_gil(
|py| match fetch(py, id).and_then(|o| o.repr().map_err(|e| e.to_string())) {
Ok(s) => s.to_string(),
Err(e) => e,
},
)
}
pub fn truthy(id: u32) -> bool {
Python::with_gil(|py| {
fetch(py, id)
.ok()
.and_then(|o| o.is_truthy().ok())
.unwrap_or(true)
})
}
pub fn class_name(id: u32) -> Option<String> {
Python::with_gil(|py| {
let obj = fetch(py, id).ok()?;
obj.getattr("__name__")
.ok()
.and_then(|n| n.extract::<String>().ok())
})
}
pub fn type_of(id: u32) -> Option<u32> {
Python::with_gil(|py| {
let ty = fetch(py, id).ok()?.get_type();
let key = ty.as_ptr() as usize;
let mut cache = type_handles().lock().expect("ffi type cache poisoned");
if let Some(existing) = cache.get(&key) {
return Some(*existing);
}
let fid = store(ty.into_any().unbind());
cache.insert(key, fid);
Some(fid)
})
}
pub fn type_name(id: u32) -> String {
Python::with_gil(|py| match fetch(py, id) {
Ok(obj) => obj
.get_type()
.name()
.map(|s| s.to_string())
.unwrap_or_else(|_| "object".into()),
Err(_) => "object".into(),
})
}