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);
route_std_streams();
install_main_module_hook();
});
BRIDGE_USABLE.load(std::sync::atomic::Ordering::Relaxed)
}
fn install_main_module_hook() {
Python::with_gil(|py| {
let Ok(main) = py.import("__main__") else {
return;
};
if let Ok(f) = wrap_pyfunction!(main_getattr, py) {
let _ = main.setattr("__getattr__", f);
}
});
}
#[pyfunction]
fn main_getattr(py: Python, name: String) -> PyResult<Py<PyAny>> {
match crate::host::try_with_host(|h| h.module_global(0, &name)).flatten() {
Some(v) => with_host(|h| value_to_py(h, py, &v))
.map(|b| b.unbind())
.map_err(rs_err),
None => Err(pyo3::exceptions::PyAttributeError::new_err(format!(
"module '__main__' has no attribute '{name}'"
))),
}
}
fn route_std_streams() {
Python::with_gil(|py| {
let (Ok(sys), Ok(io)) = (py.import("sys"), py.import("io")) else {
return;
};
for (name, stream) in [
("stdout", crate::stdio::Stream::Stdout),
("stderr", crate::stdio::Stream::Stderr),
] {
let Ok(old) = sys.getattr(name) else { continue };
if old.is_none() {
continue;
}
let kw = PyDict::new(py);
for attr in ["encoding", "errors"] {
if let Ok(v) = old.getattr(attr) {
let _ = kw.set_item(attr, v);
}
}
let _ = kw.set_item("newline", "\n");
let _ = kw.set_item("write_through", true);
let Ok(raw) = Py::new(py, PyrsStdStream { stream }) else { continue };
let Ok(new) = io
.getattr("TextIOWrapper")
.and_then(|cls| cls.call((raw,), Some(&kw)))
else {
continue;
};
let _ = new.setattr("mode", "w");
let _ = sys.setattr(name, &new);
let _ = sys.setattr(format!("__{name}__").as_str(), &new);
}
for stderr in [false, true] {
if let Some(target) = PENDING_STD_TARGET.with(|p| p.borrow_mut()[stderr as usize].take()) {
apply_std_target(py, stderr, target);
}
}
watch_sys_streams(py);
});
}
pub enum StdTarget {
Native,
Null,
Foreign(u32),
Pyrs(Value, u64),
}
thread_local! {
static PENDING_STD_TARGET: std::cell::RefCell<[Option<StdTarget>; 2]> =
const { std::cell::RefCell::new([None, None]) };
}
pub fn set_std_target(stderr: bool, target: StdTarget) {
if !INTERPRETER_STARTED.load(std::sync::atomic::Ordering::Relaxed) {
PENDING_STD_TARGET.with(|p| {
p.borrow_mut()[stderr as usize] = match target {
StdTarget::Native => None,
other => Some(other),
}
});
return;
}
Python::with_gil(|py| apply_std_target(py, stderr, target));
}
fn apply_std_target(py: Python, stderr: bool, target: StdTarget) {
let name = if stderr { "stderr" } else { "stdout" };
let Ok(sys) = py.import("sys") else { return };
if let Ok(current) = sys.getattr(name) {
if !current.is_none() {
let _ = current.call_method0("flush");
}
}
let new = match target {
StdTarget::Native => sys.getattr(format!("__{name}__").as_str()),
StdTarget::Null => Ok(py.None().into_bound(py)),
StdTarget::Foreign(id) => fetch(py, id).map_err(pyo3::exceptions::PyRuntimeError::new_err),
StdTarget::Pyrs(target, generation) => Py::new(
py,
PyrsRedirectStream {
target,
stderr,
thread: std::thread::current().id(),
generation,
},
)
.map(|p| p.into_any().into_bound(py)),
};
if let Ok(new) = new {
APPLYING_STD_TARGET.with(|a| a.set(true));
let _ = sys.setattr(name, new);
APPLYING_STD_TARGET.with(|a| a.set(false));
}
}
static STD_ASSIGNED: Mutex<[Option<StdTarget>; 2]> = Mutex::new([None, None]);
static STD_ASSIGNED_DIRTY: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
thread_local! {
static APPLYING_STD_TARGET: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
pub fn take_cpython_std_assignment(stderr: bool) -> Option<StdTarget> {
if !STD_ASSIGNED_DIRTY.load(std::sync::atomic::Ordering::Acquire) {
return None;
}
let mut pending = STD_ASSIGNED.lock().expect("std assignment lock poisoned");
let taken = pending[stderr as usize].take();
if pending.iter().all(Option::is_none) {
STD_ASSIGNED_DIRTY.store(false, std::sync::atomic::Ordering::Release);
}
taken
}
const PYDICT_EVENT_ADDED: std::ffi::c_int = 0;
const PYDICT_EVENT_MODIFIED: std::ffi::c_int = 1;
type DictWatchCallback = unsafe extern "C" fn(
std::ffi::c_int,
*mut pyo3::ffi::PyObject,
*mut pyo3::ffi::PyObject,
*mut pyo3::ffi::PyObject,
) -> std::ffi::c_int;
fn watch_sys_streams(py: Python) {
let (add, watch) = unsafe {
(
libc::dlsym(libc::RTLD_DEFAULT, c"PyDict_AddWatcher".as_ptr()),
libc::dlsym(libc::RTLD_DEFAULT, c"PyDict_Watch".as_ptr()),
)
};
if add.is_null() || watch.is_null() {
return;
}
let Ok(sys) = py.import("sys") else { return };
let Ok(dict) = sys.getattr("__dict__") else { return };
unsafe {
let add: unsafe extern "C" fn(DictWatchCallback) -> std::ffi::c_int = std::mem::transmute(add);
let watch: unsafe extern "C" fn(std::ffi::c_int, *mut pyo3::ffi::PyObject) -> std::ffi::c_int =
std::mem::transmute(watch);
let id = add(sys_dict_watcher);
if id < 0 {
pyo3::ffi::PyErr_Clear();
return;
}
if watch(id, dict.as_ptr()) < 0 {
pyo3::ffi::PyErr_Clear();
}
}
}
unsafe extern "C" fn sys_dict_watcher(
event: std::ffi::c_int,
_dict: *mut pyo3::ffi::PyObject,
key: *mut pyo3::ffi::PyObject,
new_value: *mut pyo3::ffi::PyObject,
) -> std::ffi::c_int {
if !(event == PYDICT_EVENT_ADDED || event == PYDICT_EVENT_MODIFIED)
|| key.is_null()
|| new_value.is_null()
|| APPLYING_STD_TARGET.with(|a| a.get())
{
return 0;
}
let py = unsafe { Python::assume_gil_acquired() };
let key = unsafe { Bound::from_borrowed_ptr(py, key) };
let name = key.downcast::<PyString>().ok().and_then(|s| s.to_cow().ok());
let stderr = match name.as_deref() {
Some("stdout") => false,
Some("stderr") => true,
_ => return 0,
};
let value = unsafe { Bound::from_borrowed_ptr(py, new_value) };
let target = classify_std_assignment(py, stderr, &value);
STD_ASSIGNED.lock().expect("std assignment lock poisoned")[stderr as usize] = Some(target);
STD_ASSIGNED_DIRTY.store(true, std::sync::atomic::Ordering::Release);
0
}
fn classify_std_assignment(py: Python, stderr: bool, value: &Bound<PyAny>) -> StdTarget {
if value.is_none() {
return StdTarget::Null;
}
let dunder = if stderr { "__stderr__" } else { "__stdout__" };
let native = py.import("sys").and_then(|sys| sys.getattr(dunder));
if native.is_ok_and(|n| n.is(value)) {
return StdTarget::Native;
}
if let Ok(redirect) = value.downcast::<PyrsRedirectStream>() {
let r = redirect.borrow();
return StdTarget::Pyrs(r.target.clone(), r.generation);
}
StdTarget::Foreign(store(value.clone().unbind()))
}
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,
origin: Some(match paired_exception(host, value.as_any()) {
Some(v) => crate::host::ExcOrigin::Paired(v),
None => crate::host::ExcOrigin::Raised {
handle: store(value.clone().into_any().unbind()),
addr: value.as_ptr() as usize,
},
}),
});
}
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 crate::host::export_float(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 pair_exception(host: &PyHost, v: &Value, obj: &Bound<PyAny>, handle: u32, raised_by_cpython: bool) {
host.exc_bridge
.borrow_mut()
.pair(v, handle, obj.as_ptr() as usize, raised_by_cpython);
}
fn paired_exception(host: &PyHost, obj: &Bound<PyAny>) -> Option<Value> {
host.exc_bridge.borrow().from_py.get(&(obj.as_ptr() as usize)).cloned()
}
fn exc_to_py<'py>(host: &PyHost, py: Python<'py>, v: &Value) -> Result<Option<Bound<'py, PyAny>>, String> {
let Value::Obj(id) = v else {
return Ok(None);
};
if let Some(&handle) = host.exc_bridge.borrow().to_py.get(id) {
return fetch(py, handle).map(Some);
}
let (class, args, builtin) = match host.get(v) {
Some(PyObj::Exception { class, args }) => (class.clone(), args.clone(), true),
Some(PyObj::Instance(i)) if host.class_is_exception(&i.class) => {
let args = match host.inst_attr(&i.dict, "args").map(|t| host.get(&t).cloned()) {
Some(Some(PyObj::Tuple(items))) => items,
_ => Vec::new(),
};
(i.class.clone(), args, false)
}
_ => return Ok(None),
};
let pargs = marshal_seq(host, py, &args)?;
let tup = PyTuple::new(py, pargs).map_err(|e| e.to_string())?;
let builtin_type = builtin
.then(|| py.import("builtins").and_then(|m| m.getattr(class.as_str())).ok())
.flatten()
.filter(|t| is_exception_type(py, t));
let exc = match builtin_type {
Some(ty) => ty.call1(tup).map_err(|e| e.to_string())?,
None => {
let mirror = exception_mirror(host, py, &class)?;
exception_helpers(py)?
.getattr("new")
.and_then(|new| new.call1((mirror, tup)))
.map_err(|e| e.to_string())?
}
};
let meta = host
.func_attrs
.get(id)
.filter(|_| crate::host::is_syntax_error_class(&class))
.and_then(|attrs| attrs.get("_metadata"))
.filter(|m| !matches!(m, Value::Undef));
if let Some(meta) = meta {
let pmeta = value_to_py(host, py, meta)?;
exc.setattr("_metadata", pmeta).map_err(|e| e.to_string())?;
}
let handle = store(exc.clone().unbind());
pair_exception(host, v, &exc, handle, false);
Ok(Some(exc))
}
fn is_exception_type(py: Python, t: &Bound<PyAny>) -> bool {
t.downcast::<pyo3::types::PyType>()
.ok()
.and_then(|t| t.is_subclass(&py.get_type::<pyo3::exceptions::PyBaseException>()).ok())
.unwrap_or(false)
}
fn exception_mirror<'py>(host: &PyHost, py: Python<'py>, class: &str) -> Result<Bound<'py, PyAny>, String> {
if let Some(&handle) = host.exc_bridge.borrow().mirrors.get(class) {
return fetch(py, handle);
}
let builtins = py.import("builtins").map_err(|e| e.to_string())?;
let (name, qualname, module, ancestors) = match host.classes.get(class) {
Some(c) => (c.name.clone(), c.qualname.clone(), c.module.clone(), host.mro_of(class)),
None => {
let (module, name) = class.rsplit_once('.').unwrap_or(("builtins", class));
(name.to_string(), name.to_string(), module.to_string(), crate::builtins::builtin_mro(class))
}
};
let base = ancestors
.iter()
.filter(|a| !host.classes.contains_key(*a))
.filter_map(|a| builtins.getattr(a.as_str()).ok())
.find(|t| is_exception_type(py, t))
.map_or_else(|| builtins.getattr("Exception"), Ok)
.map_err(|e| e.to_string())?;
let module = if module.is_empty() { "__main__".to_string() } else { module };
let delegate = wrap_pyfunction!(exception_mirror_delegate, py).map_err(|e| e.to_string())?;
let mirror = exception_helpers(py)?
.getattr("mirror")
.and_then(|f| f.call1((name, qualname, module, base, delegate)))
.map_err(|e| e.to_string())?;
let handle = store(mirror.clone().unbind());
host.exc_bridge.borrow_mut().mirrors.insert(class.to_string(), handle);
Ok(mirror)
}
#[pyfunction]
fn exception_mirror_delegate(py: Python, exc: Bound<PyAny>, what: String) -> PyResult<Py<PyAny>> {
let missing = || pyo3::exceptions::PyAttributeError::new_err(what.clone());
let v = with_host(|h| paired_exception(h, &exc)).ok_or_else(missing)?;
match what.as_str() {
"__str__" => {
let s = crate::builtins::py_str(&v).map_err(rs_err)?;
Ok(s.into_pyobject(py)?.into_any().unbind())
}
"__repr__" => {
let s = crate::builtins::py_repr(&v).map_err(rs_err)?;
Ok(s.into_pyobject(py)?.into_any().unbind())
}
_ => {
let attr = with_host(|h| h.get_attr(&v, &what)).map_err(|_| missing())?;
with_host(|h| value_to_py(h, py, &attr))
.map(|b| b.unbind())
.map_err(rs_err)
}
}
}
fn exception_helpers(py: Python) -> Result<Bound<PyModule>, String> {
static HELPERS: OnceLock<Py<PyModule>> = OnceLock::new();
if let Some(m) = HELPERS.get() {
return Ok(m.bind(py).clone());
}
let code = cr#"
def mirror(name, qualname, module, base, delegate):
def __str__(self):
return delegate(self, '__str__')
def __repr__(self):
return delegate(self, '__repr__')
def __getattr__(self, attr):
return delegate(self, attr)
namespace = {
'__qualname__': qualname,
'__module__': module,
'__str__': __str__,
'__repr__': __repr__,
'__getattr__': __getattr__,
}
return type(base)(name, (base,), namespace)
def new(cls, args):
return cls.__new__(cls, *args)
"#;
let module = PyModule::from_code(py, code, c"_pyrs_exceptions.py", c"_pyrs_exceptions")
.map_err(|e| e.to_string())?;
let _ = HELPERS.set(module.clone().unbind());
Ok(module)
}
#[derive(Default)]
struct BridgeIdentity {
mirrors: std::collections::HashMap<(u64, String), Py<PyAny>>,
pristine: std::collections::HashMap<usize, (u64, String)>,
callables: std::collections::HashMap<(u64, u32), Py<PyAny>>,
instances: std::collections::HashMap<(u64, u32), Py<PyAny>>,
converted: std::collections::HashMap<usize, (u64, Py<PyAny>, Value)>,
}
thread_local! {
static IDENTITY: std::cell::RefCell<BridgeIdentity> =
std::cell::RefCell::new(BridgeIdentity::default());
}
fn mirror_helper(py: Python) -> Result<Bound<PyAny>, String> {
static HELPER: OnceLock<Py<PyAny>> = OnceLock::new();
if let Some(m) = HELPER.get() {
return Ok(m.bind(py).clone());
}
let code = cr#"
import types
class PyrsMirror(type):
def __call__(cls, /, *args, **kwargs):
native = _native_class(cls)
if native is None:
return super().__call__(*args, **kwargs)
return native(*args, **kwargs)
def __setattr__(cls, name, value):
_detach(cls)
super().__setattr__(name, value)
def __delattr__(cls, name):
_detach(cls)
super().__delattr__(name)
def make_mirror(name, members):
def body(ns):
for k in members:
ns[k] = members[k]
return types.new_class(name, (object,), {'metaclass': PyrsMirror}, body)
"#;
let module = PyModule::from_code(py, code, c"_pyrs_mirror.py", c"_pyrs_mirror")
.map_err(|e| e.to_string())?;
let install = |f: PyResult<Bound<pyo3::types::PyCFunction>>| -> Result<(), String> {
module.add_function(f.map_err(|e| e.to_string())?).map_err(|e| e.to_string())
};
install(wrap_pyfunction!(_native_class, &module))?;
install(wrap_pyfunction!(_detach, &module))?;
let _ = HELPER.set(module.clone().into_any().unbind());
Ok(module.into_any())
}
#[pyfunction]
fn _native_class(py: Python, cls: &Bound<PyAny>) -> PyResult<Option<Py<PyAny>>> {
let Some(generation) = crate::host::try_with_host(|h| h.generation) else {
return Ok(None);
};
let Some(cname) = pristine_class(cls, generation) else {
return Ok(None);
};
let class = with_host(|h| h.alloc(PyObj::Class(cname)));
let proxy = PyrsCallable {
target: class,
doc: None,
module: None,
};
Ok(Some(Py::new(py, proxy)?.into_any()))
}
#[pyfunction]
fn _detach(cls: &Bound<PyAny>) {
let key = cls.as_ptr() as usize;
IDENTITY.with(|m| m.borrow_mut().pristine.remove(&key));
}
fn pristine_class(obj: &Bound<PyAny>, generation: u64) -> Option<String> {
IDENTITY.with(|m| {
m.borrow()
.pristine
.get(&(obj.as_ptr() as usize))
.filter(|(g, _)| *g == generation)
.map(|(_, cname)| cname.clone())
})
}
fn class_mirror<'py>(
host: &PyHost,
py: Python<'py>,
cname: &str,
) -> Result<Bound<'py, PyAny>, String> {
let class_def = host.classes.get(cname);
let members: Vec<(String, Value)> = class_def
.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())?;
}
let name = class_def.map_or(cname, |c| c.name.as_str());
let qualname = class_def
.map(|c| c.qualname.as_str())
.filter(|q| !q.is_empty())
.unwrap_or(name);
if !ns_dict.contains("__module__").unwrap_or(false) {
let module = class_def.map_or("__main__", |c| c.module.as_str());
let _ = ns_dict.set_item("__module__", module);
}
let _ = ns_dict.set_item("__qualname__", qualname);
let key = (host.generation, cname.to_string());
let cached = IDENTITY.with(|m| m.borrow().mirrors.get(&key).map(|o| o.clone_ref(py)));
if let Some(mirror) = cached {
let mirror = mirror.into_bound(py);
let still_pristine = IDENTITY.with(|m| {
m.borrow().pristine.contains_key(&(mirror.as_ptr() as usize))
});
if still_pristine {
let set = py
.get_type::<pyo3::types::PyType>()
.getattr("__setattr__")
.map_err(|e| e.to_string())?;
for (k, v) in ns_dict.iter() {
set.call1((&mirror, k, v)).map_err(|e| e.to_string())?;
}
}
return Ok(mirror);
}
let mirror = mirror_helper(py)?
.getattr("make_mirror")
.and_then(|f| f.call1((name, ns_dict)))
.map_err(|e| e.to_string())?;
IDENTITY.with(|m| {
let mut m = m.borrow_mut();
m.pristine.insert(mirror.as_ptr() as usize, key.clone());
m.mirrors.insert(key, mirror.clone().unbind());
});
Ok(mirror)
}
fn callable_proxy<'py>(host: &PyHost, py: Python<'py>, v: &Value) -> Result<Bound<'py, PyAny>, String> {
let Value::Obj(id) = v else {
return Err(crate::host::type_error("unsupported value for CPython call"));
};
let key = (host.generation, *id);
if let Some(p) = IDENTITY.with(|m| m.borrow().callables.get(&key).map(|o| o.clone_ref(py))) {
return Ok(p.into_bound(py));
}
let proxy = PyrsCallable {
target: v.clone(),
doc: None,
module: None,
};
let proxy = Py::new(py, proxy).map_err(|e| e.to_string())?.into_any();
IDENTITY.with(|m| m.borrow_mut().callables.insert(key, proxy.clone_ref(py)));
Ok(proxy.into_bound(py))
}
fn instance_from_mirror(
host: &mut PyHost,
py: Python,
obj: &Bound<PyAny>,
cname: String,
) -> Result<Value, String> {
let key = obj.as_ptr() as usize;
let known = IDENTITY.with(|m| {
m.borrow()
.converted
.get(&key)
.filter(|(generation, _, _)| *generation == host.generation)
.map(|(_, _, v)| v.clone())
});
if let Some(inst) = known {
return Ok(inst);
}
let inst = host.new_instance(cname.clone(), crate::host::NameMap::default());
IDENTITY.with(|m| {
m.borrow_mut()
.converted
.insert(key, (host.generation, obj.clone().unbind(), inst.clone()))
});
remember_from_py(obj, &inst);
let mut dict_attrs: Vec<(Bound<PyAny>, Bound<PyAny>)> = Vec::new();
if let Ok(dict) = obj.getattr("__dict__") {
if let Ok(dict) = dict.downcast::<PyDict>() {
dict_attrs.extend(dict.iter());
}
}
let native_slots = host.slot_names(&cname);
let mut slot_attrs: Vec<(Bound<PyAny>, Bound<PyAny>)> = Vec::new();
for name in &native_slots {
if let Ok(value) = obj.getattr(name.as_str()) {
slot_attrs.push((PyString::new(py, name).into_any(), value));
}
}
for (k, v) in dict_attrs.into_iter().chain(slot_attrs) {
let name: String = k.extract().map_err(|e| e.to_string())?;
let value = py_to_value(host, py, &v)?;
if native_slots.contains(&name) {
host.set_attr(&inst, &name, value)?;
} else {
host.instance_dict_set(&inst, &name, value);
}
}
Ok(inst)
}
struct Memo<K, V> {
depth: usize,
seen: std::collections::HashMap<K, V>,
}
impl<K, V> Default for Memo<K, V> {
fn default() -> Self {
Memo {
depth: 0,
seen: std::collections::HashMap::new(),
}
}
}
type MemoKey<K, V> = std::thread::LocalKey<std::cell::RefCell<Memo<K, V>>>;
thread_local! {
static TO_PY_MEMO: std::cell::RefCell<Memo<u32, Py<PyAny>>> =
std::cell::RefCell::new(Memo::default());
static FROM_PY_MEMO: std::cell::RefCell<Memo<usize, Value>> =
std::cell::RefCell::new(Memo::default());
}
struct MemoScope<K: 'static, V: 'static>(&'static MemoKey<K, V>);
impl<K: 'static, V: 'static> MemoScope<K, V> {
fn enter(key: &'static MemoKey<K, V>) -> Self {
key.with(|m| m.borrow_mut().depth += 1);
MemoScope(key)
}
}
impl<K: 'static, V: 'static> Drop for MemoScope<K, V> {
fn drop(&mut self) {
let finished = self.0.with(|m| {
let mut m = m.borrow_mut();
m.depth -= 1;
(m.depth == 0).then(|| std::mem::take(&mut m.seen))
});
drop(finished);
}
}
fn remember_to_py(v: &Value, obj: &Bound<PyAny>) {
if let Value::Obj(id) = v {
TO_PY_MEMO.with(|m| m.borrow_mut().seen.insert(*id, obj.clone().unbind()));
}
}
fn remember_from_py(obj: &Bound<PyAny>, v: &Value) {
FROM_PY_MEMO.with(|m| m.borrow_mut().seen.insert(obj.as_ptr() as usize, v.clone()));
}
fn value_to_py<'py>(
host: &PyHost,
py: Python<'py>,
v: &Value,
) -> Result<Bound<'py, PyAny>, String> {
let _scope = MemoScope::enter(&TO_PY_MEMO);
let Value::Obj(id) = v else {
return value_to_py_node(host, py, v);
};
if let Some(obj) = TO_PY_MEMO.with(|m| m.borrow().seen.get(id).map(|o| o.clone_ref(py))) {
return Ok(obj.into_bound(py));
}
let obj = value_to_py_node(host, py, v)?;
if matches!(
host.get(v),
Some(
PyObj::List(_)
| PyObj::Tuple(_)
| PyObj::Dict(_)
| PyObj::Set(_)
| PyObj::Frozenset(_)
| PyObj::Bytearray(_)
| PyObj::Deque { .. }
| PyObj::Instance(_)
)
) {
remember_to_py(v, &obj);
}
Ok(obj)
}
#[derive(Default)]
struct NanBridge {
by_addr: rustc_hash::FxHashMap<usize, (Py<PyAny>, f64)>,
by_bits: rustc_hash::FxHashMap<u64, usize>,
sweep_at: usize,
}
static NAN_BRIDGE: OnceLock<Mutex<NanBridge>> = OnceLock::new();
impl NanBridge {
fn with<R>(f: impl FnOnce(&mut NanBridge) -> R) -> R {
let m = NAN_BRIDGE.get_or_init(|| Mutex::new(NanBridge::default()));
f(&mut m.lock().unwrap_or_else(|e| e.into_inner()))
}
fn insert(&mut self, py: Python<'_>, obj: Py<PyAny>, nan: f64) {
if self.by_addr.len() >= self.sweep_at {
self.by_addr.retain(|_, (o, _)| o.get_refcnt(py) > 1);
let live: rustc_hash::FxHashSet<usize> = self.by_addr.keys().copied().collect();
self.by_bits.retain(|_, addr| live.contains(addr));
self.sweep_at = (self.by_addr.len() * 2).max(64);
}
let addr = obj.as_ptr() as usize;
self.by_bits.insert(nan.to_bits(), addr);
self.by_addr.insert(addr, (obj, nan));
}
}
fn float_from_py(obj: &Bound<'_, PyAny>) -> PyResult<f64> {
let f = obj.extract::<f64>()?;
if f.to_bits() & !(1 << 63) != crate::host::PY_NAN_BITS {
return Ok(f);
}
let addr = obj.as_ptr() as usize;
Ok(NanBridge::with(|t| {
if let Some((_, nan)) = t.by_addr.get(&addr) {
return *nan;
}
let nan = crate::host::fresh_nan(f.is_sign_negative());
t.insert(obj.py(), obj.clone().unbind(), nan);
nan
}))
}
fn float_to_py<'py>(py: Python<'py>, f: f64) -> PyResult<Bound<'py, PyAny>> {
if !crate::host::is_minted_nan(f) {
return crate::host::export_float(f).into_bound_py_any(py);
}
NanBridge::with(|t| {
if let Some(addr) = t.by_bits.get(&f.to_bits()) {
if let Some((obj, _)) = t.by_addr.get(addr) {
return Ok(obj.bind(py).clone());
}
}
let obj = crate::host::export_float(f).into_bound_py_any(py)?;
t.insert(py, obj.clone().unbind(), f);
Ok(obj)
})
}
fn value_to_py_node<'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(float_to_py(py, *f)),
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 list = PyList::empty(py);
remember_to_py(v, list.as_any());
for item in items {
list.append(value_to_py(host, py, item)?)
.map_err(|e| e.to_string())?;
}
Ok(list.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())?;
let (re, im) = (crate::host::export_float(*re), crate::host::export_float(*im));
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);
remember_to_py(v, dict.as_any());
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(_),
) => callable_proxy(host, py, v),
Some(PyObj::Class(cname)) => class_mirror(host, py, cname),
Some(PyObj::Exception { .. }) => exc_to_py(host, py, v)?
.ok_or_else(|| "ffi: exception did not cross".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)) => {
if let Some(exc) = exc_to_py(host, py, v)? {
return Ok(exc);
}
let Value::Obj(id) = v else {
return Err(crate::host::type_error("unsupported value for CPython call"));
};
let key = (host.generation, *id);
if let Some(p) = IDENTITY.with(|m| m.borrow().instances.get(&key).map(|o| o.clone_ref(py))) {
return Ok(p.into_bound(py));
}
let proxy = PyrsInstance { target: v.clone() };
let proxy = if crate::builtins::instance_has(host, i, "__index__") {
Py::new(py, (PyrsIndexInstance, proxy)).map(|p| p.into_any())
} else {
Py::new(py, proxy).map(|p| p.into_any())
}
.map_err(|e| e.to_string())?;
IDENTITY.with(|m| m.borrow_mut().instances.insert(key, proxy.clone_ref(py)));
Ok(proxy.into_bound(py))
}
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())
}
Some(PyObj::GenericAlias { origin, args }) => {
let (origin, args) = (origin.clone(), args.clone());
let porigin = value_to_py(host, py, &origin)?;
let pargs = marshal_seq(host, py, &args)?;
let tup = PyTuple::new(py, pargs).map_err(|e| e.to_string())?;
py.import("types")
.and_then(|m| m.getattr("GenericAlias"))
.and_then(|ga| ga.call1((porigin, tup)))
.map_err(|e| e.to_string())
}
Some(PyObj::Union { args }) => {
let args = args.clone();
let pargs = marshal_seq(host, py, &args)?;
let tup = PyTuple::new(py, pargs).map_err(|e| e.to_string())?;
py.import("typing")
.and_then(|m| m.getattr("Union"))
.and_then(|u| u.get_item(tup))
.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> {
let _scope = MemoScope::enter(&FROM_PY_MEMO);
let key = obj.as_ptr() as usize;
if let Some(v) = FROM_PY_MEMO.with(|m| m.borrow().seen.get(&key).cloned()) {
return Ok(v);
}
let v = py_to_value_node(host, py, obj)?;
if obj.is_exact_instance_of::<PyTuple>()
|| obj.is_exact_instance_of::<PySet>()
|| obj.is_exact_instance_of::<PyFrozenSet>()
{
remember_from_py(obj, &v);
}
Ok(v)
}
fn py_to_value_node(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(float_from_py(obj).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 out = host.new_list(Vec::new());
remember_from_py(obj, &out);
let items = unmarshal_seq(host, py, list.iter())?;
if let Some(PyObj::List(slot)) = host.get_mut(&out) {
*slot = items;
}
return Ok(out);
}
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 out = host.new_dict(indexmap::IndexMap::new());
remember_from_py(obj, &out);
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));
}
if let Some(PyObj::Dict(slot)) = host.get_mut(&out) {
*slot = map;
}
return Ok(out);
}
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);
}
if let Some(v) = paired_exception(host, obj) {
return Ok(v);
}
if let Some(cname) = pristine_class(obj, host.generation) {
return Ok(host.alloc(PyObj::Class(cname)));
}
if let Some(cname) = pristine_class(obj.get_type().as_any(), host.generation) {
return instance_from_mirror(host, py, obj, cname);
}
if let Some(name) = native_builtin_type(py, obj) {
return Ok(host.builtin_object(name));
}
Ok(host.alloc(PyObj::Foreign(store(obj.clone().unbind()))))
}
fn native_builtin_type(py: Python, obj: &Bound<PyAny>) -> Option<&'static str> {
if !obj.is_instance_of::<pyo3::types::PyType>() {
return None;
}
let name: String = obj.getattr("__name__").ok()?.extract().ok()?;
let native = crate::builtins::BUILTIN_TYPES
.iter()
.copied()
.find(|n| *n == name)?;
let builtin = py.import("builtins").ok()?.getattr(native).ok()?;
builtin.is(obj).then_some(native)
}
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());
}
if let Ok(c) = obj.downcast::<PyrsRedirectStream>() {
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);
call_result_to_value(h, py, &result, args, &arg_tuple, kw.as_ref())
})
}
fn call_result_to_value(
host: &mut PyHost,
py: Python,
result: &Bound<PyAny>,
args: &[Value],
arg_tuple: &Bound<PyTuple>,
kwargs: Option<&Bound<PyDict>>,
) -> Result<Value, String> {
if let Some(i) = arg_tuple.iter().position(|a| a.is(result)) {
if matches!(
host.get(&args[i]),
Some(PyObj::List(_) | PyObj::Bytearray(_) | PyObj::Deque { .. })
) {
return Ok(args[i].clone());
}
return py_to_value(host, py, result);
}
let is_kwarg = kwargs.is_some_and(|d| d.values().iter().any(|v| v.is(result)));
if !is_kwarg && result.get_refcnt() > 1 {
return reference_to_value(host, py, result);
}
py_to_value(host, 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>,
) {
let _scope = MemoScope::enter(&FROM_PY_MEMO);
let mut mutable = Vec::new();
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;
};
remember_from_py(&cpy, orig);
mutable.push((orig, cpy, kind));
}
for (orig, cpy, kind) in mutable {
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> {
let key = obj.as_ptr() as usize;
if let Some(v) = FROM_PY_MEMO.with(|m| m.borrow().seen.get(&key).cloned()) {
return Some(v);
}
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 float_from_py(obj).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 out = host.new_list(Vec::new());
remember_from_py(obj, &out);
let items = pure_seq(host, py, obj)?;
if let Some(PyObj::List(slot)) = host.get_mut(&out) {
*slot = items;
}
return Some(out);
}
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 _scope = MemoScope::enter(&TO_PY_MEMO);
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>> {
let outer = with_host(|h| h.exc.clone());
match crate::builtins::raw_getattr(&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) if !e.starts_with("AttributeError") => Err(call_err(e, outer)),
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>> {
let kind = with_host(|h| match h.get(&slf.borrow().target) {
Some(PyObj::ClassMethod(_)) => Some(true),
Some(PyObj::StaticMethod(_)) => Some(false),
_ => None,
});
match kind {
Some(true) => {
let cls = match (_owner, obj) {
(Some(owner), _) if !owner.is_none() => owner,
(_, Some(instance)) if !instance.is_none() => instance.get_type().into_any(),
_ => return Ok(slf.into_any()),
};
return py.import("types")?.getattr("MethodType")?.call1((slf, cls));
}
Some(false) => return Ok(slf.into_any()),
None => {}
}
match obj {
Some(instance) if !instance.is_none() => py
.import("types")?
.getattr("MethodType")?
.call1((slf, instance)),
_ => Ok(slf.into_any()),
}
}
fn __reduce__(&self, py: Python) -> PyResult<Py<PyAny>> {
let bound = with_host(|h| match h.get(&self.target) {
Some(PyObj::BoundMethod { recv, func }) => Some((recv.clone(), func.clone())),
_ => None,
});
if let Some((recv, func)) = bound {
let name = run_for_cpython(|| crate::builtins::raw_getattr(&func, "__name__"))?;
let getattr = py.import("builtins")?.getattr("getattr")?;
let args = with_host(|h| -> Result<_, String> {
Ok((value_to_py(h, py, &recv)?, value_to_py(h, py, &name)?))
})
.map_err(rs_err)?;
return Ok((getattr, args).into_pyobject(py)?.into_any().unbind());
}
let qualname = run_for_cpython(|| crate::builtins::raw_getattr(&self.target, "__qualname__"))?;
with_host(|h| value_to_py(h, py, &qualname))
.map(|b| b.unbind())
.map_err(rs_err)
}
#[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 = run_for_cpython(|| crate::host::invoke(&self.target, rs_args, rs_kwargs))?;
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",
));
}
let outer = with_host(|h| h.exc.clone());
self.finish_step(py, crate::host::gen_resume(&self.target, sent), outer)
}
#[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)?;
let outer = with_host(|h| h.exc.clone());
self.finish_step(py, crate::host::gen_throw(&self.target, exc_v), outer)
}
fn close(&self) -> PyResult<()> {
self.require_generator("close")?;
if with_host(|h| h.close_unstarted_gen(&self.target)) {
return Ok(());
}
let outer = with_host(|h| h.exc.clone());
let ge = with_host(|h| {
h.alloc(PyObj::Exception {
class: "GeneratorExit".into(),
args: vec![],
})
});
match crate::host::gen_throw(&self.target, ge) {
Ok(Some(_)) => {
hand_off_error(outer);
Err(pyo3::exceptions::PyRuntimeError::new_err(
"generator ignored GeneratorExit",
))
}
Ok(None) => Ok(()),
Err(e) if e.contains("GeneratorExit") || e.contains("StopIteration") => {
hand_off_error(outer);
Ok(())
}
Err(e) => Err(self.body_err(e, outer)),
}
}
}
impl PyrsIterator {
fn body_err(&self, e: String, outer: Option<Value>) -> 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));
hand_off_error(outer);
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>,
outer: Option<Value>,
) -> 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, outer)),
}
}
}
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)
}
pub fn raised_foreign(id: u32) -> Result<Value, String> {
Python::with_gil(|py| {
let obj = fetch(py, id)?;
let exc = if is_exception_type(py, &obj) {
obj.call0().map_err(|e| pyerr_to_error(py, &e))?
} else {
obj
};
let base_exc = py.get_type::<pyo3::exceptions::PyBaseException>();
if !exc.is_instance(&base_exc).unwrap_or(false) {
return Err(crate::host::type_error("exceptions must derive from BaseException"));
}
pyexc_to_value(py, &exc).map_err(|e| e.to_string())
})
}
pub fn traceback_text(handle: u32) -> Option<String> {
Python::with_gil(|py| {
let exc = fetch(py, handle).ok()?;
let tb = exc.getattr("__traceback__").ok().filter(|tb| !tb.is_none())?;
let lines = py
.import("traceback")
.and_then(|m| m.getattr("format_tb"))
.and_then(|f| f.call1((tb,)))
.ok()?;
let lines: Vec<String> = lines.extract().ok()?;
Some(lines.concat())
})
}
pub fn traceback_segment_text(handle: u32) -> Option<String> {
Python::with_gil(|py| {
let tb = fetch(py, handle).ok()?;
let lines = py
.import("traceback")
.and_then(|m| m.getattr("format_tb"))
.and_then(|f| f.call1((tb,)))
.ok()?;
let lines: Vec<String> = lines.extract().ok()?;
Some(lines.concat())
})
}
pub fn exception_type_name(handle: u32) -> Option<String> {
Python::with_gil(|py| {
let ty = fetch(py, handle).ok()?.get_type();
let qualname: String = ty.getattr("__qualname__").ok()?.extract().ok()?;
let module: Option<String> = ty.getattr("__module__").ok().and_then(|m| m.extract().ok());
Some(match module {
Some(m) if m != "builtins" && m != "__main__" => format!("{m}.{qualname}"),
_ => qualname,
})
})
}
fn pyexc_to_value(py: Python, exc: &Bound<PyAny>) -> PyResult<Value> {
if let Some(v) = with_host(|h| paired_exception(h, exc)) {
return Ok(v);
}
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<_>, _>>()?;
let v = h.alloc(PyObj::Exception { class, args });
pair_exception(h, &v, exc, store(exc.clone().unbind()), true);
Ok::<Value, String>(v)
})
.map_err(rs_err)
}
fn exc_value_to_pyerr(v: &Value) -> Option<pyo3::PyErr> {
Python::with_gil(|py| {
let exc = with_host(|h| exc_to_py(h, py, v)).ok()??;
Some(pyo3::PyErr::from_value(exc))
})
}
fn call_err(e: String, outer: Option<Value>) -> 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()),
Some(PyObj::Instance(i)) if h.class_is_exception(&i.class) => Some(h.type_name(v)),
_ => 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));
hand_off_error(outer);
err
}
fn run_for_cpython<T>(f: impl FnOnce() -> Result<T, String>) -> PyResult<T> {
let outer = with_host(|h| h.exc.clone());
f().map_err(|e| call_err(e, outer))
}
fn hand_off_error(outer: Option<Value>) {
with_host(|h| {
h.error = None;
h.exc = outer;
});
}
#[pyclass(subclass)]
struct PyrsInstance {
target: Value,
}
#[pymethods]
impl PyrsInstance {
#[getter(__class__)]
fn get_class(&self, py: Python) -> PyResult<Py<PyAny>> {
let class = with_host(|h| h.get_attr(&self.target, "__class__")).map_err(rs_err)?;
with_host(|h| value_to_py(h, py, &class))
.map(|b| b.unbind())
.map_err(rs_err)
}
fn __reduce_ex__(&self, py: Python, protocol: Bound<PyAny>) -> PyResult<Py<PyAny>> {
let protocol = with_host(|h| py_to_value(h, py, &protocol)).map_err(rs_err)?;
let r = run_for_cpython(|| {
crate::host::call_method(&self.target, "__reduce_ex__", vec![protocol], vec![])
})?;
with_host(|h| value_to_py(h, py, &r))
.map(|b| b.unbind())
.map_err(rs_err)
}
fn __reduce__(&self, py: Python) -> PyResult<Py<PyAny>> {
let r = run_for_cpython(|| crate::host::call_method(&self.target, "__reduce__", vec![], vec![]))?;
with_host(|h| value_to_py(h, py, &r))
.map(|b| b.unbind())
.map_err(rs_err)
}
fn __setattr__(&self, py: Python, name: String, value: Bound<PyAny>) -> PyResult<()> {
let v = with_host(|h| py_to_value(h, py, &value)).map_err(rs_err)?;
let name_v = with_host(|h| h.new_str(name));
run_for_cpython(|| {
crate::builtins::call_builtin_function("setattr", vec![self.target.clone(), name_v, v], vec![])
})
.map(|_| ())
}
fn __delattr__(&self, name: String) -> PyResult<()> {
let name_v = with_host(|h| h.new_str(name));
run_for_cpython(|| {
crate::builtins::call_builtin_function("delattr", vec![self.target.clone(), name_v], vec![])
})
.map(|_| ())
}
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 = run_for_cpython(|| {
crate::host::call_method(&self.target, "__getitem__", vec![key_v], vec![])
})?;
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 = run_for_cpython(|| crate::builtins::index_dunder(&target))?
.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 PyrsStdStream {
stream: crate::stdio::Stream,
}
impl PyrsStdStream {
fn fd(&self) -> i32 {
match self.stream {
crate::stdio::Stream::Stdout => 1,
crate::stdio::Stream::Stderr => 2,
}
}
}
#[pymethods]
impl PyrsStdStream {
fn write(&self, data: &[u8]) -> usize {
crate::stdio::write_bytes(self.stream, data);
data.len()
}
fn flush(&self) {
crate::stdio::flush(self.stream);
}
fn close(&self) {
self.flush();
}
fn fileno(&self) -> i32 {
self.fd()
}
fn isatty(&self) -> bool {
unsafe { libc::isatty(self.fd()) == 1 }
}
fn readable(&self) -> bool {
false
}
fn writable(&self) -> bool {
true
}
fn seekable(&self) -> bool {
false
}
#[getter]
fn closed(&self) -> bool {
false
}
#[getter]
fn name(&self) -> &'static str {
match self.stream {
crate::stdio::Stream::Stdout => "<stdout>",
crate::stdio::Stream::Stderr => "<stderr>",
}
}
}
#[pyclass]
struct PyrsRedirectStream {
target: Value,
stderr: bool,
thread: std::thread::ThreadId,
generation: u64,
}
impl PyrsRedirectStream {
fn usable(&self) -> bool {
std::thread::current().id() == self.thread
&& crate::host::try_with_host(|h| h.generation == self.generation).unwrap_or(false)
}
fn native(&self) -> crate::stdio::Stream {
if self.stderr {
crate::stdio::Stream::Stderr
} else {
crate::stdio::Stream::Stdout
}
}
}
#[pymethods]
impl PyrsRedirectStream {
fn write(&self, text: &str) -> PyResult<usize> {
if self.usable() {
run_for_cpython(|| crate::host::write_to_stream(&self.target, text))?;
} else {
crate::stdio::write_bytes(self.native(), text.as_bytes());
}
Ok(text.chars().count())
}
fn flush(&self) -> PyResult<()> {
if !self.usable() {
crate::stdio::flush(self.native());
return Ok(());
}
if with_host(|h| h.get_attr(&self.target, "flush")).is_ok() {
run_for_cpython(|| crate::host::call_method(&self.target, "flush", vec![], vec![]))?;
}
Ok(())
}
fn __getattr__(&self, py: Python, name: String) -> PyResult<Py<PyAny>> {
if !self.usable() {
return Err(pyo3::exceptions::PyAttributeError::new_err(name));
}
let v = with_host(|h| h.get_attr(&self.target, &name)).map_err(rs_err_typed)?;
with_host(|h| value_to_py(h, py, &v))
.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 = run_for_cpython(|| crate::host::call_method(&self.target, name, args, vec![]))?;
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 issubclass_values(host: &mut PyHost, sub: &Value, cls: &Value) -> Result<bool, String> {
Python::with_gil(|py| {
let a = value_to_py(host, py, sub)?;
let b = value_to_py(host, py, cls)?;
py.import("builtins")
.and_then(|m| m.getattr("issubclass"))
.and_then(|f| f.call1((a, b)))
.and_then(|r| r.is_truthy())
.map_err(|e| e.to_string())
})
}
pub fn foreign_builtin_type_name(cls_id: u32) -> Option<String> {
Python::with_gil(|py| {
let cls = fetch(py, cls_id).ok()?;
let module: String = cls.getattr("__module__").ok()?.extract().ok()?;
if module == "builtins" {
return cls.getattr("__name__").ok()?.extract().ok();
}
let qualname: String = cls.getattr("__qualname__").ok()?.extract().ok()?;
Some(format!("{module}.{qualname}"))
})
}
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 module_repr_from_spec(spec: u32) -> Option<String> {
Python::with_gil(|py| {
let spec = fetch(py, spec).ok()?;
py.import("_frozen_importlib")
.and_then(|m| m.getattr("_module_repr_from_spec"))
.and_then(|f| f.call1((spec,)))
.and_then(|r| r.extract::<String>())
.ok()
})
}
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(),
})
}
pub fn type_defines(id: u32, name: &str) -> bool {
Python::with_gil(|py| {
let Ok(obj) = fetch(py, id) else {
return false;
};
let Ok(mro) = obj.get_type().getattr("__mro__") else {
return false;
};
let Ok(classes) = mro.try_iter() else {
return false;
};
classes.flatten().any(|cls| {
cls.getattr("__dict__")
.and_then(|d| d.contains(name))
.unwrap_or(false)
})
})
}
pub fn type_qualified_name(id: u32) -> String {
Python::with_gil(|py| {
let Ok(obj) = fetch(py, id) else {
return "object".into();
};
let ty = obj.get_type();
let qualname = ty
.getattr("__qualname__")
.and_then(|q| q.extract::<String>())
.unwrap_or_else(|_| "object".into());
match ty
.getattr("__module__")
.and_then(|m| m.extract::<String>())
{
Ok(m) if m != "builtins" && m != "__main__" => format!("{m}.{qualname}"),
_ => qualname,
}
})
}