use crate::excgroup;
use crate::host::{self, ops, with_host, Instance, IterKind, PKey, PyObj};
use fusevm::{NumOp, Value, VM};
use indexmap::IndexMap;
pub fn install(vm: &mut VM) {
vm.register_builtin(ops::GETLOCAL, b_getlocal);
vm.register_builtin(ops::SETLOCAL, b_setlocal);
vm.register_builtin(ops::SETGLOBAL, b_setglobal);
vm.register_builtin(ops::DECLARE_GLOBAL, b_declare_global);
vm.register_builtin(ops::DELNAME, b_delname);
vm.register_builtin(ops::GETATTR, b_getattr);
vm.register_builtin(ops::SETATTR, b_setattr);
vm.register_builtin(ops::DELATTR, b_delattr);
vm.register_builtin(ops::GETITEM, b_getitem);
vm.register_builtin(ops::SETITEM, b_setitem);
vm.register_builtin(ops::DELITEM, b_delitem);
vm.register_builtin(ops::MKSTR, b_mkstr);
vm.register_builtin(ops::MKBYTES, b_mkbytes);
vm.register_builtin(ops::MKLIST, b_mklist);
vm.register_builtin(ops::MKTUPLE, b_mktuple);
vm.register_builtin(ops::MKSET, b_mkset);
vm.register_builtin(ops::MKSET_CONST, b_mkset_const);
vm.register_builtin(ops::MKDICT, b_mkdict);
vm.register_builtin(ops::EXTEND_LIST, b_extend_list);
vm.register_builtin(ops::EXTEND_TUPLE, b_extend_tuple);
vm.register_builtin(ops::EXTEND_SET, b_extend_set);
vm.register_builtin(ops::EXTEND_DICT, b_extend_dict);
vm.register_builtin(ops::EXTEND_STR, b_extend_str);
vm.register_builtin(ops::ELLIPSIS, b_ellipsis);
vm.register_builtin(ops::MKSLICE, b_mkslice);
vm.register_builtin(ops::CALL, b_call);
vm.register_builtin(ops::CALL_KW, b_call_kw);
vm.register_builtin(ops::CALL_METHOD, b_call_method);
vm.register_builtin(ops::CALL_METHOD_KW, b_call_method_kw);
vm.register_builtin(ops::LOAD_METHOD, b_load_method);
vm.register_builtin(ops::CALL_LOADED, b_call_loaded);
vm.register_builtin(ops::CALL_LOADED_KW, b_call_loaded_kw);
vm.register_builtin(ops::CALL_VALUE, b_call_value);
vm.register_builtin(ops::CALL_VALUE_KW, b_call_value_kw);
vm.register_builtin(ops::TRUTHY, b_truthy);
vm.register_builtin(ops::TOSTR, b_tostr);
vm.register_builtin(ops::FORMAT, b_format);
vm.register_builtin(ops::MKFUNC, b_mkfunc);
vm.register_builtin(ops::MKLAMBDA, b_mkfunc);
vm.register_builtin(ops::BUILD_CLASS, b_build_class);
vm.register_builtin(ops::GETITER, b_getiter);
vm.register_builtin(ops::FORITER, b_foriter);
vm.register_builtin(ops::GENRET, b_genret);
vm.register_builtin(ops::YIELD_FROM, b_yield_from);
vm.register_builtin(ops::AWAIT, b_await);
vm.register_builtin(ops::CONTAINS, b_contains);
vm.register_builtin(ops::IS, b_is);
vm.register_builtin(ops::RAISE, b_raise);
vm.register_builtin(ops::RERAISE, b_reraise);
vm.register_builtin(ops::SIG_RETURN, b_sig_return);
vm.register_builtin(ops::SIG_BREAK, b_sig_break);
vm.register_builtin(ops::SIG_CONTINUE, b_sig_continue);
vm.register_builtin(ops::LOOP_BODY, b_loop_body);
vm.register_builtin(ops::IMPORT, b_import);
vm.register_builtin(ops::IMPORT_FROM, b_import_from);
vm.register_builtin(ops::IMPORT_STAR, b_import_star);
vm.register_builtin(ops::IMPORT_RELATIVE, b_import_relative);
vm.register_builtin(ops::TRY_ANNOTATION, b_try_annotation);
vm.register_builtin(ops::IS_INT, b_is_int);
vm.register_builtin(ops::INTERPOLATION, b_interpolation);
vm.register_builtin(ops::TEMPLATE, b_template);
vm.register_builtin(ops::CHECK_BOUND, b_check_bound);
vm.register_builtin(ops::UNBOUND, |_, _| with_host(|h| h.alloc(PyObj::Unbound)));
vm.register_builtin(ops::TYPE_ALIAS, b_type_alias);
vm.register_builtin(ops::UNPACK, b_unpack);
vm.register_builtin(ops::BINOP, b_binop);
vm.register_builtin(ops::INPLACE, b_inplace);
vm.register_builtin(ops::UNARY, b_unary);
vm.register_builtin(ops::GETGLOBAL, b_getglobal);
vm.register_builtin(ops::GETSELF, b_getself);
vm.register_builtin(ops::ASSERT_FAIL, b_assert_fail);
vm.register_builtin(ops::TRY, b_try);
vm.register_builtin(ops::DBG_LINE, b_dbg_line);
vm.register_builtin(ops::YIELDV, b_yieldv);
vm.register_builtin(ops::DECLARE_NONLOCAL, b_declare_nonlocal);
vm.register_builtin(ops::CALL_EX, b_call_ex);
vm.register_builtin(ops::CALL_VALUE_EX, b_call_value_ex);
vm.register_builtin(ops::CALL_LOADED_EX, b_call_loaded_ex);
vm.register_builtin(ops::BUILD_ARGS, b_build_args);
vm.register_builtin(ops::BUILD_KWARGS, b_build_kwargs);
vm.register_builtin(ops::MKDICT_EX, b_mkdict_ex);
vm.register_builtin(ops::MATCH_SEQ, b_match_seq);
vm.register_builtin(ops::MATCH_MAP_CHECK, b_match_map_check);
vm.register_builtin(ops::MATCH_KEY, b_match_key);
vm.register_builtin(ops::MATCH_MAP_REST, b_match_map_rest);
vm.register_builtin(ops::MATCH_CLASS, b_match_class);
vm.register_builtin(ops::DISPLAYHOOK, b_displayhook);
}
fn pop_n(vm: &mut VM, n: usize) -> Vec<Value> {
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(vm.pop());
}
v.reverse();
v
}
fn pop_flat(vm: &mut VM, argc: u8) -> Vec<Value> {
let flat = pop_n(vm, argc as usize);
if argc != 1 {
return flat;
}
let list = with_host(|h| match h.get(&flat[0]) {
Some(PyObj::List(l)) => Some(l.clone()),
_ => None,
});
list.unwrap_or(flat)
}
fn sval(v: &Value) -> String {
if let Value::Str(s) = v {
return (**s).clone();
}
with_host(|h| h.as_str(v)).unwrap_or_default()
}
fn sref(v: &Value) -> std::borrow::Cow<'_, str> {
match v {
Value::Str(s) => std::borrow::Cow::Borrowed(s.as_str()),
_ => std::borrow::Cow::Owned(sval(v)),
}
}
pub(crate) fn record_err_line(vm: &VM) {
let idx = vm.ip.saturating_sub(1);
if let Some(&line) = vm.chunk.lines.get(idx) {
if line != 0 {
let span = crate::host::lookup_position(vm.chunk.op_hash, idx);
with_host(|h| h.set_cur_line_span(line, span));
}
}
}
fn note_call_line(vm: &VM) {
if let Some(&line) = vm.chunk.lines.get(vm.ip.wrapping_sub(1)) {
if line != 0 {
with_host(|h| h.set_cur_line(line));
}
}
}
fn abort(vm: &mut VM, e: String) -> Value {
record_err_line(vm);
with_host(|h| h.error = Some(e));
vm.ip = vm.chunk.ops.len();
Value::Undef
}
fn finish(vm: &mut VM, r: Result<Value, String>) -> Value {
match r {
Ok(v) => {
if with_host(|h| h.error.is_some() || h.signal.is_some()) {
if with_host(|h| h.error.is_some()) {
record_err_line(vm);
}
vm.ip = vm.chunk.ops.len();
}
v
}
Err(e) => abort(vm, e),
}
}
fn kw_pairs(d: &Value) -> Vec<(String, Value)> {
with_host(|h| match h.get(d) {
Some(PyObj::Dict(m)) => m
.values()
.filter_map(|(k, v)| h.as_str(k).map(|s| (s, v.clone())))
.collect(),
_ => Vec::new(),
})
}
fn b_ellipsis(_vm: &mut VM, _: u8) -> Value {
with_host(|h| h.alloc(PyObj::Ellipsis))
}
fn b_getlocal(vm: &mut VM, _: u8) -> Value {
let key = vm.pop();
let name = sref(&key);
materialize_if_main_dunder(&name);
match with_host(|h| h.read_name_checked(&name)) {
host::NameRead::Value(v) => return v,
host::NameRead::Unbound => return abort(vm, host::unbound_local_error(&name)),
host::NameRead::Missing => {}
}
if let Some(v) = host::resolve_builtin(&name) {
return v;
}
if host::is_rust_ffi_name(&name) {
return with_host(|h| h.builtin_object(&name));
}
with_host(|h| h.note_name_miss(&name));
abort(vm, host::name_error(&name))
}
fn materialize_if_main_dunder(name: &str) {
if matches!(name, "__loader__" | "__builtins__") {
host::ensure_main_dunders();
}
}
fn b_getglobal(vm: &mut VM, _: u8) -> Value {
let key = vm.pop();
let name = sref(&key);
materialize_if_main_dunder(&name);
if let Some(v) = with_host(|h| h.read_global(&name)) {
return v;
}
if let Some(v) = host::resolve_builtin(&name) {
return v;
}
if host::is_rust_ffi_name(&name) {
return with_host(|h| h.builtin_object(&name));
}
with_host(|h| h.note_name_miss(&name));
abort(vm, host::name_error(&name))
}
fn b_setlocal(vm: &mut VM, _: u8) -> Value {
let val = vm.pop();
let key = vm.pop();
with_host(|h| h.set_name(&sref(&key), val.clone()));
val
}
fn b_setglobal(vm: &mut VM, _: u8) -> Value {
let val = vm.pop();
let key = vm.pop();
with_host(|h| h.set_global(&sref(&key), val.clone()));
val
}
fn b_declare_global(vm: &mut VM, _: u8) -> Value {
let name = sval(&vm.pop());
with_host(|h| h.declare_global(&name));
Value::Undef
}
fn b_declare_nonlocal(vm: &mut VM, _: u8) -> Value {
let name = sval(&vm.pop());
with_host(|h| h.declare_nonlocal(&name));
Value::Undef
}
fn b_delname(vm: &mut VM, _: u8) -> Value {
let name = sval(&vm.pop());
match with_host(|h| h.del_name(&name)) {
Ok(()) => Value::Undef,
Err(e) => abort(vm, e),
}
}
fn b_getself(vm: &mut VM, _: u8) -> Value {
let _ = vm;
with_host(|h| h.current_self().unwrap_or(Value::Undef))
}
fn b_getattr(vm: &mut VM, _: u8) -> Value {
let name = sval(&vm.pop());
let recv = vm.pop();
let r = get_attr_desc(&recv, &name);
finish(vm, r)
}
fn instance_dunder(recv: &Value, name: &str) -> bool {
with_host(|h| match h.get(recv) {
Some(PyObj::Instance(i)) => h.class_has(&i.class, name),
_ => false,
})
}
fn is_attr_err(e: &str) -> bool {
e.starts_with("AttributeError")
}
fn get_attr_desc(recv: &Value, name: &str) -> Result<Value, String> {
let res = if instance_dunder(recv, "__getattribute__") {
let nm = with_host(|h| h.new_str(name.to_string()));
host::call_method(recv, "__getattribute__", vec![nm], vec![])
} else {
raw_getattr(recv, name)
};
match res {
Ok(v) => Ok(v),
Err(e) if is_attr_err(&e) && instance_dunder(recv, "__getattr__") => {
let nm = with_host(|h| h.new_str(name.to_string()));
host::call_method(recv, "__getattr__", vec![nm], vec![])
}
Err(e) => Err(e),
}
.inspect_err(|e| with_host(|h| h.note_attr_miss(e, recv, name, false)))
}
pub(crate) fn raw_getattr(recv: &Value, name: &str) -> Result<Value, String> {
match with_host(|h| h.plan_attr_get(recv, name)) {
host::AttrGet::Property { fget, inst, owner } => {
if matches!(fget, Value::Undef) {
let cls = with_host(|h| h.type_name(&inst));
return Err(format!(
"AttributeError: property '{name}' of '{cls}' object has no getter"
));
}
match with_host(|h| h.get(&fget).cloned()) {
Some(PyObj::Func(fv)) => {
host::run_user_func(&fv, Some(inst), owner, vec![], vec![])
}
_ => host::invoke(&fget, vec![inst], vec![]),
}
}
host::AttrGet::Descriptor { desc, inst, cls } => {
host::call_method(&desc, "__get__", vec![inst, cls], vec![])
}
host::AttrGet::CachedProperty { func, inst, name } => {
let value = match with_host(|h| h.get(&func).cloned()) {
Some(PyObj::Func(fv)) => {
host::run_user_func(&fv, Some(inst.clone()), None, vec![], vec![])?
}
_ => host::invoke(&func, vec![inst.clone()], vec![])?,
};
if with_host(|h| h.set_attr(&inst, &name, value.clone())).is_err() {
let cls = with_host(|h| h.type_name(&inst));
return Err(host::type_error(&format!(
"No '__dict__' attribute on '{cls}' instance to cache '{name}' property."
)));
}
Ok(value)
}
host::AttrGet::Annotations { func } => host::function_annotations(&func),
host::AttrGet::TypeAliasValue { alias } => host::type_alias_value(&alias),
host::AttrGet::Plain => with_host(|h| h.get_attr(recv, name)),
}
}
fn b_setattr(vm: &mut VM, _: u8) -> Value {
let val = vm.pop();
let name = sval(&vm.pop());
let recv = vm.pop();
let r = set_attr_desc(&recv, &name, val);
match r {
Ok(()) => Value::Undef,
Err(e) => abort(vm, e),
}
}
fn set_attr_desc(recv: &Value, name: &str, val: Value) -> Result<(), String> {
if instance_dunder(recv, "__setattr__") {
let nm = with_host(|h| h.new_str(name.to_string()));
return host::call_method(recv, "__setattr__", vec![nm, val], vec![]).map(|_| ());
}
raw_setattr(recv, name, val)
}
pub(crate) fn raw_setattr(recv: &Value, name: &str, val: Value) -> Result<(), String> {
match with_host(|h| h.plan_attr_set(recv, name, &val)) {
host::AttrSet::Property {
fset,
inst,
val,
owner,
} => {
if matches!(fset, Value::Undef) {
let cls = with_host(|h| h.type_name(&inst));
return Err(format!(
"AttributeError: property '{name}' of '{cls}' object has no setter"
));
}
match with_host(|h| h.get(&fset).cloned()) {
Some(PyObj::Func(fv)) => {
host::run_user_func(&fv, Some(inst), owner, vec![val], vec![]).map(|_| ())
}
_ => host::invoke(&fset, vec![inst, val], vec![]).map(|_| ()),
}
}
host::AttrSet::Descriptor { desc, inst, val } => {
host::call_method(&desc, "__set__", vec![inst, val], vec![]).map(|_| ())
}
host::AttrSet::Plain => with_host(|h| h.set_attr(recv, name, val)),
}
}
fn b_delattr(vm: &mut VM, _: u8) -> Value {
let name = sval(&vm.pop());
let recv = vm.pop();
let r = del_attr_desc(&recv, &name);
match r {
Ok(()) => Value::Undef,
Err(e) => abort(vm, e),
}
}
fn del_attr_desc(recv: &Value, name: &str) -> Result<(), String> {
if instance_dunder(recv, "__delattr__") {
let nm = with_host(|h| h.new_str(name.to_string()));
return host::call_method(recv, "__delattr__", vec![nm], vec![]).map(|_| ());
}
raw_delattr(recv, name)
}
pub(crate) fn raw_delattr(recv: &Value, name: &str) -> Result<(), String> {
match with_host(|h| h.plan_attr_del(recv, name)) {
host::AttrDel::Property { fdel, inst, owner } => {
if matches!(fdel, Value::Undef) {
let cls = with_host(|h| h.type_name(&inst));
return Err(format!(
"AttributeError: property '{name}' of '{cls}' object has no deleter"
));
}
match with_host(|h| h.get(&fdel).cloned()) {
Some(PyObj::Func(fv)) => {
host::run_user_func(&fv, Some(inst), owner, vec![], vec![]).map(|_| ())
}
_ => host::invoke(&fdel, vec![inst], vec![]).map(|_| ()),
}
}
host::AttrDel::Descriptor {
desc,
inst,
has_delete,
} => {
if !has_delete {
return Err("AttributeError: __delete__".to_string());
}
host::call_method(&desc, "__delete__", vec![inst], vec![]).map(|_| ())
}
host::AttrDel::Plain => with_host(|h| h.del_attr(recv, name)),
}
}
fn instance_defines(recv: &Value, name: &str) -> bool {
with_host(|h| match h.get(recv) {
Some(PyObj::Instance(i)) => {
let class = i.class.clone();
h.class_lookup(&class, name).is_some() || h.builtin_base_of(&class).is_some()
}
_ => false,
})
}
fn b_getitem(vm: &mut VM, _: u8) -> Value {
let idx = vm.pop();
let recv = vm.pop();
let r = getitem_value(recv, idx);
finish(vm, r)
}
pub fn getitem_value(recv: Value, idx: Value) -> Result<Value, String> {
if matches!(idx, Value::Int(_) | Value::Bool(_)) {
let fast = with_host(|h| {
matches!(
h.get(&recv),
Some(PyObj::List(_) | PyObj::Tuple(_) | PyObj::Str(_) | PyObj::Bytes(_))
)
.then(|| h.get_item(&recv, &idx))
});
if let Some(r) = fast {
return r;
}
}
if instance_defines(&recv, "__getitem__") {
let r = host::call_method(&recv, "__getitem__", vec![idx], vec![]);
return r;
}
if with_host(|h| matches!(h.get(&recv), Some(PyObj::Instance(_)))) {
let tn = with_host(|h| h.type_name(&recv));
return Err(host::type_error(&format!("'{tn}' object is not subscriptable")));
}
if let Some(r) = host::class_getitem(&recv, idx.clone()) {
return r;
}
if let Some(m) = with_host(|h| h.metaclass_method(&recv, "__getitem__")) {
let r = host::invoke(&m, vec![recv.clone(), idx], vec![]);
return r;
}
if let Some(meta) = host::dict_meta_of(&recv) {
let missing = with_host(|h| match h.to_key(&idx) {
Ok(k) => match h.get(&recv) {
Some(PyObj::Dict(d)) => !d.contains_key(&k),
_ => false,
},
Err(_) => false,
});
if missing {
match meta.kind {
host::DictKind::Counter => return Ok(Value::Int(0)),
host::DictKind::DefaultDict => {
if let Some(factory) = meta.factory {
let r = (|| {
let default = host::invoke(&factory, vec![], vec![])?;
with_host(|h| h.set_item(&recv, &idx, default.clone()))?;
Ok(default)
})();
return r;
}
}
host::DictKind::OrderedDict => {}
}
}
}
let idx = if with_host(|h| !matches!(h.get(&recv), Some(PyObj::Dict(_)))) {
if with_host(|h| matches!(h.get(&idx), Some(PyObj::Slice { .. }))) {
match normalize_slice_bounds(&idx) {
Ok(v) => v,
Err(e) => return Err(e),
}
} else {
match index_dunder(&idx) {
Ok(Some(v)) => v,
Ok(None) => idx,
Err(e) => return Err(e),
}
}
} else {
idx
};
#[cfg(feature = "stdlib-ffi")]
if let Some(id) = with_host(|h| h.foreign_id(&recv)) {
return crate::ffi::get_item_cb(id, &idx);
}
if host::is_generic_subscriptable(&recv) {
return host::generic_alias(&recv, &idx);
}
let cands = host::instance_key_candidates_for(&recv, Some(&idx));
let r = host::with_instance_key(&idx, host::KeyRole::Of(&recv), &cands, || {
with_host(|h| h.get_item(&recv, &idx))
});
r
}
fn fast_store(h: &host::PyHost, recv: &Value, idx: &Value, val: &Value) -> bool {
match h.get(recv) {
Some(PyObj::Bytearray(_)) => {
matches!(idx, Value::Int(_) | Value::Bool(_)) && !matches!(val, Value::Obj(_))
}
Some(PyObj::List(_)) => matches!(idx, Value::Int(_) | Value::Bool(_)),
Some(PyObj::Dict(_)) => h.key_cannot_collapse(idx),
_ => false,
}
}
fn store_index(recv: &Value, idx: Value) -> Result<Value, String> {
if with_host(|h| matches!(h.get(recv), Some(PyObj::Dict(_)))) {
return Ok(idx);
}
Ok(index_dunder(&idx)?.unwrap_or(idx))
}
fn b_setitem(vm: &mut VM, _: u8) -> Value {
let val = vm.pop();
let idx = vm.pop();
let recv = vm.pop();
if let Some(r) = with_host(|h| {
fast_store(h, &recv, &idx, &val).then(|| h.set_item(&recv, &idx, val.clone()))
}) {
return match r {
Ok(()) => val,
Err(e) => abort(vm, e),
};
}
if instance_defines(&recv, "__setitem__") {
let r = host::call_method(&recv, "__setitem__", vec![idx, val.clone()], vec![]);
return finish(vm, r);
}
if with_host(|h| matches!(h.get(&recv), Some(PyObj::Instance(_)))) {
let tn = with_host(|h| h.type_name(&recv));
return abort(
vm,
host::type_error(&format!("'{tn}' object does not support item assignment")),
);
}
match subscript_store(&recv, idx, val.clone()) {
Ok(()) => val,
Err(e) => abort(vm, e),
}
}
fn subscript_store(recv: &Value, idx: Value, val: Value) -> Result<(), String> {
let recv = recv.clone();
if with_host(|h| {
matches!(h.get(&idx), Some(PyObj::Slice { .. }))
&& !matches!(h.get(&recv), Some(PyObj::Dict(_) | PyObj::Deque { .. }))
}) {
let idx = normalize_slice_bounds(&idx)?;
if with_host(|h| matches!(h.get(&recv), Some(PyObj::Memoryview { .. }))) {
let repl = match as_bytes_object(&val)? {
Some(b) => b,
None => {
let tn = with_host(|h| h.type_name(&val));
return Err(host::type_error(&format!(
"a bytes-like object is required, not '{tn}'"
)));
}
};
return with_host(|h| h.set_memoryview_slice(&recv, &idx, repl));
}
let repl = host::iter_vec(&val)?;
return with_host(|h| h.set_slice_vals(&recv, &idx, repl));
}
let idx = store_index(&recv, idx)?;
let val = if with_host(|h| {
matches!(
h.get(&recv),
Some(PyObj::Bytearray(_) | PyObj::Memoryview { .. })
)
}) {
indexed(&val)?
} else {
val
};
let cands = host::instance_key_candidates_for(&recv, Some(&idx));
host::with_instance_key(&idx, host::KeyRole::Of(&recv), &cands, || {
with_host(|h| h.set_item(&recv, &idx, val.clone()))
})
}
fn b_delitem(vm: &mut VM, _: u8) -> Value {
let idx = vm.pop();
let recv = vm.pop();
if let Some(r) =
with_host(|h| fast_store(h, &recv, &idx, &Value::Undef).then(|| h.del_item(&recv, &idx)))
{
return match r {
Ok(()) => Value::Undef,
Err(e) => abort(vm, e),
};
}
if instance_defines(&recv, "__delitem__") {
let r = host::call_method(&recv, "__delitem__", vec![idx], vec![]).map(|_| Value::Undef);
return finish(vm, r);
}
if with_host(|h| matches!(h.get(&recv), Some(PyObj::Instance(_)))) {
let tn = with_host(|h| h.type_name(&recv));
return abort(
vm,
host::type_error(&format!("'{tn}' object doesn't support item deletion")),
);
}
match subscript_delete(&recv, idx) {
Ok(()) => Value::Undef,
Err(e) => abort(vm, e),
}
}
fn subscript_delete(recv: &Value, idx: Value) -> Result<(), String> {
let recv = recv.clone();
let idx = if with_host(|h| {
matches!(h.get(&idx), Some(PyObj::Slice { .. }))
&& !matches!(h.get(&recv), Some(PyObj::Dict(_)))
}) {
normalize_slice_bounds(&idx)?
} else {
store_index(&recv, idx)?
};
let cands = host::instance_key_candidates_for(&recv, Some(&idx));
host::with_instance_key(&idx, host::KeyRole::Of(&recv), &cands, || {
with_host(|h| h.del_item(&recv, &idx))
})
}
fn b_mkstr(vm: &mut VM, argc: u8) -> Value {
let parts = pop_n(vm, argc as usize);
let mut s = String::new();
for p in &parts {
s.push_str(&host::str_of(p));
}
with_host(|h| h.new_str(s))
}
fn b_mkbytes(vm: &mut VM, _argc: u8) -> Value {
let v = vm.pop();
let bytes: Vec<u8> = with_host(|h| match h.get(&v) {
Some(PyObj::Str(s)) => s.chars().map(|c| c as u32 as u8).collect(),
_ => match &v {
Value::Str(s) => s.chars().map(|c| c as u32 as u8).collect(),
_ => vec![],
},
});
with_host(|h| h.alloc(PyObj::Bytes(bytes)))
}
fn b_mklist(vm: &mut VM, argc: u8) -> Value {
let items = pop_n(vm, argc as usize);
with_host(|h| h.new_list(items))
}
fn b_mktuple(vm: &mut VM, argc: u8) -> Value {
let items = pop_n(vm, argc as usize);
with_host(|h| h.new_tuple(items))
}
fn b_mkset(vm: &mut VM, argc: u8) -> Value {
let items = pop_n(vm, argc as usize);
let mut set: IndexMap<PKey, Value> = IndexMap::new();
for it in items {
let cands = with_host(|h| host::set_local_candidates(h, &set));
let key = host::with_instance_key(&it, host::KeyRole::Set, &cands, || {
with_host(|h| h.to_key(&it))
});
match key {
Ok(k) => host::set_put(&mut set, k, it),
Err(e) => return abort(vm, e),
}
}
with_host(|h| h.new_set(set))
}
fn b_mkset_const(vm: &mut VM, argc: u8) -> Value {
let display = b_mkset(vm, argc);
with_host(|h| {
if let Some((folded, start)) = h.set_layout_of(&display) {
let empty = IndexMap::new();
let n = folded.len();
h.set_relayout_after_merge(&display, (&empty, host::SET_MINSIZE), n, Some((&folded, start)));
}
});
display
}
fn b_mkdict(vm: &mut VM, argc: u8) -> Value {
let flat = pop_n(vm, argc as usize);
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
let mut i = 0;
while i + 1 < flat.len() {
let k = flat[i].clone();
let v = flat[i + 1].clone();
let cands = with_host(|h| host::dict_local_candidates(h, &d));
let key = host::with_instance_key(&k, host::KeyRole::Dict, &cands, || {
with_host(|h| h.to_key(&k))
});
match key {
Ok(key) => host::dict_put(&mut d, key, k, v),
Err(e) => return abort(vm, e),
}
i += 2;
}
with_host(|h| h.new_dict(d))
}
fn b_extend_list(vm: &mut VM, argc: u8) -> Value {
let mut items = pop_n(vm, argc as usize);
let acc = items.remove(0);
with_host(|h| {
if let Some(PyObj::List(l)) = h.get_mut(&acc) {
l.extend(items);
}
});
acc
}
fn b_extend_tuple(vm: &mut VM, argc: u8) -> Value {
let mut items = pop_n(vm, argc as usize);
let acc = items.remove(0);
let mut all: Vec<Value> = with_host(|h| match h.get(&acc) {
Some(PyObj::Tuple(t)) => t.clone(),
_ => Vec::new(),
});
all.extend(items);
with_host(|h| h.new_tuple(all))
}
fn b_extend_set(vm: &mut VM, argc: u8) -> Value {
let mut items = pop_n(vm, argc as usize);
let acc = items.remove(0);
let mut set: IndexMap<PKey, Value> = with_host(|h| match h.get(&acc) {
Some(PyObj::Set(s)) => s.clone(),
_ => IndexMap::new(),
});
for it in items {
let cands = with_host(|h| host::set_local_candidates(h, &set));
let key = host::with_instance_key(&it, host::KeyRole::Set, &cands, || {
with_host(|h| h.to_key(&it))
});
match key {
Ok(k) => host::set_put(&mut set, k, it),
Err(e) => return abort(vm, e),
}
}
with_host(|h| {
if let Some(PyObj::Set(s)) = h.get_mut(&acc) {
*s = set;
}
});
acc
}
fn b_extend_dict(vm: &mut VM, argc: u8) -> Value {
let flat = pop_n(vm, argc as usize);
let acc = flat[0].clone();
let mut d: IndexMap<PKey, (Value, Value)> = with_host(|h| match h.get(&acc) {
Some(PyObj::Dict(m)) => m.clone(),
_ => IndexMap::new(),
});
let mut i = 1;
while i + 1 < flat.len() {
let k = flat[i].clone();
let v = flat[i + 1].clone();
let cands = with_host(|h| host::dict_local_candidates(h, &d));
let key = host::with_instance_key(&k, host::KeyRole::Dict, &cands, || {
with_host(|h| h.to_key(&k))
});
match key {
Ok(key) => host::dict_put(&mut d, key, k, v),
Err(e) => return abort(vm, e),
}
i += 2;
}
with_host(|h| {
if let Some(PyObj::Dict(m)) = h.get_mut(&acc) {
*m = d;
}
});
acc
}
fn b_extend_str(vm: &mut VM, argc: u8) -> Value {
let mut parts = pop_n(vm, argc as usize);
let acc = parts.remove(0);
let mut s = with_host(|h| h.str_of(&acc));
with_host(|h| {
for p in &parts {
s.push_str(&h.str_of(p));
}
});
with_host(|h| h.new_str(s))
}
fn b_mkslice(vm: &mut VM, _: u8) -> Value {
let step = vm.pop();
let hi = vm.pop();
let lo = vm.pop();
with_host(|h| h.alloc(PyObj::Slice { lo, hi, step }))
}
fn b_call(vm: &mut VM, argc: u8) -> Value {
note_call_line(vm);
let mut args = pop_n(vm, argc as usize);
let name = sval(&args.remove(0));
let r = host::call_named(&name, args, vec![]);
finish(vm, r)
}
fn b_call_kw(vm: &mut VM, argc: u8) -> Value {
note_call_line(vm);
let mut args = pop_n(vm, argc as usize);
let kwd = args.pop().unwrap();
let name = sval(&args.remove(0));
let kwargs = kw_pairs(&kwd);
let r = host::call_named(&name, args, kwargs);
finish(vm, r)
}
fn b_call_method(vm: &mut VM, argc: u8) -> Value {
let mut args = pop_n(vm, argc as usize);
let recv = args.remove(0);
let name = args.remove(0);
call_method_fused(vm, &recv, &sref(&name), args, vec![])
}
fn b_call_method_kw(vm: &mut VM, argc: u8) -> Value {
let mut args = pop_n(vm, argc as usize);
let kwd = args.pop().unwrap();
let recv = args.remove(0);
let name = args.remove(0);
call_method_fused(vm, &recv, &sref(&name), args, kw_pairs(&kwd))
}
fn call_method_fused(
vm: &mut VM,
recv: &Value,
name: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Value {
note_call_line(vm);
let user_lookup = with_host(|h| {
matches!(
h.get(recv),
Some(PyObj::Instance(_) | PyObj::Class(_) | PyObj::Super { .. })
)
});
if user_lookup {
let r = match resolve_method(recv, name) {
Ok((self_slot, callee)) => call_loaded(&self_slot, name, &callee, args, kwargs),
Err(e) => return abort_at_callee(vm, e),
};
return finish(vm, r);
}
match host::call_method(recv, name, args, kwargs) {
Err(e) if native_lookup_fails(recv, name) => abort_at_callee(vm, e),
r => finish(vm, r),
}
}
fn native_lookup_fails(recv: &Value, name: &str) -> bool {
with_host(|h| {
matches!(h.method_callee(recv, name), host::MethodCallee::Miss)
&& h.get_attr(recv, name).is_err()
})
}
fn abort_at_callee(vm: &mut VM, e: String) -> Value {
let idx = vm.ip.saturating_sub(1);
if let Some(&line) = vm.chunk.lines.get(idx) {
if line != 0 {
let mut span = crate::host::lookup_position(vm.chunk.op_hash, idx);
if span.anchor_start > span.start {
span.end = span.anchor_start;
span.anchor_start = 0;
span.anchor_end = 0;
span.suppress = false;
}
with_host(|h| h.set_cur_line_span(line, span));
}
}
with_host(|h| h.error = Some(e));
vm.ip = vm.chunk.ops.len();
Value::Undef
}
const NO_VALUE: Value = Value::Status(0);
fn is_no_value(v: &Value) -> bool {
matches!(v, Value::Status(0))
}
fn b_load_method(vm: &mut VM, _: u8) -> Value {
let name_v = vm.pop();
let recv = vm.pop();
match resolve_method(&recv, &sref(&name_v)) {
Ok((self_slot, callee)) => {
vm.push(self_slot);
vm.push(name_v);
callee
}
Err(e) => {
vm.push(NO_VALUE);
vm.push(name_v);
abort(vm, e)
}
}
}
fn resolve_method(recv: &Value, name: &str) -> Result<(Value, Value), String> {
if name.starts_with('.') {
return Ok((recv.clone(), NO_VALUE));
}
let full = || get_attr_desc(recv, name).map(|f| (NO_VALUE, f));
match with_host(|h| h.method_callee(recv, name)) {
host::MethodCallee::Protocol => full(),
host::MethodCallee::Fused => Ok((recv.clone(), NO_VALUE)),
host::MethodCallee::Method(f) => Ok((recv.clone(), f)),
host::MethodCallee::Callable(f) => Ok((NO_VALUE, f)),
host::MethodCallee::Miss if instance_dunder(recv, "__getattr__") => full(),
host::MethodCallee::Miss => {
with_host(|h| h.get_attr(recv, name)).map(|_| (recv.clone(), NO_VALUE))
}
}
}
fn call_loaded(
self_slot: &Value,
name: &str,
callee: &Value,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
if is_no_value(callee) {
host::call_method(self_slot, name, args, kwargs)
} else if is_no_value(self_slot) {
host::invoke(callee, args, kwargs)
} else {
host::call_bound_function(self_slot, name, callee, args, kwargs)
}
}
fn loaded_head(args: &mut Vec<Value>) -> (Value, Value, Value) {
let mut head = args.drain(..3);
let self_slot = head.next().unwrap();
let name = head.next().unwrap();
let callee = head.next().unwrap();
(self_slot, name, callee)
}
fn b_call_loaded(vm: &mut VM, argc: u8) -> Value {
note_call_line(vm);
let mut args = pop_n(vm, argc as usize);
let (self_slot, name, callee) = loaded_head(&mut args);
let r = call_loaded(&self_slot, &sref(&name), &callee, args, vec![]);
finish(vm, r)
}
fn b_call_loaded_kw(vm: &mut VM, argc: u8) -> Value {
note_call_line(vm);
let mut args = pop_n(vm, argc as usize);
let kwd = args.pop().unwrap();
let (self_slot, name, callee) = loaded_head(&mut args);
let r = call_loaded(&self_slot, &sref(&name), &callee, args, kw_pairs(&kwd));
finish(vm, r)
}
fn b_call_value(vm: &mut VM, argc: u8) -> Value {
note_call_line(vm);
let mut args = pop_n(vm, argc as usize);
let callable = args.remove(0);
let r = host::invoke(&callable, args, vec![]);
finish(vm, r)
}
fn b_call_value_kw(vm: &mut VM, argc: u8) -> Value {
note_call_line(vm);
let mut args = pop_n(vm, argc as usize);
let kwd = args.pop().unwrap();
let callable = args.remove(0);
let kwargs = kw_pairs(&kwd);
let r = host::invoke(&callable, args, kwargs);
finish(vm, r)
}
fn b_yieldv(vm: &mut VM, _: u8) -> Value {
let v = vm.pop();
match host::gen_yield(v) {
Ok(sent) => sent,
Err(e) => abort(vm, e),
}
}
fn b_yield_from(vm: &mut VM, _: u8) -> Value {
let v = vm.pop();
let it = if with_host(|h| matches!(h.get(&v), Some(PyObj::Instance(_)))) {
match iter_instance(&v) {
Ok(it) => it,
Err(e) => return abort(vm, e),
}
} else {
match with_host(|h| h.make_iter(&v)) {
Ok(it) => it,
Err(e) => return abort(vm, e),
}
};
match host::run_yield_from(it) {
Ok(ret) => ret,
Err(e) => abort(vm, e),
}
}
fn b_await(vm: &mut VM, _: u8) -> Value {
let awaitable = vm.pop();
match crate::async_rt::await_value(awaitable) {
Ok(v) => v,
Err(e) => abort(vm, e),
}
}
fn list_args(v: &Value) -> Vec<Value> {
with_host(|h| match h.get(v) {
Some(PyObj::List(l)) => l.clone(),
_ => Vec::new(),
})
}
fn b_call_ex(vm: &mut VM, _: u8) -> Value {
note_call_line(vm);
let kwd = vm.pop();
let argl = vm.pop();
let name = sval(&vm.pop());
if let Some(a) = check_kw_dup(vm, &with_host(|h| h.call_display_name(&name))) {
return a;
}
let r = host::call_named(&name, list_args(&argl), kw_pairs(&kwd));
finish(vm, r)
}
fn b_call_value_ex(vm: &mut VM, _: u8) -> Value {
note_call_line(vm);
let kwd = vm.pop();
let argl = vm.pop();
let callable = vm.pop();
if let Some(a) = check_kw_dup(vm, &host::callable_display_name(&callable)) {
return a;
}
let r = host::invoke(&callable, list_args(&argl), kw_pairs(&kwd));
finish(vm, r)
}
fn b_call_loaded_ex(vm: &mut VM, _: u8) -> Value {
note_call_line(vm);
let kwd = vm.pop();
let argl = vm.pop();
let callee = vm.pop();
let name = sval(&vm.pop());
let self_slot = vm.pop();
if with_host(|h| h.pending_kw_dup.is_some()) {
let resolved = if is_no_value(&callee) {
with_host(|h| h.get_attr(&self_slot, &name)).ok()
} else {
Some(callee.clone())
};
let disp = resolved.map_or_else(|| name.clone(), |f| host::callable_display_name(&f));
if let Some(a) = check_kw_dup(vm, &disp) {
return a;
}
}
let r = call_loaded(&self_slot, &name, &callee, list_args(&argl), kw_pairs(&kwd));
finish(vm, r)
}
fn b_build_args(vm: &mut VM, argc: u8) -> Value {
let flat = pop_flat(vm, argc);
let mut out = Vec::new();
let mut i = 0;
while i + 1 < flat.len() {
let spread = matches!(flat[i], Value::Int(1));
let val = flat[i + 1].clone();
if spread {
match host::iter_vec(&val) {
Ok(items) => out.extend(items),
Err(e) => return abort(vm, e),
}
} else {
out.push(val);
}
i += 2;
}
with_host(|h| h.new_list(out))
}
fn b_build_kwargs(vm: &mut VM, argc: u8) -> Value {
let flat = pop_flat(vm, argc);
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
let mut err: Option<host::KwSpreadError> = None;
let mut i = 0;
while i + 1 < flat.len() && err.is_none() {
let key = flat[i].clone();
let val = flat[i + 1].clone();
i += 2;
if !matches!(key, Value::Undef) {
let kstr = sval(&key);
let kv = with_host(|h| h.new_str(kstr.clone()));
let pk = PKey::Str(kstr.clone());
if d.contains_key(&pk) {
err = Some(host::KwSpreadError::Duplicate(kstr));
}
d.insert(pk, (kv, val));
continue;
}
let pairs = match mapping_pairs(&val) {
Ok(Some(p)) => p,
Ok(None) => {
let t = with_host(|h| h.type_name(&val));
err = Some(host::KwSpreadError::NotMapping(t));
break;
}
Err(e) => return abort(vm, e),
};
for (kv, v) in pairs {
let Some(k) = with_host(|h| h.as_str(&kv)) else {
err = Some(host::KwSpreadError::NonStrKey);
break;
};
let pk = PKey::Str(k.clone());
if d.contains_key(&pk) {
err = Some(host::KwSpreadError::Duplicate(k));
break;
}
d.insert(pk, (kv, v));
}
}
if let Some(e) = err {
with_host(|h| h.pending_kw_dup = Some(e));
}
with_host(|h| h.new_dict(d))
}
fn check_kw_dup(vm: &mut VM, disp: &str) -> Option<Value> {
let pending = with_host(|h| h.pending_kw_dup.take());
pending.map(|e| {
let msg = match e {
host::KwSpreadError::Duplicate(k) => {
format!("{disp}() got multiple values for keyword argument '{k}'")
}
host::KwSpreadError::NotMapping(t) => {
format!("{disp}() argument after ** must be a mapping, not {t}")
}
host::KwSpreadError::NonStrKey => "keywords must be strings".to_string(),
};
abort(vm, host::type_error(&msg))
})
}
fn mapping_pairs(v: &Value) -> Result<Option<Vec<(Value, Value)>>, String> {
let dict_pairs = |h: &host::PyHost, d: &Value| match h.get(d) {
Some(PyObj::Dict(m)) => Some(
m.values()
.map(|(kv, val)| (kv.clone(), val.clone()))
.collect::<Vec<_>>(),
),
_ => None,
};
enum Kind {
Pairs(Vec<(Value, Value)>),
Native,
Foreign,
NotMapping,
}
let kind = with_host(|h| match h.get(v) {
Some(PyObj::Dict(_)) => Kind::Pairs(dict_pairs(h, v).unwrap_or_default()),
Some(PyObj::MappingProxy { dict }) => Kind::Pairs(dict_pairs(h, dict).unwrap_or_default()),
Some(PyObj::Instance(inst)) => match dict_pairs(h, &inst.payload) {
Some(p) if h.class_lookup(&inst.class, "__iter__").is_none() => Kind::Pairs(p),
_ if h.class_lookup(&inst.class, "keys").is_some() => Kind::Native,
_ => Kind::NotMapping,
},
_ if h.foreign_id(v).is_some() => Kind::Foreign,
_ => Kind::NotMapping,
});
let keys = match kind {
Kind::Pairs(p) => return Ok(Some(p)),
Kind::NotMapping => return Ok(None),
Kind::Native | Kind::Foreign => match host::call_method(v, "keys", vec![], vec![]) {
Ok(k) => k,
Err(_) if matches!(kind, Kind::Foreign) => return Ok(None),
Err(e) => return Err(e),
},
};
let mut out = Vec::new();
for k in host::iter_vec(&keys)? {
let val = match kind {
Kind::Native => host::call_method(v, "__getitem__", vec![k.clone()], vec![])?,
_ => with_host(|h| h.get_item(v, &k))?,
};
out.push((k, val));
}
Ok(Some(out))
}
fn b_mkdict_ex(vm: &mut VM, argc: u8) -> Value {
let flat = pop_flat(vm, argc);
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
let mut i = 0;
while i + 2 < flat.len() {
let spread = matches!(flat[i], Value::Int(1));
let entries = if spread {
let m = &flat[i + 1];
match mapping_pairs(m) {
Ok(Some(p)) => p,
Ok(None) => {
let t = with_host(|h| h.type_name(m));
return abort(
vm,
host::type_error(&format!("'{t}' object is not a mapping")),
);
}
Err(e) => return abort(vm, e),
}
} else {
vec![(flat[i + 1].clone(), flat[i + 2].clone())]
};
for (k, v) in entries {
let cands = with_host(|h| {
if host::value_can_collapse(h, &k) {
host::dict_local_candidates(h, &d)
} else {
Vec::new()
}
});
match host::with_instance_key(&k, host::KeyRole::Dict, &cands, || {
with_host(|h| h.to_key(&k))
}) {
Ok(key) => {
host::dict_put(&mut d, key, k, v);
}
Err(e) => return abort(vm, e),
}
}
i += 3;
}
with_host(|h| h.new_dict(d))
}
fn len_result(r: &Value) -> Result<usize, String> {
let Some(n) = with_host(|h| h.big_val(r)) else {
return Err(host::type_error(&format!(
"'{}' object cannot be interpreted as an integer",
with_host(|h| h.type_name(r))
)));
};
if n.sign() == num_bigint::Sign::Minus {
return Err("ValueError: __len__() should return >= 0".to_string());
}
usize::try_from(&n)
.map_err(|_| "OverflowError: cannot fit 'int' into an index-sized integer".to_string())
}
pub(crate) fn py_bool(v: &Value) -> Result<bool, String> {
let (has_bool, has_len) = with_host(|h| match h.get(v) {
Some(PyObj::Instance(i)) => (
instance_has(h, i, "__bool__"),
instance_has(h, i, "__len__"),
),
_ => (false, false),
});
if has_bool {
return match host::call_method(v, "__bool__", vec![], vec![])? {
Value::Bool(b) => Ok(b),
x => Err(with_host(|h| {
host::type_error(&format!(
"__bool__ should return bool, returned {}",
h.tp_name(&x)
))
})),
};
}
if has_len {
let x = host::call_method(v, "__len__", vec![], vec![])?;
return Ok(len_result(&x)? != 0);
}
#[cfg(feature = "stdlib-ffi")]
if let Some(fid) = with_host(|h| h.foreign_id(v)) {
return Ok(crate::ffi::truthy(fid));
}
with_host(|h| {
if h.mv_released(v) {
return Err(host::MV_RELEASED.to_string());
}
Ok(h.truthy(v))
})
}
fn b_is_int(vm: &mut VM, _: u8) -> Value {
let v = vm.pop();
let is_int = match v {
Value::Int(_) => true,
Value::Obj(_) => with_host(|h| matches!(h.get(&v), Some(PyObj::BigInt(_)))),
_ => false,
};
Value::Bool(is_int)
}
fn b_interpolation(vm: &mut VM, _: u8) -> Value {
let format_spec = sval(&vm.pop());
let conv = match vm.pop() {
Value::Int(1) => Some('s'),
Value::Int(2) => Some('r'),
Value::Int(3) => Some('a'),
_ => None,
};
let expression = sval(&vm.pop());
let value = vm.pop();
with_host(|h| {
h.alloc(PyObj::Interpolation {
value,
expression,
conversion: conv,
format_spec,
})
})
}
fn b_template(vm: &mut VM, _: u8) -> Value {
let segments = vm.pop();
let items = match with_host(|h| h.get(&segments).cloned()) {
Some(PyObj::List(items)) => items,
_ => Vec::new(),
};
with_host(|h| {
let mut strings: Vec<String> = vec![String::new()];
let mut interpolations: Vec<Value> = Vec::new();
for seg in items {
if matches!(h.get(&seg), Some(PyObj::Interpolation { .. })) {
interpolations.push(seg);
strings.push(String::new());
} else {
let lit = match &seg {
Value::Str(s) => (**s).clone(),
_ => h.as_str(&seg).unwrap_or_default(),
};
strings.last_mut().unwrap().push_str(&lit);
}
}
h.alloc(PyObj::Template {
strings,
interpolations,
})
})
}
fn b_check_bound(vm: &mut VM, _: u8) -> Value {
let v = vm.pop();
let name = sval(&vm.pop());
if with_host(|h| matches!(h.get(&v), Some(PyObj::Unbound))) {
return abort(vm, host::unbound_local_error(&name));
}
v
}
fn b_truthy(vm: &mut VM, _: u8) -> Value {
let v = vm.pop();
match py_bool(&v) {
Ok(b) => Value::Bool(b),
Err(e) => abort(vm, e),
}
}
pub(crate) fn instance_has(h: &host::PyHost, i: &Instance, name: &str) -> bool {
if h.class_lookup(&i.class, name).is_some() {
return true;
}
if !matches!(i.payload, Value::Undef) {
if let Some(base) = h.builtin_base_of(&i.class) {
return host::base_provides(base, name);
}
}
false
}
fn is_int_value(r: &Value) -> bool {
with_host(|h| match r {
Value::Int(_) | Value::Bool(_) => true,
Value::Obj(_) => matches!(h.get(r), Some(PyObj::BigInt(_))),
_ => false,
})
}
pub(crate) fn index_dunder(v: &Value) -> Result<Option<Value>, String> {
let has = with_host(
|h| matches!(h.get(v), Some(PyObj::Instance(i)) if instance_has(h, i, "__index__")),
);
if !has {
return Ok(None);
}
let r = host::call_method(v, "__index__", vec![], vec![])?;
if !is_int_value(&r) {
let t = with_host(|h| h.type_name(&r));
return Err(host::type_error(&format!(
"__index__ returned non-int (type {t})"
)));
}
Ok(Some(r))
}
fn indexed(v: &Value) -> Result<Value, String> {
Ok(index_dunder(v)?.unwrap_or_else(|| v.clone()))
}
fn b_tostr(vm: &mut VM, _: u8) -> Value {
let v = vm.pop();
stringify(vm, &v, false)
}
fn b_displayhook(vm: &mut VM, _: u8) -> Value {
let v = vm.pop();
if matches!(v, Value::Undef) {
return Value::Undef;
}
let s = match py_repr(&v) {
Ok(s) => s,
Err(e) => return abort(vm, e),
};
with_host(|h| h.write_out(&format!("{s}\n"), false));
with_host(|h| h.set_global("_", v));
Value::Undef
}
fn stringify(vm: &mut VM, v: &Value, repr: bool) -> Value {
let is_inst = with_host(|h| matches!(h.get(v), Some(PyObj::Instance(_))));
if is_inst {
let method = if repr { "__repr__" } else { "__str__" };
let has = with_host(|h| match h.get(v) {
Some(PyObj::Instance(i)) => instance_has(h, i, method),
_ => false,
});
let has_fallback = !repr
&& with_host(|h| match h.get(v) {
Some(PyObj::Instance(i)) => instance_has(h, i, "__repr__"),
_ => false,
});
if has {
let r = host::call_method(v, method, vec![], vec![]);
return finish(vm, r);
} else if has_fallback {
let r = host::call_method(v, "__repr__", vec![], vec![]);
return finish(vm, r);
}
}
let s = if repr {
host::repr_of(v)
} else {
host::str_of(v)
};
with_host(|h| h.new_str(s))
}
fn format_field(v: &Value, conv: i64, spec: &str) -> Result<String, String> {
if conv != 0 {
let s = match conv {
2 => py_repr(v)?, 3 => host::ascii_of(&py_repr(v)?), _ => py_str(v)?, };
let sv = with_host(|h| h.new_str(s.clone()));
return apply_format_spec(&s, &sv, spec);
}
let has_format = with_host(|h| match h.get(v) {
Some(PyObj::Instance(i)) => h.class_lookup(&i.class, "__format__").is_some(),
#[cfg(feature = "stdlib-ffi")]
Some(PyObj::Foreign(_)) => true,
_ => false,
});
if has_format {
let specv = with_host(|h| h.new_str(spec.to_string()));
let r = host::call_method(v, "__format__", vec![specv], vec![])?;
return with_host(|h| match h.as_str(&r) {
Some(s) => Ok(s),
None => Err(host::type_error(&format!(
"__format__ must return a str, not {}",
h.tp_name(&r)
))),
});
}
let decimal = match spec.chars().last() {
Some(c) if c.is_ascii_alphabetic() || c == '%' => matches!(c, 'd' | 'n'),
_ => true,
};
let s = if !decimal && with_host(|h| h.int_str_limit_exceeded(v).is_some()) {
host::str_of(v)
} else {
py_str(v)?
};
apply_format_spec(&s, v, spec)
}
fn b_format(vm: &mut VM, _: u8) -> Value {
let spec = sval(&vm.pop());
let conv = match vm.pop() {
Value::Int(n) => n,
_ => 0,
};
let v = vm.pop();
match format_field(&v, conv, &spec) {
Ok(out) => with_host(|h| h.new_str(out)),
Err(e) => abort(vm, e),
}
}
fn b_mkfunc(vm: &mut VM, argc: u8) -> Value {
let mut args = pop_n(vm, argc as usize);
let def_id = match args.pop() {
Some(Value::Int(n)) => n as usize,
_ => return abort(vm, "internal: MKFUNC without func id".into()),
};
let nkw = match args.pop() {
Some(Value::Int(n)) => n,
_ => return abort(vm, "internal: MKFUNC without kwonly-default count".into()),
};
let kwonly_defaults = if nkw == -1 {
let list_items = |v: Option<Value>| {
with_host(|h| match v.as_ref().and_then(|v| h.get(v)) {
Some(PyObj::List(l)) => l.clone(),
_ => Vec::new(),
})
};
let kwonly = list_items(args.pop());
let defaults = list_items(args.pop());
args.extend(defaults);
kwonly
} else {
let split = args.len().saturating_sub(nkw as usize);
args.split_off(split)
};
let (annotations, annotate) = if args.is_empty() {
(Value::Undef, Value::Undef)
} else {
let raw = args.remove(0);
if with_host(|h| matches!(h.get(&raw), Some(PyObj::Func(_)))) {
(Value::Undef, raw)
} else {
(raw, Value::Undef)
}
};
let defaults = args;
let env = with_host(|h| h.env_capture_for(h.funcs[def_id].sees_class_scope));
with_host(|h| {
h.alloc(PyObj::Func(std::rc::Rc::new(host::FuncVal {
def_id,
module: h.cur_module(),
env: Some(env),
defaults,
kwonly_defaults,
bound: None,
owner: None,
annotations,
annotate,
})))
})
}
fn b_type_alias(vm: &mut VM, _: u8) -> Value {
let params = vm.pop();
let evaluate = vm.pop();
let name = sval(&vm.pop());
let flat = with_host(|h| match h.get(¶ms) {
Some(PyObj::List(l)) => l.clone(),
_ => Vec::new(),
});
let specs: Vec<(host::TypeVarKind, String)> = flat
.chunks(2)
.map(|pair| {
let kind = match pair[1] {
Value::Int(1) => host::TypeVarKind::TypeVarTuple,
Value::Int(2) => host::TypeVarKind::ParamSpec,
_ => host::TypeVarKind::TypeVar,
};
(kind, sval(&pair[0]))
})
.collect();
with_host(|h| {
let type_params: Vec<Value> = specs
.into_iter()
.map(|(kind, pname)| h.make_type_var(kind, pname, vec![], vec![]))
.collect();
let module = h
.read_global("__name__")
.unwrap_or_else(|| h.new_str("__main__".to_string()));
let params = h.new_tuple(type_params);
h.alloc(PyObj::TypeAlias {
name,
module,
params,
evaluate,
value: Value::Undef,
})
})
}
fn b_build_class(vm: &mut VM, _: u8) -> Value {
let kwargs_val = vm.pop();
let body_func = vm.pop();
let name = sval(&vm.pop());
let bases_val = vm.pop();
let metaclass = vm.pop();
#[cfg(feature = "stdlib-ffi")]
if with_host(|h| match h.get(&bases_val) {
Some(PyObj::List(l)) => l.iter().any(|b| h.foreign_id(b).is_some()),
_ => false,
}) {
let bases: Vec<Value> = with_host(|h| match h.get(&bases_val) {
Some(PyObj::List(l)) => l.clone(),
_ => Vec::new(),
});
let r = host::build_class_foreign(&name, bases, &body_func);
return finish(vm, r);
}
let bases: Vec<String> = with_host(|h| match h.get(&bases_val) {
Some(PyObj::List(l)) => l.iter().filter_map(|b| callable_name(h, b)).collect(),
_ => Vec::new(),
});
let meta_name = match &metaclass {
Value::Undef => None,
_ => with_host(|h| callable_name(h, &metaclass)),
};
let class_kwargs: Vec<(String, Value)> = with_host(|h| match h.get(&kwargs_val) {
Some(PyObj::Dict(d)) => d.values().map(|(k, v)| (h.str_of(k), v.clone())).collect(),
_ => Vec::new(),
});
let r = host::build_class(&name, bases, &body_func, meta_name, class_kwargs);
finish(vm, r)
}
fn b_getiter(vm: &mut VM, _: u8) -> Value {
let v = vm.pop();
if with_host(|h| matches!(h.get(&v), Some(PyObj::Instance(_)))) {
let r = iter_instance(&v);
return finish(vm, r);
}
if let Some(m) = with_host(|h| h.metaclass_method(&v, "__iter__")) {
let r = host::invoke(&m, vec![v.clone()], vec![]).and_then(|it| iter_drive(&it));
return finish(vm, r);
}
let r = with_host(|h| h.make_iter(&v));
finish(vm, r)
}
fn iter_drive(it: &Value) -> Result<Value, String> {
if with_host(|h| matches!(h.get(it), Some(PyObj::Instance(_)))) {
return iter_instance(it);
}
with_host(|h| h.make_iter(it))
}
fn iter_instance(v: &Value) -> Result<Value, String> {
if let Some(payload) = host::subclass_payload(v, "__iter__") {
return with_host(|h| h.make_iter(&payload));
}
let has_iter = with_host(|h| match h.get(v) {
Some(PyObj::Instance(i)) => h.class_lookup(&i.class, "__iter__").is_some(),
_ => false,
});
if has_iter {
let it = host::call_method(v, "__iter__", vec![], vec![])?;
if with_host(|h| {
matches!(
h.get(&it),
Some(PyObj::Iter(_)) | Some(PyObj::Generator { .. })
)
}) {
return Ok(it);
}
}
let items = host::iter_instance_items(v)?;
Ok(with_host(|h| h.new_iter_seq(items)))
}
fn b_genret(vm: &mut VM, _: u8) -> Value {
let it = vm.pop();
with_host(|h| match h.get(&it) {
Some(PyObj::Generator { id }) => h.gen_return_value(*id),
_ => Value::Undef,
})
}
fn b_foriter(vm: &mut VM, _: u8) -> Value {
let it = match vm.stack.last() {
Some(v) => v.clone(),
None => return abort(vm, "internal: FORITER with empty stack".into()),
};
match host::iter_step(&it) {
Ok(Some(v)) => {
vm.push(v);
Value::Bool(true)
}
Ok(None) => Value::Bool(false),
Err(e) => abort(vm, e),
}
}
fn b_contains(vm: &mut VM, _: u8) -> Value {
let container = vm.pop();
let item = vm.pop();
match contains_value(container, item) {
Ok(b) => Value::Bool(b),
Err(e) => abort(vm, e),
}
}
fn dict_get(dict: &Value, key: &Value) -> Result<Value, String> {
let cands = host::instance_key_candidates_for(dict, Some(key));
host::with_instance_key(key, host::KeyRole::Dict, &cands, || {
with_host(|h| h.get_item(dict, key))
})
}
pub fn contains_value(container: Value, item: Value) -> Result<bool, String> {
if let Some(r) = with_host(|h| {
(matches!(
h.get(&container),
Some(PyObj::Set(_) | PyObj::Frozenset(_) | PyObj::Dict(_))
) && h.key_cannot_collapse(&item))
.then(|| h.contains(&item, &container))
}) {
return r;
}
let view = with_host(|h| match h.get(&container) {
Some(PyObj::DictView { dict, kind }) if *kind != 1 => Some((dict.clone(), *kind == 2)),
_ => None,
});
if let Some((dict, is_items)) = view {
if !is_items {
return contains_value(dict, item);
}
let pair = with_host(|h| match h.get(&item) {
Some(PyObj::Tuple(t)) if t.len() == 2 => Some((t[0].clone(), t[1].clone())),
_ => None,
});
let Some((key, want)) = pair else {
return Ok(false);
};
if !contains_value(dict.clone(), key.clone())? {
return Ok(false);
}
let found = dict_get(&dict, &key)?;
return elem_equal(&found, &want);
}
if let Some(m) = with_host(|h| h.metaclass_method(&container, "__contains__")) {
let v = host::invoke(&m, vec![container, item], vec![])?;
return Ok(with_host(|h| h.truthy(&v)));
}
if with_host(|h| h.metaclass_method(&container, "__iter__").is_some()) {
return iter_membership(&container, &item);
}
if with_host(|h| matches!(h.get(&container), Some(PyObj::Instance(_)))) {
if let Some(payload) = host::subclass_payload(&container, "__contains__") {
return with_host(|h| h.contains(&item, &payload));
}
let has_contains = with_host(|h| match h.get(&container) {
Some(PyObj::Instance(i)) => h.class_lookup(&i.class, "__contains__").is_some(),
_ => false,
});
if has_contains {
let v = host::call_method(&container, "__contains__", vec![item], vec![])?;
return Ok(with_host(|h| h.truthy(&v)));
}
return match iter_membership(&container, &item) {
Ok(b) => Ok(b),
Err(e) if e.ends_with("object is not iterable") => {
let tn = with_host(|h| h.type_name(&container));
Err(host::type_error(&format!(
"argument of type '{tn}' is not a container or iterable"
)))
}
Err(e) => Err(e),
};
}
if with_host(|h| matches!(h.get(&container), Some(PyObj::Generator { .. }))) {
let items = host::iter_vec(&container)?;
return Ok(with_host(|h| items.iter().any(|x| h.equal(x, &item))));
}
if with_host(|h| {
matches!(
h.get(&container),
Some(PyObj::List(_)) | Some(PyObj::Tuple(_))
)
}) {
let elems = with_host(|h| match h.get(&container) {
Some(PyObj::List(l)) | Some(PyObj::Tuple(l)) => l.clone(),
_ => Vec::new(),
});
let any_instance = with_host(|h| {
matches!(h.get(&item), Some(PyObj::Instance(_)))
|| elems
.iter()
.any(|e| matches!(h.get(e), Some(PyObj::Instance(_))))
});
if any_instance {
for e in &elems {
match elem_equal(e, &item) {
Ok(true) => return Ok(true),
Ok(false) => {}
Err(err) => return Err(err),
}
}
return Ok(false);
}
}
#[cfg(feature = "stdlib-ffi")]
if let Some(id) = with_host(|h| h.foreign_id(&container)) {
return crate::ffi::contains_cb(id, &item);
}
let cands = host::instance_key_candidates_for(&container, Some(&item));
host::with_instance_key(&item, host::KeyRole::Of(&container), &cands, || {
with_host(|h| h.contains(&item, &container))
})
}
fn iter_membership(container: &Value, item: &Value) -> Result<bool, String> {
let items = host::iter_instance_items(container)?;
Ok(with_host(|h| items.iter().any(|x| h.equal(x, item))))
}
fn b_is(vm: &mut VM, _: u8) -> Value {
let b = vm.pop();
let a = vm.pop();
Value::Bool(same_object(&a, &b))
}
pub fn same_object(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Obj(x), Value::Obj(y)) => {
x == y
|| with_host(|h| match (h.get(a), h.get(b)) {
(Some(PyObj::Class(m)), Some(PyObj::Class(n))) => m == n,
(Some(PyObj::Builtin(m)), Some(PyObj::Builtin(n))) => m == n,
(Some(PyObj::Ellipsis), Some(PyObj::Ellipsis))
| (Some(PyObj::NotImplemented), Some(PyObj::NotImplemented)) => true,
#[cfg(feature = "stdlib-ffi")]
(Some(PyObj::Foreign(m)), Some(PyObj::Foreign(n))) => {
crate::ffi::same_object(*m, *n)
}
_ => false,
})
}
(Value::Undef, Value::Undef) => true,
(Value::Bool(x), Value::Bool(y)) => x == y,
(Value::Int(x), Value::Int(y)) => x == y,
(Value::Float(x), Value::Float(y)) => host::same_float(*x, *y),
_ => false,
}
}
fn b_sig_return(vm: &mut VM, _: u8) -> Value {
let v = vm.pop();
with_host(|h| h.signal = Some(host::Signal::Return(v.clone())));
vm.ip = vm.chunk.ops.len();
v
}
fn b_sig_break(vm: &mut VM, _: u8) -> Value {
with_host(|h| h.signal = Some(host::Signal::Break));
vm.ip = vm.chunk.ops.len();
Value::Undef
}
fn b_sig_continue(vm: &mut VM, _: u8) -> Value {
with_host(|h| h.signal = Some(host::Signal::Continue));
vm.ip = vm.chunk.ops.len();
Value::Undef
}
fn b_loop_body(vm: &mut VM, _: u8) -> Value {
let id = match vm.pop() {
Value::Int(n) => n as usize,
_ => return abort(vm, "internal: LOOP_BODY id".into()),
};
let body = match with_host(|h| h.try_def(id)) {
Some(t) => t.body,
None => return abort(vm, "internal: unknown loop-body id".into()),
};
if let Err(e) = host::run_chunk_on(body) {
return abort(vm, e);
}
let code = with_host(|h| match &h.signal {
Some(host::Signal::Break) => {
h.signal = None;
1
}
Some(host::Signal::Continue) => {
h.signal = None;
0
}
Some(host::Signal::Return(_)) => 2,
None => 0,
});
if code == 2 {
vm.ip = vm.chunk.ops.len();
}
Value::Int(code)
}
fn b_raise(vm: &mut VM, argc: u8) -> Value {
let exc = vm.pop();
let cause = if argc >= 2 { vm.pop() } else { Value::Undef };
let context = with_host(|h| h.exc.clone().unwrap_or(Value::Undef));
match host::raise_value(&exc) {
Ok(msg) => {
with_host(|h| {
if let Some(new_exc) = h.exc.clone() {
let ctx = match &context {
Value::Obj(_) if new_exc != context => context.clone(),
_ => Value::Undef,
};
h.set_exc_link(&new_exc, cause.clone(), ctx);
if argc >= 2 {
if let Value::Obj(id) = new_exc {
h.suppress_context.insert(id);
}
}
}
});
abort(vm, msg)
}
Err(e) => abort(vm, e),
}
}
fn b_reraise(vm: &mut VM, _: u8) -> Value {
let msg = with_host(|h| match h.exc.clone() {
Some(v) => Some(h.exc_line_of(&v).unwrap_or_else(|| "StopIteration".into())),
None => h.error.clone(),
});
match msg {
Some(m) => abort(vm, m),
None => abort(vm, "RuntimeError: No active exception to reraise".into()),
}
}
fn b_assert_fail(vm: &mut VM, _: u8) -> Value {
let msg = vm.pop();
let s = with_host(|h| {
if matches!(msg, Value::Undef) {
"AssertionError".to_string()
} else {
format!("AssertionError: {}", h.str_of(&msg))
}
});
with_host(|h| {
let m = if let Value::Undef = msg {
Value::Undef
} else {
msg.clone()
};
let args = if matches!(m, Value::Undef) {
vec![]
} else {
vec![m]
};
let e = h.alloc(PyObj::Exception {
class: "AssertionError".into(),
args,
});
h.exc = Some(e);
});
abort(vm, s)
}
fn b_dbg_line(vm: &mut VM, _: u8) -> Value {
let _line = vm.pop();
crate::dap::on_debug_line(vm);
Value::Undef
}
fn is_instance_with(h: &host::PyHost, v: &Value, name: &str) -> bool {
matches!(h.get(v), Some(PyObj::Instance(i)) if instance_has(h, i, name))
}
fn is_not_implemented(v: &Value) -> bool {
with_host(|h| matches!(h.get(v), Some(PyObj::NotImplemented)))
}
enum Dunder {
Value(Value),
NotImplemented,
Err(String),
}
fn dispatch_binop(a: &Value, b: &Value, lname: &str, rname: &str) -> Dunder {
dispatch_dunder_pair(a, b, lname, rname, true, None)
}
fn dispatch_arith(a: &Value, b: &Value, lname: &str, rname: &str) -> Dunder {
dispatch_dunder_pair(a, b, lname, rname, false, None)
}
fn dispatch_ternary_pow(a: &Value, b: &Value, m: &Value) -> Dunder {
dispatch_dunder_pair(a, b, "__pow__", "__rpow__", false, Some(m))
}
fn dispatch_dunder_pair(
a: &Value,
b: &Value,
lname: &str,
rname: &str,
same_type_reflects: bool,
modulus: Option<&Value>,
) -> Dunder {
let args = |first: &Value| -> Vec<Value> {
std::iter::once(first.clone()).chain(modulus.cloned()).collect()
};
let (has_fwd, mut has_refl, refl_first) = with_host(|h| {
let has_fwd = is_instance_with(h, a, lname);
let same = same_instance_type(h, a, b);
if !same_type_reflects && same {
return (has_fwd, false, false);
}
(
has_fwd,
is_instance_with(h, b, rname),
reflected_first(h, a, b, rname),
)
});
if refl_first {
match host::call_method(b, rname, args(a), vec![]) {
Ok(v) if is_not_implemented(&v) => {}
Ok(v) => return Dunder::Value(v),
Err(e) => return Dunder::Err(e),
}
has_refl = false;
}
if has_fwd {
match host::call_method(a, lname, args(b), vec![]) {
Ok(v) if is_not_implemented(&v) => {}
Ok(v) => return Dunder::Value(v),
Err(e) => return Dunder::Err(e),
}
}
if has_refl {
match host::call_method(b, rname, args(a), vec![]) {
Ok(v) if is_not_implemented(&v) => {}
Ok(v) => return Dunder::Value(v),
Err(e) => return Dunder::Err(e),
}
}
Dunder::NotImplemented
}
fn same_instance_type(h: &host::PyHost, a: &Value, b: &Value) -> bool {
match (h.get(a), h.get(b)) {
(Some(PyObj::Instance(x)), Some(PyObj::Instance(y))) => x.class == y.class,
_ => false,
}
}
fn reflected_first(h: &host::PyHost, a: &Value, b: &Value, rname: &str) -> bool {
let (Some(PyObj::Instance(ai)), Some(PyObj::Instance(bi))) = (h.get(a), h.get(b)) else {
return false;
};
if ai.class == bi.class {
return false;
}
if !h.mro_rc(&bi.class).iter().any(|c| c == &ai.class) {
return false;
}
let Some(bm) = h.class_lookup(&bi.class, rname) else {
return false;
};
match h.class_lookup(&ai.class, rname) {
None => true,
Some(am) => !identity_eq(&am, &bm),
}
}
pub(crate) fn elem_equal(elem: &Value, target: &Value) -> Result<bool, String> {
if identity_eq(elem, target) {
return Ok(true);
}
let needs_dunder = with_host(|h| {
matches!(h.get(elem), Some(PyObj::Instance(_)))
|| matches!(h.get(target), Some(PyObj::Instance(_)))
});
if needs_dunder {
return match dispatch_binop(elem, target, "__eq__", "__eq__") {
Dunder::Value(v) => Ok(with_host(|h| h.truthy(&v))),
Dunder::Err(e) => Err(e),
Dunder::NotImplemented => Ok(with_host(|h| h.equal(elem, target))),
};
}
if let Some(r) = container_user_eq(elem, target)? {
return Ok(r);
}
Ok(with_host(|h| h.equal(elem, target)))
}
const USER_EQ_DEPTH: u32 = 32;
fn needs_user_eq(h: &host::PyHost, v: &Value, depth: u32) -> bool {
if depth == 0 {
return false;
}
match h.get(v) {
Some(PyObj::Instance(_)) => true,
#[cfg(feature = "stdlib-ffi")]
Some(PyObj::Foreign(_)) => true,
Some(PyObj::List(items)) | Some(PyObj::Tuple(items)) => {
items.iter().any(|e| needs_user_eq(h, e, depth - 1))
}
Some(PyObj::Deque { items, .. }) => items.iter().any(|e| needs_user_eq(h, e, depth - 1)),
_ => false,
}
}
fn container_user_eq(a: &Value, b: &Value) -> Result<Option<bool>, String> {
let seq = with_host(|h| {
let (x, y) = match (h.get(a), h.get(b)) {
(Some(PyObj::List(x)), Some(PyObj::List(y)))
| (Some(PyObj::Tuple(x)), Some(PyObj::Tuple(y))) => (x.clone(), y.clone()),
(Some(PyObj::Deque { items: x, .. }), Some(PyObj::Deque { items: y, .. })) => {
(x.iter().cloned().collect(), y.iter().cloned().collect())
}
_ => return None,
};
let needed = x
.iter()
.chain(y.iter())
.any(|e| needs_user_eq(h, e, USER_EQ_DEPTH));
needed.then_some((x, y))
});
if let Some((x, y)) = seq {
if x.len() != y.len() {
return Ok(Some(false));
}
for (p, q) in x.iter().zip(y.iter()) {
if !elem_pair_equal(p, q)? {
return Ok(Some(false));
}
}
return Ok(Some(true));
}
dict_user_eq(a, b)
}
fn dict_user_eq(a: &Value, b: &Value) -> Result<Option<bool>, String> {
let needed = with_host(|h| match (h.get(a), h.get(b)) {
(Some(PyObj::Dict(x)), Some(PyObj::Dict(y))) => x
.values()
.chain(y.values())
.any(|(_, v)| needs_user_eq(h, v, USER_EQ_DEPTH)),
_ => false,
});
if !needed {
return Ok(None);
}
let aligned = host::align_operand(a, b)?;
let bref = aligned.as_ref().unwrap_or(b);
let paired = with_host(|h| match (h.get(a), h.get(bref)) {
(Some(PyObj::Dict(x)), Some(PyObj::Dict(y))) if x.len() == y.len() => {
let mut out = Vec::with_capacity(x.len());
for (k, (_, xv)) in x.iter() {
let (_, yv) = y.get(k)?;
out.push((xv.clone(), yv.clone()));
}
Some(out)
}
_ => None,
});
let paired = match paired {
Some(p) => p,
None => return Ok(Some(false)),
};
for (xv, yv) in &paired {
if !elem_pair_equal(xv, yv)? {
return Ok(Some(false));
}
}
Ok(Some(true))
}
fn elem_pair_equal(p: &Value, q: &Value) -> Result<bool, String> {
if matches!((p, q), (Value::Obj(i), Value::Obj(j)) if i == j) {
return Ok(true);
}
let r = numeric_hook(NumOp::Eq, p, q)?;
Ok(with_host(|h| h.truthy(&r)))
}
fn try_binop_dunder(
a: &Value,
b: &Value,
lname: &str,
rname: &str,
) -> Option<Result<Value, String>> {
if lname == "__mod__"
&& with_host(|h| {
matches!(a, Value::Str(_))
|| matches!(
h.get(a),
Some(PyObj::Str(_)) | Some(PyObj::Bytes(_)) | Some(PyObj::Bytearray(_))
)
})
{
return None;
}
let a_owned = host::subclass_operand(a, lname);
let b_owned = host::subclass_operand(b, rname);
let a = &a_owned;
let b = &b_owned;
let involved = with_host(|h| {
matches!(h.get(a), Some(PyObj::Instance(_))) || matches!(h.get(b), Some(PyObj::Instance(_)))
});
if !involved {
return None;
}
match dispatch_arith(a, b, lname, rname) {
Dunder::Value(v) => Some(Ok(v)),
Dunder::Err(e) => Some(Err(e)),
Dunder::NotImplemented => {
let sym = binop_symbol(lname);
Some(Err(unsupported_operand(sym, a, b)))
}
}
}
fn binop_symbol(lname: &str) -> &'static str {
match lname {
"__add__" => "+",
"__sub__" => "-",
"__mul__" => "*",
"__truediv__" => "/",
"__floordiv__" => "//",
"__mod__" => "%",
"__pow__" => "** or pow()",
"__matmul__" => "@",
"__and__" => "&",
"__or__" => "|",
"__xor__" => "^",
"__lshift__" => "<<",
"__rshift__" => ">>",
"__divmod__" => "divmod()",
_ => "?",
}
}
fn repeat_with_index(a: &Value, b: &Value) -> bool {
let has_index = |h: &host::PyHost, v: &Value| matches!(h.get(v), Some(PyObj::Instance(i)) if instance_has(h, i, "__index__"));
with_host(|h| {
(h.is_sequence_for_repeat(a) && has_index(h, b))
|| (h.is_sequence_for_repeat(b) && has_index(h, a))
})
}
fn sequence_op_error(op: NumOp, a: &Value, b: &Value) -> Option<String> {
fn seq_name(h: &host::PyHost, v: &Value) -> Option<&'static str> {
if matches!(v, Value::Str(_)) {
return Some("str");
}
Some(match h.get(v)? {
PyObj::List(_) => "list",
PyObj::Tuple(_) => "tuple",
PyObj::Str(_) => "str",
PyObj::Bytes(_) => "bytes",
PyObj::Bytearray(_) => "bytearray",
_ => return None,
})
}
let int_like = |h: &host::PyHost, v: &Value| {
matches!(v, Value::Int(_) | Value::Bool(_)) || matches!(h.get(v), Some(PyObj::BigInt(_)))
};
with_host(|h| match op {
NumOp::Mul => {
let other = match (seq_name(h, a), seq_name(h, b)) {
(Some(_), _) if !int_like(h, b) => b,
(_, Some(_)) if !int_like(h, a) => a,
_ => return None,
};
Some(host::type_error(&format!(
"can't multiply sequence by non-int of type '{}'",
h.tp_name(other)
)))
}
NumOp::Add => {
let t = seq_name(h, a)?;
let other = h.tp_name(b);
Some(host::type_error(&match t {
"bytes" | "bytearray" => format!("can't concat {other} to {t}"),
_ => format!("can only concatenate {t} (not \"{other}\") to {t}"),
}))
}
_ => None,
})
}
fn unsupported_operand(sym: &str, a: &Value, b: &Value) -> String {
with_host(|h| {
host::type_error(&format!(
"unsupported operand type(s) for {sym}: '{}' and '{}'",
h.tp_name(a),
h.tp_name(b)
))
})
}
fn identity_eq(a: &Value, b: &Value) -> bool {
host::value_identity(a, b)
}
fn binop_tag_dunders(tag: i64) -> Option<(&'static str, &'static str)> {
use host::binop::*;
Some(match tag {
DIV => ("__truediv__", "__rtruediv__"),
FLOORDIV => ("__floordiv__", "__rfloordiv__"),
MOD => ("__mod__", "__rmod__"),
POW => ("__pow__", "__rpow__"),
MATMUL => ("__matmul__", "__rmatmul__"),
BITAND => ("__and__", "__rand__"),
BITOR => ("__or__", "__ror__"),
BITXOR => ("__xor__", "__rxor__"),
SHL => ("__lshift__", "__rlshift__"),
SHR => ("__rshift__", "__rrshift__"),
_ => return None,
})
}
fn numop_dunders(op: NumOp) -> Option<(&'static str, &'static str)> {
use NumOp::*;
Some(match op {
Add => ("__add__", "__radd__"),
Sub => ("__sub__", "__rsub__"),
Mul => ("__mul__", "__rmul__"),
Div => ("__truediv__", "__rtruediv__"),
Mod => ("__mod__", "__rmod__"),
Pow => ("__pow__", "__rpow__"),
Eq => ("__eq__", "__eq__"),
Ne => ("__ne__", "__ne__"),
Lt => ("__lt__", "__gt__"),
Le => ("__le__", "__ge__"),
Gt => ("__gt__", "__lt__"),
Ge => ("__ge__", "__le__"),
Neg => return None,
})
}
fn b_binop(vm: &mut VM, _: u8) -> Value {
let b = vm.pop();
let a = vm.pop();
let tag = match vm.pop() {
Value::Int(n) => n,
_ => return abort(vm, "internal: BINOP tag".into()),
};
if tag == host::binop::BITOR {
if let Some(r) = mapping_subclass_or(&a, &b) {
return finish(vm, r);
}
}
let (a, b) = match binop_tag_dunders(tag) {
Some((l, r)) => (host::subclass_operand(&a, l), host::subclass_operand(&b, r)),
None => (a, b),
};
if tag == host::binop::BITAND || tag == host::binop::BITOR {
let op = if tag == host::binop::BITAND { '&' } else { '|' };
if let Some(res) = counter_binop(&a, &b, op) {
return finish(vm, res);
}
}
if let Some((l, r)) = binop_tag_dunders(tag) {
if let Some(res) = try_binop_dunder(&a, &b, l, r) {
return finish(vm, res);
}
}
if let Some(res) = view_setop_for_tag(tag).and_then(|op| dictview_setop(op, &a, &b)) {
return finish(vm, res);
}
if tag == host::binop::MOD && with_host(|h| matches!(h.get(&a), Some(PyObj::Str(_)))) {
let r = str_percent_format(&a, &b);
return finish(vm, r);
}
if tag == host::binop::MOD {
let kind = with_host(|h| match h.get(&a) {
Some(PyObj::Bytes(_)) => Some(false),
Some(PyObj::Bytearray(_)) => Some(true),
_ => None,
});
if let Some(is_ba) = kind {
let r = bytes_percent_format(&a, &b, is_ba);
return finish(vm, r);
}
}
if matches!(
tag,
host::binop::BITAND | host::binop::BITOR | host::binop::BITXOR
) {
match host::align_operand(&a, &b) {
Ok(Some(b2)) => return finish(vm, with_host(|h| h.binop(tag, &a, &b2))),
Ok(None) => {}
Err(e) => return finish(vm, Err(e)),
}
}
let r = with_host(|h| h.binop(tag, &a, &b));
finish(vm, r)
}
fn iop_dunder(tag: i64) -> Option<&'static str> {
use host::iop::*;
Some(match tag {
ADD => "__iadd__",
SUB => "__isub__",
MUL => "__imul__",
DIV => "__itruediv__",
FLOORDIV => "__ifloordiv__",
MOD => "__imod__",
POW => "__ipow__",
MATMUL => "__imatmul__",
BITAND => "__iand__",
BITOR => "__ior__",
BITXOR => "__ixor__",
SHL => "__ilshift__",
SHR => "__irshift__",
_ => return None,
})
}
fn iop_symbol(tag: i64) -> &'static str {
use host::iop::*;
match tag {
ADD => "+=",
SUB => "-=",
MUL => "*=",
DIV => "/=",
FLOORDIV => "//=",
MOD => "%=",
POW => "**=",
MATMUL => "@=",
BITAND => "&=",
BITOR => "|=",
BITXOR => "^=",
SHL => "<<=",
SHR => ">>=",
_ => "?",
}
}
fn as_inplace_error(tag: i64, e: String) -> String {
let Some(rest) = e.strip_prefix("TypeError: unsupported operand type(s) for ") else {
return e;
};
match rest.split_once(": ") {
Some((_, tail)) => format!(
"TypeError: unsupported operand type(s) for {}: {tail}",
iop_symbol(tag)
),
None => e,
}
}
fn inplace_binary_fallback(tag: i64, a: &Value, b: &Value) -> Result<Value, String> {
inplace_binary_inner(tag, a, b).map_err(|e| as_inplace_error(tag, e))
}
fn inplace_binary_inner(tag: i64, a: &Value, b: &Value) -> Result<Value, String> {
use host::iop;
let btag = match tag {
iop::ADD => return numeric_hook(NumOp::Add, a, b),
iop::SUB => return numeric_hook(NumOp::Sub, a, b),
iop::MUL => return numeric_hook(NumOp::Mul, a, b),
iop::DIV => host::binop::DIV,
iop::FLOORDIV => host::binop::FLOORDIV,
iop::MOD => host::binop::MOD,
iop::POW => host::binop::POW,
iop::MATMUL => host::binop::MATMUL,
iop::BITAND => host::binop::BITAND,
iop::BITOR => host::binop::BITOR,
iop::BITXOR => host::binop::BITXOR,
iop::SHL => host::binop::SHL,
iop::SHR => host::binop::SHR,
_ => return Err(host::type_error("internal: INPLACE tag")),
};
if let Some((l, r)) = binop_tag_dunders(btag) {
if let Some(res) = try_binop_dunder(a, b, l, r) {
return res;
}
}
if let Some(res) = view_setop_for_tag(btag).and_then(|op| dictview_setop(op, a, b)) {
return res;
}
if btag == host::binop::MOD && with_host(|h| matches!(h.get(a), Some(PyObj::Str(_)))) {
return str_percent_format(a, b);
}
if btag == host::binop::MOD {
let is_ba = with_host(|h| match h.get(a) {
Some(PyObj::Bytes(_)) => Some(false),
Some(PyObj::Bytearray(_)) => Some(true),
_ => None,
});
if let Some(is_ba) = is_ba {
return bytes_percent_format(a, b, is_ba);
}
}
if matches!(
btag,
host::binop::BITAND | host::binop::BITOR | host::binop::BITXOR
) {
if let Some(b2) = host::align_operand(a, b)? {
return with_host(|h| h.binop(btag, a, &b2));
}
}
with_host(|h| h.binop(btag, a, b))
}
fn inplace_builtin(tag: i64, a: &Value, b: &Value) -> Option<Result<Value, String>> {
use host::iop;
if !host::is_heap(a) {
return None;
}
let payload = with_host(|h| match h.get(a) {
Some(PyObj::Instance(inst)) if !matches!(inst.payload, Value::Undef) => {
Some(inst.payload.clone())
}
_ => None,
});
if let Some(p) = payload {
return match inplace_builtin(tag, &p, b) {
Some(Ok(_)) => Some(Ok(a.clone())),
other => other,
};
}
if let Some(r) = counter_inplace(tag, a, b) {
return Some(r);
}
if with_host(|h| matches!(h.get(a), Some(PyObj::List(_)))) {
match tag {
iop::ADD => {
let items = match host::iter_vec(b) {
Ok(v) => v,
Err(_) => {
return Some(Err(with_host(|h| {
host::type_error(&format!(
"'{}' object is not iterable",
h.type_name(b)
))
})));
}
};
with_host(|h| {
if let Some(PyObj::List(l)) = h.get_mut(a) {
l.extend(items);
}
});
return Some(Ok(a.clone()));
}
iop::MUL => {
let n = match inplace_repeat_count(b)? {
Ok(n) => n,
Err(e) => return Some(Err(e)),
};
with_host(|h| {
if let Some(PyObj::List(l)) = h.get_mut(a) {
let base = l.clone();
l.clear();
for _ in 0..n {
l.extend(base.clone());
}
}
});
return Some(Ok(a.clone()));
}
_ => return None,
}
}
if with_host(|h| matches!(h.get(a), Some(PyObj::Deque { .. }))) {
match tag {
iop::ADD => {
return Some(deque_method(a, "extend", std::slice::from_ref(b)).map(|_| a.clone()));
}
iop::MUL => {
let n = match inplace_repeat_count(b)? {
Ok(n) => n as i64,
Err(e) => return Some(Err(e)),
};
return Some(with_host(|h| match h.get_mut(a) {
Some(PyObj::Deque { items, maxlen }) => {
*items = host::deque_repeat(items, *maxlen, n)?;
Ok(a.clone())
}
_ => Err(host::type_error("not a deque")),
}));
}
_ => return None,
}
}
if with_host(|h| matches!(h.get(a), Some(PyObj::Bytearray(_)))) {
match tag {
iop::ADD => match as_bytes_object(b) {
Err(e) => return Some(Err(e)),
Ok(Some(bytes)) => {
with_host(|h| {
if let Some(PyObj::Bytearray(v)) = h.get_mut(a) {
v.extend_from_slice(&bytes);
}
});
return Some(Ok(a.clone()));
}
Ok(None) => {
return Some(Err(with_host(|h| {
host::type_error(&format!("can't concat {} to bytearray", h.tp_name(b)))
})));
}
},
iop::MUL => {
let n = match inplace_repeat_count(b)? {
Ok(n) => n,
Err(e) => return Some(Err(e)),
};
with_host(|h| {
if let Some(PyObj::Bytearray(v)) = h.get_mut(a) {
let base = v.clone();
v.clear();
for _ in 0..n {
v.extend_from_slice(&base);
}
}
});
return Some(Ok(a.clone()));
}
_ => return None,
}
}
if with_host(|h| matches!(h.get(a), Some(PyObj::Dict(_)))) && tag == iop::BITOR {
return Some(dict_update(a, Some(b), &[]).map(|()| a.clone()));
}
let is_set = with_host(|h| matches!(h.get(a), Some(PyObj::Set(_))));
if is_set && matches!(tag, iop::BITOR | iop::BITAND | iop::SUB | iop::BITXOR) {
let aligned = match host::align_operand(a, b) {
Ok(v) => v,
Err(e) => return Some(Err(e)),
};
let b = aligned.as_ref().unwrap_or(b);
let y = match with_host(|h| h.setmap_of(b)) {
Some(y) => y,
None => return Some(Err(unsupported_operand(iop_symbol(tag), a, b))),
};
if tag == iop::BITAND && with_host(|h| h.set_intersection_update(a, b)) {
return Some(Ok(a.clone()));
}
let y: IndexMap<PKey, Value> = match with_host(|h| h.set_entries_in_order(b)) {
Some(entries) if tag == iop::BITXOR => entries.into_iter().collect(),
_ => y,
};
let merge = match tag {
iop::BITOR => set_merge_source(b).map(|m| (m, with_host(|h| h.set_layout_of(a)))),
_ => None,
};
with_host(|h| {
if let Some(PyObj::Set(x)) = h.get_mut(a) {
match tag {
iop::BITOR => {
for (k, v) in y {
x.entry(k).or_insert(v);
}
}
iop::BITAND => {
x.retain(|k, _| y.contains_key(k));
}
iop::SUB => {
x.retain(|k, _| !y.contains_key(k));
}
_ => {
for (k, v) in y {
if x.contains_key(&k) {
x.shift_remove(&k);
} else {
x.insert(k, v);
}
}
}
}
}
});
if let Some(((incoming, src), Some(before))) = merge {
with_host(|h| h.set_relayout_after_merge(a, (&before.0, before.1), incoming, src_ref(&src)));
}
return Some(Ok(a.clone()));
}
None
}
fn inplace_repeat_count(b: &Value) -> Option<Result<usize, String>> {
let count = match index_dunder(b) {
Ok(v) => v.unwrap_or_else(|| b.clone()),
Err(e) => return Some(Err(e)),
};
match with_host(|h| h.index_fit(&count)) {
host::IndexFit::Fits(n) => Some(Ok(n.max(0) as usize)),
host::IndexFit::TooLarge(_) => Some(Err(format!("OverflowError: {}", host::INDEX_OVERFLOW))),
host::IndexFit::NotInt => None,
}
}
fn b_inplace(vm: &mut VM, _: u8) -> Value {
let b = vm.pop();
let a = vm.pop();
let tag = match vm.pop() {
Value::Int(n) => n,
_ => return abort(vm, "internal: INPLACE tag".into()),
};
if let Some(iname) = iop_dunder(tag) {
if with_host(|h| is_instance_with(h, &a, iname)) {
match host::call_method(&a, iname, vec![b.clone()], vec![]) {
Ok(v) if is_not_implemented(&v) => {
return finish(vm, inplace_binary_fallback(tag, &a, &b));
}
r => return finish(vm, r),
}
}
}
if let Some(r) = inplace_builtin(tag, &a, &b) {
return finish(vm, r);
}
finish(vm, inplace_binary_fallback(tag, &a, &b))
}
fn str_percent_format(fmt_val: &Value, args: &Value) -> Result<Value, String> {
let items: Vec<Value> = with_host(|h| match h.get(args) {
Some(PyObj::Tuple(t)) => t.clone(),
Some(PyObj::Dict(d)) => d.values().map(|(_, v)| v.clone()).collect(),
_ => vec![args.clone()],
});
let mut premap: std::collections::HashMap<u32, (String, String, String)> =
std::collections::HashMap::new();
for it in &items {
let Value::Obj(id) = it else { continue };
if premap.contains_key(id) {
continue;
}
let needs = with_host(|h| {
matches!(
h.get(it),
Some(PyObj::Instance(_))
| Some(PyObj::List(_))
| Some(PyObj::Tuple(_))
| Some(PyObj::Dict(_))
| Some(PyObj::Set(_))
| Some(PyObj::Frozenset(_))
) || h.metaclass_method(it, "__repr__").is_some()
|| h.metaclass_method(it, "__str__").is_some()
});
if !needs {
continue;
}
let s = py_str(it)?;
let r = py_repr(it)?;
let a = host::ascii_of(&r);
premap.insert(*id, (s, r, a));
}
let fmt = with_host(|h| match h.get(fmt_val) {
Some(PyObj::Str(s)) => s.clone(),
_ => String::new(),
});
with_host(|h| h.str_format_percent(&fmt, args, &premap))
}
fn bytes_percent_format(fmt_val: &Value, args: &Value, is_ba: bool) -> Result<Value, String> {
let items: Vec<Value> = with_host(|h| match h.get(args) {
Some(PyObj::Tuple(t)) => t.clone(),
Some(PyObj::Dict(d)) => d.values().map(|(_, v)| v.clone()).collect(),
_ => vec![args.clone()],
});
let mut premap: std::collections::HashMap<u32, Vec<u8>> = std::collections::HashMap::new();
for it in &items {
let Value::Obj(id) = it else { continue };
if premap.contains_key(id) {
continue;
}
let has = with_host(
|h| matches!(h.get(it), Some(PyObj::Instance(i)) if instance_has(h, i, "__bytes__")),
);
if !has {
continue;
}
let res = host::call_method(it, "__bytes__", vec![], vec![])?;
let bytes = with_host(|h| match h.get(&res) {
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => Some(b.clone()),
_ => None,
});
match bytes {
Some(b) => {
premap.insert(*id, b);
}
None => {
return Err(host::type_error(&format!(
"__bytes__ returned non-bytes (type {})",
with_host(|h| h.type_name(&res))
)))
}
}
}
let fmt = with_host(|h| match h.get(fmt_val) {
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => b.clone(),
_ => vec![],
});
with_host(|h| h.bytes_format_percent(&fmt, args, is_ba, &premap))
}
fn b_unary(vm: &mut VM, _: u8) -> Value {
let v = vm.pop();
let tag = match vm.pop() {
Value::Int(n) => n,
_ => return abort(vm, "internal: UNARY tag".into()),
};
let dunder = match tag {
host::unop::INVERT => "__invert__",
host::unop::POS => "__pos__",
_ => "",
};
if !dunder.is_empty()
&& with_host(
|h| matches!(h.get(&v), Some(PyObj::Instance(i)) if instance_has(h, i, dunder)),
)
{
let r = host::call_method(&v, dunder, vec![], vec![]);
return finish(vm, r);
}
#[cfg(feature = "stdlib-ffi")]
{
let func = match tag {
host::unop::INVERT => "invert",
host::unop::POS => "pos",
_ => "",
};
if !func.is_empty() && with_host(|h| h.foreign_id(&v).is_some()) {
return finish(vm, crate::ffi::unary_op_cb(func, &v));
}
}
let r = with_host(|h| h.unary(tag, &v));
finish(vm, r)
}
fn b_import(vm: &mut VM, _: u8) -> Value {
let name = sval(&vm.pop());
let r = host::import_module(&name);
finish(vm, r)
}
fn b_import_from(vm: &mut VM, _: u8) -> Value {
let name = sval(&vm.pop());
let module = vm.pop();
let mname = with_host(|h| match h.get(&module) {
Some(PyObj::Module { name, .. }) => Some(name.clone()),
_ => None,
});
let orig = match with_host(|h| h.get_attr(&module, &name)) {
Ok(v) => return finish(vm, Ok(v)),
Err(e) => e,
};
if let Some(pkg) = &mname {
let sub = format!("{pkg}.{name}");
match host::import_module(&sub) {
Ok(m) => return finish(vm, Ok(m)),
Err(e) => {
let sub_absent = e.contains("No module named")
&& (e.contains(&format!("'{sub}'")) || e.contains(&format!("'{name}'")));
if !sub_absent {
return finish(vm, Err(e));
}
}
}
return finish(
vm,
Err(format!(
"ImportError: cannot import name '{name}' from '{pkg}'"
)),
);
}
finish(vm, Err(orig))
}
fn b_try_annotation(vm: &mut VM, _: u8) -> Value {
let thunk = vm.pop();
let key = vm.pop();
let dict = vm.pop();
match host::invoke(&thunk, vec![], vec![]) {
Ok(v) => {
let _ = with_host(|h| h.set_item(&dict, &key, v));
}
Err(e) if e.contains("NameError") => {
with_host(|h| {
h.take_error();
});
}
Err(e) => return abort(vm, e),
}
Value::Undef
}
fn b_import_relative(vm: &mut VM, _: u8) -> Value {
let name = sval(&vm.pop());
let modpart = sval(&vm.pop());
let level = match vm.pop() {
Value::Int(n) if n > 0 => n as usize,
_ => 1,
};
let r = host::import_relative(level, &modpart, &name);
finish(vm, r)
}
fn b_import_star(vm: &mut VM, _: u8) -> Value {
let module = vm.pop();
let bindings = match with_host(|h| h.import_star_bindings(&module)) {
Ok(b) => b,
Err(e) => return abort(vm, e),
};
with_host(|h| {
for (name, val) in bindings {
h.set_name(&name, val);
}
});
Value::Undef
}
fn unpack_reports_length(v: &Value) -> bool {
with_host(|h| {
if let Some(PyObj::Instance(_)) = h.get(v) {
return false;
}
matches!(
h.get(v),
Some(PyObj::List(_)) | Some(PyObj::Tuple(_)) | Some(PyObj::Dict(_))
)
})
}
fn b_unpack(vm: &mut VM, _: u8) -> Value {
let star_idx = match vm.pop() {
Value::Int(n) => n,
_ => -1,
};
let count = match vm.pop() {
Value::Int(n) => n as usize,
_ => 0,
};
let iterable = vm.pop();
let items = match host::iter_vec(&iterable) {
Ok(v) => v,
Err(e) => return abort(vm, e),
};
let ordered: Vec<Value> = if star_idx < 0 {
if items.len() != count {
let msg = if items.len() < count {
format!(
"ValueError: not enough values to unpack (expected {count}, got {})",
items.len()
)
} else if unpack_reports_length(&iterable) {
format!(
"ValueError: too many values to unpack (expected {count}, got {})",
items.len()
)
} else {
format!("ValueError: too many values to unpack (expected {count})")
};
return abort(vm, msg);
}
items
} else {
let si = star_idx as usize;
let before = si;
let after = count - si - 1;
if items.len() < before + after {
return abort(
vm,
format!(
"ValueError: not enough values to unpack (expected at least {}, got {})",
before + after,
items.len()
),
);
}
let mid = &items[before..items.len() - after];
let mid_list = with_host(|h| h.new_list(mid.to_vec()));
let mut ordered: Vec<Value> = Vec::with_capacity(count);
ordered.extend_from_slice(&items[..before]);
ordered.push(mid_list);
ordered.extend_from_slice(&items[items.len() - after..]);
ordered
};
if ordered.is_empty() {
return Value::Undef;
}
for it in ordered[1..].iter().rev().cloned() {
vm.push(it);
}
ordered[0].clone()
}
fn b_try(vm: &mut VM, _: u8) -> Value {
let id = match vm.pop() {
Value::Int(n) => n as usize,
_ => return abort(vm, "internal: TRY id".into()),
};
let td = match with_host(|h| h.try_def(id)) {
Some(t) => t,
None => return abort(vm, "internal: unknown try id".into()),
};
let mut pending: Option<String> = None;
let mut handled_star = false;
let entry_exc = with_host(|h| h.exc.clone());
let body_res = host::run_chunk_on(td.body.clone());
let signal_after_body = with_host(|h| h.signal.is_some());
match body_res {
Ok(_) => {
if !signal_after_body {
if let Some(els) = &td.orelse {
if let Err(e) = host::run_chunk_on(els.clone()) {
pending = Some(e);
}
}
}
}
Err(e) => {
let exc = with_host(|h| {
let consistent = h
.exc
.clone()
.and_then(|x| h.exc_line_of(&x))
.map(|line| line == e)
.unwrap_or(false);
if consistent {
return h.exc.clone().unwrap();
}
let context = h.exc.clone().unwrap_or(Value::Undef);
let new = synth_exc(h, &e);
let ctx = match &context {
Value::Obj(_) if new != context => context,
_ => Value::Undef,
};
h.set_exc_link(&new, Value::Undef, ctx);
h.exc = Some(new.clone());
new
});
if td.handlers.iter().any(|h| h.star) {
pending = run_star_handlers(&td, &exc, &entry_exc);
handled_star = true;
}
let mut handled = handled_star;
for hd in td.handlers.iter().filter(|_| !handled_star) {
let (type_chunk, bind, hbody) = (&hd.typ, &hd.name, &hd.body);
let matches = match type_chunk {
None => true,
Some(tc) => {
let tv = host::run_chunk_on(tc.clone()).unwrap_or(Value::Undef);
with_host(|h| exc_matches(h, &exc, &tv))
}
};
if matches {
if let Some(name) = bind {
with_host(|h| h.set_name(name, exc.clone()));
}
with_host(|h| {
h.error = None;
h.exc = Some(exc.clone());
h.capture_exc_tb(&exc);
h.traceback.clear();
});
let hres = host::run_chunk_on(hbody.clone());
match hres {
Ok(_) => with_host(|h| h.exc = entry_exc.clone()),
Err(e2) => pending = Some(e2),
}
if let Some(name) = bind {
with_host(|h| {
let _ = h.del_name(name);
});
}
handled = true;
break;
}
}
if !handled {
pending = Some(e);
}
}
}
if let Some(fin) = &td.finalbody {
let sig_before = with_host(|h| h.signal.take());
match host::run_chunk_on(fin.clone()) {
Ok(_) => {
if with_host(|h| h.signal.is_none()) {
with_host(|h| h.signal = sig_before);
}
}
Err(e) => {
pending = Some(e);
}
}
}
if let Some(e) = pending {
return abort(vm, e);
}
if with_host(|h| h.signal.is_some()) {
vm.ip = vm.chunk.ops.len();
}
Value::Undef
}
fn run_star_handlers(td: &host::TryDef, exc: &Value, entry_exc: &Option<Value>) -> Option<String> {
with_host(|h| {
h.capture_exc_tb(exc);
h.traceback.clear();
});
let mut rest = Some(exc.clone());
let mut raised: Vec<Value> = Vec::new();
for hd in &td.handlers {
let Some(cur) = rest.clone() else { break };
let Some(tc) = &hd.typ else { continue };
let tv = host::run_chunk_on(tc.clone()).unwrap_or(Value::Undef);
let (matched, remainder) = match excgroup::eg_match(&cur, &tv) {
Ok(pair) => pair,
Err(e) => return Some(e),
};
let Some(matched) = matched else { continue };
rest = remainder;
if let Some(name) = &hd.name {
with_host(|h| h.set_name(name, matched.clone()));
}
with_host(|h| {
h.error = None;
h.exc = Some(matched.clone());
h.traceback.clear();
});
match host::run_chunk_on(hd.body.clone()) {
Ok(_) => with_host(|h| {
if h.signal.is_none() {
h.exc = entry_exc.clone();
}
}),
Err(_) => {
if let Some(e) = with_host(|h| h.exc.clone()) {
with_host(|h| h.capture_exc_tb(&e));
raised.push(e);
}
}
}
if let Some(name) = &hd.name {
with_host(|h| {
let _ = h.del_name(name);
});
}
}
raised.extend(rest);
match excgroup::prep_reraise_star(exc, &raised) {
Ok(None) => None,
Ok(Some(result)) => {
let line = host::raise_value(&result);
with_host(|h| {
h.traceback.clear();
if excgroup::is_piece_of(h, &result, exc) {
let own = match result {
Value::Obj(id) => h.exc_tb.get(&id).and_then(|tb| tb.last()).cloned(),
_ => None,
};
if let Some(host::TbEntry::Frame(f)) = own {
h.set_cur_line_span(f.line, f.span);
}
} else if let Value::Obj(id) = result {
h.tb_starts_empty.insert(id);
}
});
Some(line.unwrap_or_else(|e| e))
}
Err(e) => Some(e),
}
}
fn b_match_seq(vm: &mut VM, _: u8) -> Value {
let star = match vm.pop() {
Value::Int(n) => n,
_ => -1,
};
let count = match vm.pop() {
Value::Int(n) => n as usize,
_ => 0,
};
let subject = vm.pop();
let items = with_host(|h| match h.get(&subject) {
Some(PyObj::List(l)) | Some(PyObj::Tuple(l)) => Some(l.clone()),
_ => None,
});
let items = match items {
Some(v) => v,
None => return Value::Bool(false),
};
let ordered: Vec<Value> = if star < 0 {
if items.len() != count {
return Value::Bool(false);
}
items
} else {
let si = star as usize;
let after = count - si - 1;
if items.len() < si + after {
return Value::Bool(false);
}
let mid = items[si..items.len() - after].to_vec();
let mid_list = with_host(|h| h.new_list(mid));
let mut out = Vec::with_capacity(count);
out.extend_from_slice(&items[..si]);
out.push(mid_list);
out.extend_from_slice(&items[items.len() - after..]);
out
};
let list = with_host(|h| h.new_list(ordered));
vm.push(list);
Value::Bool(true)
}
fn b_match_map_check(vm: &mut VM, _: u8) -> Value {
let subject = vm.pop();
Value::Bool(with_host(|h| {
matches!(h.get(&subject), Some(PyObj::Dict(_)))
}))
}
fn b_match_key(vm: &mut VM, _: u8) -> Value {
let key = vm.pop();
let subject = vm.pop();
let k = match with_host(|h| h.to_key(&key)) {
Ok(k) => k,
Err(e) => return abort(vm, e),
};
let got = with_host(|h| match h.get(&subject) {
Some(PyObj::Dict(d)) => d.get(&k).map(|(_, v)| v.clone()),
_ => None,
});
match got {
Some(v) => {
vm.push(v);
Value::Bool(true)
}
None => Value::Bool(false),
}
}
fn b_match_map_rest(vm: &mut VM, _: u8) -> Value {
let keylist = vm.pop();
let subject = vm.pop();
let keys = match host::iter_vec(&keylist) {
Ok(ks) => ks,
Err(e) => return abort(vm, e),
};
let mut d = with_host(|h| match h.get(&subject) {
Some(PyObj::Dict(d)) => d.clone(),
_ => IndexMap::new(),
});
for kv in &keys {
if let Ok(k) = with_host(|h| h.to_key(kv)) {
d.shift_remove(&k);
}
}
with_host(|h| h.new_dict(d))
}
fn match_pos_msg(cname: &str, accepts: usize, given: usize) -> String {
let plural = if accepts == 1 { "" } else { "s" };
format!("{cname}() accepts {accepts} positional sub-pattern{plural} ({given} given)")
}
fn b_match_class(vm: &mut VM, argc: u8) -> Value {
let all = pop_n(vm, argc as usize);
if all.len() < 3 {
return abort(vm, "internal: MATCH_CLASS arity".into());
}
let subject = all[0].clone();
let class = all[1].clone();
let npos = match all[2] {
Value::Int(n) => n as usize,
_ => 0,
};
let names_list = match all.get(3) {
Some(v) if all.len() == 4 => with_host(|h| match h.get(v) {
Some(PyObj::List(l)) => Some(l.clone()),
_ => None,
}),
_ => None,
};
let kwnames: Vec<String> = names_list.as_deref().unwrap_or(&all[3..]).iter().map(sval).collect();
match isinstance_dispatch(&subject, &class) {
Ok(true) => {}
Ok(false) => return Value::Bool(false),
Err(e) => return abort(vm, e),
}
let cname = with_host(|h| callable_name(h, &class)).unwrap_or_default();
let mut vals: Vec<Value> = Vec::new();
if npos > 0 {
if is_builtin_type(&cname) {
if npos != 1 {
return abort(vm, host::type_error(&match_pos_msg(&cname, 1, npos)));
}
vals.push(subject.clone());
} else {
let margs = with_host(|h| h.get_attr(&class, "__match_args__")).ok();
let names: Vec<String> = match margs {
Some(v) => match host::iter_vec(&v) {
Ok(items) => items.iter().map(sval).collect(),
Err(e) => return abort(vm, e),
},
None => Vec::new(),
};
if npos > names.len() {
return abort(
vm,
host::type_error(&match_pos_msg(&cname, names.len(), npos)),
);
}
for i in 0..npos {
let attr = match names.get(i) {
Some(a) => a.clone(),
None => return Value::Bool(false),
};
match with_host(|h| h.get_attr(&subject, &attr)) {
Ok(v) => vals.push(v),
Err(_) => return Value::Bool(false),
}
}
}
}
for name in &kwnames {
match with_host(|h| h.get_attr(&subject, name)) {
Ok(v) => vals.push(v),
Err(_) => return Value::Bool(false),
}
}
let list = with_host(|h| h.new_list(vals));
vm.push(list);
Value::Bool(true)
}
const SYNTAX_ERROR_ATTRS: [&str; 9] = [
"msg",
"filename",
"lineno",
"offset",
"text",
"end_lineno",
"end_offset",
"_metadata",
"print_file_and_line",
];
fn syntax_error_init(e: &Value, args: &[Value]) -> Result<(), String> {
let mut vals: [Value; 9] = std::array::from_fn(|_| Value::Undef);
if let Some(m) = args.first() {
vals[0] = m.clone();
}
if let [_, info] = args {
let info = host::iter_vec(info)?;
if info.len() < 4 {
return Err(host::type_error(&format!(
"function takes at least 4 arguments ({} given)",
info.len()
)));
}
if info.len() > 7 {
return Err(host::type_error(&format!(
"function takes at most 7 arguments ({} given)",
info.len()
)));
}
for (slot, v) in vals[1..].iter_mut().zip(info.into_iter().take(7)) {
*slot = v;
}
}
with_host(|h| {
for (name, v) in SYNTAX_ERROR_ATTRS.iter().zip(vals) {
let _ = h.set_attr(e, name, v);
}
});
Ok(())
}
pub(crate) fn name_error_attrs(class: &str) -> Option<&'static [&'static str]> {
match class {
"AttributeError" => Some(&["name", "obj"]),
"NameError" | "UnboundLocalError" => Some(&["name"]),
_ => None,
}
}
fn name_error_init(e: &Value, class: &str, kwargs: &[(String, Value)]) -> Result<(), String> {
let Some(attrs) = name_error_attrs(class) else {
return Ok(());
};
let fname = if class == "AttributeError" {
"AttributeError"
} else {
"NameError"
};
if kwargs.len() > attrs.len() {
let plural = if attrs.len() == 1 { "" } else { "s" };
return Err(host::type_error(&format!(
"{fname}() takes at most {} keyword argument{plural} ({} given)",
attrs.len(),
kwargs.len()
)));
}
if let Some((k, _)) = kwargs.iter().find(|(k, _)| !attrs.contains(&k.as_str())) {
let names: Vec<String> = attrs.iter().map(|a| a.to_string()).collect();
let hint = crate::suggest::closest(&names, k)
.map(|s| format!(". Did you mean '{s}'?"))
.unwrap_or_default();
return Err(host::type_error(&format!(
"{fname}() got an unexpected keyword argument '{k}'{hint}"
)));
}
with_host(|h| {
for attr in attrs {
let _ = h.set_attr(e, attr, kw_get(kwargs, attr).unwrap_or(Value::Undef));
}
});
Ok(())
}
pub(crate) fn construct_builtin_exception(
class: &str,
args: Vec<Value>,
kwargs: &[(String, Value)],
) -> Result<Value, String> {
let e = with_host(|h| {
h.alloc(PyObj::Exception {
class: class.to_string(),
args: args.clone(),
})
});
if host::is_syntax_error_class(class) {
syntax_error_init(&e, &args)?;
}
if crate::excunicode::is_unicode_error_class(class) {
crate::excunicode::init(&e, class, &args)?;
}
name_error_init(&e, class, kwargs)?;
if class == "re.PatternError" {
pattern_error_init(&e, &args, kwargs)?;
}
Ok(e)
}
fn pattern_error_init(e: &Value, args: &[Value], kwargs: &[(String, Value)]) -> Result<(), String> {
let param = |i: usize, name: &str| args.get(i).cloned().or_else(|| kw_get(kwargs, name));
let msg = param(0, "msg").ok_or_else(|| {
host::type_error("PatternError.__init__() missing 1 required positional argument: 'msg'")
})?;
let pattern = param(1, "pattern").unwrap_or(Value::Undef);
let pos = param(2, "pos").unwrap_or(Value::Undef);
let text = host::str_of(&msg);
with_host(|h| pattern_error_bind(h, e, text, msg, pattern, pos))
}
fn pattern_error_bind(
h: &mut host::PyHost,
e: &Value,
mut text: String,
msg: Value,
pattern: Value,
pos: Value,
) -> Result<(), String> {
let (mut lineno, mut colno) = (Value::Undef, Value::Undef);
if !matches!(pattern, Value::Undef) && !matches!(pos, Value::Undef) {
let Value::Int(p) = pos else {
return Err(host::type_error(&format!(
"%d format: a real number is required, not {}",
h.type_name(&pos)
)));
};
text = format!("{text} at position {p}");
let units: Vec<u32> = match h.get(&pattern) {
Some(PyObj::Bytes(b)) => b.iter().map(|&c| c as u32).collect(),
_ => match h.as_str(&pattern) {
Some(s) => s.chars().map(|c| c as u32).collect(),
None => {
return Err(format!(
"AttributeError: '{}' object has no attribute 'count'",
h.type_name(&pattern)
))
}
},
};
let before = &units[..usize::try_from(p).unwrap_or(0).min(units.len())];
let newline = '\n' as u32;
let line = before.iter().filter(|&&c| c == newline).count() as i64 + 1;
let last_newline = before.iter().rposition(|&c| c == newline).map_or(-1, |i| i as i64);
lineno = Value::Int(line);
colno = Value::Int(p - last_newline);
if units.contains(&newline) {
text = format!("{text} (line {line}, column {})", p - last_newline);
}
}
let message = h.new_str(text);
if let Some(PyObj::Exception { args, .. }) = h.get_mut(e) {
*args = vec![message];
}
for (name, v) in [
("msg", msg),
("pattern", pattern),
("pos", pos),
("lineno", lineno),
("colno", colno),
] {
let _ = h.set_attr(e, name, v);
}
Ok(())
}
fn pattern_error_from(h: &mut host::PyHost, p: crate::sre_parse::PendingPatternError) -> Value {
let e = h.alloc(PyObj::Exception {
class: "re.PatternError".into(),
args: vec![],
});
let msg = h.new_str(p.msg.clone());
let pattern = match (p.pos, p.istext) {
(None, _) => Value::Undef,
(Some(_), true) => h.new_str(p.pattern.iter().collect::<String>()),
(Some(_), false) => h.alloc(PyObj::Bytes(p.pattern.iter().map(|&c| c as u8).collect())),
};
let pos = p.pos.map_or(Value::Undef, |n| Value::Int(n as i64));
let _ = pattern_error_bind(h, &e, p.msg, msg, pattern, pos);
e
}
fn synth_exc(h: &mut host::PyHost, err: &str) -> Value {
let (err, pos) = crate::parser::split_syntax_error(err);
let (class, msg) = match err.split_once(": ") {
Some((c, m)) => (c.to_string(), m.to_string()),
None => (err.to_string(), String::new()),
};
if class == "re.PatternError" {
if let Some(pending) = crate::sre_parse::take_pending(err) {
return pattern_error_from(h, pending);
}
}
if let Some((eno, strerror, filename)) = parse_oserror(&msg) {
if is_oserror_class(&class) {
let se = h.new_str(strerror.clone());
let e = h.alloc(PyObj::Exception {
class,
args: vec![Value::Int(eno), se.clone()],
});
let _ = h.set_attr(&e, "errno", Value::Int(eno));
let _ = h.set_attr(&e, "strerror", se);
let fv = match filename {
Some(f) => h.new_str(f),
None => Value::Undef,
};
let _ = h.set_attr(&e, "filename", fv);
let _ = h.set_attr(&e, "filename2", Value::Undef);
return e;
}
}
let mut recorded = match &h.foreign_exc {
Some(f) if f.line == err => h.foreign_exc.take(),
_ => None,
};
let origin = recorded.as_mut().and_then(|f| f.origin.take());
let raised = match origin {
Some(crate::host::ExcOrigin::Paired(v)) => return v,
Some(crate::host::ExcOrigin::Raised { handle, addr }) => Some((handle, addr)),
None => None,
};
let attrs = recorded
.as_ref()
.map(|f| f.attrs.clone())
.unwrap_or_default();
let unicode = crate::excunicode::take_pending(err);
let args = match (unicode, recorded) {
(Some(u), _) => crate::excunicode::arg_values(h, &u),
(None, Some(f)) => f.args,
(None, None) if msg.is_empty() => vec![],
(None, None) => {
let s = h.new_str(msg.clone());
vec![s]
}
};
let e = h.alloc(PyObj::Exception {
class: class.clone(),
args: args.clone(),
});
if let Some((handle, addr)) = raised {
h.exc_bridge.borrow_mut().pair(&e, handle, addr, true);
}
if crate::excunicode::is_unicode_error_class(&class) {
crate::excunicode::bind_args(h, &e, &class, &args);
}
for (name, val) in attrs {
let _ = h.set_attr(&e, &name, val);
}
if let Some(attrs) = name_error_attrs(&class) {
let recorded = (class == "AttributeError")
.then(|| h.attr_miss_for(err))
.flatten();
let (name, obj) = match recorded {
Some((n, o)) => (Some(n), o),
None => (missing_identifier(&class, &msg), Value::Undef),
};
let nv = name.map_or(Value::Undef, |n| h.new_str(n));
let _ = h.set_attr(&e, attrs[0], nv);
if let Some(obj_attr) = attrs.get(1) {
let _ = h.set_attr(&e, obj_attr, obj);
}
}
if host::is_syntax_error_class(&class) {
let (text, lineno) = split_line_suffix(&msg);
let mut vals: [Value; 9] = std::array::from_fn(|_| Value::Undef);
if !msg.is_empty() {
vals[0] = h.new_str(text.to_string());
}
if let Some(n) = lineno {
vals[2] = Value::Int(n);
}
if let Some(p) = &pos {
let int = |n: Option<i64>| n.map_or(Value::Undef, Value::Int);
vals[1] = p.filename.clone().map_or(Value::Undef, |f| h.new_str(f));
vals[2] = int(p.lineno);
vals[3] = int(p.offset);
vals[4] = p.text.clone().map_or(Value::Undef, |t| h.new_str(t));
vals[5] = int(p.end_lineno);
vals[6] = int(p.end_offset);
if let Some(m) = &p.metadata {
let source = m.source.clone().map_or(Value::Undef, |s| h.new_str(s));
vals[7] = h.new_tuple(vec![Value::Int(m.lineno), Value::Int(m.offset), source]);
}
if !p.bare_args {
let details = h.new_tuple(vals[1..7].to_vec());
if let Some(PyObj::Exception { args, .. }) = h.get_mut(&e) {
args.push(details);
}
}
}
for (name, v) in SYNTAX_ERROR_ATTRS.iter().zip(vals) {
let _ = h.set_attr(&e, name, v);
}
}
e
}
fn split_line_suffix(msg: &str) -> (&str, Option<i64>) {
if let Some(head) = msg.strip_suffix(')') {
if let Some((text, n)) = head.rsplit_once(" (line ") {
if let Ok(n) = n.parse() {
return (text, Some(n));
}
}
}
(msg, None)
}
fn is_oserror_class(class: &str) -> bool {
matches!(
class,
"OSError"
| "IOError"
| "EnvironmentError"
| "BlockingIOError"
| "ChildProcessError"
| "ConnectionError"
| "BrokenPipeError"
| "ConnectionAbortedError"
| "ConnectionRefusedError"
| "ConnectionResetError"
| "FileExistsError"
| "FileNotFoundError"
| "InterruptedError"
| "IsADirectoryError"
| "NotADirectoryError"
| "PermissionError"
| "ProcessLookupError"
| "TimeoutError"
)
}
fn parse_oserror(msg: &str) -> Option<(i64, String, Option<String>)> {
let rest = msg.strip_prefix("[Errno ")?;
let (num, rest) = rest.split_once("] ")?;
let eno: i64 = num.parse().ok()?;
match rest.rsplit_once(": '") {
Some((strerror, tail)) if tail.ends_with('\'') => Some((
eno,
strerror.to_string(),
Some(tail[..tail.len() - 1].to_string()),
)),
_ => Some((eno, rest.to_string(), None)),
}
}
fn missing_identifier(class: &str, msg: &str) -> Option<String> {
let quoted = |after: &str| -> Option<String> {
let rest = msg.split_once(after)?.1;
let inner = rest.strip_prefix('\'')?;
let end = inner.find('\'')?;
Some(inner[..end].to_string())
};
match class {
"NameError" | "UnboundLocalError" => quoted("name "),
"AttributeError" => quoted("has no attribute "),
_ => None,
}
}
pub(crate) fn exc_matches(h: &host::PyHost, exc: &Value, typ: &Value) -> bool {
let exc_class = match h.get(exc) {
Some(PyObj::Exception { class, .. }) => class.clone(),
Some(PyObj::Instance(i)) => i.class.clone(),
_ => h.type_name(exc),
};
if let Some(PyObj::Tuple(ts)) = h.get(typ) {
return ts.iter().any(|t| exc_matches(h, exc, t));
}
let want = match callable_name(h, typ) {
Some(n) => n,
#[cfg(feature = "stdlib-ffi")]
None => match h.foreign_id(typ).and_then(crate::ffi::class_name) {
Some(n) => n,
None => return false,
},
#[cfg(not(feature = "stdlib-ffi"))]
None => return false,
};
exception_isa(&exc_class, &want, h)
}
#[cfg(feature = "stdlib-ffi")]
fn foreign_super(args: Vec<Value>) -> Result<Value, String> {
let builtins = crate::ffi::import("builtins")?;
let sup = with_host(|h| crate::ffi::get_attr(h, builtins, "super"))?;
host::invoke(&sup, args, Vec::new())
}
fn callable_name(h: &host::PyHost, v: &Value) -> Option<String> {
match h.get(v) {
Some(PyObj::Builtin(n)) => Some(n.clone()),
Some(PyObj::Class(n)) => Some(n.clone()),
Some(PyObj::NamedTupleType { type_name, .. }) => Some(type_name.clone()),
_ => None,
}
}
#[cfg(feature = "stdlib-ffi")]
fn foreign_binop_func(op: NumOp) -> Option<&'static str> {
use NumOp::*;
Some(match op {
Add => "add",
Sub => "sub",
Mul => "mul",
Div => "truediv",
Mod => "mod",
Pow => "pow",
Eq => "eq",
Ne => "ne",
Lt => "lt",
Le => "le",
Gt => "gt",
Ge => "ge",
Neg => return None,
})
}
pub fn numeric_hook(op: NumOp, a: &Value, b: &Value) -> Result<Value, String> {
let r = numeric_hook_inner(op, a, b);
if host::equal_overflowed() {
return Err(host::comparison_recursion_error());
}
r
}
fn new_object_result(v: Value) -> Value {
match v {
Value::Float(f) => Value::Float(host::new_float(f)),
v => v,
}
}
fn numeric_hook_inner(op: NumOp, a: &Value, b: &Value) -> Result<Value, String> {
use NumOp::*;
if let Some(r) = deque_subclass_op(op, a, b) {
return r;
}
let (fwd, refl) = match op {
Neg => ("__neg__", ""),
_ => numop_dunders(op).unwrap_or(("", "")),
};
let a_owned = if fwd.is_empty() {
a.clone()
} else {
host::subclass_operand(a, fwd)
};
let b_owned = if refl.is_empty() {
b.clone()
} else {
host::subclass_operand(b, refl)
};
let a = &a_owned;
let b = &b_owned;
match op {
Add | Sub => {
let sym = if matches!(op, Add) { '+' } else { '-' };
if let Some(res) = counter_binop(a, b, sym) {
return res;
}
}
_ => {}
}
#[cfg(feature = "stdlib-ffi")]
if let Some(func) = foreign_binop_func(op) {
if (host::is_heap(a) || host::is_heap(b))
&& with_host(|h| h.foreign_id(a).is_some() || h.foreign_id(b).is_some())
{
return crate::ffi::binary_op_cb(func, a, b);
}
}
#[cfg(feature = "stdlib-ffi")]
if matches!(op, Neg) && with_host(|h| h.foreign_id(a).is_some()) {
return crate::ffi::unary_op_cb("neg", a);
}
let a_inst = with_host(|h| matches!(h.get(a), Some(PyObj::Instance(_))));
let b_inst = with_host(|h| matches!(h.get(b), Some(PyObj::Instance(_))));
if !a_inst && !b_inst {
if let Some(r) = collections_compare(op, a, b) {
return r;
}
if matches!(op, Eq | Ne) {
if let Some(r) = container_user_eq(a, b)? {
return Ok(Value::Bool(if matches!(op, Eq) { r } else { !r }));
}
}
if matches!(op, Sub) {
if let Some(res) = dictview_setop(ViewSetOp::Sub, a, b) {
return res;
}
}
if matches!(op, Sub | Eq | Ne | Lt | Le | Gt | Ge) {
if let Some(b2) = host::align_operand(a, b)? {
return with_host(|h| h.arith(op, a, &b2));
}
}
return with_host(|h| h.arith(op, a, b)).map(new_object_result);
}
match op {
Eq => match dispatch_binop(a, b, "__eq__", "__eq__") {
Dunder::Value(v) => Ok(v),
Dunder::Err(e) => Err(e),
Dunder::NotImplemented => Ok(Value::Bool(identity_eq(a, b))),
},
Ne => match dispatch_binop(a, b, "__ne__", "__ne__") {
Dunder::Value(v) => Ok(v),
Dunder::Err(e) => Err(e),
Dunder::NotImplemented => match dispatch_binop(a, b, "__eq__", "__eq__") {
Dunder::Value(v) => Ok(Value::Bool(!with_host(|h| h.truthy(&v)))),
Dunder::Err(e) => Err(e),
Dunder::NotImplemented => Ok(Value::Bool(!identity_eq(a, b))),
},
},
Lt | Le | Gt | Ge => {
let (l, r) = numop_dunders(op).unwrap();
match dispatch_binop(a, b, l, r) {
Dunder::Value(v) => Ok(v),
Dunder::Err(e) => Err(e),
Dunder::NotImplemented => {
if let Some(res) = total_ordering_derive(op, a, b) {
if !matches!(&res, Ok(v) if is_not_implemented(v)) {
return res;
}
}
let refl = match op {
Lt => Gt,
Le => Ge,
Gt => Lt,
_ => Le,
};
if let Some(res) = total_ordering_derive(refl, b, a) {
if !matches!(&res, Ok(v) if is_not_implemented(v)) {
return res;
}
}
let sym = match op {
Lt => "<",
Le => "<=",
Gt => ">",
_ => ">=",
};
Err(with_host(|h| {
host::type_error(&format!(
"'{sym}' not supported between instances of '{}' and '{}'",
h.tp_name(a),
h.tp_name(b)
))
}))
}
}
}
Neg => {
if a_inst
&& with_host(
|h| matches!(h.get(a), Some(PyObj::Instance(i)) if instance_has(h, i, "__neg__")),
)
{
host::call_method(a, "__neg__", vec![], vec![])
} else {
#[cfg(feature = "stdlib-ffi")]
if with_host(|h| h.foreign_id(a).is_some()) {
return crate::ffi::unary_op_cb("neg", a);
}
with_host(|h| h.arith(op, a, b))
}
}
Mul if repeat_with_index(a, b) => {
let (a2, b2) = (indexed(a)?, indexed(b)?);
with_host(|h| h.arith(op, &a2, &b2))
}
Add | Sub | Mul | Div | Mod | Pow => {
let (l, r) = numop_dunders(op).unwrap();
match dispatch_arith(a, b, l, r) {
Dunder::Value(v) => Ok(v),
Dunder::Err(e) => Err(e),
Dunder::NotImplemented => Err(sequence_op_error(op, a, b)
.unwrap_or_else(|| unsupported_operand(binop_symbol(l), a, b))),
}
}
}
}
pub fn is_builtin_function(name: &str) -> bool {
BUILTIN_FUNCS.contains(&name)
|| name.starts_with("math.")
|| name.starts_with("collections.")
|| name.starts_with("textwrap.")
|| name.starts_with("statistics.")
|| name.starts_with("asyncio.")
}
fn sum_is_long(v: &Value) -> bool {
matches!(v, Value::Int(_) | Value::Bool(_))
|| with_host(|h| matches!(h.get(v), Some(PyObj::BigInt(_))))
}
fn sum_bad_start(start: &Value) -> Option<&'static str> {
if matches!(start, Value::Str(_)) {
return Some("strings [use ''.join(seq) instead]");
}
with_host(|h| match h.get(start) {
Some(PyObj::Str(_)) => Some("strings [use ''.join(seq) instead]"),
Some(PyObj::Bytes(_)) => Some("bytes [use b''.join(seq) instead]"),
Some(PyObj::Bytearray(_)) => Some("bytearray [use b''.join(seq) instead]"),
_ => None,
})
}
#[inline]
fn cs_add(hi: f64, lo: f64, x: f64) -> (f64, f64) {
let t = hi + x;
let lo = if hi.abs() >= x.abs() {
lo + ((hi - t) + x)
} else {
lo + ((x - t) + hi)
};
(t, lo)
}
#[inline]
fn cs_to_double(hi: f64, lo: f64) -> f64 {
if lo != 0.0 && lo.is_finite() {
hi + lo
} else {
hi
}
}
pub const UNINSTANTIABLE_TYPES: &[&str] = &[
"member_descriptor",
"wrapper_descriptor",
"method_descriptor",
"classmethod_descriptor",
"getset_descriptor",
"method-wrapper",
"iterator",
"callable_iterator",
"list_iterator",
"list_reverseiterator",
"tuple_iterator",
"str_iterator",
"str_ascii_iterator",
"bytes_iterator",
"bytearray_iterator",
"range_iterator",
"longrange_iterator",
"set_iterator",
"memory_iterator",
"dict_keyiterator",
"dict_valueiterator",
"dict_itemiterator",
"dict_reversekeyiterator",
"dict_reversevalueiterator",
"dict_reverseitemiterator",
];
pub fn is_type_like_builtin(name: &str) -> bool {
is_builtin_type(name) || is_exception_class(name)
}
pub fn shadows_builtin_type(name: &str) -> bool {
is_type_like_builtin(name)
|| matches!(
name,
"property"
| "staticmethod"
| "classmethod"
| "super"
| "slice"
| "zip"
| "map"
| "filter"
| "enumerate"
| "reversed"
)
}
pub const OBJECT_DUNDERS: &[&str] = &[
"__class__",
"__delattr__",
"__dir__",
"__doc__",
"__eq__",
"__format__",
"__ge__",
"__getattribute__",
"__getstate__",
"__gt__",
"__hash__",
"__init__",
"__init_subclass__",
"__le__",
"__lt__",
"__ne__",
"__new__",
"__reduce__",
"__reduce_ex__",
"__repr__",
"__setattr__",
"__sizeof__",
"__str__",
"__subclasshook__",
];
pub const OBJECT_METHOD_DUNDERS: &[&str] = &[
"__delattr__",
"__dir__",
"__eq__",
"__format__",
"__ge__",
"__getattribute__",
"__getstate__",
"__gt__",
"__hash__",
"__init__",
"__init_subclass__",
"__le__",
"__lt__",
"__ne__",
"__reduce__",
"__reduce_ex__",
"__repr__",
"__setattr__",
"__sizeof__",
"__str__",
"__subclasshook__",
];
pub const UNHASHABLE_TYPES: &[&str] = &[
"list",
"dict",
"set",
"bytearray",
"deque",
"defaultdict",
"OrderedDict",
"Counter",
"dict_keys",
"dict_values",
"dict_items",
"array",
];
const CONTAINER_DUNDERS: &[&str] = &[
"__len__",
"__getitem__",
"__setitem__",
"__delitem__",
"__iter__",
"__contains__",
"__bool__",
];
pub fn is_object_dunder_method(tn: &str, name: &str) -> bool {
const SIZED: &[&str] = &[
"str",
"bytes",
"bytearray",
"list",
"tuple",
"dict",
"set",
"frozenset",
"range",
"memoryview",
"array",
"deque",
"defaultdict",
"OrderedDict",
"Counter",
"dict_keys",
"dict_values",
"dict_items",
];
const SUBSCRIPTABLE: &[&str] = &[
"str",
"bytes",
"bytearray",
"list",
"tuple",
"dict",
"range",
"memoryview",
"array",
"deque",
"defaultdict",
"OrderedDict",
"Counter",
];
const MUTABLE: &[&str] = &[
"bytearray",
"list",
"dict",
"memoryview",
"array",
"deque",
"defaultdict",
"OrderedDict",
"Counter",
];
const LAZY: &[&str] = &[
"generator",
"enumerate",
"zip",
"map",
"filter",
"reversed",
"TextIOWrapper",
"BufferedReader",
"BufferedWriter",
"BufferedRandom",
"FileIO",
"BytesIO",
"StringIO",
];
const CONTAINS_EXCLUDED: &[&str] = &["dict_values", "memoryview"];
const HAS_BOOL: &[&str] = &[
"int",
"float",
"complex",
"bool",
"NoneType",
"NotImplementedType",
"range",
];
match name {
n if OBJECT_METHOD_DUNDERS.contains(&n) => true,
"__bool__" => HAS_BOOL.contains(&tn),
"__len__" => SIZED.contains(&tn),
"__iter__" => SIZED.contains(&tn) || LAZY.contains(&tn),
"__contains__" => SIZED.contains(&tn) && !CONTAINS_EXCLUDED.contains(&tn),
"__getitem__" => SUBSCRIPTABLE.contains(&tn),
"__setitem__" | "__delitem__" => MUTABLE.contains(&tn),
_ => false,
}
}
pub fn is_type_object_name(n: &str) -> bool {
is_type_like_builtin(n)
|| matches!(
n,
"zip" | "map"
| "filter"
| "enumerate"
| "reversed"
| "slice"
| "super"
| "property"
| "classmethod"
| "staticmethod"
| "collections.deque"
| "collections.defaultdict"
| "collections.OrderedDict"
| "collections.Counter"
| "_contextvars.ContextVar"
| "_contextvars.Token"
| "_contextvars.Context"
| "_struct.Struct"
| "os.stat_result"
)
|| n.strip_prefix("_io.")
.is_some_and(|t| crate::stdlib::pyio::STREAM_TYPES.contains(&t))
|| (!n.contains('.') && !is_builtin_function(n))
}
pub fn native_type_key(name: &str) -> &str {
match name.strip_prefix("collections.") {
Some(bare) if is_native_collection(bare) => bare,
_ => name,
}
}
pub fn is_native_collection(name: &str) -> bool {
matches!(name, "deque" | "Counter" | "OrderedDict" | "defaultdict")
}
pub fn builtin_mro(name: &str) -> Vec<String> {
let mut chain = vec![name.to_string()];
if name == "bool" {
chain.push("int".to_string());
} else if matches!(
native_type_key(name),
"Counter" | "OrderedDict" | "defaultdict"
) {
chain.push("dict".to_string());
} else if is_exception_class(name) {
let mut cur = name;
while let Some((_, parent)) = EXC_PARENTS.iter().find(|(c, _)| *c == cur) {
chain.push((*parent).to_string());
cur = parent;
}
}
if name != "object" {
chain.push("object".to_string());
}
chain
}
pub fn type_classmethods(name: &str) -> &'static [&'static str] {
match name {
"dict" | "OrderedDict" | "defaultdict" | "Counter" => &["fromkeys"],
"int" | "bool" => &["from_bytes"],
"bytes" | "bytearray" => &["fromhex"],
"float" => &["fromhex", "from_number"],
"complex" => &["from_number"],
_ => &[],
}
}
pub const BUILTIN_TYPES: &[&str] = &[
"int",
"float",
"str",
"bool",
"list",
"tuple",
"dict",
"set",
"frozenset",
"bytes",
"bytearray",
"memoryview",
"complex",
"object",
"type",
"range",
];
fn is_builtin_type(name: &str) -> bool {
BUILTIN_TYPES.contains(&name)
}
pub fn builtin_names() -> Vec<&'static str> {
let mut names: Vec<&'static str> = BUILTIN_FUNCS.to_vec();
names.extend_from_slice(BUILTIN_TYPES);
names.push("BaseException");
names.extend(EXC_PARENTS.iter().map(|(c, _)| *c));
names
}
pub fn is_exception_class(name: &str) -> bool {
EXC_PARENTS.iter().any(|(c, _)| *c == name) || name == "BaseException"
}
pub fn is_known_builtin(name: &str) -> bool {
is_builtin_function(name) || is_builtin_type(name) || is_exception_class(name)
}
const BUILTIN_FUNCS: &[&str] = &[
"print",
"len",
"range",
"abs",
"min",
"max",
"sum",
"sorted",
"reversed",
"enumerate",
"zip",
"map",
"filter",
"any",
"all",
"round",
"divmod",
"pow",
"type",
"isinstance",
"issubclass",
"__import__",
"hasattr",
"getattr",
"setattr",
"delattr",
"id",
"hash",
"ord",
"chr",
"hex",
"oct",
"bin",
"repr",
"ascii",
"iter",
"next",
"input",
"format",
"vars",
"dir",
"callable",
"open",
"super",
"staticmethod",
"classmethod",
"property",
"exit",
"quit",
"slice",
"eval",
"exec",
"compile",
"globals",
"locals",
];
pub fn py_str(v: &Value) -> Result<String, String> {
if let Some(e) = with_host(|h| h.int_str_limit_exceeded(v)) {
return Err(e);
}
if let Some(m) = with_host(|h| h.metaclass_method(v, "__str__")) {
let r = host::invoke(&m, vec![v.clone()], vec![])?;
return Ok(with_host(|h| h.str_of(&r)));
}
if with_host(|h| h.metaclass_method(v, "__repr__").is_some()) {
return py_repr(v);
}
if with_host(|h| matches!(h.get(v), Some(PyObj::Instance(_)))) {
let (has_str, has_repr, is_exc) = with_host(|h| match h.get(v) {
Some(PyObj::Instance(i)) => (
h.class_lookup(&i.class, "__str__").is_some(),
h.class_lookup(&i.class, "__repr__").is_some(),
h.class_is_exception(&i.class),
),
_ => (false, false, false),
});
if has_str {
let r = host::call_method(v, "__str__", vec![], vec![])?;
return Ok(with_host(|h| h.str_of(&r)));
}
if is_exc {
return Ok(with_host(|h| h.str_of(v)));
}
if has_repr {
let r = host::call_method(v, "__repr__", vec![], vec![])?;
return Ok(with_host(|h| h.str_of(&r)));
}
}
if with_host(|h| {
matches!(
h.get(v),
Some(PyObj::List(_))
| Some(PyObj::Tuple(_))
| Some(PyObj::Dict(_))
| Some(PyObj::Set(_))
| Some(PyObj::Frozenset(_))
)
}) {
return py_repr(v);
}
if let Some(e) = with_host(|h| h.int_subclass_limit_exceeded(v)) {
return Err(e);
}
Ok(host::str_of(v))
}
pub fn py_repr(v: &Value) -> Result<String, String> {
if let Some(e) = with_host(|h| h.int_str_limit_exceeded(v)) {
return Err(e);
}
if let Some(m) = with_host(|h| h.metaclass_method(v, "__repr__")) {
let r = host::invoke(&m, vec![v.clone()], vec![])?;
return Ok(with_host(|h| h.str_of(&r)));
}
if with_host(|h| matches!(h.get(v), Some(PyObj::Instance(_)))) {
let has_repr = with_host(|h| match h.get(v) {
Some(PyObj::Instance(i)) => h.class_lookup(&i.class, "__repr__").is_some(),
_ => false,
});
if has_repr {
let r = host::call_method(v, "__repr__", vec![], vec![])?;
return Ok(with_host(|h| h.str_of(&r)));
}
}
let sl = with_host(|h| match h.get(v) {
Some(PyObj::Slice { lo, hi, step }) => Some((lo.clone(), hi.clone(), step.clone())),
_ => None,
});
if let Some((lo, hi, step)) = sl {
return Ok(format!(
"slice({}, {}, {})",
py_repr(&lo)?,
py_repr(&hi)?,
py_repr(&step)?
));
}
enum Cont {
List(Vec<Value>),
Tuple(Vec<Value>, Option<(String, Vec<String>)>),
Set(Vec<Value>),
Frozenset(Vec<Value>),
Dict(Vec<(Value, Value)>, Option<(host::DictKind, Option<Value>)>),
}
let cont = with_host(|h| match h.get(v) {
Some(PyObj::List(l)) => Some(Cont::List(l.clone())),
Some(PyObj::Tuple(l)) => {
let nt = match v {
Value::Obj(i) => h
.nt_meta
.get(i)
.map(|m| (m.type_name.clone(), m.fields.clone())),
_ => None,
};
Some(Cont::Tuple(l.clone(), nt))
}
Some(PyObj::Set(s)) => Some(Cont::Set(h.set_ordered_values(v, s))),
Some(PyObj::Frozenset(s)) => Some(Cont::Frozenset(h.set_ordered_values(v, s))),
Some(PyObj::Dict(d)) => {
let meta = match v {
Value::Obj(i) => h.dict_meta.get(i).map(|m| (m.kind, m.factory.clone())),
_ => None,
};
let mut pairs: Vec<(Value, Value)> = d.values().cloned().collect();
if meta.as_ref().map(|(k, _)| *k) == Some(host::DictKind::Counter) {
pairs.sort_by(|a, b| h.count_order(&b.1, &a.1));
}
Some(Cont::Dict(pairs, meta))
}
_ => None,
});
let reprs =
|elems: &[Value]| -> Result<Vec<String>, String> { elems.iter().map(py_repr).collect() };
if let Some(cont) = cont {
let id = if let Value::Obj(i) = v { *i } else { 0 };
let marker = match &cont {
Cont::List(_) => "[...]",
Cont::Tuple(..) => "(...)",
Cont::Set(_) | Cont::Dict(..) => "{...}",
Cont::Frozenset(_) => "frozenset(...)",
};
if host::repr_guard_enter(id) {
return Ok(marker.to_string());
}
let build = || -> Result<String, String> {
Ok(match cont {
Cont::List(e) => format!("[{}]", reprs(&e)?.join(", ")),
Cont::Tuple(e, Some((type_name, fields))) => {
let p = reprs(&e)?;
let inner: Vec<String> = fields
.iter()
.zip(p.iter())
.map(|(f, x)| format!("{f}={x}"))
.collect();
format!("{type_name}({})", inner.join(", "))
}
Cont::Tuple(e, None) => {
let p = reprs(&e)?;
if p.len() == 1 {
format!("({},)", p[0])
} else {
format!("({})", p.join(", "))
}
}
Cont::Set(e) if e.is_empty() => "set()".into(),
Cont::Set(e) => format!("{{{}}}", reprs(&e)?.join(", ")),
Cont::Frozenset(e) if e.is_empty() => "frozenset()".into(),
Cont::Frozenset(e) => format!("frozenset({{{}}})", reprs(&e)?.join(", ")),
Cont::Dict(pairs, meta) => {
let mut p = Vec::with_capacity(pairs.len());
for (k, val) in &pairs {
p.push(format!("{}: {}", py_repr(k)?, py_repr(val)?));
}
let body = format!("{{{}}}", p.join(", "));
let empty = pairs.is_empty();
match meta.as_ref().map(|(k, _)| *k) {
Some(host::DictKind::Counter) if empty => "Counter()".into(),
Some(host::DictKind::Counter) => format!("Counter({body})"),
Some(host::DictKind::OrderedDict) if empty => "OrderedDict()".into(),
Some(host::DictKind::OrderedDict) => format!("OrderedDict({body})"),
Some(host::DictKind::DefaultDict) => {
let f = match meta.and_then(|(_, f)| f) {
Some(fv) => py_repr(&fv)?,
None => "None".into(),
};
format!("defaultdict({f}, {body})")
}
None => body,
}
}
})
};
let result = build();
host::repr_guard_leave(id);
return result;
}
if let Some(e) = with_host(|h| h.int_subclass_limit_exceeded(v)) {
return Err(e);
}
Ok(host::repr_of(v))
}
fn kw_get(kwargs: &[(String, Value)], name: &str) -> Option<Value> {
kwargs
.iter()
.find(|(k, _)| k == name)
.map(|(_, v)| v.clone())
}
pub fn call_builtin_function(
name: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
if UNINSTANTIABLE_TYPES.contains(&name) {
return Err(host::type_error(&format!("cannot create '{name}' instances")));
}
if let Some(m) = name.strip_prefix("math.") {
return call_math(m, &args, &kwargs);
}
if let Some(f) = name.strip_prefix("copy.") {
return call_copy(f, &args);
}
if name == "functools.total_ordering" {
return call_total_ordering(&args);
}
if name == "contextlib.redirect_stdout" || name == "contextlib.redirect_stderr" {
let target = arg0(&args)?;
let stderr = name.ends_with("stderr");
return Ok(with_host(|h| {
h.alloc(PyObj::Redirect {
stderr,
target,
saved: None,
})
}));
}
if name == "functools.cached_property" {
let func = arg0(&args)?;
return Ok(with_host(|h| {
h.alloc(PyObj::CachedProperty {
func,
name: String::new(),
})
}));
}
if let Some(f) = name.strip_prefix("sys.") {
return call_sys(f, args, kwargs);
}
if let Some(f) = name.strip_prefix("asyncio.") {
return call_asyncio(f, args, kwargs);
}
if let Some(f) = name.strip_prefix("posix.") {
return call_posix(f, args, kwargs);
}
if let Some(f) = name.strip_prefix("_typing.") {
return call_typing(f, args, kwargs);
}
if let Some(f) = name.strip_prefix("time.") {
return call_time(f, args);
}
if let Some(f) = name.strip_prefix("re.") {
if f == "PatternError" {
return construct_builtin_exception(name, args, &kwargs);
}
return call_re(f, args, kwargs);
}
if let Some(f) = name.strip_prefix("atexit.") {
return match f {
"register" => {
let func = arg0(&args)?;
let rest: Vec<Value> = args.iter().skip(1).cloned().collect();
with_host(|h| h.atexit_callbacks.push((func.clone(), rest, kwargs)));
Ok(func)
}
"unregister" => {
let func = arg0(&args)?;
with_host(|h| {
h.atexit_callbacks
.retain(|(f, _, _)| !identity_eq(f, &func))
});
Ok(Value::Undef)
}
"_run_exitfuncs" => {
host::run_atexit_callbacks();
Ok(Value::Undef)
}
"_clear" => {
with_host(|h| h.atexit_callbacks.clear());
Ok(Value::Undef)
}
"_ncallbacks" => Ok(Value::Int(with_host(|h| h.atexit_callbacks.len()) as i64)),
_ => Err(host::type_error(&format!("atexit has no function '{f}'"))),
};
}
if let Some(f) = name.strip_prefix("_thread_core.") {
return match f {
"allocate_lock" => Ok(with_host(|h| {
h.alloc(PyObj::Lock {
count: 0,
reentrant: false,
})
})),
"RLock" => Ok(with_host(|h| {
h.alloc(PyObj::Lock {
count: 0,
reentrant: true,
})
})),
"get_ident" | "get_native_id" => Ok(Value::Int(1)),
"start_new_thread" => {
let func = arg0(&args)?;
let a = args.get(1).cloned().unwrap_or(Value::Undef);
let call_args = host::iter_vec(&a).unwrap_or_default();
host::invoke(&func, call_args, vec![])?;
Ok(Value::Int(1))
}
_ => Err(format!(
"AttributeError: module '_thread_core' has no attribute '{f}'"
)),
};
}
if let Some(f) = name.strip_prefix("itertools.") {
return call_itertools(f, args, kwargs);
}
if name == "_string.formatter_parser" {
return string_formatter_parser(&args);
}
if name == "_string.formatter_field_name_split" {
return string_formatter_field_name_split(&args);
}
if let Some(base) = name.strip_suffix(".__new__").filter(|b| {
matches!(
*b,
"int" | "str" | "float" | "tuple" | "frozenset" | "list" | "dict" | "set"
)
}) {
let cls = arg0(&args)?;
let rest: Vec<Value> = args.iter().skip(1).cloned().collect();
let cname = with_host(|h| match h.get(&cls) {
Some(PyObj::Class(n)) => Some(n.clone()),
_ => None,
});
return match cname {
Some(c) => {
let payload = call_builtin_function(base, rest, kwargs)?;
Ok(with_host(|h| h.new_instance_payload(c, payload)))
}
None => call_builtin_function(base, rest, kwargs),
};
}
if name == "mappingproxy" {
let src = arg0(&args)?;
let dict = with_host(|h| match h.get(&src) {
Some(PyObj::Dict(_)) => Some(src.clone()),
Some(PyObj::MappingProxy { dict }) => Some(dict.clone()),
Some(PyObj::Instance(i)) if matches!(h.get(&i.payload), Some(PyObj::Dict(_))) => {
Some(i.payload.clone())
}
_ => None,
})
.ok_or_else(|| host::type_error("mappingproxy() argument must be a mapping, not ..."))?;
return Ok(with_host(|h| h.alloc(PyObj::MappingProxy { dict })));
}
if name == "object.__new__" {
let cls = arg0(&args)?;
let cname = with_host(|h| match h.get(&cls) {
Some(PyObj::Class(n)) => Some(n.clone()),
_ => None,
});
return match cname {
Some(c) => Ok(with_host(|h| h.new_instance(c, host::NameMap::default()))),
None => Err(host::type_error(
"object.__new__(X): X is not a type object",
)),
};
}
match name {
"dict.fromkeys" => return dict_fromkeys(None, &args),
"OrderedDict.fromkeys" => return dict_fromkeys(Some(host::DictKind::OrderedDict), &args),
"defaultdict.fromkeys" => return dict_fromkeys(Some(host::DictKind::DefaultDict), &args),
"Counter.fromkeys" => return dict_fromkeys(Some(host::DictKind::Counter), &args),
_ => {}
}
if name == "str.maketrans" {
return str_maketrans(&args);
}
if name == "bytes.maketrans" || name == "bytearray.maketrans" {
return bytes_maketrans(&args);
}
if name == "int.from_bytes" {
return int_from_bytes(&args, &kwargs);
}
if name == "float.fromhex" {
let s = with_host(|h| args.first().and_then(|v| h.as_str(v)))
.ok_or_else(|| host::type_error("float.fromhex() argument must be str"))?;
return Ok(Value::Float(float_fromhex(&s)?));
}
if name == "float.from_number" || name == "complex.from_number" {
let v = arg0(&args)?;
let tn = with_host(|h| h.type_name(&v));
let numeric = matches!(tn.as_str(), "int" | "bool" | "float")
|| (name == "complex.from_number" && tn == "complex");
if !numeric {
return Err(host::type_error(&format!("must be real number, not {tn}")));
}
let ctor = name.strip_suffix(".from_number").unwrap_or(name);
return call_builtin_function(ctor, vec![v], vec![]);
}
if name == "bytes.fromhex" || name == "bytearray.fromhex" {
let b = bytes_fromhex(&args)?;
return Ok(with_host(|h| {
if name == "bytearray.fromhex" {
h.alloc(PyObj::Bytearray(b))
} else {
h.alloc(PyObj::Bytes(b))
}
}));
}
if let Some((tp, meth)) = name.split_once('.') {
if tp == "bool" && type_classmethods(tp).contains(&meth) {
let n = call_builtin_function(&format!("int.{meth}"), args, kwargs)?;
return Ok(Value::Bool(with_host(|h| h.truthy(&n))));
}
if meth == "__class_getitem__" && type_extra_names(tp).contains(&meth) {
let ty = with_host(|h| h.alloc(PyObj::Builtin(tp.to_string())));
return host::generic_alias(&ty, &arg0(&args)?);
}
if meth == "__getformat__" && tp == "float" {
return float_getformat(args.first());
}
}
if let Some((tp, meth)) = name.split_once('.') {
if (is_builtin_type(tp) || is_native_collection(tp))
&& type_has_method(tp, meth)
&& !type_classmethods(tp).contains(&meth)
{
let Some(recv) = args.first().cloned() else {
return Err(host::type_error(&format!(
"descriptor '{meth}' of '{tp}' object needs an argument"
)));
};
if let Some(e) = unbound_receiver_error(tp, meth, &recv) {
return Err(e);
}
return call_type_method(&recv, meth, args[1..].to_vec(), kwargs);
}
}
if name == "_tokenize.TokenizerIter" {
let source = arg0(&args)?;
let src = match with_host(|h| h.as_str(&source)) {
Some(s) => s,
None => crate::stdlib::pytokenize::drain_readline(&source)?,
};
let sv = with_host(|h| h.new_str(src));
return with_host(|h| crate::stdlib::pytokenize::tokenizer_iter(h, &[sv], &kwargs));
}
if name == "_imp.create_builtin" {
let spec = arg0(&args)?;
let n = with_host(|h| h.get_attr(&spec, "name").ok().and_then(|v| h.as_str(&v)));
return match n {
Some(n) => host::import_module(crate::stdlib::pyimp::builtin_source(&n)),
None => Ok(Value::Undef),
};
}
if let Some(f) = name.strip_prefix("marshal.") {
if let Some(r) = with_host(|h| crate::stdlib::pyimp::call(h, f, &args)) {
return r;
}
}
if let Some(f) = name.strip_prefix("_imp.") {
if let Some(r) = with_host(|h| crate::stdlib::pyimp::call(h, f, &args)) {
return r;
}
}
if let Some(f) = name.strip_prefix("_opcode.") {
if let Some(r) = with_host(|h| crate::stdlib::pyopcode::call(h, f, &args)) {
return r;
}
}
if let Some(f) = name.strip_prefix("_hash.") {
if let Some(algo) = crate::stdlib::pyhash::Algo::from_name(f) {
return with_host(|h| crate::stdlib::pyhash::construct(h, algo, &args, &kwargs));
}
}
if name == "_csv.reader" {
let src = arg0(&args)?;
let dialect =
with_host(|h| crate::stdlib::pycsv::Dialect::resolve_public(h, args.get(1), &kwargs))?;
let lines = host::iter_vec(&src)?;
let mut text = String::new();
for l in &lines {
let s = with_host(|h| h.as_str(l)).unwrap_or_default();
text.push_str(&s);
if !s.ends_with('\n') && !s.ends_with('\r') {
text.push('\n');
}
}
return Ok(with_host(|h| {
let rows: Vec<Value> = crate::stdlib::pycsv::parse_rows(&text, &dialect)
.into_iter()
.map(|r| {
let fields: Vec<Value> = r.into_iter().map(|f| h.new_str(f)).collect();
h.new_list(fields)
})
.collect();
h.alloc(host::PyObj::CsvReader {
rows,
idx: 0,
dialect: Box::new(dialect),
})
}));
}
if let Some(f) = name.strip_prefix("_csv.") {
if let Some(r) = with_host(|h| crate::stdlib::pycsv::call(h, f, &args, &kwargs)) {
return r;
}
}
if let Some(f) = name.strip_prefix("_signal.") {
if let Some(r) = with_host(|h| crate::stdlib::pysignal::call(h, f, &args)) {
return r;
}
}
if let Some(f) = name.strip_prefix("_io.") {
if let Some(r) = with_host(|h| crate::stdlib::pyio::call(h, f, &args, &kwargs)) {
return r;
}
}
if let Some(f) = name.strip_prefix("_struct.") {
if let Some(r) = with_host(|h| crate::stdlib::pystruct::call(h, f, &args)) {
return r;
}
}
if let Some(f) = name.strip_prefix("_codecs.") {
if let Some(r) = crate::stdlib::codecs::call_unborrowed(f, &args, &kwargs) {
return r;
}
if let Some(r) = with_host(|h| crate::stdlib::codecs::call(h, f, &args, &kwargs)) {
return r;
}
}
if let Some(f) = name.strip_prefix("binascii.") {
if let Some(r) = with_host(|h| crate::stdlib::binascii::call(h, f, &args, &kwargs)) {
return r;
}
}
if let Some(f) = name.strip_prefix("collections.") {
return construct_collection(f, args, kwargs);
}
if let Some(f) = name.strip_prefix("_contextvars.") {
return match f {
"ContextVar" => {
let a0 = arg0(&args)?;
let name = with_host(|h| h.as_str(&a0))
.ok_or_else(|| host::type_error("ContextVar() argument 1 must be str"))?;
let default = args
.get(1)
.cloned()
.or_else(|| kw_get(&kwargs, "default"))
.map(Box::new);
Ok(with_host(|h| {
h.alloc(PyObj::ContextVar {
name,
default,
value: None,
})
}))
}
"Context" | "copy_context" => Ok(with_host(|h| h.alloc(PyObj::ContextObj))),
_ => Err(host::name_error(&format!("_contextvars.{f}"))),
};
}
if name == excgroup::GROUP || name == excgroup::BASE_GROUP {
return with_host(|h| excgroup::construct(h, name, &args));
}
if is_exception_class(name) {
return construct_builtin_exception(name, args, &kwargs);
}
reject_kwargs(name, &kwargs)?;
match name {
"print" => {
let text_kw = |name: &str, default: &str| -> Result<String, String> {
match kw_get(&kwargs, name) {
None | Some(Value::Undef) => Ok(default.to_string()),
Some(v) => with_host(|h| h.as_str(&v)).ok_or_else(|| {
host::type_error(&format!(
"{name} must be None or a string, not {}",
with_host(|h| h.type_name(&v))
))
}),
}
};
let sep = text_kw("sep", " ")?;
let end = text_kw("end", "\n")?;
let flush = match kw_get(&kwargs, "flush") {
None => false,
Some(v) => py_bool(&v)?,
};
let file = match kw_get(&kwargs, "file").filter(|f| !matches!(f, Value::Undef)) {
Some(f) => Some(f),
None => match with_host(|h| h.std_target(false)) {
Some(Value::Undef) => return Ok(Value::Undef),
target => target,
},
};
let write = |s: &str| match &file {
Some(f) => host::write_to_stream(f, s),
None => {
with_host(|h| h.write_out(s, false));
Ok(())
}
};
for (i, a) in args.iter().enumerate() {
if i > 0 {
write(&sep)?;
}
write(&py_str(a)?)?;
}
write(&end)?;
if flush {
match &file {
Some(f) => host::flush_stream(f)?,
None => crate::stdio::flush(crate::stdio::Stream::Stdout),
}
}
Ok(Value::Undef)
}
"len" => {
check_arity("len", "len", Arity::ExactlyOne, args.len())?;
let v = arg0(&args)?;
let n = py_len(&v)?;
Ok(Value::Int(n as i64))
}
"range" => make_range(&args),
"slice" => {
let (lo, hi, step) = match args.len() {
1 => (Value::Undef, args[0].clone(), Value::Undef),
2 => (args[0].clone(), args[1].clone(), Value::Undef),
3 => (args[0].clone(), args[1].clone(), args[2].clone()),
0 => {
return Err(host::type_error(
"slice expected at least 1 argument, got 0",
))
}
n => {
return Err(host::type_error(&format!(
"slice expected at most 3 arguments, got {n}"
)))
}
};
Ok(with_host(|h| h.alloc(PyObj::Slice { lo, hi, step })))
}
"abs" => {
check_arity("abs", "abs", Arity::ExactlyOne, args.len())?;
let v = arg0(&args)?;
if with_host(
|h| matches!(h.get(&v), Some(PyObj::Instance(i)) if instance_has(h, i, "__abs__")),
) {
return host::call_method(&v, "__abs__", vec![], vec![]);
}
let v = host::subclass_operand(&v, "__abs__");
#[cfg(feature = "stdlib-ffi")]
if with_host(|h| h.foreign_id(&v).is_some()) {
return crate::ffi::unary_op_cb("abs", &v);
}
with_host(|h| match &v {
Value::Int(n) => Ok(match n.checked_abs() {
Some(a) => Value::Int(a),
None => h.norm_big(-num_bigint::BigInt::from(*n)),
}),
Value::Float(f) => Ok(Value::Float(host::new_float(f.abs()))),
Value::Bool(b) => Ok(Value::Int(*b as i64)),
Value::Obj(_) if matches!(h.get(&v), Some(PyObj::BigInt(_))) => match h.get(&v) {
Some(PyObj::BigInt(b)) => {
let b = b.clone();
Ok(h.norm_big(b.magnitude().clone().into()))
}
_ => unreachable!(),
},
Value::Obj(_) if matches!(h.get(&v), Some(PyObj::Complex(..))) => match h.get(&v) {
Some(PyObj::Complex(r, i)) => Ok(Value::Float(r.hypot(*i))),
_ => unreachable!(),
},
_ => Err(host::type_error(&format!(
"bad operand type for abs(): '{}'",
h.type_name(&v)
))),
})
}
"min" => reduce_minmax(&args, &kwargs, false),
"max" => reduce_minmax(&args, &kwargs, true),
"sum" => {
let total = args.len() + kwargs.len();
if args.is_empty() {
return Err(host::type_error(
"sum() takes at least 1 positional argument (0 given)",
));
}
if total > 2 {
return Err(host::type_error(&format!(
"sum() takes at most 2 arguments ({total} given)"
)));
}
let [seq, startv] = bind_named(
"sum",
["iterable", "start"],
1,
1,
KwStyle::Unexpected,
&args,
&kwargs,
)?;
let seq = seq.ok_or_else(|| {
host::type_error("sum() takes at least 1 positional argument (0 given)")
})?;
let start = startv.unwrap_or(Value::Int(0));
if let Some(msg) = sum_bad_start(&start) {
return Err(host::type_error(&format!("sum() can't sum {msg}")));
}
let items = host::iter_vec(&seq)?;
let mut acc = start;
let mut idx = 0;
while idx < items.len() && sum_is_long(&acc) {
let item = &items[idx];
acc = if sum_is_long(item) {
with_host(|h| h.arith(NumOp::Add, &acc, item))?
} else {
numeric_hook(NumOp::Add, &acc, item)?
};
idx += 1;
if !sum_is_long(item) {
break;
}
}
if let Value::Float(mut hi) = acc {
let mut lo = 0.0f64;
while idx < items.len() {
let item = &items[idx];
let x = if let Value::Float(x) = *item {
x
} else if sum_is_long(item) {
with_host(|h| h.num_val(item)).unwrap()
} else {
break;
};
let (nh, nl) = cs_add(hi, lo, x);
hi = nh;
lo = nl;
idx += 1;
}
acc = Value::Float(cs_to_double(hi, lo));
}
while idx < items.len() {
acc = numeric_hook(NumOp::Add, &acc, &items[idx])?;
idx += 1;
}
Ok(acc)
}
"sorted" => py_sorted(&args, &kwargs),
"reversed" => {
let v = arg0(&args)?;
if with_host(|h| matches!(h.get(&v), Some(PyObj::Instance(_)))) {
if let Some(payload) = host::subclass_payload(&v, "__reversed__") {
return call_builtin_function("reversed", vec![payload], vec![]);
}
let (has_rev, has_gi) = with_host(|h| match h.get(&v) {
Some(PyObj::Instance(i)) => (
h.class_lookup(&i.class, "__reversed__").is_some(),
h.class_lookup(&i.class, "__getitem__").is_some()
&& h.class_lookup(&i.class, "__len__").is_some(),
),
_ => (false, false),
});
if has_rev {
return host::call_method(&v, "__reversed__", vec![], vec![]);
}
if has_gi {
let n = py_len(&v)?;
let mut items = Vec::with_capacity(n);
for i in (0..n as i64).rev() {
items.push(host::call_method(
&v,
"__getitem__",
vec![Value::Int(i)],
vec![],
)?);
}
return Ok(with_host(|h| h.new_iter_kind(items, IterKind::Reversed)));
}
return Err(host::type_error(&format!(
"'{}' object is not reversible",
with_host(|h| h.type_name(&v))
)));
}
let not_reversible = with_host(|h| match v {
Value::Undef | Value::Bool(_) | Value::Int(_) | Value::Float(_) => true,
Value::Obj(_) => matches!(
h.get(&v),
Some(
PyObj::BigInt(_)
| PyObj::Complex(..)
| PyObj::Set(_)
| PyObj::Frozenset(_)
| PyObj::Generator { .. }
| PyObj::Iter(_)
| PyObj::Zip { .. }
| PyObj::MapObj { .. }
| PyObj::FilterObj { .. }
| PyObj::EnumerateObj { .. }
| PyObj::ItertoolsIter { .. }
| PyObj::CallIter { .. }
)
),
_ => false,
});
if not_reversible {
return Err(host::type_error(&format!(
"'{}' object is not reversible",
with_host(|h| h.type_name(&v))
)));
}
let kind = with_host(|h| match h.get(&v) {
Some(PyObj::List(_)) => IterKind::ListReverse,
Some(PyObj::Dict(_)) => IterKind::DictReverseKey,
Some(PyObj::DictView { .. }) => h.view_iter_kind(&v).reversed(),
Some(PyObj::Range { .. }) => IterKind::Range,
Some(PyObj::BigRange { .. }) => IterKind::LongRange,
_ => IterKind::Reversed,
});
let mut items = host::iter_vec(&v)?;
items.reverse();
Ok(with_host(|h| h.new_iter_kind(items, kind)))
}
"enumerate" => {
let [it, startv] = bind_named(
"enumerate",
["iterable", "start"],
0,
1,
KwStyle::Invalid,
&args,
&kwargs,
)?;
let it = it.ok_or_else(|| {
host::type_error("enumerate() missing required argument 'iterable' (pos 1)")
})?;
let source = host::make_iterator(&it)?;
let start = startv
.and_then(|v| with_host(|h| h.as_int(&v)))
.unwrap_or(0);
Ok(with_host(|h| {
h.alloc(PyObj::EnumerateObj {
source,
next: start,
done: false,
})
}))
}
"zip" => {
if let Some((k, _)) = kwargs.iter().find(|(k, _)| k != "strict") {
return Err(host::type_error(&format!(
"zip() got an unexpected keyword argument '{k}'"
)));
}
let mut sources = Vec::with_capacity(args.len());
for a in &args {
sources.push(host::make_iterator(a)?);
}
let strict = kw_get(&kwargs, "strict")
.map(|v| with_host(|h| h.truthy(&v)))
.unwrap_or(false);
Ok(with_host(|h| {
h.alloc(PyObj::Zip {
sources,
strict,
done: false,
})
}))
}
"map" => {
if kwargs.len() > 1 {
return Err(host::type_error(&format!(
"map() takes at most 1 keyword argument ({} given)",
kwargs.len()
)));
}
if let Some((k, _)) = kwargs.iter().find(|(k, _)| k != "strict") {
return Err(host::type_error(&format!(
"map() got an unexpected keyword argument '{k}'"
)));
}
if args.len() < 2 {
return Err(host::type_error("map() must have at least two arguments."));
}
let f = arg0(&args)?;
let mut sources = Vec::with_capacity(args.len().saturating_sub(1));
for a in &args[1..] {
sources.push(host::make_iterator(a)?);
}
Ok(with_host(|h| {
h.alloc(PyObj::MapObj {
func: f,
sources,
done: false,
})
}))
}
"filter" => {
let f = arg0(&args)?;
let it = args.get(1).ok_or_else(|| {
host::type_error(&format!("filter expected 2 arguments, got {}", args.len()))
})?;
let source = host::make_iterator(it)?;
Ok(with_host(|h| {
h.alloc(PyObj::FilterObj {
func: f,
source,
done: false,
})
}))
}
"any" => {
let items = host::iter_vec(&arg0(&args)?)?;
for x in &items {
if py_bool(x)? {
return Ok(Value::Bool(true));
}
}
Ok(Value::Bool(false))
}
"all" => {
let items = host::iter_vec(&arg0(&args)?)?;
for x in &items {
if !py_bool(x)? {
return Ok(Value::Bool(false));
}
}
Ok(Value::Bool(true))
}
"round" => {
let [num, ndv] = bind_named(
"round",
["number", "ndigits"],
0,
1,
KwStyle::Unexpected,
&args,
&kwargs,
)?;
let v = num.ok_or_else(|| {
host::type_error("round() missing required argument 'number' (pos 1)")
})?;
let has_nd = matches!(&ndv, Some(x) if !matches!(x, Value::Undef));
let nd = ndv.as_ref().and_then(|v| with_host(|h| h.as_int(v)));
match &v {
Value::Bool(b) => Ok(round_int(&num_bigint::BigInt::from(*b as i64), has_nd, nd)),
Value::Int(n) => Ok(round_int(&num_bigint::BigInt::from(*n), has_nd, nd)),
Value::Obj(_) if with_host(|h| matches!(h.get(&v), Some(PyObj::BigInt(_)))) => {
let n = with_host(|h| h.big_val(&v)).unwrap();
Ok(round_int(&n, has_nd, nd))
}
Value::Float(f) => round_float(*f, has_nd, nd),
_ => {
let has_round = with_host(|h| match h.get(&v) {
Some(PyObj::Instance(i)) => h.class_lookup(&i.class, "__round__").is_some(),
_ => h.foreign_id(&v).is_some(),
});
if has_round {
let margs = match (&ndv, has_nd) {
(Some(n), true) => vec![n.clone()],
_ => vec![],
};
host::call_method(&v, "__round__", margs, vec![])
} else {
Err(host::type_error(&format!(
"type {} doesn't define __round__ method",
with_host(|h| h.type_name(&v))
)))
}
}
}
}
"divmod" => {
let a = arg0(&args)?;
let b = args.get(1).cloned().unwrap_or(Value::Int(0));
if let Some(r) = try_binop_dunder(&a, &b, "__divmod__", "__rdivmod__") {
return r;
}
let name_it = |e: String| {
e.replace(
"unsupported operand type(s) for //:",
"unsupported operand type(s) for divmod():",
)
};
let q = with_host(|h| h.binop(host::binop::FLOORDIV, &a, &b)).map_err(name_it)?;
let r = with_host(|h| h.binop(host::binop::MOD, &a, &b))?;
Ok(with_host(|h| h.new_tuple(vec![q, r])))
}
"pow" => {
let (a, b, m) = pow_args(&args, &kwargs)?;
match m {
None | Some(Value::Undef) => numeric_hook(NumOp::Pow, &a, &b),
Some(m) => ternary_pow(&a, &b, &m),
}
}
"type" => {
if args.len() == 3 {
let base_names: Vec<String> = with_host(|h| match h.get(&args[1]) {
Some(PyObj::Tuple(items)) | Some(PyObj::List(items)) => items
.iter()
.filter_map(|b| match h.get(b) {
Some(PyObj::Class(n)) => Some(n.clone()),
Some(PyObj::Builtin(n)) if n != "object" => Some(n.clone()),
_ => None,
})
.collect(),
_ => vec![],
});
let meta = with_host(|h| host::default_metaclass(h, &base_names));
if meta != "type" {
let metaobj = with_host(|h| h.alloc(PyObj::Class(meta)));
return host::invoke(
&metaobj,
vec![args[0].clone(), args[1].clone(), args[2].clone()],
kwargs,
);
}
return type_new(&args[0], &args[1], &args[2]);
}
if args.len() != 1 {
return Err(host::type_error("type() takes 1 or 3 arguments"));
}
let v = arg0(&args)?;
#[cfg(feature = "stdlib-ffi")]
if let Some(fid) = with_host(|h| h.foreign_id(&v)) {
if let Some(tid) = crate::ffi::type_of(fid) {
if let Some(name) = crate::ffi::foreign_builtin_type_name(tid)
.filter(|n| BUILTIN_TYPES.contains(&n.as_str()))
{
return Ok(with_host(|h| h.builtin_object(&name)));
}
return Ok(with_host(|h| h.alloc(PyObj::Foreign(tid))));
}
}
Ok(with_host(|h| h.builtin_type_of(&v)))
}
"SimpleNamespace" => {
if !args.is_empty() {
return Err(host::type_error("no positional arguments expected"));
}
Ok(with_host(|h| {
let mut attrs = host::NameMap::default();
for (k, v) in kwargs {
attrs.insert(k, v);
}
h.alloc(PyObj::Namespace { attrs })
}))
}
"GenericAlias" => {
let origin = arg0(&args)?;
let idx = args.get(1).cloned().unwrap_or(Value::Undef);
Ok(with_host(|h| {
let alias_args = match h.get(&idx) {
Some(PyObj::Tuple(items)) => items.clone(),
_ => vec![idx.clone()],
};
h.alloc(PyObj::GenericAlias {
origin,
args: alias_args,
})
}))
}
"staticmethod" => {
let f = arg0(&args)?;
Ok(with_host(|h| h.alloc(PyObj::StaticMethod(f))))
}
"classmethod" => {
let f = arg0(&args)?;
Ok(with_host(|h| h.alloc(PyObj::ClassMethod(f))))
}
"super" => {
let (owner, instance) = if args.is_empty() {
let owner = with_host(|h| h.current_owner())
.ok_or_else(|| "RuntimeError: super(): no arguments".to_string())?;
let inst = with_host(|h| h.current_self())
.ok_or_else(|| "RuntimeError: super(): no arguments".to_string())?;
#[cfg(feature = "stdlib-ffi")]
if let Some(cls) = with_host(|h| h.foreign_class_cell(&owner)) {
return foreign_super(vec![cls, inst]);
}
(owner, inst)
} else {
let cls = arg0(&args)?;
#[cfg(feature = "stdlib-ffi")]
if with_host(|h| h.foreign_id(&cls)).is_some() {
return foreign_super(args);
}
let owner = with_host(|h| callable_name(h, &cls))
.ok_or_else(|| host::type_error("super() argument 1 must be a type"))?;
let inst = args.get(1).cloned().unwrap_or(Value::Undef);
(owner, inst)
};
Ok(with_host(|h| h.alloc(PyObj::Super { owner, instance })))
}
"isinstance" => {
let v = arg0(&args)?;
let cls = args.get(1).cloned().unwrap_or(Value::Undef);
isinstance_dispatch(&v, &cls).map(Value::Bool)
}
"issubclass" => {
let a0 = arg0(&args)?;
let a1 = args.get(1).cloned().unwrap_or(Value::Undef);
issubclass_dispatch(&a0, &a1).map(Value::Bool)
}
"__import__" => {
let name_v = arg0(&args)?;
let name = with_host(|h| h.str_of(&name_v));
let fromlist = args.get(3).cloned().or_else(|| {
kwargs
.iter()
.find(|(k, _)| k == "fromlist")
.map(|(_, v)| v.clone())
});
let fromlist_empty = match fromlist {
None | Some(Value::Undef) => true,
Some(v) => with_host(|h| !h.truthy(&v)),
};
let target = if fromlist_empty {
name.split('.').next().unwrap_or(&name).to_string()
} else {
name
};
host::import_module(&target)
}
"hasattr" => {
let v = arg0(&args)?;
let n = with_host(|h| h.str_of(&args.get(1).cloned().unwrap_or(Value::Undef)));
match get_attr_desc(&v, &n) {
Ok(_) => Ok(Value::Bool(true)),
Err(e) if is_attr_err(&e) => Ok(Value::Bool(false)),
Err(e) => Err(e),
}
}
"getattr" => {
let v = arg0(&args)?;
let n = with_host(|h| h.str_of(&args.get(1).cloned().unwrap_or(Value::Undef)));
match get_attr_desc(&v, &n) {
Ok(x) => Ok(x),
Err(e) => match args.get(2) {
Some(d) if is_attr_err(&e) => Ok(d.clone()),
_ => Err(e),
},
}
}
"setattr" => {
let v = arg0(&args)?;
let n = with_host(|h| h.str_of(&args.get(1).cloned().unwrap_or(Value::Undef)));
let val = args.get(2).cloned().unwrap_or(Value::Undef);
set_attr_desc(&v, &n, val)?;
Ok(Value::Undef)
}
"delattr" => {
let v = arg0(&args)?;
let n = with_host(|h| h.str_of(&args.get(1).cloned().unwrap_or(Value::Undef)));
del_attr_desc(&v, &n)?;
Ok(Value::Undef)
}
"id" => {
let v = arg0(&args)?;
Ok(Value::Int(match v {
Value::Obj(i) => i as i64,
Value::Int(n) => n,
_ => 0,
}))
}
"hash" => {
let v = arg0(&args)?;
if with_host(|h| matches!(h.get(&v), Some(PyObj::Instance(_)))) {
return host::instance_hash_value(&v).map(Value::Int);
}
let k = host::with_instance_key(&v, host::KeyRole::Bare, &[], || {
with_host(|h| h.to_key(&v))
})?;
Ok(Value::Int(hash_key(&k)))
}
"ord" => {
let a0 = arg0(&args)?;
let n = with_host(|h| -> Result<i64, String> {
if let Some(s) = h.as_str(&a0) {
let len = s.chars().count();
return if len == 1 {
Ok(s.chars().next().unwrap() as i64)
} else {
Err(host::type_error(&format!(
"ord() expected a character, but string of length {len} found"
)))
};
}
match h.get(&a0) {
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) if b.len() == 1 => {
Ok(b[0] as i64)
}
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => {
Err(host::type_error(&format!(
"ord() expected a character, but string of length {} found",
b.len()
)))
}
_ => Err(host::type_error(&format!(
"ord() expected string of length 1, but {} found",
h.type_name(&a0)
))),
}
})?;
Ok(Value::Int(n))
}
"chr" => {
let a0 = indexed(&arg0(&args)?)?;
let n = match with_host(|h| h.index_fit(&a0)) {
host::IndexFit::Fits(n) => n,
host::IndexFit::TooLarge(_) => {
return Err("ValueError: chr() arg not in range(0x110000)".to_string())
}
host::IndexFit::NotInt => {
return Err(host::type_error(&format!(
"'{}' object cannot be interpreted as an integer",
with_host(|h| h.type_name(&a0))
)))
}
};
if !(0..=0x10FFFF).contains(&n) {
return Err("ValueError: chr() arg not in range(0x110000)".to_string());
}
match char::from_u32(n as u32) {
Some(c) => Ok(with_host(|h| h.new_str(c.to_string()))),
None => Err("ValueError: chr() arg not in range(0x110000)".to_string()),
}
}
"hex" => int_radix(&args, 16, "0x"),
"oct" => int_radix(&args, 8, "0o"),
"bin" => int_radix(&args, 2, "0b"),
"repr" => {
let v = arg0(&args)?;
let s = py_repr(&v)?;
Ok(with_host(|h| h.new_str(s)))
}
"ascii" => {
let v = arg0(&args)?;
let out = host::ascii_of(&py_repr(&v)?);
Ok(with_host(|h| h.new_str(out)))
}
"format" => {
let v = arg0(&args)?;
let spec =
with_host(|h| h.str_of(&args.get(1).cloned().unwrap_or_else(|| Value::str(""))));
let out = format_field(&v, 0, &spec)?;
Ok(with_host(|h| h.new_str(out)))
}
"iter" => {
let v = arg0(&args)?;
if let Some(sentinel) = args.get(1) {
let sentinel = sentinel.clone();
return Ok(with_host(|h| {
h.alloc(PyObj::CallIter {
func: v,
sentinel,
done: false,
})
}));
}
let proto = with_host(|h| match h.get(&v) {
Some(PyObj::Instance(i)) => Some((
h.class_lookup(&i.class, "__iter__").is_some(),
h.class_lookup(&i.class, "__getitem__").is_some(),
)),
_ => None,
});
match proto {
Some((true, _)) => host::call_iter_dunder(&v),
Some((false, true)) => {
let items = host::iter_instance_items(&v)?;
Ok(with_host(|h| h.new_iter_seq(items)))
}
_ => with_host(|h| h.make_iter(&v)),
}
}
"next" => {
let it = arg0(&args)?;
if !host::is_iterator(&it) {
return Err(host::type_error(&format!(
"'{}' object is not an iterator",
with_host(|h| h.type_name(&it))
)));
}
match host::iter_step(&it)? {
Some(v) => Ok(v),
None => match args.get(1) {
Some(d) => Ok(d.clone()),
None => {
if with_host(|h| matches!(h.get(&it), Some(PyObj::Generator { .. }))) {
let e = host::gen_stop_iteration(&it);
Err(exc_error_string(&e))
} else {
Err("StopIteration".into())
}
}
},
}
}
"input" => {
host::flush_stderr()?;
if let Some(p) = args.first() {
let s = py_str(p)?;
host::write_stdout(&s)?;
}
host::flush_stdout()?;
let mut line = String::new();
let _ = std::io::stdin().read_line(&mut line);
let line = line
.trim_end_matches('\n')
.trim_end_matches('\r')
.to_string();
Ok(with_host(|h| h.new_str(line)))
}
"exit" | "quit" => {
let exc = with_host(|h| {
h.alloc(PyObj::Exception {
class: "SystemExit".into(),
args,
})
});
Err(host::raise_value(&exc)?)
}
"callable" => {
let v = arg0(&args)?;
Ok(Value::Bool(with_host(|h| {
match h.get(&v) {
Some(PyObj::Func(_))
| Some(PyObj::Builtin(_))
| Some(PyObj::Class(_))
| Some(PyObj::NamedTupleType { .. })
| Some(PyObj::Partial { .. })
| Some(PyObj::LruCache { .. })
| Some(PyObj::StaticMethod(_))
| Some(PyObj::ClassMethod(_))
| Some(PyObj::BoundMethod { .. }) => true,
Some(PyObj::Instance(i)) => h.class_lookup(&i.class, "__call__").is_some(),
_ => false,
}
})))
}
"property" => {
let fget = args
.first()
.cloned()
.or_else(|| kw_get(&kwargs, "fget"))
.unwrap_or(Value::Undef);
let fset = args
.get(1)
.cloned()
.or_else(|| kw_get(&kwargs, "fset"))
.unwrap_or(Value::Undef);
let fdel = args
.get(2)
.cloned()
.or_else(|| kw_get(&kwargs, "fdel"))
.unwrap_or(Value::Undef);
Ok(with_host(|h| {
h.alloc(PyObj::Property {
fget,
fset,
fdel,
name: String::new(),
})
}))
}
"vars" => match args.first() {
Some(v) => with_host(|h| h.get_attr(v, "__dict__")).map_err(|e| {
if e.starts_with("AttributeError") {
host::type_error("vars() argument must have __dict__ attribute")
} else {
e
}
}),
None => call_builtin_function("locals", vec![], vec![]),
},
"dir" => {
if args.is_empty() {
host::ensure_main_dunders();
}
if let Some(v) = args.first() {
if instance_dunder(v, "__dir__") {
let seq = host::call_method(v, "__dir__", vec![], vec![])?;
return call_builtin_function("sorted", vec![seq], vec![]);
}
if let Some(m) = with_host(|h| h.metaclass_method(v, "__dir__")) {
let seq = host::invoke(&m, vec![v.clone()], vec![])?;
return call_builtin_function("sorted", vec![seq], vec![]);
}
}
Ok(with_host(|h| {
let names = match args.first() {
Some(v) => h.dir_names(v),
None => {
if h.frame_depth() > 1 {
let mut ns: Vec<String> =
h.caller_locals().into_iter().map(|(k, _)| k).collect();
ns.sort();
ns
} else {
let slot = h.cur_module();
let mut ns: Vec<String> = h
.module_globals_pairs(slot)
.into_iter()
.map(|(k, _)| k)
.collect();
ns.sort();
ns
}
}
};
let items: Vec<Value> = names.into_iter().map(|n| h.new_str(n)).collect();
h.new_list(items)
}))
}
"globals" => {
host::ensure_main_dunders();
Ok(with_host(|h| {
let slot = h.cur_module();
h.module_dict(slot)
}))
}
"locals" => {
if with_host(|h| h.frame_depth() > 1) {
Ok(str_keyed_dict(with_host(|h| {
h.caller_locals().into_iter().collect()
})))
} else {
host::ensure_main_dunders();
Ok(with_host(|h| {
let slot = h.cur_module();
h.module_dict(slot)
}))
}
}
"eval" | "exec" => run_pysource(name == "eval", &args),
"compile" => builtin_compile(&args, &kwargs),
"int" => {
if let Some((k, _)) = kwargs.iter().find(|(k, _)| k != "base") {
return Err(host::type_error(&format!(
"int() got an unexpected keyword argument '{k}'"
)));
}
if args.is_empty() && !kwargs.is_empty() {
return Err(host::type_error("int() missing string argument"));
}
let mut a = args.clone();
if let Some(base) = kw_get(&kwargs, "base") {
if a.len() >= 2 {
a[1] = base;
} else {
if a.is_empty() {
a.push(Value::str(""));
}
a.push(base);
}
}
construct_int(&a)
}
"float" => construct_float(&args),
"str" => {
let [obj, enc, errs] = bind_named(
"str",
["object", "encoding", "errors"],
0,
0,
KwStyle::Unexpected,
&args,
&kwargs,
)?;
let v = obj.unwrap_or_else(|| Value::str(""));
if enc.is_some() || errs.is_some() {
let dargs: Vec<Value> = [enc, errs].into_iter().flatten().collect();
return call_type_method(&v, "decode", dargs, vec![]);
}
let s = py_str(&v)?;
Ok(with_host(|h| h.new_str(s)))
}
"bool" => {
let v = args.first().cloned().unwrap_or(Value::Bool(false));
Ok(Value::Bool(py_bool(&v)?))
}
"list" => {
let items = match args.first() {
Some(v) => host::iter_vec(v)?,
None => vec![],
};
Ok(with_host(|h| h.new_list(items)))
}
"tuple" => {
let items = match args.first() {
Some(v) => host::iter_vec(v)?,
None => vec![],
};
Ok(with_host(|h| h.new_tuple(items)))
}
"set" | "frozenset" => {
let items = match args.first() {
Some(v) => host::iter_vec(v)?,
None => vec![],
};
let mut s: IndexMap<PKey, Value> = IndexMap::new();
for it in items {
let cands = with_host(|h| host::set_local_candidates(h, &s));
let k = host::with_instance_key(&it, host::KeyRole::Set, &cands, || {
with_host(|h| h.to_key(&it))
})?;
host::set_put(&mut s, k, it);
}
let merged_from = args.first().and_then(set_merge_source);
Ok(with_host(|h| {
let out = h.new_setlike(s, name == "frozenset");
if let Some((incoming, src)) = merged_from {
let empty = IndexMap::new();
h.set_relayout_after_merge(&out, (&empty, host::SET_MINSIZE), incoming, src_ref(&src));
}
out
}))
}
"dict" => construct_dict(&args, &kwargs),
"complex" => {
let args: Vec<Value> = if kwargs.is_empty() {
args
} else {
bind_named(
"complex",
["real", "imag"],
0,
0,
KwStyle::Unexpected,
&args,
&kwargs,
)?
.into_iter()
.map(|s| s.unwrap_or(Value::Float(0.0)))
.collect()
};
if let Some(first) = args.first() {
if let Some(s) = with_host(|h| h.as_str(first)) {
if args.len() > 1 {
return Err(host::type_error(&format!(
"complex() argument 'real' must be a real number, not {}",
with_host(|h| h.type_name(first))
)));
}
let (r, i) = parse_complex(&s)?;
return Ok(with_host(|h| h.alloc(PyObj::Complex(r, i))));
}
if let Some((r, i)) = with_host(|h| match h.get(first) {
Some(PyObj::Complex(r, i)) => Some((*r, *i)),
_ => None,
}) {
if args.len() == 1 {
return Ok(with_host(|h| h.alloc(PyObj::Complex(r, i))));
}
}
}
let (r, ri) = match args.first() {
Some(v) => complex_parts(v, args.len() > 1)?,
None => (0.0, 0.0),
};
let i = match args.get(1) {
Some(v) => complex_imag(v)?,
None => 0.0,
};
Ok(with_host(|h| h.alloc(PyObj::Complex(r, i + ri))))
}
"bytes" | "bytearray" => {
let slots = bind_named(
name,
["source", "encoding", "errors"],
0,
0,
KwStyle::Unexpected,
&args,
&kwargs,
)?;
let [source, encoding, errors] = &slots;
let is_text = source
.as_ref()
.is_some_and(|s| with_host(|h| h.as_str(s).is_some()));
let misuse = match (is_text, encoding.is_some(), errors.is_some()) {
(true, false, _) => Some("string argument without an encoding"),
(false, true, _) => Some("encoding without a string argument"),
(false, false, true) => Some("errors without a string argument"),
_ => None,
};
if let Some(msg) = misuse {
return Err(host::type_error(msg));
}
if name == "bytes" {
if let Some(src) = source {
let has = with_host(|h| {
matches!(h.get(src), Some(PyObj::Instance(i)) if instance_has(h, i, "__bytes__"))
});
if has {
let r = host::call_method(src, "__bytes__", vec![], vec![])?;
if with_host(|h| matches!(h.get(&r), Some(PyObj::Bytes(_)))) {
return Ok(r);
}
return Err(with_host(|h| {
host::type_error(&format!(
"__bytes__ returned non-bytes (type {})",
h.tp_name(&r)
))
}));
}
}
}
let bargs: Vec<Value> = slots.into_iter().flatten().collect();
let b = build_bytes(&bargs)?;
Ok(with_host(|h| {
if name == "bytes" {
h.alloc(PyObj::Bytes(b))
} else {
h.alloc(PyObj::Bytearray(b))
}
}))
}
"memoryview" => {
let [obj] = bind_named(
"memoryview",
["object"],
0,
1,
KwStyle::Unexpected,
&args,
&kwargs,
)?;
let v = obj.unwrap();
with_host(|h| {
let (len, readonly) = match h.get(&v) {
Some(PyObj::Bytes(b)) => (b.len(), true),
Some(PyObj::Bytearray(b)) => (b.len(), false),
Some(PyObj::Memoryview {
obj,
start,
len,
readonly,
released,
..
}) => {
if *released {
return Err(host::MV_RELEASED.to_string());
}
let (obj, start, len, readonly) = (obj.clone(), *start, *len, *readonly);
return Ok(h.alloc(PyObj::Memoryview {
obj,
start,
len,
readonly,
released: false,
hashed: Default::default(),
}));
}
_ => {
return Err(host::type_error(&format!(
"memoryview: a bytes-like object is required, not '{}'",
h.type_name(&v)
)))
}
};
Ok(h.alloc(PyObj::Memoryview {
obj: v.clone(),
start: 0,
len,
readonly,
released: false,
hashed: Default::default(),
}))
})
}
"open" => {
let file = kw_get(&kwargs, "file")
.or_else(|| args.first().cloned())
.ok_or_else(|| host::type_error("open() missing required argument: 'file'"))?;
let path = with_host(|h| h.as_str(&file))
.ok_or_else(|| host::type_error("open() argument 'file' must be str"))?;
let mode = kw_get(&kwargs, "mode")
.or_else(|| args.get(1).cloned())
.and_then(|v| with_host(|h| h.as_str(&v)))
.unwrap_or_else(|| "r".into());
let str_arg = |name: &str, pos: usize| -> Option<String> {
kw_get(&kwargs, name)
.or_else(|| args.get(pos).cloned())
.filter(|v| !matches!(v, Value::Undef))
.and_then(|v| with_host(|h| h.as_str(&v)))
};
let encoding = str_arg("encoding", 3);
let newline = str_arg("newline", 5);
host::open_file(&path, &mode, encoding.as_deref(), newline.as_deref())
}
"object" => {
if !args.is_empty() || !kwargs.is_empty() {
return Err(host::type_error("object() takes no arguments"));
}
Ok(with_host(|h| {
h.new_instance("object".into(), host::NameMap::default())
}))
}
_ => match host::call_rust_ffi(name, &args) {
Some(r) => r,
None => Err(host::name_error(name)),
},
}
}
fn as_f(v: &Value) -> Option<f64> {
match v {
Value::Int(n) => Some(*n as f64),
Value::Float(f) => Some(*f),
Value::Bool(b) => Some(*b as i64 as f64),
_ => None,
}
}
fn as_f_wide(v: &Value) -> Result<f64, String> {
if let Some(f) = as_f(v) {
return Ok(f);
}
match with_host(|h| h.big_val(v)) {
Some(n) => {
use num_traits::ToPrimitive;
n.to_f64()
.filter(|f| f.is_finite())
.ok_or_else(|| "OverflowError: int too large to convert to float".to_string())
}
None => Ok(0.0),
}
}
fn pow_args(
args: &[Value],
kwargs: &[(String, Value)],
) -> Result<(Value, Value, Option<Value>), String> {
let [base, exp, m] = bind_named(
"pow",
["base", "exp", "mod"],
0,
2,
KwStyle::Unexpected,
args,
kwargs,
)?;
Ok((base.unwrap(), exp.unwrap(), m))
}
const NO_KWARG_BUILTINS: &[&str] = &[
"abs",
"aiter",
"all",
"any",
"ascii",
"bin",
"bool",
"callable",
"chr",
"delattr",
"dir",
"divmod",
"filter",
"float",
"format",
"frozenset",
"getattr",
"globals",
"hasattr",
"hash",
"hex",
"id",
"isinstance",
"issubclass",
"iter",
"len",
"list",
"locals",
"next",
"oct",
"ord",
"range",
"repr",
"reversed",
"set",
"setattr",
"slice",
"staticmethod",
"classmethod",
"super",
"tuple",
"vars",
];
fn reject_kwargs(name: &str, kwargs: &[(String, Value)]) -> Result<(), String> {
if !kwargs.is_empty() && NO_KWARG_BUILTINS.contains(&name) {
return Err(host::type_error(&format!(
"{name}() takes no keyword arguments"
)));
}
Ok(())
}
#[derive(Clone, Copy, PartialEq)]
enum KwStyle {
Unexpected,
Invalid,
}
fn bind_named<const N: usize>(
fname: &str,
names: [&str; N],
posonly: usize,
required: usize,
style: KwStyle,
args: &[Value],
kwargs: &[(String, Value)],
) -> Result<[Option<Value>; N], String> {
if args.len() > N {
return Err(host::type_error(&format!(
"{fname}() takes at most {N} arguments ({} given)",
args.len()
)));
}
let mut slots: [Option<Value>; N] = std::array::from_fn(|_| None);
for (i, v) in args.iter().enumerate() {
slots[i] = Some(v.clone());
}
let unknown = |k: &str| {
host::type_error(&match style {
KwStyle::Unexpected => format!("{fname}() got an unexpected keyword argument '{k}'"),
KwStyle::Invalid => format!("'{k}' is an invalid keyword argument for {fname}()"),
})
};
let mut deferred: Option<String> = None;
for (k, v) in kwargs {
match names.iter().position(|n| n == k) {
Some(i) if i < args.len() => {
return Err(host::type_error(&format!(
"argument for {fname}() given by name ('{k}') and position ({})",
i + 1
)))
}
Some(i) if i < posonly => deferred = deferred.or_else(|| Some(unknown(k))),
Some(i) => slots[i] = Some(v.clone()),
None => deferred = deferred.or_else(|| Some(unknown(k))),
}
}
if style == KwStyle::Invalid {
if let Some(e) = deferred {
return Err(e);
}
}
for (i, name) in names[..required].iter().enumerate() {
if slots[i].is_none() {
return Err(host::type_error(&format!(
"{fname}() missing required argument '{name}' (pos {})",
i + 1
)));
}
}
match deferred {
Some(e) => Err(e),
None => Ok(slots),
}
}
fn ternary_pow(a: &Value, b: &Value, m: &Value) -> Result<Value, String> {
let a = &host::subclass_operand(a, "__pow__");
let b = &host::subclass_operand(b, "__rpow__");
let m = &host::subclass_operand(m, "__pow__");
#[derive(PartialEq)]
enum Slot {
Int,
Float,
Complex,
User,
}
let slot_of = |v: &Value| {
with_host(|h| match v {
Value::Int(_) | Value::Bool(_) => Some(Slot::Int),
Value::Float(_) => Some(Slot::Float),
_ => match h.get(v) {
Some(PyObj::BigInt(_)) => Some(Slot::Int),
Some(PyObj::Complex(..)) => Some(Slot::Complex),
Some(PyObj::Instance(_)) => Some(Slot::User),
_ => None,
},
})
};
let mut offered: Vec<Slot> = Vec::new();
for v in [a, b, m] {
let Some(slot) = slot_of(v) else { continue };
if offered.contains(&slot) {
continue;
}
match slot {
Slot::Int => {
let all_int = [a, b, m]
.into_iter()
.all(|x| slot_of(x) == Some(Slot::Int));
if all_int {
return pow_mod(a, b, m);
}
}
Slot::Float => {
return Err(host::type_error(
"pow() 3rd argument not allowed unless all arguments are integers",
))
}
Slot::Complex => return Err("ValueError: complex modulo".into()),
Slot::User => match dispatch_ternary_pow(a, b, m) {
Dunder::Value(v) => return Ok(v),
Dunder::Err(e) => return Err(e),
Dunder::NotImplemented => {}
},
}
offered.push(slot);
}
Err(with_host(|h| {
host::type_error(&format!(
"unsupported operand type(s) for ** or pow(): '{}', '{}', '{}'",
h.tp_name(a),
h.tp_name(b),
h.tp_name(m)
))
}))
}
fn pow_mod(a: &Value, b: &Value, m: &Value) -> Result<Value, String> {
use num_bigint::BigInt;
use num_traits::ToPrimitive;
let (base, exp, modulus) = match with_host(|h| (h.big_val(a), h.big_val(b), h.big_val(m))) {
(Some(x), Some(y), Some(z)) => (x, y, z),
_ => {
return Err(host::type_error(
"pow() 3rd argument not allowed unless all arguments are integers",
))
}
};
let zero = BigInt::from(0);
if modulus == zero {
return Err("ValueError: pow() 3rd argument cannot be 0".into());
}
let (base, exp) = if exp < zero {
use num_integer::Integer;
let m_abs = if modulus < zero {
-&modulus
} else {
modulus.clone()
};
let base_r = base.mod_floor(&m_abs);
let egcd = base_r.extended_gcd(&m_abs);
if egcd.gcd != BigInt::from(1) {
return Err("ValueError: base is not invertible for the given modulus".into());
}
(egcd.x.mod_floor(&m_abs), -exp)
} else {
(base, exp)
};
let raw = base.modpow(&exp, &modulus);
let r = &raw % &modulus;
let r = if r != zero && (r < zero) != (modulus < zero) {
r + &modulus
} else {
r
};
Ok(with_host(|h| match r.to_i64() {
Some(n) => Value::Int(n),
None => h.alloc(PyObj::BigInt(r)),
}))
}
pub fn eval_in_current_frame(src: &str) -> Result<Value, String> {
let source = with_host(|h| h.new_str(src.to_string()));
run_pysource(true, &[source])
}
fn run_pysource(want_value: bool, args: &[Value]) -> Result<Value, String> {
let fname = if want_value { "eval" } else { "exec" };
let compiled = args.first().and_then(|v| {
with_host(|h| match h.get(v) {
Some(PyObj::CompiledSource {
source,
filename,
mode,
}) => Some((source.clone(), filename.clone(), mode.clone())),
_ => None,
})
});
let (filename, source_mode) = match &compiled {
Some((_, f, m)) => (f.clone(), m.clone()),
None => ("<string>".to_string(), fname.to_string()),
};
let result_wanted = want_value;
let want_value = source_mode == "eval";
let src = match args.first() {
Some(_) if compiled.is_some() => compiled.map(|c| c.0).unwrap_or_default(),
Some(v) => with_host(|h| h.as_str(v)).ok_or_else(|| {
host::type_error(&format!(
"{fname}() arg 1 must be a string, bytes or code object"
))
})?,
None => {
return Err(host::type_error(&format!(
"{fname}() takes at least 1 positional argument (0 given)"
)))
}
};
let ns_arg = |i: usize| match args.get(i) {
Some(v) if !matches!(v, Value::Undef) => Some(v.clone()),
_ => None,
};
let globals_arg = ns_arg(1);
let locals_arg = ns_arg(2);
let explicit_ns = globals_arg.is_some() || locals_arg.is_some();
let in_function = with_host(|h| h.frame_depth() > 1);
if want_value {
crate::parser::check_eval_source(&src)
.map_err(|e| crate::parser::with_filename(e, &filename))?;
}
const TMP: &str = "__pyrs_eval_result__";
let to_compile = if want_value {
format!("{TMP} = (\n{src}\n)")
} else {
src
};
let overlay_base: Option<host::NameMap> = if !explicit_ns && in_function {
let mut base = with_host(|h| h.snapshot_globals());
for (k, v) in with_host(|h| h.caller_locals()) {
base.insert(k, v);
}
Some(base)
} else {
None
};
let parked = with_host(|h| h.enter_module_scope());
let mut pre_existing: host::NameMap = host::NameMap::default();
let fresh_mod: Option<usize> = if explicit_ns {
let mut ns: host::NameMap = host::NameMap::default();
for d in [globals_arg.as_ref(), locals_arg.as_ref()]
.into_iter()
.flatten()
{
for (k, v) in dict_str_pairs(d)? {
ns.insert(k, v);
}
}
pre_existing = ns.clone();
Some(with_host(|h| {
let mid = h.new_module_slot(ns);
h.swap_module(mid)
}))
} else {
None
};
let overlay_saved: Option<host::NameMap> = if let Some(base) = overlay_base {
let snap = with_host(|h| h.snapshot_globals());
with_host(|h| h.replace_globals(base));
Some(snap)
} else {
None
};
let result = (|| -> Result<Value, String> {
let prog = if source_mode == "single" {
crate::compile_interactive(&to_compile)
} else {
crate::compile(&to_compile)
}
.map_err(|e| crate::parser::with_filename(e, &filename))?;
for (line, msg) in &prog.warnings {
eprintln!("{filename}:{line}: SyntaxWarning: {msg}");
}
let chunk = crate::load_merged(prog);
crate::host::run_chunk_on(chunk)?;
let value = if want_value {
with_host(|h| h.del_global(TMP)).unwrap_or(Value::Undef)
} else {
Value::Undef
};
Ok(if result_wanted { value } else { Value::Undef })
})();
if let Some(saved_mod) = fresh_mod {
let target = locals_arg.as_ref().or(globals_arg.as_ref());
if let Some(t) = target {
for (k, v) in with_host(|h| h.globals_pairs()) {
if k == TMP {
continue;
}
if pre_existing.get(&k).is_some_and(|old| {
std::mem::discriminant(old) == std::mem::discriminant(&v)
&& match (old, &v) {
(Value::Obj(a), Value::Obj(b)) => a == b,
_ => with_host(|h| h.equal(old, &v)),
}
}) {
continue;
}
let key = with_host(|h| h.new_str(k));
with_host(|h| h.set_item(t, &key, v))?;
}
}
with_host(|h| h.swap_module(saved_mod));
}
with_host(|h| h.restore_scope(parked));
if let Some(mut saved) = overlay_saved {
for name in exec_global_names(&to_compile) {
if let Some(v) = with_host(|h| {
h.globals_pairs()
.into_iter()
.find(|(k, _)| *k == name)
.map(|(_, v)| v)
}) {
saved.insert(name, v);
}
}
with_host(|h| h.replace_globals(saved));
}
result
}
fn builtin_compile(args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
const PARAMS: [&str; 6] = [
"source",
"filename",
"mode",
"flags",
"dont_inherit",
"optimize",
];
const PY_CF_ONLY_AST: i64 = 0x400;
if args.len() > PARAMS.len() {
return Err(host::type_error(&format!(
"compile() takes at most 6 arguments ({} given)",
args.len()
)));
}
let mut slots: Vec<Option<Value>> = vec![None; PARAMS.len()];
for (slot, a) in slots.iter_mut().zip(args) {
*slot = Some(a.clone());
}
for (k, v) in kwargs {
match PARAMS.iter().position(|p| p == k) {
Some(i) => slots[i] = Some(v.clone()),
None if k == "_feature_version" => {}
None => {
return Err(host::type_error(&format!(
"compile() got an unexpected keyword argument '{k}'"
)))
}
}
}
let need = |i: usize| {
slots[i].clone().ok_or_else(|| {
host::type_error(&format!(
"compile() missing required argument '{}' (pos {})",
PARAMS[i],
i + 1
))
})
};
let (source_v, filename_v, mode_v) = (need(0)?, need(1)?, need(2)?);
let type_of = |v: &Value| with_host(|h| h.type_name(v));
let text_of = |v: &Value| {
with_host(|h| match h.get(v) {
Some(PyObj::Bytes(b)) => Some(String::from_utf8_lossy(b).into_owned()),
_ => h.as_str(v),
})
};
let source = text_of(&source_v)
.ok_or_else(|| host::type_error("compile() arg 1 must be a string, bytes or AST object"))?;
let filename = text_of(&filename_v).ok_or_else(|| {
host::type_error(&format!(
"expected str, bytes or os.PathLike object, not {}",
type_of(&filename_v)
))
})?;
let mode = with_host(|h| h.as_str(&mode_v)).ok_or_else(|| {
host::type_error(&format!(
"compile() argument 'mode' must be str, not {}",
type_of(&mode_v)
))
})?;
if !matches!(mode.as_str(), "exec" | "eval" | "single") {
return Err("ValueError: compile() mode must be 'exec', 'eval' or 'single'".to_string());
}
let flags = match &slots[3] {
None => 0,
Some(Value::Int(n)) => *n,
Some(Value::Bool(b)) => *b as i64,
Some(other) => {
return Err(host::type_error(&format!(
"'{}' object cannot be interpreted as an integer",
type_of(other)
)))
}
};
if source.contains('\0') {
return Err("SyntaxError: source code string cannot contain null bytes".to_string());
}
if flags & PY_CF_ONLY_AST != 0 {
let ast = host::import_module("ast")?;
let parse = with_host(|h| h.get_attr(&ast, "parse"))?;
return host::invoke(&parse, vec![source_v, filename_v, mode_v], Vec::new());
}
let named = |e: String| crate::parser::with_filename(e, &filename);
if mode == "eval" {
crate::parser::check_eval_source(&source).map_err(named)?;
} else {
crate::compile(&source).map_err(named)?;
}
Ok(with_host(|h| {
h.alloc(PyObj::CompiledSource {
source,
filename,
mode,
})
}))
}
fn exec_global_names(src: &str) -> Vec<String> {
fn walk(stmts: &[crate::ast::Stmt], out: &mut Vec<String>) {
use crate::ast::StmtKind::*;
for st in stmts {
match &st.kind {
Global(names) => out.extend(names.iter().cloned()),
If { body, orelse, .. } | While { body, orelse, .. } => {
walk(body, out);
walk(orelse, out);
}
For { body, orelse, .. } => {
walk(body, out);
walk(orelse, out);
}
With { body, .. } | ClassDef { body, .. } => walk(body, out),
Try {
body,
handlers,
orelse,
finalbody,
} => {
walk(body, out);
for h in handlers {
walk(&h.body, out);
}
walk(orelse, out);
walk(finalbody, out);
}
Match { cases, .. } => {
for c in cases {
walk(&c.body, out);
}
}
_ => {}
}
}
}
let mut out = Vec::new();
if !src.contains("global") {
return out;
}
if let Ok(stmts) = crate::parser::parse(src) {
walk(&stmts, &mut out);
}
out
}
fn str_keyed_dict(pairs: Vec<(String, Value)>) -> Value {
with_host(|h| {
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::with_capacity(pairs.len());
for (k, v) in pairs {
let key = h.new_str(k.clone());
d.insert(PKey::Str(k), (key, v));
}
h.new_dict(d)
})
}
fn dict_str_pairs(d: &Value) -> Result<Vec<(String, Value)>, String> {
if let Some(slot) = with_host(|h| h.module_dict_slot(d)) {
return Ok(with_host(|h| h.module_globals_pairs(slot)));
}
with_host(|h| {
let target = match h.get(d) {
Some(PyObj::MappingProxy { dict }) => dict.clone(),
_ => d.clone(),
};
match h.get(&target) {
Some(PyObj::Dict(m)) => Ok(m
.iter()
.filter_map(|(_, (kv, v))| h.as_str(kv).map(|s| (s, v.clone())))
.collect()),
_ => Err(host::type_error("globals must be a real dictionary")),
}
})
}
fn arg0(args: &[Value]) -> Result<Value, String> {
args.first()
.cloned()
.ok_or_else(|| host::type_error("missing required argument"))
}
pub fn hash_key(k: &PKey) -> i64 {
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
match k {
PKey::Foreign { hash, .. } | PKey::Instance { hash, .. } => *hash,
PKey::None => crate::pyhash::NONE,
PKey::Int(n) => crate::pyhash::int_i64(*n),
PKey::Big(b) => crate::pyhash::int_big(b),
PKey::Str(s) => crate::pyhash::string(s),
PKey::Bytes(b) => crate::pyhash::buffer(b),
PKey::Tuple(ks) | PKey::Range(ks) => {
let elems: Vec<i64> = ks.iter().map(hash_key).collect();
crate::pyhash::tuple(&elems)
}
PKey::Frozenset(ks) => {
let elems: Vec<i64> = ks.iter().map(hash_key).collect();
crate::pyhash::frozenset(&elems)
}
PKey::Slice(ks) => {
let mut elems = [crate::pyhash::NONE; 3];
for (slot, k) in elems.iter_mut().zip(ks) {
*slot = hash_key(k);
}
crate::pyhash::slice(&elems)
}
PKey::FloatBits(bits) => match crate::pyhash::double(f64::from_bits(*bits)) {
Some(h) => h,
None => {
let mut h = DefaultHasher::new();
hash_pkey_into(k, &mut h);
h.finish() as i64
}
},
PKey::Complex(re, im) => {
match crate::pyhash::complex(f64::from_bits(*re), f64::from_bits(*im)) {
Some(h) => h,
None => {
let mut h = DefaultHasher::new();
hash_pkey_into(k, &mut h);
h.finish() as i64
}
}
}
PKey::Class(_) | PKey::Singleton(_) => {
let mut h = DefaultHasher::new();
hash_pkey_into(k, &mut h);
h.finish() as i64
}
}
}
fn hash_pkey_into<H: std::hash::Hasher>(k: &PKey, st: &mut H) {
use std::hash::Hash;
match k {
PKey::Instance { hash, .. } | PKey::Foreign { hash, .. } => {
st.write_u8(0);
hash.hash(st);
}
PKey::Tuple(ks) => {
st.write_u8(1);
st.write_usize(ks.len());
for e in ks {
hash_pkey_into(e, st);
}
}
PKey::Frozenset(ks) => {
st.write_u8(2);
st.write_usize(ks.len());
for e in ks {
hash_pkey_into(e, st);
}
}
other => {
st.write_u8(3);
other.hash(st);
}
}
}
pub fn py_len(v: &Value) -> Result<usize, String> {
#[cfg(feature = "stdlib-ffi")]
if let Some(fid) = with_host(|h| h.foreign_id(v)) {
return crate::ffi::len(fid);
}
if let Some(m) = with_host(|h| h.metaclass_method(v, "__len__")) {
let r = host::invoke(&m, vec![v.clone()], vec![])?;
return len_result(&r);
}
with_host(|h| match h.get(v) {
Some(PyObj::Str(s)) => {
let Value::Obj(id) = *v else { unreachable!() };
Ok(h.str_char_len(id, s))
}
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => Ok(b.len()),
Some(PyObj::Memoryview { released: true, .. }) => Err(host::MV_RELEASED.to_string()),
Some(PyObj::Memoryview { len, .. }) => Ok(*len),
Some(PyObj::Deque { items, .. }) => Ok(items.len()),
Some(PyObj::List(l)) | Some(PyObj::Tuple(l)) => Ok(l.len()),
Some(PyObj::StructTime { .. }) => Ok(9),
Some(PyObj::Dict(d)) => Ok(d.len()),
Some(PyObj::Set(s)) | Some(PyObj::Frozenset(s)) => Ok(s.len()),
Some(PyObj::DictView { dict, .. }) | Some(PyObj::MappingProxy { dict }) => {
Ok(match h.get(dict) {
Some(PyObj::Dict(d)) => d.len(),
_ => 0,
})
}
Some(PyObj::Range { start, stop, step }) => {
i64::try_from(host::range_len_exact(*start, *stop, *step).max(0))
.map(|n| n as usize)
.map_err(|_| {
"OverflowError: Python int too large to convert to C ssize_t".to_string()
})
}
Some(PyObj::BigRange { start, stop, step }) => {
use num_traits::ToPrimitive;
host::big_range_len(start, stop, step)
.to_usize()
.ok_or_else(|| {
"OverflowError: Python int too large to convert to C ssize_t".to_string()
})
}
#[cfg(feature = "stdlib-ffi")]
Some(PyObj::Foreign(id)) => crate::ffi::len(*id),
_ => Err(host::type_error(&format!(
"object of type '{}' has no len()",
h.type_name(v)
))),
})
.or_else(|e| {
if with_host(|h| matches!(h.get(v), Some(PyObj::Instance(_)))) {
let r = host::call_method(v, "__len__", vec![], vec![])?;
len_result(&r)
} else {
Err(e)
}
})
}
fn make_range(args: &[Value]) -> Result<Value, String> {
if !matches!(args.len(), 1..=3) {
return Err(host::type_error("range expected 1 to 3 arguments"));
}
let args: Vec<Value> = args.iter().map(indexed).collect::<Result<_, _>>()?;
let args = &args[..];
let small: Option<Vec<i64>> = args.iter().map(|v| with_host(|h| h.as_int(v))).collect();
if let Some(ints) = small {
let (start, stop, step) = match ints.len() {
1 => (0, ints[0], 1),
2 => (ints[0], ints[1], 1),
_ => (ints[0], ints[1], ints[2]),
};
if step == 0 {
return Err("ValueError: range() arg 3 must not be zero".into());
}
return Ok(with_host(|h| h.alloc(PyObj::Range { start, stop, step })));
}
let bigs: Vec<num_bigint::BigInt> = args
.iter()
.map(|v| {
with_host(|h| h.big_val(v)).ok_or_else(|| {
host::type_error(&format!(
"'{}' object cannot be interpreted as an integer",
with_host(|h| h.type_name(v))
))
})
})
.collect::<Result<_, _>>()?;
use num_traits::{One, Zero};
let (start, stop, step) = match bigs.len() {
1 => (
num_bigint::BigInt::zero(),
bigs[0].clone(),
num_bigint::BigInt::one(),
),
2 => (bigs[0].clone(), bigs[1].clone(), num_bigint::BigInt::one()),
_ => (bigs[0].clone(), bigs[1].clone(), bigs[2].clone()),
};
if step.is_zero() {
return Err("ValueError: range() arg 3 must not be zero".into());
}
Ok(with_host(|h| {
h.alloc(PyObj::BigRange { start, stop, step })
}))
}
fn reduce_minmax(
args: &[Value],
kwargs: &[(String, Value)],
want_max: bool,
) -> Result<Value, String> {
if args.is_empty() {
let name = if want_max { "max" } else { "min" };
return Err(host::type_error(&format!(
"{name} expected at least 1 argument, got 0"
)));
}
let items = if args.len() == 1 {
host::iter_vec(&args[0])?
} else {
args.to_vec()
};
if items.is_empty() {
if let Some(d) = kw_get(kwargs, "default") {
return Ok(d);
}
return Err(format!(
"ValueError: {}() iterable argument is empty",
if want_max { "max" } else { "min" }
));
}
let key = kw_get(kwargs, "key");
let mut best = items[0].clone();
let mut best_k = eval_key(&key, &best)?;
for it in &items[1..] {
let k = eval_key(&key, it)?;
let cmp = if want_max { NumOp::Gt } else { NumOp::Lt };
let take = numeric_hook(cmp, &k, &best_k)?;
if with_host(|h| h.truthy(&take)) {
best = it.clone();
best_k = k;
}
}
Ok(best)
}
fn eval_key(key: &Option<Value>, v: &Value) -> Result<Value, String> {
match key {
Some(f) if !matches!(f, Value::Undef) => host::invoke(f, vec![v.clone()], vec![]),
_ => Ok(v.clone()),
}
}
fn py_sorted(args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
if args.is_empty() {
return Err(host::type_error(&format!(
"sorted expected 1 argument, got {}",
args.len()
)));
}
if let Some((k, _)) = kwargs.iter().find(|(k, _)| k != "key" && k != "reverse") {
return Err(host::type_error(&format!(
"sort() got an unexpected keyword argument '{k}'"
)));
}
let mut items = host::iter_vec(&arg0(args)?)?;
let key = kw_get(kwargs, "key");
let reverse = kw_get(kwargs, "reverse")
.map(|v| with_host(|h| h.truthy(&v)))
.unwrap_or(false);
let mut keyed: Vec<(Value, Value)> = Vec::with_capacity(items.len());
for it in items.drain(..) {
let k = eval_key(&key, &it)?;
keyed.push((k, it));
}
let mut err: Option<String> = None;
keyed.sort_by(|a, b| {
if err.is_some() {
return std::cmp::Ordering::Equal;
}
let (lo, hi) = if reverse { (&b.0, &a.0) } else { (&a.0, &b.0) };
match numeric_hook(NumOp::Lt, lo, hi) {
Ok(v) if with_host(|h| h.truthy(&v)) => std::cmp::Ordering::Less,
Ok(_) => {
match numeric_hook(NumOp::Lt, hi, lo) {
Ok(v2) if with_host(|h| h.truthy(&v2)) => std::cmp::Ordering::Greater,
Ok(_) => std::cmp::Ordering::Equal,
Err(e) => {
err = Some(e);
std::cmp::Ordering::Equal
}
}
}
Err(e) => {
err = Some(e);
std::cmp::Ordering::Equal
}
}
});
if let Some(e) = err {
return Err(e);
}
let out: Vec<Value> = keyed.into_iter().map(|(_, v)| v).collect();
Ok(with_host(|h| h.new_list(out)))
}
fn round_int(n: &num_bigint::BigInt, has_nd: bool, nd: Option<i64>) -> Value {
let d = if has_nd { nd.unwrap_or(0) } else { 0 };
if d >= 0 {
return with_host(|h| h.norm_big(n.clone()));
}
use num_integer::Integer;
use num_traits::Zero;
let k = (-d) as u32;
let base = num_bigint::BigInt::from(10).pow(k);
let (q, r) = n.div_mod_floor(&base); let half = &base / 2u32; let rounded = if r < half {
q
} else if r > half {
q + 1
} else if (&q % 2u32).is_zero() {
q } else {
q + 1
};
with_host(|h| h.norm_big(rounded * base))
}
fn round_float(f: f64, has_nd: bool, nd: Option<i64>) -> Result<Value, String> {
let ndigits = if has_nd { nd } else { None };
if !f.is_finite() {
if ndigits.is_some() {
return Ok(Value::Float(f));
}
return Err(if f.is_nan() {
"ValueError: cannot convert float NaN to integer".into()
} else {
"OverflowError: cannot convert float infinity to integer".into()
});
}
Ok(match ndigits {
None => {
let s = format!("{f:.0}");
match s.parse::<i64>() {
Ok(v) => Value::Int(v),
Err(_) => match s.parse::<num_bigint::BigInt>() {
Ok(b) => with_host(|h| h.norm_big(b)),
Err(_) => Value::Int(0),
},
}
}
Some(d) if d >= 0 => {
let s = format!("{f:.*}", d as usize);
Value::Float(s.parse::<f64>().unwrap_or(f))
}
Some(d) => {
let p = 10f64.powi((-d) as i32);
let scaled = f / p;
let rounded: f64 = format!("{scaled:.0}").parse().unwrap_or(scaled);
Value::Float(rounded * p)
}
})
}
fn int_radix(args: &[Value], radix: u32, prefix: &str) -> Result<Value, String> {
let a0 = arg0(args)?;
let a0 = match index_dunder(&a0)? {
Some(v) => v,
None => a0,
};
let n = with_host(|h| h.big_val(&a0)).ok_or_else(|| {
let t = with_host(|h| h.type_name(&a0));
host::type_error(&format!("'{t}' object cannot be interpreted as an integer"))
})?;
use num_bigint::Sign;
let sign = if n.sign() == Sign::Minus { "-" } else { "" };
let body = n.magnitude().to_str_radix(radix);
Ok(with_host(|h| h.new_str(format!("{sign}{prefix}{body}"))))
}
fn construct_int(args: &[Value]) -> Result<Value, String> {
let v = match args.first() {
Some(v) => v.clone(),
None => return Ok(Value::Int(0)),
};
if args.len() < 2 {
#[cfg(feature = "stdlib-ffi")]
if let Some(id) = with_host(|h| h.foreign_id(&v)) {
return crate::ffi::to_int_cb(id);
}
let has_int = with_host(
|h| matches!(h.get(&v), Some(PyObj::Instance(i)) if instance_has(h, i, "__int__")),
);
if has_int {
let r = host::call_method(&v, "__int__", vec![], vec![])?;
if !is_int_value(&r) {
let t = with_host(|h| h.type_name(&r));
return Err(host::type_error(&format!(
"__int__ returned non-int (type {t})"
)));
}
return Ok(r);
}
if let Some(r) = index_dunder(&v)? {
return Ok(r);
}
}
let base = args
.get(1)
.and_then(|b| with_host(|h| h.as_int(b)))
.unwrap_or(10);
if base != 0 && !(2..=36).contains(&base) {
return Err("ValueError: int() base must be >= 2 and <= 36, or 0".to_string());
}
with_host(|h| match &v {
Value::Int(n) => Ok(Value::Int(*n)),
Value::Bool(b) => Ok(Value::Int(*b as i64)),
Value::Float(f) => {
if !f.is_finite() {
return Err(if f.is_nan() {
"ValueError: cannot convert float NaN to integer".to_string()
} else {
"OverflowError: cannot convert float infinity to integer".to_string()
});
}
let t = f.trunc();
if t >= i64::MIN as f64 && t <= i64::MAX as f64 {
Ok(Value::Int(t as i64))
} else {
use num_traits::FromPrimitive;
let b = num_bigint::BigInt::from_f64(t)
.ok_or_else(|| host::type_error("cannot convert float to integer"))?;
Ok(h.norm_big(b))
}
}
Value::Obj(_) if matches!(h.get(&v), Some(PyObj::BigInt(_))) => Ok(v.clone()),
_ => {
let s = match h.get(&v) {
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => {
b.iter().map(|&c| c as char).collect::<String>()
}
Some(PyObj::Memoryview { .. }) => {
h.mv_bytes(&v)?.iter().map(|&c| c as char).collect::<String>()
}
_ => h.as_str(&v).ok_or_else(|| {
host::type_error(&format!(
"int() argument must be a string, a bytes-like object or a real number, not '{}'",
h.type_name(&v)
))
})?,
};
let orig_repr: String = h.repr_of(&v).chars().take(200).collect();
let s = s.trim();
let (neg, rest) = if let Some(r) = s.strip_prefix('-') {
(true, r)
} else if let Some(r) = s.strip_prefix('+') {
(false, r)
} else {
(false, s)
};
let base_arg = base;
let mut base = base;
let mut had_no_prefix = false;
let body = if let Some(r) = rest.strip_prefix("0x").or_else(|| rest.strip_prefix("0X"))
{
if base == 0 {
base = 16;
}
if base == 16 {
r
} else {
rest
}
} else if let Some(r) = rest.strip_prefix("0o").or_else(|| rest.strip_prefix("0O")) {
if base == 0 {
base = 8;
}
if base == 8 {
r
} else {
rest
}
} else if let Some(r) = rest.strip_prefix("0b").or_else(|| rest.strip_prefix("0B")) {
if base == 0 {
base = 2;
}
if base == 2 {
r
} else {
rest
}
} else {
if base == 0 {
base = 10;
}
had_no_prefix = true;
rest
};
let err = || {
format!("ValueError: invalid literal for int() with base {base_arg}: {orig_repr}")
};
let digits = strip_int_underscores(body, !had_no_prefix).ok_or_else(err)?;
if digits.is_empty() {
return Err(err());
}
if had_no_prefix
&& base_arg == 0
&& digits.len() > 1
&& digits.starts_with('0')
&& !digits.chars().all(|c| c == '0')
{
return Err(err());
}
let mut ascii = String::with_capacity(digits.len());
for c in digits.chars() {
let d = int_digit_value(c).ok_or_else(err)?;
if d >= base as u32 {
return Err(err());
}
ascii.push(char::from_digit(d, 36).unwrap_or('z'));
}
let limit = host::int_max_str_digits();
if limit > 0 && !(base as u32).is_power_of_two() && ascii.len() > limit {
return Err(host::int_max_str_digits_error(limit, Some(ascii.len())));
}
match num_bigint::BigInt::parse_bytes(ascii.as_bytes(), base as u32) {
Some(b) => {
let b = if neg { -b } else { b };
Ok(h.norm_big(b))
}
None => Err(err()),
}
}
})
}
fn strip_int_underscores(body: &str, after_prefix: bool) -> Option<String> {
if body.ends_with('_') || body.contains("__") {
return None;
}
let trimmed = match body.strip_prefix('_') {
Some(rest) if after_prefix => rest,
Some(_) => return None,
None => body,
};
Some(trimmed.replace('_', ""))
}
fn int_digit_value(c: char) -> Option<u32> {
if let Some(d) = c.to_digit(36) {
return Some(d);
}
if !is_decimal_char(c) {
return None;
}
let start = c as u32;
for back in 0..10u32 {
let cur = char::from_u32(start - back)?;
let prev = start.checked_sub(back + 1).and_then(char::from_u32);
if !prev.is_some_and(is_decimal_char) || back == 9 {
let _ = cur;
return Some(back);
}
}
None
}
fn parse_complex(s: &str) -> Result<(f64, f64), String> {
let err = || "ValueError: complex() arg is a malformed string".to_string();
let mut t = s.trim();
if let Some(inner) = t.strip_prefix('(').and_then(|x| x.strip_suffix(')')) {
t = inner.trim();
}
if t.is_empty() {
return Err(err());
}
let bytes = t.as_bytes();
let has_j = matches!(bytes[bytes.len() - 1], b'j' | b'J');
if !has_j {
return Ok((parse_py_float(t).ok_or_else(err)?, 0.0));
}
let body = &t[..t.len() - 1]; let bb = body.as_bytes();
let mut split = None;
for i in (1..bb.len()).rev() {
if (bb[i] == b'+' || bb[i] == b'-') && !matches!(bb[i - 1], b'e' | b'E') {
split = Some(i);
break;
}
}
match split {
Some(k) => {
let r = parse_py_float(body[..k].trim()).ok_or_else(err)?;
let i = parse_imag(&body[k..])?;
Ok((r, i))
}
None => Ok((0.0, parse_imag(body)?)),
}
}
fn parse_imag(s: &str) -> Result<f64, String> {
let err = || "ValueError: complex() arg is a malformed string".to_string();
match s.trim() {
"" | "+" => Ok(1.0),
"-" => Ok(-1.0),
t => parse_py_float(t).ok_or_else(err),
}
}
fn parse_py_float(s: &str) -> Option<f64> {
let f = s.trim().replace('_', "").parse::<f64>().ok()?;
Some(if f.is_nan() { host::fresh_nan(f.is_sign_negative()) } else { f })
}
fn complex_parts(v: &Value, two_args: bool) -> Result<(f64, f64), String> {
if let Some(rc) = with_host(|h| match h.get(v) {
Some(PyObj::Complex(r, i)) => Some((*r, *i)),
_ => None,
}) {
return Ok(rc);
}
let has_complex = with_host(
|h| matches!(h.get(v), Some(PyObj::Instance(i)) if instance_has(h, i, "__complex__")),
);
if has_complex {
let r = host::call_method(v, "__complex__", vec![], vec![])?;
return with_host(|h| match h.get(&r) {
Some(PyObj::Complex(re, im)) => Ok((*re, *im)),
_ => Err(host::type_error(&format!(
"__complex__ returned non-complex (type {})",
h.type_name(&r)
))),
});
}
match math_real(v) {
Ok(f) => Ok((f, 0.0)),
Err(_) if two_args => Err(host::type_error(&format!(
"complex() argument 'real' must be a real number, not {}",
with_host(|h| h.type_name(v))
))),
Err(_) => Err(host::type_error(&format!(
"complex() argument must be a string or a number, not {}",
with_host(|h| h.type_name(v))
))),
}
}
fn complex_imag(v: &Value) -> Result<f64, String> {
math_real(v).map_err(|_| {
host::type_error(&format!(
"complex() argument 'imag' must be a real number, not {}",
with_host(|h| h.type_name(v))
))
})
}
fn construct_float(args: &[Value]) -> Result<Value, String> {
let v = match args.first() {
Some(v) => v.clone(),
None => return Ok(Value::Float(0.0)),
};
#[cfg(feature = "stdlib-ffi")]
if let Some(id) = with_host(|h| h.foreign_id(&v)) {
return crate::ffi::to_float(id).map(Value::Float);
}
let has_float = with_host(
|h| matches!(h.get(&v), Some(PyObj::Instance(i)) if instance_has(h, i, "__float__")),
);
if has_float {
let r = host::call_method(&v, "__float__", vec![], vec![])?;
let ok = matches!(r, Value::Float(_));
if !ok {
let t = with_host(|h| h.type_name(&r));
let owner = with_host(|h| h.type_name(&v));
return Err(host::type_error(&format!(
"{owner}.__float__ returned non-float (type {t})"
)));
}
return Ok(r);
}
if let Some(r) = index_dunder(&v)? {
let f = with_host(|h| {
use num_traits::ToPrimitive;
match &r {
Value::Int(n) => *n as f64,
Value::Bool(b) => *b as i64 as f64,
_ => match h.big_val(&r) {
Some(b) => b.to_f64().unwrap_or(f64::INFINITY),
None => 0.0,
},
}
});
return Ok(Value::Float(f));
}
with_host(|h| match &v {
Value::Int(n) => Ok(Value::Float(*n as f64)),
Value::Float(f) => Ok(Value::Float(*f)),
Value::Bool(b) => Ok(Value::Float(*b as i64 as f64)),
Value::Obj(_) if matches!(h.get(&v), Some(PyObj::BigInt(_))) => {
use num_traits::ToPrimitive;
match h.get(&v) {
Some(PyObj::BigInt(b)) => b
.to_f64()
.filter(|f| f.is_finite())
.map(Value::Float)
.ok_or_else(|| "OverflowError: int too large to convert to float".to_string()),
_ => unreachable!(),
}
}
_ => {
let bytes = match h.get(&v) {
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => {
Some(b.iter().map(|&c| c as char).collect::<String>())
}
_ => None,
};
let shown = bytes.as_ref().map(|_| h.repr_of(&v));
let s = match bytes {
Some(b) => b,
None => h.as_str(&v).ok_or_else(|| {
host::type_error(&format!(
"float() argument must be a string or a real number, not '{}'",
h.type_name(&v)
))
})?,
};
let shown = shown.unwrap_or_else(|| format!("'{s}'"));
parse_py_float(&s)
.map(Value::Float)
.ok_or_else(|| format!("ValueError: could not convert string to float: {shown}"))
}
})
}
fn construct_dict(args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
if let Some(v) = args.first() {
let dict_map = with_host(|h| {
let src = match h.get(v) {
Some(PyObj::Dict(_)) => Some(v.clone()),
Some(PyObj::MappingProxy { dict }) => Some(dict.clone()),
Some(PyObj::Instance(inst))
if !matches!(inst.payload, Value::Undef)
&& h.class_lookup(&inst.class, "__iter__").is_none() =>
{
Some(inst.payload.clone())
}
_ => None,
};
src.and_then(|s| match h.get(&s) {
Some(PyObj::Dict(m)) => Some(m.clone()),
_ => None,
})
});
let has_keys = with_host(|h| match h.get(v) {
Some(PyObj::Instance(i)) => h.class_lookup(&i.class, "keys").is_some(),
_ => false,
}) || with_host(|h| h.foreign_id(v)).is_some();
let foreign_keys = if dict_map.is_none() && has_keys {
host::call_method(v, "keys", vec![], vec![]).ok()
} else {
None
};
if let Some(m) = dict_map {
d = m;
} else if let Some(keys) = foreign_keys {
let is_native = with_host(|h| matches!(h.get(v), Some(PyObj::Instance(_))));
for k in host::iter_vec(&keys)? {
let val = if is_native {
host::call_method(v, "__getitem__", vec![k.clone()], vec![])?
} else {
with_host(|h| h.get_item(v, &k))?
};
let key = with_host(|h| h.to_key(&k))?;
host::dict_put(&mut d, key, k, val);
}
} else {
for (k, val) in dict_update_seq(v)? {
let cands = with_host(|h| host::dict_local_candidates(h, &d));
let key = host::with_instance_key(&k, host::KeyRole::Dict, &cands, || {
with_host(|h| h.to_key(&k))
})?;
host::dict_put(&mut d, key, k, val);
}
}
}
for (k, v) in kwargs {
let kv = with_host(|h| h.new_str(k.clone()));
d.insert(PKey::Str(k.clone()), (kv, v.clone()));
}
Ok(with_host(|h| h.new_dict(d)))
}
fn string_formatter_parser(args: &[Value]) -> Result<Value, String> {
let s = with_host(|h| args.first().and_then(|v| h.as_str(v)))
.ok_or_else(|| host::type_error("formatter_parser() argument must be str"))?;
let cs: Vec<char> = s.chars().collect();
let n = cs.len();
let mut i = 0;
type Field = (String, Option<String>, Option<String>, Option<char>);
let mut out: Vec<Field> = Vec::new();
while i < n {
let mut lit = String::new();
let mut has_field = false;
while i < n {
let c = cs[i];
if c == '{' || c == '}' {
if i + 1 < n && cs[i + 1] == c {
lit.push(c);
i += 2;
continue;
}
if c == '}' {
return Err("ValueError: Single '}' encountered in format string".into());
}
has_field = true;
i += 1; break;
}
lit.push(c);
i += 1;
}
if !has_field {
if !lit.is_empty() {
out.push((lit, None, None, None));
}
break;
}
let mut field_name = String::new();
let mut conversion: Option<char> = None;
let mut format_spec = String::new();
while i < n && cs[i] != '!' && cs[i] != ':' && cs[i] != '}' {
field_name.push(cs[i]);
i += 1;
}
if i < n && cs[i] == '!' {
i += 1;
if i >= n || cs[i] == '}' || cs[i] == ':' {
return Err(
"ValueError: end of string while looking for conversion specifier".into(),
);
}
conversion = Some(cs[i]);
i += 1;
}
if i < n && cs[i] == ':' {
i += 1;
let mut depth = 1;
while i < n {
let c = cs[i];
if c == '{' {
depth += 1;
} else if c == '}' {
depth -= 1;
if depth == 0 {
break;
}
}
format_spec.push(c);
i += 1;
}
}
if i >= n || cs[i] != '}' {
return Err("ValueError: expected '}' before end of string".into());
}
i += 1; out.push((lit, Some(field_name), Some(format_spec), conversion));
}
Ok(with_host(|h| {
let mut tuples: Vec<Value> = Vec::with_capacity(out.len());
for (lit, fname, fspec, conv) in out {
let lit_v = h.new_str(lit);
let fname_v = fname.map(|s| h.new_str(s)).unwrap_or(Value::Undef);
let fspec_v = fspec.map(|s| h.new_str(s)).unwrap_or(Value::Undef);
let conv_v = conv
.map(|c| h.new_str(c.to_string()))
.unwrap_or(Value::Undef);
tuples.push(h.new_tuple(vec![lit_v, fname_v, fspec_v, conv_v]));
}
h.new_list(tuples)
}))
}
fn string_formatter_field_name_split(args: &[Value]) -> Result<Value, String> {
let s = with_host(|h| args.first().and_then(|v| h.as_str(v)))
.ok_or_else(|| host::type_error("formatter_field_name_split() argument must be str"))?;
let cs: Vec<char> = s.chars().collect();
let n = cs.len();
let mut i = 0;
let mut first = String::new();
while i < n && cs[i] != '.' && cs[i] != '[' {
first.push(cs[i]);
i += 1;
}
let mut rest: Vec<(bool, String, bool)> = Vec::new();
while i < n {
if cs[i] == '.' {
i += 1;
let mut nm = String::new();
while i < n && cs[i] != '.' && cs[i] != '[' {
nm.push(cs[i]);
i += 1;
}
rest.push((true, nm, false));
} else if cs[i] == '[' {
i += 1;
let mut key = String::new();
while i < n && cs[i] != ']' {
key.push(cs[i]);
i += 1;
}
if i >= n {
return Err("ValueError: Missing ']' in format string".into());
}
i += 1; let is_int = !key.is_empty() && key.chars().all(|c| c.is_ascii_digit());
rest.push((false, key, is_int));
} else {
return Err(
"ValueError: Only '.' or '[' may follow ']' in format field specifier".into(),
);
}
}
let first_is_int = !first.is_empty() && first.chars().all(|c| c.is_ascii_digit());
Ok(with_host(|h| {
let first_v = if first_is_int {
Value::Int(first.parse().unwrap_or(0))
} else {
h.new_str(first)
};
let mut items: Vec<Value> = Vec::with_capacity(rest.len());
for (is_attr, key, key_is_int) in rest {
let key_v = if !is_attr && key_is_int {
Value::Int(key.parse().unwrap_or(0))
} else {
h.new_str(key)
};
items.push(h.new_tuple(vec![Value::Bool(is_attr), key_v]));
}
let rest_list = h.new_list(items);
h.new_tuple(vec![first_v, rest_list])
}))
}
fn call_itertools(
name: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
use host::{ItKind, PyObj};
let mk = |kind: ItKind,
sources: Vec<Value>,
func: Value,
nums: Vec<i64>,
buf: Vec<Value>,
flag: bool| {
with_host(|h| {
h.alloc(PyObj::ItertoolsIter {
kind,
sources,
func,
nums,
buf,
flag,
done: false,
})
})
};
let iter_of = |v: &Value| -> Result<Value, String> { host::make_iterator(v) };
let as_i = |v: &Value| with_host(|h| h.as_int(v));
let kw = |k: &str| kwargs.iter().find(|(n, _)| n == k).map(|(_, v)| v.clone());
match name {
"count" => {
let start = args
.first()
.cloned()
.or_else(|| kw("start"))
.unwrap_or(Value::Int(0));
let step = args
.get(1)
.cloned()
.or_else(|| kw("step"))
.unwrap_or(Value::Int(1));
Ok(mk(
ItKind::Count,
vec![],
Value::Undef,
vec![],
vec![start, step],
false,
))
}
"repeat" => {
let obj = args.first().cloned().unwrap_or(Value::Undef);
let times = args
.get(1)
.cloned()
.or_else(|| kw("times"))
.and_then(|v| as_i(&v))
.unwrap_or(-1);
Ok(mk(
ItKind::Repeat,
vec![],
Value::Undef,
vec![times],
vec![obj],
false,
))
}
"cycle" => {
let src = iter_of(&arg0(&args)?)?;
Ok(mk(
ItKind::Cycle,
vec![src],
Value::Undef,
vec![0],
vec![],
false,
))
}
"chain" => {
let mut sources = Vec::with_capacity(args.len());
for a in &args {
sources.push(iter_of(a)?);
}
Ok(mk(
ItKind::Chain,
sources,
Value::Undef,
vec![0],
vec![],
false,
))
}
"chain.from_iterable" => {
let outer = host::iter_vec(&arg0(&args)?)?;
let mut sources = Vec::with_capacity(outer.len());
for a in &outer {
sources.push(iter_of(a)?);
}
Ok(mk(
ItKind::Chain,
sources,
Value::Undef,
vec![0],
vec![],
false,
))
}
"accumulate" => {
let src = iter_of(&arg0(&args)?)?;
let func = args
.get(1)
.cloned()
.or_else(|| kw("func"))
.unwrap_or(Value::Undef);
let initial = kw("initial").filter(|v| !matches!(v, Value::Undef));
let (buf, seeded) = match initial {
Some(v) => (vec![v], true),
None => (vec![], false),
};
Ok(mk(ItKind::Accumulate, vec![src], func, vec![], buf, seeded))
}
"starmap" => {
let func = arg0(&args)?;
let src = iter_of(
args.get(1)
.ok_or_else(|| host::type_error("starmap expected 2 arguments"))?,
)?;
Ok(mk(ItKind::StarMap, vec![src], func, vec![], vec![], false))
}
"compress" => {
let data = iter_of(&arg0(&args)?)?;
let sel = iter_of(
args.get(1)
.ok_or_else(|| host::type_error("compress expected 2 arguments"))?,
)?;
Ok(mk(
ItKind::Compress,
vec![data, sel],
Value::Undef,
vec![],
vec![],
false,
))
}
"dropwhile" => {
let func = arg0(&args)?;
let src = iter_of(
args.get(1)
.ok_or_else(|| host::type_error("dropwhile expected 2 arguments"))?,
)?;
Ok(mk(ItKind::DropWhile, vec![src], func, vec![], vec![], true))
}
"takewhile" => {
let func = arg0(&args)?;
let src = iter_of(
args.get(1)
.ok_or_else(|| host::type_error("takewhile expected 2 arguments"))?,
)?;
Ok(mk(
ItKind::TakeWhile,
vec![src],
func,
vec![],
vec![],
false,
))
}
"filterfalse" => {
let func = args.first().cloned().unwrap_or(Value::Undef);
let src = iter_of(
args.get(1)
.ok_or_else(|| host::type_error("filterfalse expected 2 arguments"))?,
)?;
Ok(mk(
ItKind::FilterFalse,
vec![src],
func,
vec![],
vec![],
false,
))
}
"islice" => {
let src = iter_of(&arg0(&args)?)?;
let (start, stop, step) = if args.len() <= 2 {
(0, args.get(1).and_then(&as_i).unwrap_or(-1), 1)
} else {
(
args.get(1).and_then(&as_i).unwrap_or(0),
args.get(2).and_then(&as_i).unwrap_or(-1),
args.get(3).and_then(as_i).unwrap_or(1),
)
};
Ok(mk(
ItKind::ISlice,
vec![src],
Value::Undef,
vec![start, stop, step, 0],
vec![],
false,
))
}
"zip_longest" => {
let fill = kw("fillvalue").unwrap_or(Value::Undef);
let mut sources = Vec::with_capacity(args.len());
for a in &args {
sources.push(iter_of(a)?);
}
Ok(mk(ItKind::ZipLongest, sources, fill, vec![], vec![], false))
}
"pairwise" => {
let src = iter_of(&arg0(&args)?)?;
Ok(mk(
ItKind::Pairwise,
vec![src],
Value::Undef,
vec![],
vec![],
false,
))
}
"batched" => {
let src = iter_of(&arg0(&args)?)?;
let n = args
.get(1)
.cloned()
.or_else(|| kw("n"))
.and_then(|v| as_i(&v))
.ok_or_else(|| host::type_error("batched() missing argument 'n'"))?;
if n < 1 {
return Err("ValueError: n must be at least one".into());
}
let strict = kw("strict").map(|v| with_host(|h| h.truthy(&v))) == Some(true);
Ok(mk(
ItKind::Batched,
vec![src],
Value::Undef,
vec![n],
vec![],
strict,
))
}
"product" => itertools_product(&args, &kwargs),
"permutations" => itertools_permutations(&args),
"combinations" => itertools_combinations(&args, false),
"combinations_with_replacement" => itertools_combinations(&args, true),
"tee" => itertools_tee(&args),
"groupby" => itertools_groupby(&args, &kwargs),
_ => Err(format!(
"AttributeError: module 'itertools' has no attribute '{name}'"
)),
}
}
fn list_iter(items: Vec<Value>) -> Value {
with_host(|h| {
let l = h.new_list(items);
h.make_iter(&l).unwrap_or(l)
})
}
fn itertools_product(args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
let repeat = kwargs
.iter()
.find(|(n, _)| n == "repeat")
.and_then(|(_, v)| with_host(|h| h.as_int(v)))
.unwrap_or(1)
.max(0) as usize;
let mut pools: Vec<Vec<Value>> = Vec::new();
for a in args {
pools.push(host::iter_vec(a)?);
}
let base = pools.clone();
for _ in 1..repeat {
pools.extend(base.iter().cloned());
}
if repeat == 0 {
pools.clear();
}
let mut result: Vec<Vec<Value>> = vec![vec![]];
for pool in &pools {
let mut next = Vec::new();
for prefix in &result {
for item in pool {
let mut row = prefix.clone();
row.push(item.clone());
next.push(row);
}
}
result = next;
}
let tuples: Vec<Value> = result
.into_iter()
.map(|r| with_host(|h| h.new_tuple(r)))
.collect();
Ok(list_iter(tuples))
}
fn negative_r_error() -> String {
"ValueError: r must be non-negative".to_string()
}
fn itertools_permutations(args: &[Value]) -> Result<Value, String> {
let pool = host::iter_vec(&arg0(args)?)?;
let n = pool.len();
let r = match args
.get(1)
.filter(|v| !matches!(v, Value::Undef))
.and_then(|v| with_host(|h| h.as_int(v)))
{
Some(x) if x < 0 => return Err(negative_r_error()),
Some(x) => x as usize,
None => n,
};
let mut out: Vec<Value> = Vec::new();
if r <= n {
let mut indices: Vec<usize> = (0..n).collect();
let mut cycles: Vec<usize> = (n - r + 1..=n).rev().collect();
let emit = |indices: &[usize]| {
let row: Vec<Value> = indices[..r].iter().map(|&i| pool[i].clone()).collect();
with_host(|h| h.new_tuple(row))
};
out.push(emit(&indices));
'outer: loop {
let mut i = r;
loop {
if i == 0 {
break 'outer;
}
i -= 1;
cycles[i] -= 1;
if cycles[i] == 0 {
let first = indices[i];
for j in i..n - 1 {
indices[j] = indices[j + 1];
}
indices[n - 1] = first;
cycles[i] = n - i;
} else {
let j = n - cycles[i];
indices.swap(i, j);
out.push(emit(&indices));
break;
}
}
}
}
Ok(list_iter(out))
}
fn itertools_combinations(args: &[Value], with_repl: bool) -> Result<Value, String> {
let pool = host::iter_vec(&arg0(args)?)?;
let n = pool.len();
let r = args
.get(1)
.and_then(|v| with_host(|h| h.as_int(v)))
.ok_or_else(|| host::type_error("combinations() missing r"))?;
if r < 0 {
return Err(negative_r_error());
}
let r = r as usize;
let mut out: Vec<Value> = Vec::new();
let emit = |indices: &[usize]| {
let row: Vec<Value> = indices.iter().map(|&i| pool[i].clone()).collect();
with_host(|h| h.new_tuple(row))
};
if with_repl {
if n == 0 && r > 0 {
return Ok(list_iter(out));
}
let mut indices = vec![0usize; r];
out.push(emit(&indices));
loop {
let mut i = r;
loop {
if i == 0 {
return Ok(list_iter(out));
}
i -= 1;
if indices[i] != n - 1 {
break;
}
}
let v = indices[i] + 1;
indices[i..r].fill(v);
out.push(emit(&indices));
}
} else {
if r > n {
return Ok(list_iter(out));
}
let mut indices: Vec<usize> = (0..r).collect();
out.push(emit(&indices));
loop {
let mut i = r;
loop {
if i == 0 {
return Ok(list_iter(out));
}
i -= 1;
if indices[i] != i + n - r {
break;
}
}
indices[i] += 1;
for j in i + 1..r {
indices[j] = indices[j - 1] + 1;
}
out.push(emit(&indices));
}
}
}
fn itertools_tee(args: &[Value]) -> Result<Value, String> {
let items = host::iter_vec(&arg0(args)?)?;
let n = args
.get(1)
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(2)
.max(0) as usize;
let iters: Vec<Value> = (0..n).map(|_| list_iter(items.clone())).collect();
Ok(with_host(|h| h.new_tuple(iters)))
}
fn itertools_groupby(args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
let src = host::make_iterator(&arg0(args)?)?;
let key = args
.get(1)
.cloned()
.or_else(|| kw_get(kwargs, "key"))
.unwrap_or(Value::Undef);
Ok(with_host(|h| {
h.alloc(PyObj::ItertoolsIter {
kind: host::ItKind::GroupBy,
sources: vec![src],
func: key,
nums: vec![0, 0, 0, 0],
buf: vec![Value::Undef, Value::Undef, Value::Undef],
flag: false,
done: false,
})
}))
}
pub fn random_method(id: u32, name: &str, args: &[Value]) -> Result<Value, String> {
match name {
"seed" => {
let key: Vec<u32> = match args.first() {
None | Some(Value::Undef) => {
let mut buf = [0u8; 32];
use std::io::Read;
let _ = std::fs::File::open("/dev/urandom")
.and_then(|mut f| f.read_exact(&mut buf));
buf.chunks(4)
.map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]]))
.collect()
}
Some(a) => {
let big = with_host(|h| h.big_val(a))
.ok_or_else(|| host::type_error("seed must be an integer"))?;
let (_, digits) = big.to_u32_digits();
if digits.is_empty() {
vec![0]
} else {
digits
}
}
};
with_host(|h| h.mt_states.entry(id).or_default().init_by_array(&key));
Ok(Value::Undef)
}
"random" => Ok(Value::Float(with_host(|h| {
h.mt_states.entry(id).or_default().random()
}))),
"getrandbits" => {
let k = args
.first()
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(0);
if k <= 0 {
return Err("ValueError: number of bits must be greater than zero".into());
}
Ok(with_host(|h| {
let st = h.mt_states.entry(id).or_default();
if k <= 32 {
return Value::Int((st.next_u32() >> (32 - k)) as i64);
}
let words = ((k - 1) / 32 + 1) as usize;
let mut result = num_bigint::BigInt::from(0);
let mut kk = k;
for i in 0..words {
let mut r = st.next_u32();
if kk < 32 {
r >>= 32 - kk;
}
result |= num_bigint::BigInt::from(r) << (32 * i as u32);
kk -= 32;
}
h.norm_big(result)
}))
}
"getstate" => Ok(with_host(|h| {
let s = h.mt_states.entry(id).or_default().state();
let vals: Vec<Value> = s.into_iter().map(|w| Value::Int(w as i64)).collect();
h.new_tuple(vals)
})),
"setstate" => {
let tup = arg0(args)?;
let items = host::iter_vec(&tup)?;
let s: Vec<u32> = items
.iter()
.map(|v| with_host(|h| h.as_int(v)).unwrap_or(0) as u32)
.collect();
with_host(|h| h.mt_states.entry(id).or_default().set_state(&s));
Ok(Value::Undef)
}
_ => Err(format!(
"AttributeError: '_random.Random' object has no method '{name}'"
)),
}
}
fn call_posix(name: &str, args: Vec<Value>, kwargs: Vec<(String, Value)>) -> Result<Value, String> {
let path_arg =
|i: usize| -> Option<String> { args.get(i).and_then(|v| with_host(|h| h.as_str(v))) };
let str_v = |s: String| with_host(|h| h.new_str(s));
let io_err = |e: std::io::Error| -> String {
let code = e.raw_os_error().unwrap_or(0);
format!("OSError: [Errno {code}] {e}")
};
match name {
"getcwd" => std::env::current_dir()
.map(|p| str_v(p.to_string_lossy().into_owned()))
.map_err(io_err),
"getcwdb" => std::env::current_dir()
.map(|p| with_host(|h| h.alloc(PyObj::Bytes(p.to_string_lossy().as_bytes().to_vec()))))
.map_err(io_err),
"chdir" => {
let p = path_arg(0).ok_or_else(|| host::type_error("chdir: path required"))?;
std::env::set_current_dir(&p).map_err(io_err)?;
Ok(Value::Undef)
}
"listdir" => {
let p = path_arg(0).unwrap_or_else(|| ".".to_string());
let mut names = Vec::new();
for entry in std::fs::read_dir(&p).map_err(io_err)? {
let e = entry.map_err(io_err)?;
names.push(str_v(e.file_name().to_string_lossy().into_owned()));
}
Ok(with_host(|h| h.new_list(names)))
}
"stat" | "lstat" => {
let p = path_arg(0).ok_or_else(|| host::type_error("stat: path required"))?;
let md = if name == "lstat" {
std::fs::symlink_metadata(&p)
} else {
std::fs::metadata(&p)
}
.map_err(io_err)?;
Ok(build_stat_result(&md))
}
"mkdir" => {
let p = path_arg(0).ok_or_else(|| host::type_error("mkdir: path required"))?;
std::fs::create_dir(&p).map_err(io_err)?;
Ok(Value::Undef)
}
"makedirs" => {
let p = path_arg(0).ok_or_else(|| host::type_error("makedirs: path required"))?;
std::fs::create_dir_all(&p).map_err(io_err)?;
Ok(Value::Undef)
}
"rmdir" => {
let p = path_arg(0).ok_or_else(|| host::type_error("rmdir: path required"))?;
std::fs::remove_dir(&p).map_err(io_err)?;
Ok(Value::Undef)
}
"remove" | "unlink" => {
let p = path_arg(0).ok_or_else(|| host::type_error("remove: path required"))?;
std::fs::remove_file(&p).map_err(io_err)?;
Ok(Value::Undef)
}
"rename" | "replace" => {
let src = path_arg(0).ok_or_else(|| host::type_error("rename: src required"))?;
let dst = path_arg(1).ok_or_else(|| host::type_error("rename: dst required"))?;
std::fs::rename(&src, &dst).map_err(io_err)?;
Ok(Value::Undef)
}
"getpid" => Ok(Value::Int(std::process::id() as i64)),
"getppid" => Ok(Value::Int(unsafe { libc::getppid() } as i64)),
"getuid" | "geteuid" => Ok(Value::Int(unsafe { libc::getuid() } as i64)),
"getgid" | "getegid" => Ok(Value::Int(unsafe { libc::getgid() } as i64)),
"getpgrp" => Ok(Value::Int(unsafe { libc::getpgrp() } as i64)),
"umask" => {
let m = args
.first()
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(0);
let prev = unsafe { libc::umask(m as libc::mode_t) };
Ok(Value::Int(prev as i64))
}
"urandom" => {
let n = args
.first()
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(0)
.max(0) as usize;
let mut buf = vec![0u8; n];
use std::io::Read;
std::fs::File::open("/dev/urandom")
.and_then(|mut f| f.read_exact(&mut buf))
.map_err(io_err)?;
Ok(with_host(|h| h.alloc(PyObj::Bytes(buf))))
}
"getenv" => {
let key = path_arg(0).ok_or_else(|| host::type_error("getenv: key required"))?;
match std::env::var(&key) {
Ok(val) => Ok(str_v(val)),
Err(_) => Ok(args.get(1).cloned().unwrap_or(Value::Undef)),
}
}
"putenv" => {
let key = path_arg(0).ok_or_else(|| host::type_error("putenv: key required"))?;
let val = path_arg(1).unwrap_or_default();
std::env::set_var(&key, &val);
Ok(Value::Undef)
}
"unsetenv" => {
let key = path_arg(0).ok_or_else(|| host::type_error("unsetenv: key required"))?;
std::env::remove_var(&key);
Ok(Value::Undef)
}
"access" => {
let p = path_arg(0).ok_or_else(|| host::type_error("access: path required"))?;
let mode = args
.get(1)
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(0);
let cp =
std::ffi::CString::new(p).map_err(|_| "ValueError: embedded null".to_string())?;
Ok(Value::Bool(
unsafe { libc::access(cp.as_ptr(), mode as i32) } == 0,
))
}
"fspath" => {
let a = arg0(&args)?;
if with_host(|h| matches!(h.get(&a), Some(PyObj::Str(_)) | Some(PyObj::Bytes(_))))
|| matches!(a, Value::Str(_))
{
Ok(a)
} else {
host::call_method(&a, "__fspath__", vec![], vec![])
}
}
"strerror" => {
let code = args
.first()
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(0);
let msg = std::io::Error::from_raw_os_error(code as i32).to_string();
Ok(str_v(msg))
}
"system" => {
let cmd = path_arg(0).ok_or_else(|| host::type_error("system: command required"))?;
let status = std::process::Command::new("/bin/sh")
.arg("-c")
.arg(&cmd)
.status()
.map_err(io_err)?;
Ok(Value::Int((status.code().unwrap_or(0) as i64) << 8))
}
"cpu_count" => Ok(std::thread::available_parallelism()
.map(|n| Value::Int(n.get() as i64))
.unwrap_or(Value::Undef)),
"isatty" => {
let fd = args
.first()
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(-1);
Ok(Value::Bool(unsafe { libc::isatty(fd as i32) } == 1))
}
"_exit" => {
let code = args
.first()
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(0);
std::process::exit(code as i32);
}
"abort" => std::process::exit(134),
"sync" | "sched_yield" => Ok(Value::Undef),
"_create_environ" => Ok(with_host(|h| {
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
for (k, val) in std::env::vars() {
let kb = k.into_bytes();
let kv = h.alloc(PyObj::Bytes(kb.clone()));
let vv = h.alloc(PyObj::Bytes(val.into_bytes()));
d.insert(PKey::Bytes(kb), (kv, vv));
}
h.alloc(PyObj::Dict(d))
})),
"close" => {
let fd = args
.first()
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(-1);
unsafe { libc::close(fd as i32) };
Ok(Value::Undef)
}
"get_inheritable" => Ok(Value::Bool(false)),
"set_inheritable" | "fsync" | "utime" | "chmod" | "truncate" | "ftruncate" => {
Ok(Value::Undef)
}
"readlink" => {
let p = path_arg(0).ok_or_else(|| host::type_error("readlink: path required"))?;
std::fs::read_link(&p)
.map(|t| str_v(t.to_string_lossy().into_owned()))
.map_err(io_err)
}
_ => Err(format!(
"AttributeError: module 'posix' has no attribute '{name}'"
)),
}
.inspect_err(|_e| {
let _ = &kwargs;
})
}
fn build_stat_result(md: &std::fs::Metadata) -> Value {
use std::os::unix::fs::MetadataExt;
let fields = [
("st_mode", md.mode() as i64),
("st_ino", md.ino() as i64),
("st_dev", md.dev() as i64),
("st_nlink", md.nlink() as i64),
("st_uid", md.uid() as i64),
("st_gid", md.gid() as i64),
("st_size", md.size() as i64),
("st_atime", md.atime()),
("st_mtime", md.mtime()),
("st_ctime", md.ctime()),
];
with_host(|h| {
let vals: Vec<Value> = fields.iter().map(|(_, v)| Value::Int(*v)).collect();
let tup = h.new_tuple(vals);
if let Value::Obj(i) = tup {
h.nt_meta.insert(
i,
host::NtMeta {
type_name: "os.stat_result".to_string(),
fields: fields.iter().map(|(k, _)| (*k).to_string()).collect(),
},
);
}
tup
})
}
fn call_sys(name: &str, args: Vec<Value>, kwargs: Vec<(String, Value)>) -> Result<Value, String> {
match name {
"get_int_max_str_digits" => Ok(Value::Int(host::int_max_str_digits() as i64)),
"set_int_max_str_digits" => {
let [n] = bind_named(
"set_int_max_str_digits",
["maxdigits"],
0,
1,
KwStyle::Unexpected,
&args,
&kwargs,
)?;
let n = n.unwrap_or(Value::Undef);
let n = match with_host(|h| h.index_fit(&n)) {
host::IndexFit::Fits(n) if i32::try_from(n).is_ok() => n,
host::IndexFit::NotInt => {
return Err(host::type_error(&format!(
"'{}' object cannot be interpreted as an integer",
with_host(|h| h.type_name(&n))
)))
}
_ => {
return Err(
"OverflowError: Python int too large to convert to C int".to_string()
)
}
};
match usize::try_from(n) {
Ok(n) if n == 0 || n >= host::INT_MAX_STR_DIGITS_THRESHOLD => {
with_host(|h| h.set_int_max_str_digits(n));
Ok(Value::Undef)
}
_ => Err("ValueError: maxdigits must be >= 640 or 0 for unlimited".to_string()),
}
}
"exit" => {
let exc = with_host(|h| {
h.alloc(PyObj::Exception {
class: "SystemExit".into(),
args,
})
});
Err(host::raise_value(&exc)?)
}
"getsizeof" => sys_getsizeof(&args, &kwargs),
"getrecursionlimit" => Ok(Value::Int(1000)),
"setrecursionlimit" => {
let a0 = arg0(&args)?;
match with_host(|h| h.index_fit(&a0)) {
host::IndexFit::TooLarge(_) => {
Err("OverflowError: Python int too large to convert to C int".to_string())
}
host::IndexFit::Fits(n) if n > i32::MAX as i64 || n < i32::MIN as i64 => {
Err("OverflowError: Python int too large to convert to C int".to_string())
}
host::IndexFit::Fits(n) if n >= 1 => Ok(Value::Undef),
host::IndexFit::NotInt => Err(host::type_error(&format!(
"'{}' object cannot be interpreted as an integer",
with_host(|h| h.type_name(&a0))
))),
_ => Err("ValueError: recursion limit must be greater or equal than 1".to_string()),
}
}
"getfilesystemencoding" | "getdefaultencoding" => {
Ok(with_host(|h| h.new_str("utf-8".to_string())))
}
"getfilesystemencodeerrors" => Ok(with_host(|h| h.new_str("surrogateescape".to_string()))),
"intern" => Ok(args.into_iter().next().unwrap_or(Value::Undef)),
"audit" => Ok(Value::Undef),
"exc_info" | "exception" => {
let cur = with_host(|h| h.exc.clone());
let want_triple = name == "exc_info";
Ok(with_host(|h| match cur {
Some(exc) if !want_triple => exc,
Some(exc) => {
let cls = h.get_attr(&exc, "__class__").unwrap_or(Value::Undef);
h.new_tuple(vec![cls, exc, Value::Undef])
}
None if !want_triple => Value::Undef,
None => h.new_tuple(vec![Value::Undef, Value::Undef, Value::Undef]),
}))
}
"excepthook" | "unraisablehook" => {
let value = if name == "excepthook" {
args.get(1).cloned().unwrap_or(Value::Undef)
} else {
args.first().cloned().unwrap_or(Value::Undef)
};
let text = with_host(|h| h.str_of(&value));
eprintln!("{text}");
Ok(Value::Undef)
}
"is_finalizing" => Ok(Value::Bool(false)),
"_getframe" => {
let depth = args
.first()
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(0)
.max(0) as usize;
Ok(with_host(|h| h.current_frame_object(depth)))
}
_ => Err(format!(
"AttributeError: module 'sys' has no attribute '{name}'"
)),
}
}
fn call_asyncio(
name: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
use crate::async_rt;
match name {
"run" => async_rt::run(args.into_iter().next().unwrap_or(Value::Undef)),
"sleep" => {
let mut it = args.into_iter();
let delay = it.next().and_then(num_f).unwrap_or(0.0);
let result = it.next().unwrap_or(Value::Undef);
Ok(async_rt::sleep(delay, result))
}
"gather" => {
let return_exceptions = kw_get(&kwargs, "return_exceptions")
.map(|v| with_host(|h| h.truthy(&v)))
.unwrap_or(false);
async_rt::gather(args, return_exceptions)
}
"create_task" => {
let coro = args.into_iter().next().unwrap_or(Value::Undef);
let name = kw_get(&kwargs, "name").map(|v| with_host(|h| h.str_of(&v)));
async_rt::create_task(coro, name)
}
"ensure_future" => async_rt::ensure_future(args.into_iter().next().unwrap_or(Value::Undef)),
"wait_for" => {
let mut it = args.into_iter();
let aw = it.next().unwrap_or(Value::Undef);
let timeout = it
.next()
.or_else(|| kw_get(&kwargs, "timeout"))
.and_then(num_f);
async_rt::wait_for(aw, timeout)
}
"get_event_loop" | "get_running_loop" | "new_event_loop" => Ok(async_rt::event_loop()),
"Future" => Ok(async_rt::new_future()),
"wait" => {
let mut it = args.into_iter();
let aws = host::iter_vec(&it.next().unwrap_or(Value::Undef))?;
let timeout = it
.next()
.or_else(|| kw_get(&kwargs, "timeout"))
.and_then(num_f);
let return_when = kw_get(&kwargs, "return_when")
.map(|v| async_rt::ReturnWhen::parse(&with_host(|h| h.str_of(&v))))
.unwrap_or(async_rt::ReturnWhen::AllCompleted);
async_rt::wait(aws, timeout, return_when)
}
"as_completed" => {
let aws = host::iter_vec(&args.into_iter().next().unwrap_or(Value::Undef))?;
async_rt::as_completed(aws)
}
"Event" => Ok(async_rt::new_event()),
"Lock" => Ok(async_rt::new_lock()),
"Queue" => {
let maxsize = kw_get(&kwargs, "maxsize")
.or_else(|| args.into_iter().next())
.and_then(num_f)
.map(|f| f.max(0.0) as usize)
.unwrap_or(0);
Ok(async_rt::new_queue(maxsize))
}
_ => Err(format!(
"AttributeError: module 'asyncio' has no attribute '{name}'"
)),
}
}
fn num_f(v: Value) -> Option<f64> {
match v {
Value::Int(n) => Some(n as f64),
Value::Float(f) => Some(f),
Value::Bool(b) => Some(if b { 1.0 } else { 0.0 }),
_ => None,
}
}
fn call_total_ordering(args: &[Value]) -> Result<Value, String> {
let cls = arg0(args)?;
let name = match with_host(|h| h.get(&cls).cloned()) {
Some(PyObj::Class(n)) => n,
_ => return Err(host::type_error("total_ordering: argument must be a class")),
};
let roots = ["__lt__", "__le__", "__gt__", "__ge__"];
let has_root = with_host(|h| roots.iter().any(|m| h.class_lookup(&name, m).is_some()));
if !has_root {
return Err(host::type_error(
"must define at least one ordering operation: < > <= >=",
));
}
with_host(|h| h.mark_total_ordering(&name));
Ok(cls)
}
fn total_ordering_derive(op: NumOp, selfv: &Value, other: &Value) -> Option<Result<Value, String>> {
use NumOp::*;
let class = with_host(|h| match h.get(selfv) {
Some(PyObj::Instance(i)) if h.is_total_ordering(&i.class) => Some(i.class.clone()),
_ => None,
})?;
let target = match op {
Lt => "__lt__",
Le => "__le__",
Gt => "__gt__",
Ge => "__ge__",
_ => return None,
};
let root = ["__lt__", "__le__", "__gt__", "__ge__"]
.into_iter()
.find(|m| with_host(|h| h.class_lookup(&class, m).is_some()))?;
if target == root {
return None; }
let rv = match host::call_method(selfv, root, vec![other.clone()], vec![]) {
Ok(v) => v,
Err(e) => return Some(Err(e)),
};
if is_not_implemented(&rv) {
return Some(Ok(rv));
}
let r = with_host(|h| h.truthy(&rv));
let needs_eq = matches!(
(root, target),
("__lt__", "__gt__")
| ("__lt__", "__le__")
| ("__le__", "__ge__")
| ("__le__", "__lt__")
| ("__gt__", "__lt__")
| ("__gt__", "__ge__")
| ("__ge__", "__le__")
| ("__ge__", "__gt__")
);
let eq = if needs_eq {
match dispatch_binop(selfv, other, "__eq__", "__eq__") {
Dunder::Value(v) => with_host(|h| h.truthy(&v)),
Dunder::Err(e) => return Some(Err(e)),
Dunder::NotImplemented => identity_eq(selfv, other),
}
} else {
false
};
let result = match (root, target) {
("__lt__", "__gt__") => !r && !eq,
("__lt__", "__le__") => r || eq,
("__lt__", "__ge__") => !r,
("__le__", "__ge__") => !r || eq,
("__le__", "__lt__") => r && !eq,
("__le__", "__gt__") => !r,
("__gt__", "__lt__") => !r && !eq,
("__gt__", "__ge__") => r || eq,
("__gt__", "__le__") => !r,
("__ge__", "__le__") => !r || eq,
("__ge__", "__gt__") => r && !eq,
("__ge__", "__lt__") => !r,
_ => return None,
};
Some(Ok(Value::Bool(result)))
}
pub fn call_copy(name: &str, args: &[Value]) -> Result<Value, String> {
let v = arg0(args)?;
match name {
"copy" => copy_shallow(&v),
"deepcopy" => {
let mut memo = std::collections::HashMap::new();
copy_deep(&v, &mut memo)
}
_ => Err(host::type_error(&format!(
"module 'copy' has no attribute '{name}'"
))),
}
}
fn copy_shallow(v: &Value) -> Result<Value, String> {
enum K {
List(Vec<Value>),
Dict(IndexMap<PKey, (Value, Value)>, Option<host::DictMeta>),
Set(IndexMap<PKey, Value>),
Bytearray(Vec<u8>),
Instance(Instance),
Same,
}
let k = with_host(|h| match h.get(v) {
Some(PyObj::List(l)) => K::List(l.clone()),
Some(PyObj::Dict(d)) => {
let meta = match v {
Value::Obj(i) => h.dict_meta.get(i).cloned(),
_ => None,
};
K::Dict(d.clone(), meta)
}
Some(PyObj::Set(s)) => K::Set(s.clone()),
Some(PyObj::Bytearray(b)) => K::Bytearray(b.clone()),
Some(PyObj::Instance(inst)) => K::Instance(inst.clone()),
_ => K::Same,
});
Ok(with_host(|h| match k {
K::List(l) => h.new_list(l),
K::Dict(d, meta) => {
let nd = h.new_dict(d);
if let (Some(meta), Value::Obj(i)) = (meta, &nd) {
h.dict_meta.insert(*i, meta);
}
nd
}
K::Set(s) => h.new_set(s),
K::Bytearray(b) => h.alloc(PyObj::Bytearray(b)),
K::Instance(inst) => {
let pairs = match h.get(&inst.dict) {
Some(PyObj::Dict(d)) => d.clone(),
_ => IndexMap::new(),
};
let dict = h.alloc(PyObj::Dict(pairs));
let out = h.alloc(PyObj::Instance(Instance {
class: inst.class,
dict,
payload: inst.payload,
}));
let slots = h.slot_values_of(v);
h.set_slot_values(&out, slots);
out
}
K::Same => v.clone(),
}))
}
fn copy_deep(v: &Value, memo: &mut std::collections::HashMap<u32, Value>) -> Result<Value, String> {
if let Value::Obj(i) = v {
if let Some(c) = memo.get(i) {
return Ok(c.clone());
}
}
enum K {
List(Vec<Value>),
Tuple(Vec<Value>),
Dict(Vec<(Value, Value)>, Option<host::DictMeta>),
Set(Vec<Value>),
Bytearray(Vec<u8>),
Instance(String, Vec<(Value, Value)>, Value),
Same,
}
let k = with_host(|h| match h.get(v) {
Some(PyObj::List(l)) => K::List(l.clone()),
Some(PyObj::Tuple(l)) => K::Tuple(l.clone()),
Some(PyObj::Dict(d)) => {
let meta = match v {
Value::Obj(i) => h.dict_meta.get(i).cloned(),
_ => None,
};
K::Dict(d.values().cloned().collect(), meta)
}
Some(PyObj::Set(s)) => K::Set(s.values().cloned().collect()),
Some(PyObj::Bytearray(b)) => K::Bytearray(b.clone()),
Some(PyObj::Instance(inst)) => {
let pairs = match h.get(&inst.dict) {
Some(PyObj::Dict(d)) => d.values().cloned().collect(),
_ => Vec::new(),
};
K::Instance(inst.class.clone(), pairs, inst.payload.clone())
}
_ => K::Same,
});
let id = if let Value::Obj(i) = v {
Some(*i)
} else {
None
};
match k {
K::List(items) => {
let out = with_host(|h| h.new_list(Vec::new()));
if let Some(i) = id {
memo.insert(i, out.clone());
}
let mut copied = Vec::with_capacity(items.len());
for it in &items {
copied.push(copy_deep(it, memo)?);
}
with_host(|h| {
if let Some(PyObj::List(l)) = h.get_mut(&out) {
*l = copied;
}
});
Ok(out)
}
K::Tuple(items) => {
let mut copied = Vec::with_capacity(items.len());
for it in &items {
copied.push(copy_deep(it, memo)?);
}
let out = with_host(|h| h.new_tuple(copied));
if let Some(i) = id {
memo.insert(i, out.clone());
}
Ok(out)
}
K::Dict(pairs, meta) => {
let out = with_host(|h| h.new_dict(IndexMap::new()));
if let Some(i) = id {
memo.insert(i, out.clone());
}
let mut nd: IndexMap<PKey, (Value, Value)> = IndexMap::new();
for (k, val) in &pairs {
let ck = copy_deep(k, memo)?;
let cv = copy_deep(val, memo)?;
let key = with_host(|h| h.to_key(&ck))?;
nd.insert(key, (ck, cv));
}
with_host(|h| {
if let Some(PyObj::Dict(d)) = h.get_mut(&out) {
*d = nd;
}
if let (Some(meta), Value::Obj(i)) = (meta, &out) {
h.dict_meta.insert(*i, meta);
}
});
Ok(out)
}
K::Set(items) => {
let mut copied: IndexMap<PKey, Value> = IndexMap::new();
for it in &items {
let ci = copy_deep(it, memo)?;
let key = with_host(|h| h.to_key(&ci))?;
copied.insert(key, ci);
}
let out = with_host(|h| h.new_set(copied));
if let Some(i) = id {
memo.insert(i, out.clone());
}
Ok(out)
}
K::Bytearray(b) => {
let out = with_host(|h| h.alloc(PyObj::Bytearray(b)));
if let Some(i) = id {
memo.insert(i, out.clone());
}
Ok(out)
}
K::Instance(class, pairs, payload) => {
let dict = with_host(|h| h.alloc(PyObj::Dict(IndexMap::new())));
let out = with_host(|h| {
h.alloc(PyObj::Instance(Instance {
class,
dict: dict.clone(),
payload,
}))
});
if let Some(i) = id {
memo.insert(i, out.clone());
}
let mut nd: IndexMap<PKey, (Value, Value)> = IndexMap::new();
for (k, val) in &pairs {
let cv = copy_deep(val, memo)?;
let key = with_host(|h| h.to_key(k))?;
nd.insert(key, (k.clone(), cv));
}
with_host(|h| {
if let Some(PyObj::Dict(d)) = h.get_mut(&dict) {
*d = nd;
}
});
let mut slots = Vec::new();
for (name, val) in with_host(|h| h.slot_values_of(v)) {
slots.push((name, copy_deep(&val, memo)?));
}
with_host(|h| h.set_slot_values(&out, slots));
Ok(out)
}
K::Same => Ok(v.clone()),
}
}
fn f64_to_int(h: &mut host::PyHost, x: f64) -> Value {
if x >= i64::MIN as f64 && x <= i64::MAX as f64 {
return Value::Int(x as i64);
}
use num_traits::FromPrimitive;
match num_bigint::BigInt::from_f64(x) {
Some(b) => h.norm_big(b),
None => Value::Int(0),
}
}
enum Arity {
ExactlyOne,
NoArgs,
VarExact(usize),
VarRange(usize, usize),
NoPositional,
ClinicPositional(usize),
}
fn check_arity(name: &str, qual: &str, spec: Arity, argc: usize) -> Result<(), String> {
let plural = |k: usize| if k == 1 { "argument" } else { "arguments" };
match spec {
Arity::ExactlyOne if argc != 1 => Err(host::type_error(&format!(
"{qual}() takes exactly one argument ({argc} given)"
))),
Arity::NoArgs if argc != 0 => Err(host::type_error(&format!(
"{qual}() takes no arguments ({argc} given)"
))),
Arity::VarExact(k) if argc != k => Err(host::type_error(&format!(
"{name} expected {k} {}, got {argc}",
plural(k)
))),
Arity::VarRange(_, max) if argc > max => Err(host::type_error(&format!(
"{name} expected at most {max} {}, got {argc}",
plural(max)
))),
Arity::VarRange(min, _) if argc < min => Err(host::type_error(&format!(
"{name} expected at least {min} {}, got {argc}",
plural(min)
))),
Arity::NoPositional if argc != 0 => Err(host::type_error(&format!(
"{name}() takes no positional arguments"
))),
Arity::ClinicPositional(k) if argc != k => Err(host::type_error(&format!(
"{name}() takes exactly {k} positional {} ({argc} given)",
plural(k)
))),
_ => Ok(()),
}
}
fn math_arity(name: &str) -> Option<Arity> {
Some(match name {
"log" | "perm" => Arity::VarRange(1, 2),
"pow" | "atan2" | "copysign" | "fmod" | "remainder" | "ldexp" | "comb" | "sumprod" => {
Arity::VarExact(2)
}
"gcd" | "lcm" | "hypot" => return None,
"isclose" => Arity::ClinicPositional(2),
_ => Arity::ExactlyOne,
})
}
fn call_typing(f: &str, args: Vec<Value>, kwargs: Vec<(String, Value)>) -> Result<Value, String> {
use host::TypeVarKind;
if f == "_idfunc" {
return Ok(args.into_iter().next().unwrap_or(Value::Undef));
}
let kind = match f {
"TypeVar" => Some(TypeVarKind::TypeVar),
"ParamSpec" => Some(TypeVarKind::ParamSpec),
"TypeVarTuple" => Some(TypeVarKind::TypeVarTuple),
_ => None,
};
if let Some(kind) = kind {
let name = with_host(|h| h.as_str(&args[0]))
.ok_or_else(|| host::type_error(&format!("{f}() argument 1 must be str")))?;
let constraints: Vec<Value> = args.iter().skip(1).cloned().collect();
return Ok(with_host(|h| {
h.make_type_var(kind, name, constraints, kwargs)
}));
}
Err(host::name_error(&format!("_typing.{f}")))
}
fn rewrite_class_backspace(
pattern: &str,
verbose: bool,
ascii: bool,
ignorecase: bool,
) -> std::borrow::Cow<'_, str> {
if !ascii && !pattern.contains('[') {
return std::borrow::Cow::Borrowed(pattern);
}
let ascii_fold = ascii && ignorecase;
let chars: Vec<char> = pattern.chars().collect();
let mut out = String::with_capacity(pattern.len());
let mut in_class = false;
let mut class_start = 0;
let mut i = 0;
while i < chars.len() {
match chars[i] {
'\\' if i + 1 < chars.len() => {
if in_class && chars[i + 1] == 'b' {
out.push_str("\\x08");
} else if let Some(class) = ascii
.then(|| ascii_escape(chars[i + 1], in_class))
.flatten()
{
out.push_str(class);
} else if ascii_fold
&& !in_class
&& chars[i + 1] == 'x'
&& is_high_hex(&chars[i + 2..])
{
out.push_str("(?-i:\\x");
out.extend(&chars[i + 2..i + 4]);
out.push(')');
i += 4;
continue;
} else {
out.push('\\');
out.push(chars[i + 1]);
}
i += 2;
continue;
}
'(' if !in_class && ascii && chars.get(i + 1) == Some(&'?') => {
let rest: String = chars[i + 2..].iter().take(3).collect();
let named = rest.starts_with("P<")
|| rest.starts_with("P=")
|| rest.starts_with('(')
|| (rest.starts_with('<') && !rest[1..].starts_with(['=', '!']));
let flag_letters = chars[i + 2..]
.iter()
.take_while(|c| c.is_ascii_alphabetic() || **c == '-')
.count();
let after_flags = chars.get(i + 2 + flag_letters).copied();
let close = if named && (rest.starts_with("P<") || rest.starts_with('<')) {
Some('>')
} else if named || rest.starts_with('#') {
Some(')')
} else if flag_letters > 0 && matches!(after_flags, Some(':' | ')')) {
after_flags
} else {
None
};
if let Some(close) = close {
if let Some(len) = chars[i..].iter().position(|&c| c == close) {
out.extend(&chars[i..=i + len]);
i += len + 1;
continue;
}
}
}
c if ascii && !in_class && matches!(c, 'k' | 'K' | 's' | 'S') => {
out.push_str("(?-u:");
out.push(c);
out.push(')');
i += 1;
continue;
}
'[' if !in_class => {
in_class = true;
class_start = out.len();
out.push('[');
i += 1;
if chars.get(i) == Some(&'^') {
out.push('^');
i += 1;
}
if chars.get(i) == Some(&']') {
out.push_str("\\]");
i += 1;
}
continue;
}
'[' if in_class => {
out.push_str("\\[");
i += 1;
continue;
}
c if verbose && in_class && (c == ' ' || c == '\t') => {
out.push_str(if c == ' ' { "\\x20" } else { "\\t" });
i += 1;
continue;
}
c if in_class && matches!(c, '&' | '~' | '|') && chars.get(i + 1) == Some(&c) => {
out.push('\\');
out.push(c);
out.push(c);
i += 2;
continue;
}
']' if in_class => {
in_class = false;
out.push(']');
i += 1;
if ascii_fold {
if let Some(folded) = ascii_fold_class(&out[class_start..]) {
out.truncate(class_start);
out.push_str(&folded);
}
}
continue;
}
c if ascii_fold && !in_class && !c.is_ascii() => {
out.push_str("(?-i:");
out.push(c);
out.push(')');
i += 1;
continue;
}
_ => {}
}
out.push(chars[i]);
i += 1;
}
std::borrow::Cow::Owned(out)
}
fn ascii_fold_class(class: &str) -> Option<String> {
let inner = class.strip_prefix('[')?.strip_suffix(']')?;
let body: Vec<char> = inner.strip_prefix('^').unwrap_or(inner).chars().collect();
let mut partners = String::new();
let mut i = 0;
while i < body.len() {
let Some(lo) = class_member(&body, &mut i)? else {
continue;
};
let mut hi = lo;
if body.get(i) == Some(&'-') && i + 1 < body.len() {
i += 1;
hi = class_member(&body, &mut i)??;
}
for (first, last) in [('a', 'z'), ('A', 'Z')] {
let (from, to) = (lo.max(first), hi.min(last));
if from <= to {
let swap = |c: char| (c as u8 ^ 0x20) as char;
partners.push(swap(from));
partners.push('-');
partners.push(swap(to));
}
}
}
Some(format!("(?-i:{}{partners}])", &class[..class.len() - 1]))
}
fn class_member(body: &[char], i: &mut usize) -> Option<Option<char>> {
let c = body[*i];
*i += 1;
match c {
'[' => {
let len = body[*i..].iter().position(|&ch| ch == ']')?;
*i += len + 1;
Some(None)
}
'\\' => {
let e = *body.get(*i)?;
*i += 1;
Some(Some(match e {
'x' => {
let hex: String = body.get(*i..*i + 2)?.iter().collect();
*i += 2;
char::from_u32(u32::from_str_radix(&hex, 16).ok()?)?
}
'n' => '\n',
't' => '\t',
'r' => '\r',
'f' => '\x0c',
'v' => '\x0b',
'a' => '\x07',
_ if !e.is_ascii_alphanumeric() => e,
_ => return None,
}))
}
_ => Some(Some(c)),
}
}
fn ascii_escape(c: char, in_class: bool) -> Option<&'static str> {
Some(match (c, in_class) {
('w', false) => "(?-i:[0-9A-Za-z_])",
('W', false) => "(?-i:[^0-9A-Za-z_])",
('d', false) => "[0-9]",
('D', false) => "[^0-9]",
('s', false) => "[\\t\\n\\x0B\\x0C\\r\\x20]",
('S', false) => "[^\\t\\n\\x0B\\x0C\\r\\x20]",
('b', false) => "(?-u:\\b)",
('B', false) => "(?-u:\\B)",
('w', true) => "0-9A-Za-z_",
('W', true) => "[^0-9A-Za-z_]",
('d', true) => "0-9",
('D', true) => "[^0-9]",
('s', true) => "\\t\\n\\x0B\\x0C\\r\\x20",
('S', true) => "[^\\t\\n\\x0B\\x0C\\r\\x20]",
_ => return None,
})
}
fn is_high_hex(rest: &[char]) -> bool {
rest.len() >= 2
&& rest[..2].iter().all(|c| c.is_ascii_hexdigit())
&& matches!(rest[0], '8'..='9' | 'a'..='f' | 'A'..='F')
}
fn exact_sum_push(partials: &mut Vec<f64>, mut x: f64) {
let mut i = 0;
for j in 0..partials.len() {
let mut y = partials[j];
if x.abs() < y.abs() {
std::mem::swap(&mut x, &mut y);
}
let hi = x + y;
let lo = y - (hi - x);
if lo != 0.0 {
partials[i] = lo;
i += 1;
}
x = hi;
}
partials.truncate(i);
partials.push(x);
}
fn re_compile_raw(
pattern: &str,
flags: i64,
is_bytes: bool,
) -> Result<crate::regexpr::PyRegex, String> {
let mut inline = String::new();
if flags & 2 != 0 {
inline.push('i');
}
if flags & 8 != 0 {
inline.push('m');
}
if flags & 16 != 0 {
inline.push('s');
}
if flags & 64 != 0 {
inline.push('x');
}
let ascii = is_bytes || (flags | re_inline_flags(pattern)) & 256 != 0;
let ignorecase = (flags | re_inline_flags(pattern)) & 2 != 0;
let pattern = rewrite_class_backspace(pattern, flags & 64 != 0, ascii, ignorecase);
let full = if inline.is_empty() {
pattern.to_string()
} else {
format!("(?{inline}){pattern}")
};
crate::regexpr::PyRegex::new(&full)
}
fn latin1_decode(b: &[u8]) -> String {
b.iter().map(|&c| c as char).collect()
}
fn latin1_encode(s: &str) -> Vec<u8> {
s.chars().map(|c| c as u8).collect()
}
fn re_subject(v: &Value, is_bytes: bool) -> Result<String, String> {
if let Some(s) = with_host(|h| h.as_str(v)) {
if is_bytes {
return Err(host::type_error(
"cannot use a bytes pattern on a string-like object",
));
}
return Ok(s);
}
match as_bytes_object(v).ok().flatten() {
Some(b) if is_bytes => Ok(latin1_decode(&b)),
Some(_) => Err(host::type_error(
"cannot use a string pattern on a bytes-like object",
)),
None => Err(host::type_error(&format!(
"expected string or bytes-like object, got '{}'",
with_host(|h| h.type_name(v))
))),
}
}
pub(crate) fn re_text_value(is_bytes: bool, s: &str) -> Value {
with_host(|h| {
if is_bytes {
h.alloc(PyObj::Bytes(latin1_encode(s)))
} else {
h.new_str(s.to_string())
}
})
}
fn re_compile(pattern: &Value, flags: i64) -> Result<Value, String> {
if with_host(|h| matches!(h.get(pattern), Some(PyObj::Pattern { .. }))) {
return Ok(pattern.clone());
}
let (src, is_bytes) = match with_host(|h| h.as_str(pattern)) {
Some(s) => (s, false),
None => with_host(|h| match h.get(pattern) {
Some(PyObj::Bytes(b)) => Some(latin1_decode(b)),
_ => None,
})
.map(|s| (s, true))
.ok_or_else(|| host::type_error("first argument must be string or compiled pattern"))?,
};
let key = (src.clone(), flags, is_bytes);
if let Some(p) = with_host(|h| h.re_cache.get(&key).cloned()) {
return Ok(p);
}
let chars: Vec<char> = src.chars().collect();
let shown = crate::sre_parse::check(&chars, !is_bytes, flags)
.map_err(|refusal| crate::sre_parse::raise(refusal, &chars, !is_bytes))?;
let re = re_compile_raw(&src, flags, is_bytes).map_err(|msg| {
let refusal = crate::sre_parse::Refusal::Pattern { msg, pos: None };
crate::sre_parse::raise(refusal, &chars, !is_bytes)
})?;
let groups = re.captures_len().saturating_sub(1);
Ok(with_host(|h| {
let id = h.regexes.len();
h.regexes.push(re);
let p = h.alloc(PyObj::Pattern {
id,
pattern: src,
flags: shown,
groups,
is_bytes,
});
h.re_cache.insert(key, p.clone());
p
}))
}
fn re_inline_flags(src: &str) -> i64 {
let mut bits = 0;
let mut rest = src;
while let Some(body) = rest.strip_prefix("(?") {
let Some(end) = body.find(')') else { break };
let letters = &body[..end];
let mut group = 0;
for c in letters.chars() {
group |= match c {
'a' => 256,
'i' => 2,
'L' => 4,
'm' => 8,
's' => 16,
'u' => 32,
'x' => 64,
_ => return bits,
};
}
if letters.is_empty() {
break;
}
bits |= group;
rest = &body[end + 1..];
}
bits
}
#[allow(clippy::too_many_arguments)]
fn re_build_match(
pat: &Value,
pat_id: usize,
subject: &Value,
text: &str,
m_spans: Vec<Option<(usize, usize)>>,
pos: usize,
endpos: usize,
is_bytes: bool,
) -> Value {
let named: Vec<(String, usize)> = with_host(|h| h.regexes[pat_id].named_groups());
with_host(|h| {
h.alloc(PyObj::Match {
re: pat.clone(),
string: subject.clone(),
text: text.to_string(),
spans: m_spans,
named,
pos,
endpos,
is_bytes,
})
})
}
fn re_first_match(
pat_id: usize,
text: &str,
pos: usize,
anchored: bool,
) -> Option<Vec<Option<(usize, usize)>>> {
with_host(|h| {
let spans = h.regexes[pat_id].captures_at(text, pos)?;
if anchored && spans.first().copied().flatten().map(|(s, _)| s) != Some(pos) {
return None;
}
Some(spans)
})
}
fn re_window(text: &str, pos: Option<&Value>, endpos: Option<&Value>) -> (usize, usize) {
let nchars = text.chars().count() as i64;
let to_byte = |v: Option<&Value>| {
v.and_then(|v| with_host(|h| h.as_int(v)))
.map(|i| crate::regexpr::byte_index_of(text, i.clamp(0, nchars) as usize))
};
(
to_byte(pos).unwrap_or(0),
to_byte(endpos).unwrap_or(text.len()),
)
}
fn call_re(f: &str, args: Vec<Value>, kwargs: Vec<(String, Value)>) -> Result<Value, String> {
let flag_arg = |args: &[Value], idx: usize| -> i64 {
args.get(idx)
.and_then(|v| with_host(|h| h.as_int(v)))
.or_else(|| {
kwargs
.iter()
.find(|(k, _)| k == "flags")
.and_then(|(_, v)| with_host(|h| h.as_int(v)))
})
.unwrap_or(0)
};
match f {
"escape" => {
let a = arg0(&args)?;
let s = with_host(|h| h.as_str(&a))
.ok_or_else(|| host::type_error("re.escape() requires a string"))?;
Ok(with_host(|h| h.new_str(regex::escape(&s))))
}
"purge" => Ok(Value::Undef),
"compile" => re_compile(&arg0(&args)?, flag_arg(&args, 1)),
"match" | "search" | "fullmatch" => {
let pat = re_compile(&arg0(&args)?, flag_arg(&args, 2))?;
let text = args.get(1).cloned().unwrap_or(Value::Undef);
re_pattern_method(&pat, f, &[text], &[])
}
"findall" | "finditer" => {
let pat = re_compile(&arg0(&args)?, flag_arg(&args, 2))?;
let text = args.get(1).cloned().unwrap_or(Value::Undef);
re_pattern_method(&pat, f, &[text], &kwargs)
}
"split" => {
let pat = re_compile(&arg0(&args)?, flag_arg(&args, 3))?;
let rest: Vec<Value> = args.iter().skip(1).take(2).cloned().collect();
re_pattern_method(&pat, f, &rest, &kwargs)
}
"sub" | "subn" => {
let pat = re_compile(&arg0(&args)?, flag_arg(&args, 4))?;
let rest: Vec<Value> = args.iter().skip(1).take(3).cloned().collect();
re_pattern_method(&pat, f, &rest, &kwargs)
}
_ => Err(host::name_error(&format!("re.{f}"))),
}
}
pub fn re_pattern_method(
pat: &Value,
method: &str,
args: &[Value],
kwargs: &[(String, Value)],
) -> Result<Value, String> {
let kw_int = |name: &str| -> Option<i64> {
kwargs
.iter()
.find(|(k, _)| k == name)
.and_then(|(_, v)| with_host(|h| h.as_int(v)))
};
let (pat_id, groups, is_bytes) = match with_host(|h| h.get(pat).cloned()) {
Some(PyObj::Pattern {
id,
groups,
is_bytes,
..
}) => (id, groups, is_bytes),
_ => return Err(host::type_error("not a compiled pattern")),
};
let subject = args.first().cloned().unwrap_or(Value::Undef);
match method {
"match" | "search" | "fullmatch" => {
let text = re_subject(&subject, is_bytes)?;
let (pos, endpos) = re_window(&text, args.get(1), args.get(2));
let window = text.get(..endpos.max(pos)).unwrap_or(&text);
let anchored = method == "match" || method == "fullmatch";
match re_first_match(pat_id, window, pos, anchored) {
Some(spans) => {
if method == "fullmatch"
&& spans.first().copied().flatten().map(|(_, e)| e) != Some(endpos)
{
return Ok(Value::Undef);
}
Ok(re_build_match(
pat, pat_id, &subject, &text, spans, pos, endpos, is_bytes,
))
}
None => Ok(Value::Undef),
}
}
"findall" => {
let text = re_subject(&subject, is_bytes)?;
let all = with_host(|h| h.regexes[pat_id].all_captures(&text));
let grp = |spans: &crate::regexpr::Spans, i: usize| {
let s = spans
.get(i)
.copied()
.flatten()
.map_or("", |(a, b)| &text[a..b]);
re_text_value(is_bytes, s)
};
let vals: Vec<Value> = all
.iter()
.map(|spans| match groups {
0 => grp(spans, 0),
1 => grp(spans, 1),
_ => {
let t: Vec<Value> = (1..=groups).map(|i| grp(spans, i)).collect();
with_host(|h| h.new_tuple(t))
}
})
.collect();
Ok(with_host(|h| h.new_list(vals)))
}
"finditer" => {
let scanner = re_scanner(pat, pat_id, &subject, args, is_bytes)?;
Ok(with_host(|h| {
let func = h.alloc(PyObj::Builtin("__base_method__.search".into()));
let search = h.alloc(PyObj::BoundMethod {
recv: scanner,
func,
});
h.alloc(PyObj::CallIter {
func: search,
sentinel: Value::Undef,
done: false,
})
}))
}
"scanner" => re_scanner(pat, pat_id, &subject, args, is_bytes),
"sub" | "subn" => {
let repl = subject;
let target = args.get(1).cloned().unwrap_or(Value::Undef);
let count = args
.get(2)
.and_then(|v| with_host(|h| h.as_int(v)))
.or_else(|| kw_int("count"))
.unwrap_or(0);
re_sub(
pat,
pat_id,
is_bytes,
&repl,
&target,
count,
method == "subn",
)
}
"split" => {
let text = re_subject(&subject, is_bytes)?;
let maxsplit = args
.get(1)
.and_then(|v| with_host(|h| h.as_int(v)))
.or_else(|| kw_int("maxsplit"))
.unwrap_or(0);
let (spans, groups) = with_host(|h| {
let re = &h.regexes[pat_id];
(re.all_captures(&text), re.captures_len())
});
let mut vals: Vec<Value> = Vec::new();
let mut last = 0usize;
for (n, m) in spans.iter().enumerate() {
if maxsplit > 0 && n as i64 >= maxsplit {
break;
}
let Some((s, e)) = m.first().copied().flatten() else {
continue;
};
vals.push(re_text_value(is_bytes, &text[last..s]));
for g in 1..groups {
vals.push(match m.get(g).copied().flatten() {
Some((a, b)) => re_text_value(is_bytes, &text[a..b]),
None => Value::Undef,
});
}
last = e;
}
vals.push(re_text_value(is_bytes, &text[last..]));
Ok(with_host(|h| h.new_list(vals)))
}
_ => Err(host::type_error(&format!(
"Pattern has no method '{method}'"
))),
}
}
fn re_scanner(
pat: &Value,
pat_id: usize,
subject: &Value,
args: &[Value],
is_bytes: bool,
) -> Result<Value, String> {
let text = re_subject(subject, is_bytes)?;
let (pos, endpos) = re_window(&text, args.get(1), args.get(2));
Ok(with_host(|h| {
h.alloc(PyObj::ReScanner {
pattern: pat.clone(),
pat_id,
string: subject.clone(),
text,
pos,
endpos,
next: Some(pos),
must_advance: false,
is_bytes,
})
}))
}
pub fn re_scanner_method(sc: &Value, method: &str) -> Result<Value, String> {
let Some(PyObj::ReScanner {
pattern,
pat_id,
string,
text,
pos,
endpos,
next,
must_advance,
is_bytes,
}) = with_host(|h| h.get(sc).cloned())
else {
return Err(host::type_error("not a scanner"));
};
if !matches!(method, "match" | "search") {
return Err(format!(
"AttributeError: '_sre.SRE_Scanner' object has no attribute '{method}'"
));
}
let Some(start) = next else {
return Ok(Value::Undef);
};
let window = text.get(..endpos.max(start)).unwrap_or(&text);
let spans = if method == "search" {
with_host(|h| h.regexes[pat_id].search_from(window, start, must_advance))
} else {
re_first_match(pat_id, window, start, true).filter(|spans| {
let empty_here = spans.first().copied().flatten() == Some((start, start));
!(must_advance && empty_here)
})
};
let span = spans.as_ref().and_then(|s| s.first().copied().flatten());
with_host(|h| {
if let Some(PyObj::ReScanner {
next, must_advance, ..
}) = h.get_mut(sc)
{
*next = span.map(|(_, e)| e);
*must_advance = span.is_some_and(|(s, e)| s == e);
}
});
Ok(match spans {
Some(spans) => re_build_match(
&pattern, pat_id, &string, &text, spans, pos, endpos, is_bytes,
),
None => Value::Undef,
})
}
fn re_is_text(v: &Value) -> bool {
with_host(|h| h.as_str(v).is_some()) || !matches!(as_bytes_object(v), Ok(None))
}
fn re_join_error(index: usize, want_bytes: bool, found: &str) -> String {
let want = if want_bytes {
"a bytes-like object"
} else {
"str instance"
};
host::type_error(&format!(
"sequence item {index}: expected {want}, {found} found"
))
}
#[allow(clippy::too_many_arguments)]
fn re_sub(
pat: &Value,
pat_id: usize,
is_bytes: bool,
repl: &Value,
subject: &Value,
count: i64,
want_count: bool,
) -> Result<Value, String> {
let text = re_subject(subject, is_bytes)?;
let template = if re_is_text(repl) {
let t_bytes = with_host(|h| h.as_str(repl)).is_none();
let src = re_template_text(repl)?;
let (groups, named) = with_host(|h| {
let re = &h.regexes[pat_id];
(re.captures_len() - 1, re.named_groups())
});
Some((re_parse_template(&src, !t_bytes, groups, &named)?, t_bytes))
} else {
if !re_is_callable(repl) {
return Err(host::type_error(&format!(
"decoding to str: need a bytes-like object, {} found",
with_host(|h| h.type_name(repl))
)));
}
None
};
let all: Vec<Vec<Option<(usize, usize)>>> =
with_host(|h| h.regexes[pat_id].all_captures(&text));
let mut out = String::new();
let mut last = 0usize;
let mut n = 0i64;
let mut items = 0usize;
let mut bad_item: Option<(usize, String)> = None;
for s in all {
if count > 0 && n >= count {
break;
}
let (ms, me) = s.first().copied().flatten().unwrap_or((0, 0));
if last < ms {
out.push_str(&text[last..ms]);
items += 1;
}
let piece = match &template {
Some((pieces, t_bytes)) => {
let piece = re_expand(pieces, *t_bytes, is_bytes, &text, &s)?;
if *t_bytes != is_bytes {
let found = if *t_bytes { "bytes" } else { "str" };
bad_item.get_or_insert((items, found.to_string()));
}
Some(piece)
}
None => {
let m = re_build_match(pat, pat_id, subject, &text, s, 0, text.len(), is_bytes);
let r = host::invoke(repl, vec![m], vec![])?;
if matches!(r, Value::Undef) {
None
} else {
let piece = if is_bytes {
as_bytes_object(&r)
.ok()
.flatten()
.map(|b| latin1_decode(&b))
} else {
with_host(|h| h.as_str(&r))
};
if piece.is_none() {
bad_item.get_or_insert((items, with_host(|h| h.type_name(&r))));
}
Some(piece.unwrap_or_default())
}
}
};
if let Some(piece) = piece {
out.push_str(&piece);
items += 1;
}
last = me;
n += 1;
}
if let Some((index, found)) = bad_item {
return Err(re_join_error(index, is_bytes, &found));
}
out.push_str(&text[last..]);
finish_sub(re_text_value(is_bytes, &out), n, want_count)
}
fn re_template_text(t: &Value) -> Result<String, String> {
if let Some(s) = with_host(|h| h.as_str(t)) {
return Ok(s);
}
Ok(as_bytes_object(t)?
.map(|b| latin1_decode(&b))
.unwrap_or_default())
}
fn re_is_callable(v: &Value) -> bool {
with_host(|h| match h.get(v) {
Some(PyObj::Func(_))
| Some(PyObj::Builtin(_))
| Some(PyObj::Class(_))
| Some(PyObj::NamedTupleType { .. })
| Some(PyObj::Partial { .. })
| Some(PyObj::LruCache { .. })
| Some(PyObj::StaticMethod(_))
| Some(PyObj::ClassMethod(_))
| Some(PyObj::BoundMethod { .. }) => true,
#[cfg(feature = "stdlib-ffi")]
Some(PyObj::Foreign(_)) => true,
Some(PyObj::Instance(i)) => h.class_lookup(&i.class, "__call__").is_some(),
_ => false,
})
}
fn finish_sub(result: Value, n: i64, want_count: bool) -> Result<Value, String> {
if want_count {
Ok(with_host(|h| h.new_tuple(vec![result, Value::Int(n)])))
} else {
Ok(result)
}
}
enum ReplPiece {
Lit(String),
Group(usize),
}
fn re_template_error(src: &[char], istext: bool, msg: &str, pos: usize) -> String {
let msg = if istext { msg.to_string() } else { host::ascii_of(msg) };
let refusal = crate::sre_parse::Refusal::Pattern { msg, pos: Some(pos) };
crate::sre_parse::raise(refusal, src, istext)
}
fn re_parse_template(
src: &str,
istext: bool,
groups: usize,
named: &[(String, usize)],
) -> Result<Vec<ReplPiece>, String> {
let s: Vec<char> = src.chars().collect();
let err = |msg: &str, pos: usize| re_template_error(&s, istext, msg, pos);
let is_oct = |c: Option<&char>| matches!(c, Some('0'..='7'));
let mut pieces: Vec<ReplPiece> = Vec::new();
let mut lit = String::new();
let mut i = 0usize;
while i < s.len() {
if s[i] != '\\' {
lit.push(s[i]);
i += 1;
continue;
}
let Some(&c) = s.get(i + 1) else {
return Err(err("bad escape (end of pattern)", s.len() - 1));
};
i += 2;
let group = match c {
'g' => {
if s.get(i) != Some(&'<') {
return Err(err("missing <", i));
}
i += 1;
let start = i;
let Some(len) = s[start..].iter().position(|&ch| ch == '>') else {
return Err(if start == s.len() {
err("missing group name", s.len())
} else {
err("missing >, unterminated name", start)
});
};
i = start + len + 1;
if len == 0 {
return Err(err("missing group name", start));
}
let name: String = s[start..start + len].iter().collect();
if name.chars().all(|ch| ch.is_ascii_digit()) {
match name.parse::<usize>() {
Ok(n) => (n, start),
Err(_) => {
return Err(err(&format!("invalid group reference {name}"), start));
}
}
} else {
if let Some(msg) = crate::sre_parse::group_name_error(&name, istext) {
return Err(err(&msg, start));
}
match named.iter().find(|(n, _)| *n == name) {
Some((_, idx)) => (*idx, start),
None => {
let name = host::quote_str(&name);
return Err(format!("IndexError: unknown group name {name}"));
}
}
}
}
'0' => {
let mut v = 0u32;
for _ in 0..2 {
if !is_oct(s.get(i)) {
break;
}
v = v * 8 + s[i].to_digit(8).unwrap_or(0);
i += 1;
}
lit.push(char::from_u32(v & 0xff).unwrap_or('\0'));
continue;
}
'1'..='9' => {
let mut digits = String::from(c);
if s.get(i).is_some_and(|d| d.is_ascii_digit()) {
digits.push(s[i]);
i += 1;
if is_oct(Some(&c))
&& is_oct(digits.chars().nth(1).as_ref())
&& is_oct(s.get(i))
{
digits.push(s[i]);
i += 1;
let v = u32::from_str_radix(&digits, 8).unwrap_or(0);
if v > 0o377 {
return Err(err(
&format!("octal escape value \\{digits} outside of range 0-0o377"),
i - digits.len() - 1,
));
}
lit.push(char::from_u32(v).unwrap_or('\0'));
continue;
}
}
(
digits.parse::<usize>().unwrap_or(usize::MAX),
i - digits.len(),
)
}
_ => {
match c {
'a' => lit.push('\x07'),
'b' => lit.push('\x08'),
'f' => lit.push('\x0c'),
'n' => lit.push('\n'),
'r' => lit.push('\r'),
't' => lit.push('\t'),
'v' => lit.push('\x0b'),
'\\' => lit.push('\\'),
_ if c.is_ascii_alphabetic() => {
return Err(err(&format!("bad escape \\{c}"), i - 2));
}
_ => {
lit.push('\\');
lit.push(c);
}
}
continue;
}
};
let (index, pos) = group;
if index > groups {
return Err(err(&format!("invalid group reference {index}"), pos));
}
pieces.push(ReplPiece::Lit(std::mem::take(&mut lit)));
pieces.push(ReplPiece::Group(index));
}
pieces.push(ReplPiece::Lit(lit));
Ok(pieces)
}
fn re_expand(
pieces: &[ReplPiece],
t_bytes: bool,
m_bytes: bool,
text: &str,
spans: &[Option<(usize, usize)>],
) -> Result<String, String> {
let mut out = String::new();
let mut items = 0usize;
for (i, p) in pieces.iter().enumerate() {
match p {
ReplPiece::Lit(s) => {
if i == 0 || !s.is_empty() {
out.push_str(s);
items += 1;
}
}
ReplPiece::Group(g) => {
if let Some((a, b)) = spans.get(*g).copied().flatten() {
if t_bytes != m_bytes {
let found = if m_bytes { "bytes" } else { "str" };
return Err(re_join_error(items, t_bytes, found));
}
out.push_str(&text[a..b]);
items += 1;
}
}
}
}
Ok(out)
}
pub fn re_match_method(m: &Value, method: &str, args: &[Value]) -> Result<Value, String> {
let (text, spans, named, is_bytes) = match with_host(|h| h.get(m).cloned()) {
Some(PyObj::Match {
text,
spans,
named,
is_bytes,
..
}) => (text, spans, named, is_bytes),
_ => return Err(host::type_error("not a match object")),
};
let ngroups = spans.len();
let group_idx = |g: &Value| -> Option<usize> {
if let Some(i) = with_host(|h| h.as_int(g)) {
return (i >= 0 && (i as usize) < ngroups).then_some(i as usize);
}
with_host(|h| h.as_str(g))
.and_then(|name| named.iter().find(|(n, _)| *n == name).map(|(_, i)| *i))
};
let group_str = |idx: usize| -> Value {
match spans.get(idx).copied().flatten() {
Some((s, e)) => re_text_value(is_bytes, text.get(s..e).unwrap_or("")),
None => Value::Undef, }
};
match method {
"group" => {
if args.is_empty() {
return Ok(group_str(0));
}
if args.len() == 1 {
let idx =
group_idx(&args[0]).ok_or_else(|| "IndexError: no such group".to_string())?;
return Ok(group_str(idx));
}
let mut vals: Vec<Value> = Vec::with_capacity(args.len());
for g in args {
let idx = group_idx(g).ok_or_else(|| "IndexError: no such group".to_string())?;
vals.push(group_str(idx));
}
Ok(with_host(|h| h.new_tuple(vals)))
}
"groups" => {
let default = args.first().cloned().unwrap_or(Value::Undef);
let vals: Vec<Value> = (1..spans.len())
.map(|i| match group_str(i) {
Value::Undef => default.clone(),
v => v,
})
.collect();
Ok(with_host(|h| h.new_tuple(vals)))
}
"groupdict" => {
let named = named.clone();
let mut d: indexmap::IndexMap<host::PKey, (Value, Value)> = indexmap::IndexMap::new();
for (name, idx) in named {
let k = with_host(|h| h.new_str(name.clone()));
d.insert(host::PKey::Str(name), (k, group_str(idx)));
}
Ok(with_host(|h| h.new_dict(d)))
}
"expand" => {
let t = args.first().cloned().unwrap_or(Value::Undef);
if !re_is_text(&t) {
return Err(host::type_error(&format!(
"decoding to str: need a bytes-like object, {} found",
with_host(|h| h.type_name(&t))
)));
}
let t_bytes = with_host(|h| h.as_str(&t)).is_none();
let pieces = re_parse_template(&re_template_text(&t)?, !t_bytes, ngroups - 1, &named)?;
let out = re_expand(&pieces, t_bytes, is_bytes, &text, &spans)?;
Ok(re_text_value(t_bytes, &out))
}
"start" | "end" | "span" => {
let idx = match args.first() {
Some(g) => group_idx(g).ok_or_else(|| "IndexError: no such group".to_string())?,
None => 0,
};
match spans.get(idx).copied().flatten() {
Some((s, e)) => {
let s = crate::regexpr::char_index_of(&text, s) as i64;
let e = crate::regexpr::char_index_of(&text, e) as i64;
Ok(match method {
"start" => Value::Int(s),
"end" => Value::Int(e),
_ => with_host(|h| h.new_tuple(vec![Value::Int(s), Value::Int(e)])),
})
}
None => Ok(match method {
"span" => with_host(|h| h.new_tuple(vec![Value::Int(-1), Value::Int(-1)])),
_ => Value::Int(-1),
}),
}
}
_ => Err(host::type_error(&format!("Match has no method '{method}'"))),
}
}
pub fn tz_info() -> (i64, i64, i64, String, String) {
use std::time::{SystemTime, UNIX_EPOCH};
let t = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0) as libc::time_t;
let mut tm: libc::tm = unsafe { std::mem::zeroed() };
unsafe { libc::localtime_r(&t, &mut tm) };
let gmtoff = tm.tm_gmtoff as i64;
let zone = unsafe {
std::ffi::CStr::from_ptr(tm.tm_zone)
.to_string_lossy()
.into_owned()
};
let is_dst = tm.tm_isdst > 0;
let timezone = if is_dst { -gmtoff + 3600 } else { -gmtoff };
(timezone, timezone - 3600, is_dst as i64, zone.clone(), zone)
}
fn monotonic_origin() -> std::time::Instant {
use std::sync::OnceLock;
static ORIGIN: OnceLock<std::time::Instant> = OnceLock::new();
*ORIGIN.get_or_init(std::time::Instant::now)
}
fn struct_time_from_tm(h: &mut host::PyHost, tm: &libc::tm, gmtoff: i64, zone: &str) -> Value {
let z = h.new_str(zone.to_string());
let fields = vec![
Value::Int(tm.tm_year as i64 + 1900),
Value::Int(tm.tm_mon as i64 + 1),
Value::Int(tm.tm_mday as i64),
Value::Int(tm.tm_hour as i64),
Value::Int(tm.tm_min as i64),
Value::Int(tm.tm_sec as i64),
Value::Int((tm.tm_wday as i64 + 6) % 7), Value::Int(tm.tm_yday as i64 + 1),
Value::Int(tm.tm_isdst as i64),
Value::Int(gmtoff),
z,
];
h.alloc(PyObj::StructTime { fields })
}
fn struct_time_to_tm(args: &[Value]) -> Result<libc::tm, String> {
let seq = host::iter_vec(&arg0(args)?)?;
if seq.len() < 9 {
return Err(host::type_error(
"argument must be a struct_time or sequence of length 9",
));
}
let g = |i: usize| with_host(|h| h.as_int(&seq[i])).unwrap_or(0) as libc::c_int;
let mut tm: libc::tm = unsafe { std::mem::zeroed() };
tm.tm_year = g(0) - 1900;
tm.tm_mon = g(1) - 1;
tm.tm_mday = g(2);
tm.tm_hour = g(3);
tm.tm_min = g(4);
tm.tm_sec = g(5);
tm.tm_wday = (g(6) + 1) % 7;
tm.tm_yday = g(7) - 1;
tm.tm_isdst = g(8);
Ok(tm)
}
fn strftime_libc(fmt: &str, tm: &mut libc::tm) -> String {
let cfmt = match std::ffi::CString::new(fmt) {
Ok(c) => c,
Err(_) => return String::new(),
};
let mut buf = vec![0u8; 256];
let n = unsafe {
libc::strftime(
buf.as_mut_ptr() as *mut libc::c_char,
buf.len(),
cfmt.as_ptr(),
tm,
)
};
String::from_utf8_lossy(&buf[..n]).into_owned()
}
fn call_time(f: &str, args: Vec<Value>) -> Result<Value, String> {
use std::time::{SystemTime, UNIX_EPOCH};
let now_secs = || {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs_f64())
.unwrap_or(0.0)
};
let secs_arg = |args: &[Value]| -> i64 {
args.first()
.and_then(|v| with_host(|h| h.num_val(v)))
.map(|f| f as i64)
.unwrap_or_else(|| now_secs() as i64)
};
match f {
"time" => Ok(Value::Float(now_secs())),
"time_ns" => Ok(Value::Int(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos() as i64)
.unwrap_or(0),
)),
"monotonic" | "perf_counter" | "process_time" => {
Ok(Value::Float(monotonic_origin().elapsed().as_secs_f64()))
}
"monotonic_ns" | "perf_counter_ns" | "process_time_ns" => {
Ok(Value::Int(monotonic_origin().elapsed().as_nanos() as i64))
}
"sleep" => {
let secs = args
.first()
.and_then(|v| with_host(|h| h.num_val(v)))
.unwrap_or(0.0);
if secs > 0.0 {
std::thread::sleep(std::time::Duration::from_secs_f64(secs));
}
Ok(Value::Undef)
}
"gmtime" | "localtime" => {
let t = secs_arg(&args) as libc::time_t;
let mut tm: libc::tm = unsafe { std::mem::zeroed() };
let (gmtoff, zone) = unsafe {
if f == "gmtime" {
libc::gmtime_r(&t, &mut tm);
(0i64, "UTC".to_string())
} else {
libc::localtime_r(&t, &mut tm);
let z = std::ffi::CStr::from_ptr(tm.tm_zone)
.to_string_lossy()
.into_owned();
(tm.tm_gmtoff as i64, z)
}
};
Ok(with_host(|h| struct_time_from_tm(h, &tm, gmtoff, &zone)))
}
"mktime" => {
let mut tm = struct_time_to_tm(&args)?;
let t = unsafe { libc::mktime(&mut tm) };
Ok(Value::Float(t as f64))
}
"strftime" => {
let fmt0 = args.first().ok_or_else(|| {
host::type_error("strftime() takes at least 1 argument (0 given)")
})?;
let fmt = with_host(|h| h.as_str(fmt0))
.ok_or_else(|| host::type_error("strftime() argument 1 must be str"))?;
let mut tm = if args.len() > 1 {
struct_time_to_tm(&args[1..])?
} else {
let t = now_secs() as libc::time_t;
let mut tm: libc::tm = unsafe { std::mem::zeroed() };
unsafe { libc::localtime_r(&t, &mut tm) };
tm
};
Ok(with_host(|h| h.new_str(strftime_libc(&fmt, &mut tm))))
}
"struct_time" => {
let seq = host::iter_vec(&arg0(&args)?)?;
if seq.len() < 9 {
return Err(host::type_error(
"time.struct_time() takes a sequence of length 9 to 11",
));
}
let mut fields: Vec<Value> = seq.into_iter().take(11).collect();
while fields.len() < 11 {
fields.push(Value::Undef);
}
Ok(with_host(|h| h.alloc(PyObj::StructTime { fields })))
}
"asctime" | "ctime" => {
let mut tm = if f == "asctime" && !args.is_empty() {
struct_time_to_tm(&args)?
} else {
let t = now_secs() as libc::time_t;
let mut tm: libc::tm = unsafe { std::mem::zeroed() };
unsafe { libc::localtime_r(&t, &mut tm) };
tm
};
Ok(with_host(|h| {
h.new_str(
strftime_libc("%a %b %e %H:%M:%S %Y", &mut tm)
.trim()
.to_string(),
)
}))
}
_ => Err(host::name_error(&format!("time.{f}"))),
}
}
fn math_arg_repr(x: f64) -> String {
with_host(|h| h.repr_of(&Value::Float(x)))
}
fn math_domain_error(name: &str, x: f64, args: &[Value]) -> Option<String> {
if x.is_nan() {
return None;
}
let msg = match name {
"sqrt" if x < 0.0 => format!("expected a nonnegative input, got {}", math_arg_repr(x)),
"log" | "log2" | "log10" if x <= 0.0 => "expected a positive input".to_string(),
"log" => {
let base = args.get(1).and_then(as_f)?;
if base <= 0.0 && !base.is_nan() {
"expected a positive input".to_string()
} else {
return None;
}
}
"log1p" if x <= -1.0 => format!("expected argument value > -1, got {}", math_arg_repr(x)),
"asin" | "acos" if x.abs() > 1.0 => format!(
"expected a number in range from -1 up to 1, got {}",
math_arg_repr(x)
),
"acosh" if x < 1.0 => format!(
"expected argument value not less than 1, got {}",
math_arg_repr(x)
),
"atanh" if x.abs() >= 1.0 => format!(
"expected a number between -1 and 1, got {}",
math_arg_repr(x)
),
"gamma" if x == f64::NEG_INFINITY => format!(
"expected a noninteger or positive integer, got {}",
math_arg_repr(x)
),
"gamma" | "lgamma" if x <= 0.0 && x.fract() == 0.0 => format!(
"expected a noninteger or positive integer, got {}",
math_arg_repr(x)
),
_ => return None,
};
Some(format!("ValueError: {msg}"))
}
#[cfg(unix)]
fn platform_erf(x: f64) -> f64 {
unsafe extern "C" {
fn erf(x: f64) -> f64;
}
unsafe { erf(x) }
}
#[cfg(unix)]
fn platform_erfc(x: f64) -> f64 {
unsafe extern "C" {
fn erfc(x: f64) -> f64;
}
unsafe { erfc(x) }
}
#[cfg(not(unix))]
fn platform_erf(x: f64) -> f64 {
libm::erf(x)
}
#[cfg(not(unix))]
fn platform_erfc(x: f64) -> f64 {
libm::erfc(x)
}
const LANCZOS_N: usize = 13;
const LANCZOS_G: f64 = 6.02468004077673;
const LANCZOS_G_MINUS_HALF: f64 = 5.52468004077673;
const LANCZOS_NUM_COEFFS: [f64; LANCZOS_N] = [
23531376880.41076,
42919803642.6491,
35711959237.35567,
17921034426.03721,
6039542586.352028,
1439720407.3117216,
248874557.86205417,
31426415.585400194,
2876370.6289353725,
186056.26539522348,
8071.672002365816,
210.82427775157936,
2.5066282746310002,
];
const LANCZOS_DEN_COEFFS: [f64; LANCZOS_N] = [
0.0,
39916800.0,
120543840.0,
150917976.0,
105258076.0,
45995730.0,
13339535.0,
2637558.0,
357423.0,
32670.0,
1925.0,
66.0,
1.0,
];
const GAMMA_INTEGRAL: [f64; 23] = [
1.0,
1.0,
2.0,
6.0,
24.0,
120.0,
720.0,
5040.0,
40320.0,
362880.0,
3628800.0,
39916800.0,
479001600.0,
6227020800.0,
87178291200.0,
1307674368000.0,
20922789888000.0,
355687428096000.0,
6402373705728000.0,
121645100408832000.0,
2432902008176640000.0,
51090942171709440000.0,
1124000727777607680000.0,
];
const MATH_LOGPI: f64 = 1.144729885849400174143427351353058711647;
#[inline]
fn fp_contract(a: f64, b: f64, c: f64) -> f64 {
#[cfg(target_arch = "aarch64")]
{
a.mul_add(b, c)
}
#[cfg(not(target_arch = "aarch64"))]
{
a * b + c
}
}
fn lanczos_sum(x: f64) -> f64 {
let mut num = 0.0f64;
let mut den = 0.0f64;
if x < 5.0 {
for i in (0..LANCZOS_N).rev() {
num = fp_contract(num, x, LANCZOS_NUM_COEFFS[i]);
den = fp_contract(den, x, LANCZOS_DEN_COEFFS[i]);
}
} else {
for i in 0..LANCZOS_N {
num = num / x + LANCZOS_NUM_COEFFS[i];
den = den / x + LANCZOS_DEN_COEFFS[i];
}
}
num / den
}
fn m_sinpi(x: f64) -> f64 {
let y = x.abs() % 2.0;
let n = (2.0 * y).round() as i32;
let pi = std::f64::consts::PI;
let r = match n {
0 => (pi * y).sin(),
1 => (pi * (y - 0.5)).cos(),
2 => (pi * (1.0 - y)).sin(),
3 => -(pi * (y - 1.5)).cos(),
_ => (pi * (y - 2.0)).sin(),
};
1.0f64.copysign(x) * r
}
fn m_tgamma(x: f64) -> f64 {
if !x.is_finite() {
return if x.is_nan() || x > 0.0 { x } else { f64::NAN };
}
if x == 0.0 {
return f64::INFINITY.copysign(x);
}
if x == x.floor() {
if x < 0.0 {
return f64::NAN; }
if x <= GAMMA_INTEGRAL.len() as f64 {
return GAMMA_INTEGRAL[x as usize - 1];
}
}
let absx = x.abs();
if absx < 1e-20 {
return 1.0 / x;
}
if absx > 200.0 {
return if x < 0.0 {
0.0 / m_sinpi(x)
} else {
f64::INFINITY
};
}
let y = absx + LANCZOS_G_MINUS_HALF;
let mut z = if absx > LANCZOS_G_MINUS_HALF {
let q = y - absx;
q - LANCZOS_G_MINUS_HALF
} else {
let q = y - LANCZOS_G_MINUS_HALF;
q - absx
};
z = z * LANCZOS_G / y;
let mut r;
if x < 0.0 {
r = -std::f64::consts::PI / m_sinpi(absx) / absx * y.exp() / lanczos_sum(absx);
r = fp_contract(-z, r, r);
if absx < 140.0 {
r /= y.powf(absx - 0.5);
} else {
let sqrtpow = y.powf(absx / 2.0 - 0.25);
r /= sqrtpow;
r /= sqrtpow;
}
} else {
r = lanczos_sum(absx) / y.exp();
r = fp_contract(z, r, r);
if absx < 140.0 {
r *= y.powf(absx - 0.5);
} else {
let sqrtpow = y.powf(absx / 2.0 - 0.25);
r *= sqrtpow;
r *= sqrtpow;
}
}
r
}
fn m_lgamma(x: f64) -> f64 {
if !x.is_finite() {
return if x.is_nan() { x } else { f64::INFINITY };
}
if x == x.floor() && x <= 2.0 {
return if x <= 0.0 { f64::INFINITY } else { 0.0 };
}
let absx = x.abs();
if absx < 1e-20 {
return -absx.ln();
}
let mut r = lanczos_sum(absx).ln() - LANCZOS_G;
r = fp_contract(absx - 0.5, (absx + LANCZOS_G - 0.5).ln() - 1.0, r);
if x < 0.0 {
r = MATH_LOGPI - m_sinpi(absx).abs().ln() - absx.ln() - r;
}
r
}
fn mod_domain_error(x: f64, y: f64) -> Option<String> {
if x.is_nan() || y.is_nan() {
return None;
}
(x.is_infinite() || y == 0.0).then(|| "ValueError: math domain error".to_string())
}
fn math_range_checked(x: f64, out: f64) -> Result<Value, String> {
if out.is_infinite() && x.is_finite() {
return Err("OverflowError: math range error".to_string());
}
Ok(Value::Float(out))
}
const MATH_REAL_FNS: &[&str] = &[
"sqrt",
"fabs",
"sin",
"cos",
"tan",
"asin",
"acos",
"atan",
"sinh",
"cosh",
"tanh",
"asinh",
"acosh",
"atanh",
"exp",
"exp2",
"expm1",
"log",
"log2",
"log10",
"log1p",
"lgamma",
"gamma",
"erf",
"erfc",
"cbrt",
"degrees",
"radians",
"isnan",
"isinf",
"isfinite",
"atan2",
"hypot",
"copysign",
"fmod",
"remainder",
"pow",
"isclose",
];
fn math_real(v: &Value) -> Result<f64, String> {
if let Some(f) = as_f(v) {
return Ok(f);
}
let coercible = with_host(|h| {
if matches!(h.get(v), Some(PyObj::BigInt(_))) {
return true;
}
#[cfg(feature = "stdlib-ffi")]
if h.foreign_id(v).is_some() {
return true;
}
matches!(h.get(v), Some(PyObj::Instance(i))
if instance_has(h, i, "__float__") || instance_has(h, i, "__index__"))
});
if coercible {
return match construct_float(std::slice::from_ref(v))? {
Value::Float(f) => Ok(f),
other => Ok(as_f(&other).unwrap_or(0.0)),
};
}
Err(host::type_error(&format!(
"must be real number, not {}",
with_host(|h| h.type_name(v))
)))
}
fn math_round_to_int(name: &str, v: &Value) -> Result<Value, String> {
let dunder = match name {
"floor" => "__floor__",
"ceil" => "__ceil__",
_ => "__trunc__",
};
let has =
with_host(|h| matches!(h.get(v), Some(PyObj::Instance(i)) if instance_has(h, i, dunder)));
if has {
return host::call_method(v, dunder, vec![], vec![]);
}
let is_int = with_host(|h| {
matches!(v, Value::Int(_) | Value::Bool(_)) || matches!(h.get(v), Some(PyObj::BigInt(_)))
});
if is_int {
return Ok(match v {
Value::Bool(b) => Value::Int(*b as i64),
other => other.clone(),
});
}
if dunder == "__trunc__" {
if !matches!(v, Value::Float(_)) {
return Err(host::type_error(&format!(
"type {} doesn't define __trunc__ method",
with_host(|h| h.type_name(v))
)));
}
}
let f = math_real(v)?;
let r = match name {
"floor" => f.floor(),
"ceil" => f.ceil(),
_ => f.trunc(),
};
Ok(with_host(|h| f64_to_int(h, r)))
}
fn math_integer(v: &Value) -> Result<num_bigint::BigInt, String> {
if let Some(n) = with_host(|h| h.big_val(v)) {
return Ok(n);
}
if let Some(r) = index_dunder(v)? {
if let Some(n) = with_host(|h| h.big_val(&r)) {
return Ok(n);
}
}
Err(host::type_error(&format!(
"'{}' object cannot be interpreted as an integer",
with_host(|h| h.type_name(v))
)))
}
fn call_math(name: &str, args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
Ok(match call_math_value(name, args, kwargs)? {
Value::Float(f) => Value::Float(host::new_float(f)),
v => v,
})
}
fn call_math_value(name: &str, args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
if let Some(spec) = math_arity(name) {
check_arity(name, &format!("math.{name}"), spec, args.len())?;
}
if matches!(name, "floor" | "ceil" | "trunc") {
return math_round_to_int(name, args.first().unwrap_or(&Value::Undef));
}
let coerced: Vec<Value>;
let args: &[Value] = if MATH_REAL_FNS.contains(&name) {
coerced = args
.iter()
.map(|v| math_real(v).map(Value::Float))
.collect::<Result<Vec<_>, _>>()?;
&coerced
} else if name == "ldexp" && !args.is_empty() {
coerced = std::iter::once(Value::Float(math_real(&args[0])?))
.chain(args[1..].iter().cloned())
.collect();
&coerced
} else {
args
};
let f0 = args.first().and_then(as_f).unwrap_or(0.0);
if let Some(e) = math_domain_error(name, f0, args) {
return Err(e);
}
match name {
"sqrt" => Ok(Value::Float(f0.sqrt())),
"fabs" => Ok(Value::Float(f0.abs())),
"isclose" => {
let a = f0;
let b = args.get(1).and_then(as_f).unwrap_or(0.0);
let mut rel = 1e-9;
let mut abs_tol = 0.0;
for (k, v) in kwargs.iter() {
match k.as_str() {
"rel_tol" => rel = math_real(v)?,
"abs_tol" => abs_tol = math_real(v)?,
_ => {
return Err(host::type_error(&format!(
"isclose() got an unexpected keyword argument '{k}'"
)))
}
}
}
if rel < 0.0 || abs_tol < 0.0 {
return Err("ValueError: tolerances must be non-negative".to_string());
}
let close = if a == b {
true
} else if a.is_infinite() || b.is_infinite() || a.is_nan() || b.is_nan() {
false
} else {
let diff = (b - a).abs();
diff <= (rel * b).abs() || diff <= (rel * a).abs() || diff <= abs_tol
};
Ok(Value::Bool(close))
}
"remainder" => {
let b = args.get(1).and_then(as_f).unwrap_or(0.0);
if let Some(e) = mod_domain_error(f0, b) {
return Err(e);
}
if b.is_infinite() {
return Ok(Value::Float(f0));
}
let n = (f0 / b).round_ties_even();
let r = f0 - n * b;
Ok(Value::Float(if r == 0.0 {
0.0_f64.copysign(f0)
} else {
r
}))
}
"sin" => Ok(Value::Float(f0.sin())),
"cos" => Ok(Value::Float(f0.cos())),
"tan" => Ok(Value::Float(f0.tan())),
"asin" => Ok(Value::Float(f0.asin())),
"acos" => Ok(Value::Float(f0.acos())),
"atan" => Ok(Value::Float(f0.atan())),
"sinh" => math_range_checked(f0, f0.sinh()),
"cosh" => math_range_checked(f0, f0.cosh()),
"tanh" => Ok(Value::Float(f0.tanh())),
"asinh" => Ok(Value::Float(f0.asinh())),
"acosh" => Ok(Value::Float(f0.acosh())),
"atanh" => Ok(Value::Float(f0.atanh())),
"exp" => math_range_checked(f0, f0.exp()),
"exp2" => math_range_checked(f0, f0.exp2()),
"expm1" => Ok(Value::Float(f0.exp_m1())),
"log2" => Ok(Value::Float(f0.log2())),
"log10" => Ok(Value::Float(f0.log10())),
"log1p" => Ok(Value::Float(f0.ln_1p())),
"lgamma" => math_range_checked(f0, m_lgamma(f0)),
"gamma" => math_range_checked(f0, m_tgamma(f0)),
"erf" => Ok(Value::Float(platform_erf(f0))),
"erfc" => Ok(Value::Float(platform_erfc(f0))),
"cbrt" => Ok(Value::Float(f0.cbrt())),
"degrees" => Ok(Value::Float(f0.to_degrees())),
"radians" => Ok(Value::Float(f0.to_radians())),
"isnan" => Ok(Value::Bool(f0.is_nan())),
"isinf" => Ok(Value::Bool(f0.is_infinite())),
"isfinite" => Ok(Value::Bool(f0.is_finite())),
"atan2" => {
let f1 = args.get(1).and_then(as_f).unwrap_or(0.0);
Ok(Value::Float(f0.atan2(f1)))
}
"hypot" => {
let coords: Vec<f64> = args.iter().filter_map(as_f).collect();
if coords.iter().any(|c| c.is_infinite()) {
return Ok(Value::Float(f64::INFINITY));
}
let sumsq: f64 = coords.iter().map(|c| c * c).sum();
Ok(Value::Float(sumsq.sqrt()))
}
"copysign" => {
let f1 = args.get(1).and_then(as_f).unwrap_or(0.0);
Ok(Value::Float(f0.copysign(f1)))
}
"fmod" => {
let f1 = args.get(1).and_then(as_f).unwrap_or(0.0);
if let Some(e) = mod_domain_error(f0, f1) {
return Err(e);
}
Ok(Value::Float(f0 % f1))
}
"ldexp" => {
let f1 = args.get(1).and_then(as_f).unwrap_or(0.0);
Ok(Value::Float(f0 * 2f64.powi(f1 as i32)))
}
"isqrt" => {
let n = math_integer(&args[0])?;
if n.sign() == num_bigint::Sign::Minus {
return Err("ValueError: isqrt() argument must be nonnegative".into());
}
Ok(with_host(|h| h.norm_big(n.sqrt())))
}
"log" => {
let base = args.get(1).and_then(as_f);
if base == Some(1.0) {
return Err("ZeroDivisionError: division by zero".to_string());
}
Ok(Value::Float(match base {
Some(b) => f0.log(b),
None => f0.ln(),
}))
}
"pow" => {
let e = args.get(1).and_then(as_f).unwrap_or(0.0);
let domain = (f0 == 0.0 && e < 0.0 && e.is_finite())
|| (f0 < 0.0 && e.is_finite() && e.fract() != 0.0);
if domain {
return Err("ValueError: math domain error".to_string());
}
math_range_checked(f0, f0.powf(e))
}
"gcd" => {
use num_integer::Integer;
let mut acc = num_bigint::BigInt::from(0);
for a in args {
let n = math_integer(a)?;
acc = acc.gcd(&n);
}
Ok(with_host(|h| h.norm_big(acc)))
}
"lcm" => {
use num_integer::Integer;
let mut acc = num_bigint::BigInt::from(1);
for a in args {
let n = math_integer(a)?;
if n.sign() == num_bigint::Sign::NoSign {
return Ok(Value::Int(0));
}
acc = acc.lcm(&n);
}
Ok(with_host(|h| h.norm_big(acc)))
}
"factorial" => {
use num_traits::ToPrimitive as _;
let big = math_integer(&args[0])?;
if big.sign() == num_bigint::Sign::Minus {
return Err("ValueError: factorial() not defined for negative values".into());
}
let n = big.to_i64().ok_or_else(|| {
"OverflowError: factorial() argument should not exceed 9223372036854775807"
.to_string()
})?;
let mut acc = num_bigint::BigInt::from(1);
for i in 2..=n {
acc *= i;
}
Ok(with_host(|h| {
use num_traits::ToPrimitive;
match acc.to_i64() {
Some(v) => Value::Int(v),
None => h.alloc(PyObj::BigInt(acc)),
}
}))
}
"prod" => {
let items = with_host(|h| h.iter_items(&args[0]))?;
let start = kwargs
.iter()
.find(|(k, _)| k == "start")
.map(|(_, v)| v.clone())
.unwrap_or(Value::Int(1));
let all_int = std::iter::once(&start)
.chain(items.iter())
.all(|v| with_host(|h| h.big_val(v)).is_some());
if all_int {
use num_traits::ToPrimitive;
let mut acc = with_host(|h| h.big_val(&start)).unwrap();
for v in &items {
acc *= with_host(|h| h.big_val(v)).unwrap();
}
return Ok(with_host(|h| match acc.to_i64() {
Some(n) => Value::Int(n),
None => h.alloc(PyObj::BigInt(acc)),
}));
}
let mut acc = as_f(&start).ok_or_else(|| host::type_error("must be a number"))?;
for v in &items {
acc *= as_f(v).ok_or_else(|| host::type_error("must be a number"))?;
}
Ok(Value::Float(acc))
}
"fsum" => {
let items = with_host(|h| h.iter_items(&args[0]))?;
let mut partials: Vec<f64> = Vec::new();
for it in &items {
let x = as_f(it).ok_or_else(|| host::type_error("must be real number"))?;
exact_sum_push(&mut partials, x);
}
Ok(Value::Float(partials.iter().sum()))
}
"sumprod" => {
let (p, q) =
with_host(|h| Ok::<_, String>((h.iter_items(&args[0])?, h.iter_items(&args[1])?)))?;
if p.len() != q.len() {
return Err("ValueError: Inputs are not the same length".into());
}
let all_int = with_host(|h| p.iter().chain(q.iter()).all(|v| h.big_val(v).is_some()));
if all_int {
let mut acc = num_bigint::BigInt::from(0);
for (a, b) in p.iter().zip(&q) {
let (x, y) = with_host(|h| (h.big_val(a).unwrap(), h.big_val(b).unwrap()));
acc += x * y;
}
return Ok(with_host(|h| h.norm_big(acc)));
}
let mut partials: Vec<f64> = Vec::new();
for (a, b) in p.iter().zip(&q) {
let x = as_f(a).ok_or_else(|| host::type_error("must be real number"))?;
let y = as_f(b).ok_or_else(|| host::type_error("must be real number"))?;
let prod = x * y;
let err = x.mul_add(y, -prod);
exact_sum_push(&mut partials, prod);
if err != 0.0 {
exact_sum_push(&mut partials, err);
}
}
Ok(Value::Float(partials.iter().sum()))
}
"comb" | "perm" => {
use num_bigint::BigInt;
use num_traits::{ToPrimitive, Zero};
let n = math_integer(&args[0])?;
let is_comb = name == "comb";
let k = match args.get(1) {
Some(v) => math_integer(v)?,
None if is_comb => {
return Err(host::type_error("comb() takes exactly two arguments"))
}
None => {
if n.sign() == num_bigint::Sign::Minus {
return Err(
"ValueError: factorial() not defined for negative values".into()
);
}
n.clone()
}
};
if n.sign() == num_bigint::Sign::Minus {
return Err("ValueError: n must be a non-negative integer".to_string());
}
if k.sign() == num_bigint::Sign::Minus {
return Err("ValueError: k must be a non-negative integer".to_string());
}
if k > n {
return Ok(Value::Int(0));
}
let k = if is_comb && &n - &k < k { &n - &k } else { k };
{
use num_traits::ToPrimitive as _;
if k.to_i64().is_none() {
return Err(
"OverflowError: factorial() argument should not exceed 9223372036854775807"
.to_string(),
);
}
}
let mut num = BigInt::from(1);
let mut i = &n - &k + 1;
while i <= n {
num *= &i;
i += 1;
}
let result = if is_comb {
let mut den = BigInt::from(1);
let mut j = BigInt::from(2);
while j <= k {
den *= &j;
j += 1;
}
if den.is_zero() {
num
} else {
num / den
}
} else {
num
};
Ok(with_host(|h| match result.to_i64() {
Some(v) => Value::Int(v),
None => h.alloc(PyObj::BigInt(result)),
}))
}
_ => Err(host::name_error(&format!("math.{name}"))),
}
}
const EXC_PARENTS: &[(&str, &str)] = &[
("Exception", "BaseException"),
("ArithmeticError", "Exception"),
("ZeroDivisionError", "ArithmeticError"),
("OverflowError", "ArithmeticError"),
("FloatingPointError", "ArithmeticError"),
("LookupError", "Exception"),
("IndexError", "LookupError"),
("KeyError", "LookupError"),
("ValueError", "Exception"),
("UnsupportedOperation", "ValueError"),
("_csv.Error", "Exception"),
("struct.error", "Exception"),
("binascii.Error", "ValueError"),
("binascii.Incomplete", "Exception"),
("re.PatternError", "Exception"),
("UnicodeError", "ValueError"),
("TypeError", "Exception"),
("NameError", "Exception"),
("UnboundLocalError", "NameError"),
("AttributeError", "Exception"),
("RuntimeError", "Exception"),
("RecursionError", "RuntimeError"),
("NotImplementedError", "RuntimeError"),
("StopIteration", "Exception"),
("StopAsyncIteration", "Exception"),
("AssertionError", "Exception"),
("ImportError", "Exception"),
("ModuleNotFoundError", "ImportError"),
("OSError", "Exception"),
("IOError", "OSError"),
("FileNotFoundError", "OSError"),
("PermissionError", "OSError"),
("KeyboardInterrupt", "BaseException"),
("SystemExit", "BaseException"),
("GeneratorExit", "BaseException"),
("MemoryError", "Exception"),
("EOFError", "Exception"),
("NotADirectoryError", "OSError"),
("IsADirectoryError", "OSError"),
("IndentationError", "SyntaxError"),
("TabError", "IndentationError"),
("SyntaxError", "Exception"),
("UnicodeDecodeError", "UnicodeError"),
("UnicodeEncodeError", "UnicodeError"),
("UnicodeTranslateError", "UnicodeError"),
("ConnectionError", "OSError"),
("BrokenPipeError", "ConnectionError"),
("TimeoutError", "OSError"),
("CancelledError", "BaseException"),
("InvalidStateError", "Exception"),
("QueueEmpty", "Exception"),
("QueueFull", "Exception"),
("Warning", "Exception"),
("UserWarning", "Warning"),
("DeprecationWarning", "Warning"),
("PendingDeprecationWarning", "Warning"),
("SyntaxWarning", "Warning"),
("RuntimeWarning", "Warning"),
("FutureWarning", "Warning"),
("ImportWarning", "Warning"),
("UnicodeWarning", "Warning"),
("BytesWarning", "Warning"),
("ResourceWarning", "Warning"),
("EncodingWarning", "Warning"),
("SystemError", "Exception"),
("ReferenceError", "Exception"),
("BufferError", "Exception"),
("EnvironmentError", "Exception"),
("PythonFinalizationError", "RuntimeError"),
("BlockingIOError", "OSError"),
("ChildProcessError", "OSError"),
("FileExistsError", "OSError"),
("InterruptedError", "OSError"),
("ProcessLookupError", "OSError"),
("ConnectionAbortedError", "ConnectionError"),
("ConnectionRefusedError", "ConnectionError"),
("ConnectionResetError", "ConnectionError"),
("BaseExceptionGroup", "BaseException"),
("ExceptionGroup", "Exception"),
];
fn exc_parent(name: &str) -> Option<&'static str> {
EXC_PARENTS
.iter()
.find(|(c, _)| *c == name)
.map(|(_, p)| *p)
}
pub(crate) fn exception_isa(exc_class: &str, want: &str, h: &host::PyHost) -> bool {
if exc_class == want {
return true;
}
if want == "BaseException" {
if is_exception_class(exc_class)
|| h.classes.contains_key(exc_class) && h.class_is_exception(exc_class)
{
return true;
}
return h
.foreign_exc_bases
.get(exc_class)
.is_some_and(|bases| bases.iter().any(|b| b == "BaseException"))
|| exception_isa(exc_class, "Exception", h);
}
if want == excgroup::BASE_GROUP
&& exc_class != excgroup::BASE_GROUP
&& exception_isa(exc_class, excgroup::GROUP, h)
{
return true;
}
let mut cur = exc_class;
while let Some(p) = exc_parent(cur) {
if p == want {
return true;
}
cur = p;
}
if h.classes.contains_key(exc_class) {
for c in h.mro_of(exc_class) {
if c == want {
return true;
}
if let Some(p) = exc_parent(&c) {
if exception_isa(p, want, h) {
return true;
}
}
}
}
if let Some(bases) = h.foreign_exc_bases.get(exc_class) {
if bases.iter().any(|b| b == want) {
return true;
}
}
if want == "Exception"
&& !is_exception_class(exc_class)
&& !h.classes.contains_key(exc_class)
&& !is_type_like_builtin(exc_class)
&& !UNINSTANTIABLE_TYPES.contains(&exc_class)
&& !matches!(
exc_class,
"NoneType" | "function" | "module" | "method" | "builtin_function_or_method"
)
{
return true;
}
false
}
fn isinstance_dispatch(v: &Value, cls: &Value) -> Result<bool, String> {
if let Some(PyObj::Tuple(ts)) = with_host(|h| h.get(cls).cloned()) {
for t in ts {
if isinstance_dispatch(v, &t)? {
return Ok(true);
}
}
return Ok(false);
}
if let Some(PyObj::Union { args }) = with_host(|h| h.get(cls).cloned()) {
for m in args {
if matches!(m, Value::Undef) {
if matches!(v, Value::Undef) {
return Ok(true);
}
} else if isinstance_dispatch(v, &m)? {
return Ok(true);
}
}
return Ok(false);
}
if let Some(r) = host::metaclass_hook(cls, "__instancecheck__", v.clone()) {
let res = r?;
return Ok(with_host(|h| h.truthy(&res)));
}
#[cfg(feature = "stdlib-ffi")]
if let Some(cls_id) = with_host(|h| h.foreign_id(cls)) {
if with_host(|h| h.foreign_id(v).is_none()) {
if let Some(name) = crate::ffi::foreign_builtin_type_name(cls_id) {
if with_host(|h| h.type_name(v)) == name {
return Ok(true);
}
}
}
return with_host(|h| crate::ffi::isinstance_foreign(h, v, cls_id));
}
#[cfg(feature = "stdlib-ffi")]
if let Some(v_id) = with_host(|h| h.foreign_id(v)) {
if let Some(want) = with_host(|h| callable_name(h, cls)) {
if crate::ffi::foreign_isinstance_of_builtin(v_id, &want) {
return Ok(true);
}
}
}
if with_host(|h| matches!(h.get(cls), Some(PyObj::GenericAlias { .. }))) {
return Err(host::type_error(
"isinstance() argument 2 cannot be a parameterized generic",
));
}
if plainly_not_a_class(cls) {
return Err(host::type_error(
"isinstance() arg 2 must be a type, a tuple of types, or a union",
));
}
Ok(with_host(|h| isinstance(h, v, cls)))
}
fn plainly_not_a_class(v: &Value) -> bool {
match v {
Value::Undef | Value::Bool(_) | Value::Int(_) | Value::Float(_) | Value::Str(_) => true,
Value::Obj(_) => with_host(|h| {
matches!(
h.get(v),
Some(
PyObj::Str(_)
| PyObj::BigInt(_)
| PyObj::List(_)
| PyObj::Tuple(_)
| PyObj::Dict(_)
| PyObj::Set(_)
| PyObj::Frozenset(_)
| PyObj::Bytes(_)
| PyObj::Func(_)
)
)
}),
_ => false,
}
}
fn issubclass_dispatch(sub: &Value, cls: &Value) -> Result<bool, String> {
if let Some(PyObj::Tuple(ts)) = with_host(|h| h.get(cls).cloned()) {
for t in ts {
if issubclass_dispatch(sub, &t)? {
return Ok(true);
}
}
return Ok(false);
}
if let Some(r) = host::metaclass_hook(cls, "__subclasscheck__", sub.clone()) {
let res = r?;
return Ok(with_host(|h| h.truthy(&res)));
}
#[cfg(feature = "stdlib-ffi")]
if with_host(|h| h.foreign_id(sub).is_some() || h.foreign_id(cls).is_some()) {
return with_host(|h| crate::ffi::issubclass_values(h, sub, cls));
}
if plainly_not_a_class(sub) {
return Err(host::type_error("issubclass() arg 1 must be a class"));
}
let a = match with_host(|h| callable_name(h, sub)) {
Some(n) => n,
None => return Err(host::type_error("issubclass() arg 1 must be a class")),
};
if plainly_not_a_class(cls) {
return Err(host::type_error(
"issubclass() arg 2 must be a class, a tuple of classes, or a union",
));
}
let b = with_host(|h| callable_name(h, cls)).unwrap_or_default();
Ok(with_host(|h| type_isa(h, &a, &b)))
}
fn isinstance(h: &host::PyHost, v: &Value, cls: &Value) -> bool {
if let Some(PyObj::Tuple(ts)) = h.get(cls) {
return ts.iter().any(|t| isinstance(h, v, t));
}
let want = match callable_name(h, cls) {
Some(n) => n,
None => return false,
};
let want = native_type_key(&want).to_string();
if want == "type" {
match h.get(v) {
Some(PyObj::Class(_)) | Some(PyObj::NamedTupleType { .. }) => return true,
Some(PyObj::Builtin(n)) if is_type_object_name(n) => return true,
_ => {}
}
}
let vt = h.type_name(v);
if type_isa(h, &vt, &want) {
return true;
}
match h.get(v) {
Some(PyObj::Tuple(_)) if want == "tuple" => true,
Some(PyObj::Dict(_)) if want == "dict" => true,
_ => false,
}
}
fn type_isa(h: &host::PyHost, a: &str, b: &str) -> bool {
let (a, b) = (native_type_key(a), native_type_key(b));
if a == b || b == "object" {
return true;
}
if a == "bool" && b == "int" {
return true;
}
if b == "dict" && matches!(a, "Counter" | "defaultdict" | "OrderedDict") {
return true;
}
if exception_isa(a, b, h) {
return true;
}
if h.classes.contains_key(a) {
return h.mro_of(a).iter().any(|c| native_type_key(c) == b);
}
false
}
pub fn type_method_names(typename: &str) -> Option<&'static [&'static str]> {
Some(match typename {
"str" => STR_METHODS,
"bytes" => BYTES_METHODS,
"bytearray" => BYTEARRAY_METHODS,
"memoryview" => MEMORYVIEW_METHODS,
"list" => LIST_METHODS,
"dict" => DICT_METHODS,
"set" => SET_METHODS,
"frozenset" => FROZENSET_METHODS,
"tuple" => TUPLE_METHODS,
"range" => &["index", "count"],
"slice" => &["indices"],
"deque" => DEQUE_METHODS,
"complex" => COMPLEX_METHODS,
"property" => PROPERTY_METHODS,
"generator" => GENERATOR_METHODS,
"TextIOWrapper" | "BufferedReader" | "BufferedWriter" | "BufferedRandom" => FILE_METHODS,
"_io.StringIO" | "_io.BytesIO" => STREAM_METHODS,
"Future" | "Task" => FUTURE_METHODS,
"_UnixSelectorEventLoop" => LOOP_METHODS,
_ => return None,
})
}
const CONTAINS_TYPES: &[&str] = &[
"str",
"bytes",
"bytearray",
"list",
"tuple",
"dict",
"set",
"frozenset",
"range",
"deque",
"dict_keys",
"dict_items",
];
const ITER_PROTOCOL_TYPES: &[&str] = &[
"zip",
"map",
"filter",
"enumerate",
"iterator",
"callable_iterator",
];
pub fn type_dir_names(typename: &str) -> Vec<&'static str> {
let typename = native_type_key(typename);
let mut out = own_dir_names(typename);
if inherits_object_surface(typename) {
out.extend_from_slice(OBJECT_DUNDERS);
}
out.sort_unstable();
out.dedup();
out
}
fn inherits_object_surface(typename: &str) -> bool {
OBJECT_ONLY_TYPES.contains(&typename) || !own_dir_names(typename).is_empty()
}
fn own_dir_names(typename: &str) -> Vec<&'static str> {
let mut out: Vec<&'static str> = Vec::new();
out.extend_from_slice(type_data_attrs(typename));
out.extend_from_slice(type_classmethods(typename));
out.extend_from_slice(type_extra_names(typename));
if let Some(list) = type_method_names(typename) {
out.extend_from_slice(list);
}
out.extend_from_slice(op_dunders(typename));
match typename {
"type" => {
out.extend_from_slice(TYPE_OBJECT_METHODS);
out.extend_from_slice(&[
"__abstractmethods__",
"__annotate__",
"__annotations__",
"__base__",
"__bases__",
"__dict__",
"__module__",
"__mro__",
"__name__",
"__qualname__",
"__subclasses__",
"__text_signature__",
"__type_params__",
]);
}
"int" | "bool" => {
out.extend_from_slice(INT_METHODS);
out.extend_from_slice(INT_DUNDERS);
}
"float" => {
out.extend_from_slice(FLOAT_METHODS);
out.extend_from_slice(FLOAT_DUNDERS);
}
"OrderedDict" => {
out.extend_from_slice(DICT_METHODS);
out.push("move_to_end");
}
"defaultdict" => {
out.extend_from_slice(DICT_METHODS);
out.extend_from_slice(DEFAULTDICT_EXTRA);
}
"Counter" => {
out.extend_from_slice(DICT_METHODS);
out.extend_from_slice(COUNTER_EXTRA);
}
"dict_keys" | "dict_items" => out.push("isdisjoint"),
"Context" => out.extend_from_slice(&["run", "copy", "get", "keys", "values", "items"]),
"getset_descriptor"
| "member_descriptor"
| "wrapper_descriptor"
| "classmethod_descriptor"
| "method-wrapper" => out.extend_from_slice(&["__get__", "__set__", "__delete__"]),
"coroutine" => {
out.extend_from_slice(GENERATOR_METHODS);
out.push("__await__");
}
"async_generator" => {
out.extend_from_slice(&["__aiter__", "__anext__", "asend", "athrow", "aclose"])
}
"Event" => out.extend_from_slice(&["set", "clear", "is_set", "wait"]),
"Lock" => {
out.extend_from_slice(&["acquire", "release", "locked", "__aenter__", "__aexit__"])
}
"lock" => out.extend_from_slice(&[
"acquire",
"release",
"locked",
"_at_fork_reinit",
"__enter__",
"__exit__",
]),
"RLock" => out.extend_from_slice(&[
"acquire",
"release",
"locked",
"_is_owned",
"_at_fork_reinit",
"__enter__",
"__exit__",
]),
"Queue" => out.extend_from_slice(&[
"put",
"get",
"put_nowait",
"get_nowait",
"qsize",
"empty",
"full",
]),
t if t.starts_with("itertools.") || ITER_PROTOCOL_TYPES.contains(&t) => {
out.extend_from_slice(&["__next__", "__iter__"])
}
t if is_exception_class(t) => {
out.extend_from_slice(&["with_traceback", "add_note"]);
if t == excgroup::GROUP || t == excgroup::BASE_GROUP {
out.extend_from_slice(&["split", "subgroup", "derive"]);
}
}
_ => {}
}
if CONTAINS_TYPES.contains(&typename) {
out.push("__contains__");
}
for n in CONTAINER_DUNDERS {
if is_object_dunder_method(typename, n) {
out.push(n);
}
}
out
}
fn type_extra_names(tn: &str) -> &'static [&'static str] {
match tn {
"str" => &["__getnewargs__"],
"bytes" => &["__getnewargs__", "__bytes__", "__buffer__"],
"bytearray" => &[
"__buffer__",
"__release_buffer__",
"__alloc__",
"copy",
"resize",
],
"int" | "bool" => &["__getnewargs__"],
"float" => &["__getnewargs__", "__getformat__"],
"complex" => &["__getnewargs__", "__complex__", "__neg__", "__pos__"],
"tuple" => &["__getnewargs__", "__class_getitem__"],
"list" | "dict" | "deque" | "OrderedDict" | "defaultdict" | "Counter" => {
&["__reversed__", "__class_getitem__"]
}
"set" | "frozenset" | "enumerate" => &["__class_getitem__"],
"range" | "dict_keys" | "dict_values" | "dict_items" => &["__reversed__"],
"memoryview" => &["__class_getitem__", "__buffer__", "__release_buffer__"],
_ => &[],
}
}
pub fn type_data_attrs(tn: &str) -> &'static [&'static str] {
match tn {
"int" | "bool" => &["real", "imag", "numerator", "denominator"],
"float" | "complex" => &["real", "imag"],
"slice" | "range" => &["start", "stop", "step"],
"property" => &["fget", "fset", "fdel", "__name__", "__isabstractmethod__"],
"generator" | "coroutine" => &["__name__", "__qualname__", "gi_running", "gi_suspended"],
"deque" => &["maxlen"],
"defaultdict" => &["default_factory"],
"dict_keys" | "dict_values" | "dict_items" => &["mapping"],
"memoryview" => &[
"obj",
"nbytes",
"format",
"itemsize",
"ndim",
"readonly",
"shape",
"strides",
"contiguous",
"c_contiguous",
"f_contiguous",
],
_ => &[],
}
}
const OBJECT_ONLY_TYPES: &[&str] = &["object", "NoneType", "ellipsis", "NotImplementedType"];
pub fn type_has_method(typename: &str, name: &str) -> bool {
let typename = native_type_key(typename);
if OBJECT_DUNDERS.contains(&name) && inherits_object_surface(typename) {
return true;
}
if CONTAINER_DUNDERS.contains(&name) && is_object_dunder_method(typename, name) {
return true;
}
if type_extra_names(typename).contains(&name) {
return true;
}
if type_data_attrs(typename).contains(&name) || type_classmethods(typename).contains(&name) {
return true;
}
if name == "__contains__" && CONTAINS_TYPES.contains(&typename) {
return true;
}
if op_dunders(typename).contains(&name) {
return true;
}
if let Some(list) = type_method_names(typename) {
return list.contains(&name);
}
let list: &[&str] = match typename {
"OrderedDict" => return DICT_METHODS.contains(&name) || name == "move_to_end",
"defaultdict" => {
return DICT_METHODS.contains(&name) || DEFAULTDICT_EXTRA.contains(&name)
}
"Counter" => return DICT_METHODS.contains(&name) || COUNTER_EXTRA.contains(&name),
"dict_keys" | "dict_items" => return name == "isdisjoint",
"Context" => return matches!(name, "run" | "copy" | "get" | "keys" | "values" | "items"),
"getset_descriptor"
| "member_descriptor"
| "wrapper_descriptor"
| "classmethod_descriptor"
| "method-wrapper" => return matches!(name, "__get__" | "__set__" | "__delete__"),
"int" | "bool" => return INT_METHODS.contains(&name) || INT_DUNDERS.contains(&name),
"float" => return FLOAT_METHODS.contains(&name) || FLOAT_DUNDERS.contains(&name),
_ if typename.starts_with("itertools.") || ITER_PROTOCOL_TYPES.contains(&typename) => {
return matches!(name, "__next__" | "__iter__")
}
"coroutine" => return GENERATOR_METHODS.contains(&name) || name == "__await__",
"async_generator" => {
return matches!(
name,
"__aiter__" | "__anext__" | "asend" | "athrow" | "aclose"
)
}
"Event" => return matches!(name, "set" | "clear" | "is_set" | "wait"),
"redirect_stdout" | "redirect_stderr" => return matches!(name, "__enter__" | "__exit__"),
"Lock" => {
return matches!(
name,
"acquire" | "release" | "locked" | "__aenter__" | "__aexit__"
)
}
_ if is_exception_class(typename) && matches!(name, "with_traceback" | "add_note") => {
return true
}
excgroup::GROUP | excgroup::BASE_GROUP
if matches!(name, "split" | "subgroup" | "derive") =>
{
return true
}
"lock" => {
return matches!(
name,
"acquire" | "release" | "locked" | "_at_fork_reinit" | "__enter__" | "__exit__"
)
}
"RLock" => {
return matches!(
name,
"acquire"
| "release"
| "locked"
| "_is_owned"
| "_at_fork_reinit"
| "__enter__"
| "__exit__"
)
}
"Queue" => {
return matches!(
name,
"put" | "get" | "put_nowait" | "get_nowait" | "qsize" | "empty" | "full"
)
}
_ => &[],
};
list.contains(&name)
}
const STREAM_METHODS: &[&str] = &[
"read",
"read1",
"readline",
"readlines",
"write",
"writelines",
"seek",
"tell",
"truncate",
"getvalue",
"close",
"flush",
"readable",
"writable",
"seekable",
"isatty",
"__enter__",
"__exit__",
"__iter__",
"__next__",
];
const FUTURE_METHODS: &[&str] = &[
"set_result",
"set_exception",
"result",
"exception",
"done",
"cancelled",
"cancel",
"add_done_callback",
"get_name",
"__await__",
"__iter__",
];
const LOOP_METHODS: &[&str] = &[
"run_until_complete",
"create_task",
"create_future",
"call_soon",
"call_later",
"time",
"is_running",
"is_closed",
"stop",
"close",
"run_forever",
"get_debug",
"set_debug",
];
const PROPERTY_METHODS: &[&str] = &[
"getter",
"setter",
"deleter",
"__get__",
"__set__",
"__delete__",
"__set_name__",
"__isabstractmethod__",
];
const GENERATOR_METHODS: &[&str] = &["send", "throw", "close", "__next__", "__iter__"];
const BYTES_METHODS: &[&str] = &[
"decode",
"fromhex",
"hex",
"index",
"rindex",
"find",
"rfind",
"count",
"startswith",
"endswith",
"upper",
"lower",
"split",
"rsplit",
"splitlines",
"join",
"replace",
"strip",
"lstrip",
"rstrip",
"partition",
"rpartition",
"removeprefix",
"removesuffix",
"swapcase",
"title",
"capitalize",
"zfill",
"expandtabs",
"center",
"ljust",
"rjust",
"translate",
"maketrans",
"isalpha",
"isdigit",
"isalnum",
"isspace",
"isupper",
"islower",
"istitle",
"isascii",
];
const BYTEARRAY_METHODS: &[&str] = &[
"append",
"extend",
"insert",
"reverse",
"remove",
"pop",
"clear",
"decode",
"fromhex",
"hex",
"index",
"rindex",
"find",
"rfind",
"count",
"startswith",
"endswith",
"upper",
"lower",
"split",
"rsplit",
"splitlines",
"join",
"replace",
"strip",
"lstrip",
"rstrip",
"partition",
"rpartition",
"removeprefix",
"removesuffix",
"swapcase",
"title",
"capitalize",
"zfill",
"expandtabs",
"center",
"ljust",
"rjust",
"translate",
"maketrans",
"isalpha",
"isdigit",
"isalnum",
"isspace",
"isupper",
"islower",
"istitle",
"isascii",
];
const MEMORYVIEW_METHODS: &[&str] = &[
"tobytes",
"hex",
"tolist",
"release",
"__enter__",
"__exit__",
];
const DEQUE_METHODS: &[&str] = &[
"append",
"appendleft",
"pop",
"popleft",
"extend",
"extendleft",
"rotate",
"clear",
"count",
"index",
"remove",
"insert",
"copy",
"__copy__",
"reverse",
];
const FILE_METHODS: &[&str] = &[
"read",
"readline",
"readlines",
"write",
"writelines",
"close",
"flush",
"readable",
"writable",
"seekable",
"seek",
"tell",
"truncate",
"fileno",
"isatty",
"__enter__",
"__exit__",
"__iter__",
"__next__",
];
const STR_METHODS: &[&str] = &[
"upper",
"lower",
"strip",
"lstrip",
"rstrip",
"split",
"rsplit",
"splitlines",
"join",
"replace",
"startswith",
"endswith",
"find",
"rfind",
"index",
"count",
"capitalize",
"title",
"format",
"isdigit",
"isalpha",
"isalnum",
"isspace",
"isupper",
"islower",
"zfill",
"center",
"ljust",
"rjust",
"encode",
"removeprefix",
"removesuffix",
"swapcase",
"casefold",
"rindex",
"partition",
"rpartition",
"isnumeric",
"isdecimal",
"isidentifier",
"istitle",
"isprintable",
"expandtabs",
"translate",
"format_map",
"isascii",
"maketrans",
];
const LIST_METHODS: &[&str] = &[
"append", "extend", "insert", "remove", "pop", "clear", "index", "count", "sort", "reverse",
"copy",
];
const DICT_METHODS: &[&str] = &[
"keys",
"values",
"items",
"get",
"pop",
"popitem",
"setdefault",
"update",
"clear",
"copy",
"fromkeys",
];
const SET_METHODS: &[&str] = &[
"add",
"remove",
"discard",
"pop",
"clear",
"union",
"intersection",
"difference",
"issubset",
"issuperset",
"isdisjoint",
"update",
"intersection_update",
"difference_update",
"symmetric_difference_update",
"copy",
"symmetric_difference",
];
const FROZENSET_METHODS: &[&str] = &[
"union",
"intersection",
"difference",
"issubset",
"issuperset",
"isdisjoint",
"copy",
"symmetric_difference",
];
const TUPLE_METHODS: &[&str] = &["count", "index"];
const INT_METHODS: &[&str] = &[
"bit_length",
"bit_count",
"to_bytes",
"as_integer_ratio",
"is_integer",
"conjugate",
];
const FLOAT_METHODS: &[&str] = &["is_integer", "as_integer_ratio", "hex", "conjugate"];
const COMPLEX_METHODS: &[&str] = &["conjugate", "__abs__"];
const INT_DUNDERS: &[&str] = &[
"__add__",
"__radd__",
"__sub__",
"__rsub__",
"__mul__",
"__rmul__",
"__truediv__",
"__rtruediv__",
"__floordiv__",
"__rfloordiv__",
"__mod__",
"__rmod__",
"__pow__",
"__rpow__",
"__divmod__",
"__rdivmod__",
"__and__",
"__rand__",
"__or__",
"__ror__",
"__xor__",
"__rxor__",
"__lshift__",
"__rlshift__",
"__rshift__",
"__rrshift__",
"__neg__",
"__pos__",
"__abs__",
"__invert__",
"__eq__",
"__ne__",
"__lt__",
"__le__",
"__gt__",
"__ge__",
"__index__",
"__int__",
"__float__",
"__trunc__",
"__floor__",
"__ceil__",
"__round__",
"__bool__",
"__repr__",
"__str__",
"__hash__",
];
const FLOAT_DUNDERS: &[&str] = &[
"__add__",
"__radd__",
"__sub__",
"__rsub__",
"__mul__",
"__rmul__",
"__truediv__",
"__rtruediv__",
"__floordiv__",
"__rfloordiv__",
"__mod__",
"__rmod__",
"__pow__",
"__rpow__",
"__divmod__",
"__rdivmod__",
"__neg__",
"__pos__",
"__abs__",
"__eq__",
"__ne__",
"__lt__",
"__le__",
"__gt__",
"__ge__",
"__int__",
"__float__",
"__trunc__",
"__floor__",
"__ceil__",
"__round__",
"__bool__",
"__repr__",
"__str__",
"__hash__",
];
fn op_dunders(typename: &str) -> &'static [&'static str] {
const TEXT: &[&str] = &["__add__", "__mul__", "__rmul__", "__mod__", "__rmod__"];
const MUT_TEXT: &[&str] = &[
"__add__", "__iadd__", "__mul__", "__rmul__", "__imul__", "__mod__", "__rmod__",
];
const SEQ: &[&str] = &["__add__", "__mul__", "__rmul__"];
const MUT_SEQ: &[&str] = &["__add__", "__iadd__", "__mul__", "__rmul__", "__imul__"];
const DICT: &[&str] = &["__or__", "__ror__", "__ior__"];
const VIEW: &[&str] = &[
"__sub__", "__rsub__", "__and__", "__rand__", "__or__", "__ror__", "__xor__", "__rxor__",
];
const SET: &[&str] = &[
"__sub__", "__rsub__", "__isub__", "__and__", "__rand__", "__iand__", "__or__", "__ror__",
"__ior__", "__xor__", "__rxor__", "__ixor__",
];
const FROZENSET: &[&str] = &[
"__sub__", "__rsub__", "__and__", "__rand__", "__or__", "__ror__", "__xor__", "__rxor__",
];
const COMPLEX: &[&str] = &[
"__add__",
"__radd__",
"__sub__",
"__rsub__",
"__mul__",
"__rmul__",
"__truediv__",
"__rtruediv__",
"__pow__",
"__rpow__",
];
match typename {
"str" | "bytes" => TEXT,
"bytearray" => MUT_TEXT,
"tuple" => SEQ,
"list" | "deque" => MUT_SEQ,
"dict" | "defaultdict" | "OrderedDict" => DICT,
"Counter" => &["__ror__"],
"set" => SET,
"dict_keys" | "dict_items" => VIEW,
"frozenset" => FROZENSET,
"complex" => COMPLEX,
_ => &[],
}
}
fn op_dunder_accepts(tn: &str, name: &str, b: &Value) -> bool {
match tn {
"set" | "frozenset" => {
with_host(|h| matches!(h.get(b), Some(PyObj::Set(_) | PyObj::Frozenset(_))))
}
"dict" | "defaultdict" | "OrderedDict" | "Counter" if name != "__ior__" => {
with_host(|h| matches!(h.get(b), Some(PyObj::Dict(_))))
}
"complex" => is_num_like(b) || with_host(|h| h.is_complex(b)),
_ => true,
}
}
fn op_dunder(tn: &str, recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
use host::iop;
let b = args.first().cloned().unwrap_or(Value::Undef);
if !op_dunder_accepts(tn, name, &b) {
return Ok(with_host(|h| h.alloc(PyObj::NotImplemented)));
}
let b = if matches!(name, "__mul__" | "__rmul__" | "__imul__") && tn != "complex" {
let count = index_dunder(&b)?.unwrap_or(b);
match with_host(|h| h.index_fit(&count)) {
host::IndexFit::Fits(_) => count,
host::IndexFit::TooLarge(_) => {
return Err(format!("OverflowError: {}", host::INDEX_OVERFLOW));
}
host::IndexFit::NotInt => {
return Err(with_host(|h| {
host::type_error(&format!(
"'{}' object cannot be interpreted as an integer",
h.tp_name(&count)
))
}));
}
}
} else {
b
};
let inplace = match name {
"__iadd__" => Some(iop::ADD),
"__isub__" => Some(iop::SUB),
"__imul__" => Some(iop::MUL),
"__iand__" => Some(iop::BITAND),
"__ior__" => Some(iop::BITOR),
"__ixor__" => Some(iop::BITXOR),
_ => None,
};
if let Some(tag) = inplace {
if let Some(r) = inplace_builtin(tag, recv, &b) {
return r;
}
return inplace_binary_fallback(tag, recv, &b);
}
let (tag, swap) = match name {
"__add__" => (iop::ADD, false),
"__radd__" => (iop::ADD, true),
"__sub__" => (iop::SUB, false),
"__rsub__" => (iop::SUB, true),
"__mul__" => (iop::MUL, false),
"__rmul__" => (iop::MUL, true),
"__truediv__" => (iop::DIV, false),
"__rtruediv__" => (iop::DIV, true),
"__mod__" => (iop::MOD, false),
"__rmod__" => (iop::MOD, true),
"__pow__" => (iop::POW, false),
"__rpow__" => (iop::POW, true),
"__and__" => (iop::BITAND, false),
"__rand__" => (iop::BITAND, true),
"__or__" => (iop::BITOR, false),
"__ror__" => (iop::BITOR, true),
"__xor__" => (iop::BITXOR, false),
"__rxor__" => (iop::BITXOR, true),
_ => return Err(host::type_error(&format!("internal: op dunder {name}"))),
};
let (l, r) = if swap { (&b, recv) } else { (recv, &b) };
inplace_binary_fallback(tag, l, r)
}
fn is_num_dunder(tn: &str, name: &str) -> bool {
match tn {
"float" => FLOAT_DUNDERS.contains(&name),
_ => INT_DUNDERS.contains(&name), }
}
fn nt_instance_method(
recv: &Value,
name: &str,
args: &[Value],
kwargs: &[(String, Value)],
) -> Option<Result<Value, String>> {
let (items, fields, type_name) = with_host(|h| match (h.get(recv), recv) {
(Some(PyObj::Tuple(items)), Value::Obj(i)) => h
.nt_meta
.get(i)
.map(|m| (items.clone(), m.fields.clone(), m.type_name.clone())),
_ => None,
})?;
match name {
"_asdict" => Some(Ok(with_host(|h| {
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
for (f, v) in fields.iter().zip(items.iter()) {
let kv = h.new_str(f.clone());
d.insert(PKey::Str(f.clone()), (kv, v.clone()));
}
h.new_dict(d)
}))),
"_replace" => {
let mut new_items = items.clone();
for (k, v) in kwargs {
match fields.iter().position(|f| f == k) {
Some(idx) => new_items[idx] = v.clone(),
None => {
return Some(Err(host::type_error(&format!(
"{type_name}() got an unexpected keyword argument '{k}'"
))))
}
}
}
Some(host::namedtuple_construct(
&type_name,
&fields,
new_items,
vec![],
))
}
"count" | "index" => Some(tuple_method(recv, name, args)),
_ => None,
}
}
fn context_var_method(recv: &Value, name: &str, args: &[Value]) -> Option<Result<Value, String>> {
let is_var = with_host(|h| matches!(h.get(recv), Some(PyObj::ContextVar { .. })));
if !is_var {
return None;
}
Some(match name {
"get" => with_host(|h| match h.get(recv) {
Some(PyObj::ContextVar {
name,
default,
value,
}) => {
if let Some(v) = value {
return Ok((**v).clone());
}
if let Some(v) = args.first() {
return Ok(v.clone());
}
match default {
Some(d) => Ok((**d).clone()),
None => Err(format!("LookupError: <ContextVar name='{name}'>")),
}
}
_ => unreachable!("checked above"),
}),
"set" => {
let new = args.first().cloned().unwrap_or(Value::Undef);
Ok(with_host(|h| {
let old = match h.get_mut(recv) {
Some(PyObj::ContextVar { value, .. }) => value.replace(Box::new(new)),
_ => None,
};
h.alloc(PyObj::ContextToken {
var: Box::new(recv.clone()),
old,
})
}))
}
"reset" => {
let tok = args.first().cloned().unwrap_or(Value::Undef);
let old = with_host(|h| match h.get(&tok) {
Some(PyObj::ContextToken { old, .. }) => Some(old.clone()),
_ => None,
});
match old {
Some(old) => {
with_host(|h| {
if let Some(PyObj::ContextVar { value, .. }) = h.get_mut(recv) {
*value = old;
}
});
Ok(Value::Undef)
}
None => Err(host::type_error("ContextVar.reset() expects a Token")),
}
}
_ => Err(host::type_error(&format!(
"'ContextVar' object has no attribute '{name}'"
))),
})
}
fn richcmp_accepts(tn: &str, other: &str, ordering: bool) -> bool {
match tn {
"int" | "bool" => matches!(other, "int" | "bool"),
"float" => matches!(other, "float" | "int" | "bool"),
"complex" => !ordering && matches!(other, "complex" | "float" | "int" | "bool"),
"str" | "list" | "tuple" | "slice" => other == tn,
"dict" | "defaultdict" | "OrderedDict" | "Counter" => {
!ordering && matches!(other, "dict" | "defaultdict" | "OrderedDict" | "Counter")
}
"range" => !ordering && other == "range",
"set" | "frozenset" => matches!(other, "set" | "frozenset"),
"bytes" => other == "bytes",
"bytearray" => matches!(other, "bytes" | "bytearray"),
"deque" | "array" | "memoryview" => other == tn,
_ => false,
}
}
fn richcmp_by_identity(tn: &str) -> bool {
!richcmp_accepts(tn, tn, false) && !richcmp_accepts(tn, tn, true)
}
fn object_sizeof(recv: &Value) -> i64 {
const HEADER: i64 = 16;
const SLOT: i64 = std::mem::size_of::<Value>() as i64;
with_host(|h| {
HEADER
+ match h.get(recv) {
Some(PyObj::Str(s)) => s.len() as i64,
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => b.len() as i64,
Some(PyObj::List(items)) | Some(PyObj::Tuple(items)) => items.len() as i64 * SLOT,
Some(PyObj::Dict(d)) => d.len() as i64 * SLOT * 2,
Some(PyObj::Set(s)) => s.len() as i64 * SLOT,
Some(PyObj::Frozenset(s)) => s.len() as i64 * SLOT,
_ => SLOT,
}
})
}
fn sys_getsizeof(args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
let [obj, default] = bind_named(
"getsizeof",
["object", "default"],
0,
1,
KwStyle::Unexpected,
args,
kwargs,
)?;
let obj = obj.unwrap_or(Value::Undef);
#[cfg(feature = "stdlib-ffi")]
if with_host(|h| h.foreign_id(&obj)).is_some() {
let sys = crate::ffi::import("sys")?;
let f = with_host(|h| crate::ffi::get_attr(h, sys, "getsizeof"))?;
return host::invoke(&f, args.to_vec(), kwargs.to_vec());
}
match sizeof_with_preheader(&obj) {
Ok(n) => Ok(Value::Int(n)),
Err(e) => {
let class = e.split_once(": ").map_or(e.as_str(), |(c, _)| c);
match default {
Some(d) if with_host(|h| exception_isa(class, "TypeError", h)) => {
with_host(|h| {
h.take_error();
});
Ok(d)
}
_ => Err(e),
}
}
}
}
fn sizeof_with_preheader(obj: &Value) -> Result<i64, String> {
let res = if let Some(m) = with_host(|h| h.metaclass_method(obj, "__sizeof__")) {
host::invoke(&m, vec![obj.clone()], vec![])?
} else if with_host(|h| {
matches!(h.get(obj), Some(PyObj::Class(_)))
|| matches!(h.type_name(obj).as_str(), "type")
}) {
Value::Int(object_sizeof(obj))
} else {
host::call_method(obj, "__sizeof__", vec![], vec![])?
};
let size = match res {
Value::Int(n) => n,
Value::Bool(b) => i64::from(b),
ref v => match with_host(|h| h.get(v).cloned()) {
Some(PyObj::BigInt(_)) => {
return Err(
"OverflowError: Python int too large to convert to C ssize_t".to_string()
)
}
Some(PyObj::Instance(host::Instance {
payload: Value::Int(n),
..
})) => n,
_ => return Err(host::type_error("an integer is required")),
},
};
if size < 0 {
return Err("ValueError: __sizeof__() should return >= 0".to_string());
}
Ok(size + with_host(|h| h.sizeof_preheader(obj)))
}
fn reduce_newobj(recv: &Value, tn: &str) -> Result<Value, String> {
let copyreg = host::import_module("copyreg")?;
let newobj = with_host(|h| h.get_attr(©reg, "__newobj__"))?;
let cls = with_host(|h| h.alloc(PyObj::Builtin(tn.to_string())));
let mut newargs = vec![cls];
if let Ok(extra) = type_getnewargs(recv, tn) {
newargs.extend(extra);
}
let (listitems, dictitems) = with_host(|h| match h.get(recv) {
Some(PyObj::List(_)) => (h.make_iter(recv).ok(), None),
Some(PyObj::Dict(_)) => {
let items = h.alloc(PyObj::DictView {
dict: recv.clone(),
kind: 2,
});
(None, h.make_iter(&items).ok())
}
_ => (None, None),
});
Ok(with_host(|h| {
let args = h.new_tuple(newargs);
let items = [
newobj,
args,
Value::Undef,
listitems.unwrap_or(Value::Undef),
dictitems.unwrap_or(Value::Undef),
];
h.new_tuple(items.to_vec())
}))
}
fn type_getnewargs(recv: &Value, tn: &str) -> Result<Vec<Value>, String> {
match tn {
"str" | "bytes" | "float" | "tuple" => Ok(vec![recv.clone()]),
"int" | "bool" => Ok(vec![match recv {
Value::Bool(b) => Value::Int(*b as i64),
other => other.clone(),
}]),
"complex" => match with_host(|h| h.get(recv).cloned()) {
Some(PyObj::Complex(r, i)) => Ok(vec![Value::Float(r), Value::Float(i)]),
_ => Ok(vec![Value::Float(0.0), Value::Float(0.0)]),
},
_ => Err(format!(
"AttributeError: '{tn}' object has no attribute '__getnewargs__'"
)),
}
}
fn bytearray_reduce_args(bytes: Vec<u8>, protocol: i64) -> Vec<Value> {
if bytes.is_empty() {
return Vec::new();
}
with_host(|h| {
if protocol < 3 {
let latin1: String = bytes.iter().map(|&b| b as char).collect();
vec![h.new_str(latin1), h.new_str("latin-1")]
} else {
vec![h.alloc(PyObj::Bytes(bytes))]
}
})
}
fn object_reduce(recv: &Value, tn: &str, protocol: Option<i64>) -> Result<Value, String> {
let own = |args: Vec<Value>| {
with_host(|h| {
let cls = h.alloc(PyObj::Builtin(tn.to_string()));
let a = h.new_tuple(args);
h.new_tuple(vec![cls, a, Value::Undef])
})
};
match tn {
"set" | "frozenset" => {
let items = host::iter_vec(recv)?;
let lst = with_host(|h| h.new_list(items));
Ok(own(vec![lst]))
}
"bytearray" => {
let bytes = with_host(|h| match h.get(recv) {
Some(PyObj::Bytearray(b)) => b.clone(),
_ => Vec::new(),
});
Ok(own(bytearray_reduce_args(bytes, protocol.unwrap_or(2))))
}
"range" | "slice" => {
let fields = with_host(|h| -> Result<Vec<Value>, String> {
Ok(vec![
h.get_attr(recv, "start")?,
h.get_attr(recv, "stop")?,
h.get_attr(recv, "step")?,
])
})?;
Ok(with_host(|h| {
let cls = h.alloc(PyObj::Builtin(tn.to_string()));
let a = h.new_tuple(fields);
h.new_tuple(vec![cls, a])
}))
}
"ellipsis" => Ok(with_host(|h| h.new_str("Ellipsis"))),
"NotImplementedType" => Ok(with_host(|h| h.new_str("NotImplemented"))),
_ => match protocol {
Some(p) if p >= 2 => reduce_newobj(recv, tn),
_ => Err(host::type_error(&format!("cannot pickle '{tn}' object"))),
},
}
}
fn object_dunder(
recv: &Value,
tn: &str,
name: &str,
args: &[Value],
) -> Option<Result<Value, String>> {
let arg0 = || args.first().cloned().unwrap_or(Value::Undef);
let attr_name = |v: &Value| with_host(|h| h.as_str(v));
let not_implemented = || Ok(with_host(|h| h.alloc(PyObj::NotImplemented)));
Some(match name {
"__repr__" | "__str__" => return None,
"__init__" | "__init_subclass__" | "__getstate__" => Ok(Value::Undef),
"__subclasshook__" => not_implemented(),
"__dir__" => Ok(with_host(|h| {
let items: Vec<Value> = h
.dir_names(recv)
.into_iter()
.map(|n| h.new_str(n))
.collect();
h.new_list(items)
})),
"__getattribute__" => match attr_name(&arg0()) {
Some(n) => with_host(|h| h.get_attr(recv, &n)),
None => Err(host::type_error("attribute name must be string")),
},
"__setattr__" | "__delattr__" => {
let Some(n) = attr_name(&arg0()) else {
return Some(Err(host::type_error("attribute name must be string")));
};
if n == "__class__" {
let vt = with_host(|h| h.type_name(&args.get(1).cloned().unwrap_or(Value::Undef)));
return Some(Err(host::type_error(&format!(
"__class__ must be set to a class, not '{vt}' object"
))));
}
let known = with_host(|h| h.dir_names(recv)).contains(&n);
Err(if known {
format!("AttributeError: '{tn}' object attribute '{n}' is read-only")
} else {
format!(
"AttributeError: '{tn}' object has no attribute '{n}' and no __dict__ for \
setting new attributes"
)
})
}
"__eq__" | "__ne__" => {
let other = arg0();
let ot = with_host(|h| h.type_name(&other));
let eq = if richcmp_by_identity(tn) {
if same_object(recv, &other) {
true
} else {
return Some(not_implemented());
}
} else if richcmp_accepts(tn, &ot, false) {
with_host(|h| h.equal(recv, &other))
} else {
return Some(not_implemented());
};
Ok(Value::Bool(if name == "__eq__" { eq } else { !eq }))
}
"__lt__" | "__le__" | "__gt__" | "__ge__" => {
let other = arg0();
let ot = with_host(|h| h.type_name(&other));
if !richcmp_accepts(tn, &ot, true) {
return Some(not_implemented());
}
let op = match name {
"__lt__" => NumOp::Lt,
"__le__" => NumOp::Le,
"__gt__" => NumOp::Gt,
_ => NumOp::Ge,
};
with_host(|h| h.compare(op, recv, &other))
}
"__hash__" => {
if UNHASHABLE_TYPES.contains(&tn) {
return Some(Err(host::type_error("'NoneType' object is not callable")));
}
call_builtin_function("hash", vec![recv.clone()], vec![])
}
"__format__" => call_builtin_function("format", vec![recv.clone(), arg0()], vec![]),
"__sizeof__" => Ok(Value::Int(object_sizeof(recv))),
"__reduce__" => object_reduce(recv, tn, None),
"__reduce_ex__" => {
let p = with_host(|h| h.big_val(&arg0()))
.and_then(|b| i64::try_from(b).ok())
.unwrap_or(0);
object_reduce(recv, tn, Some(p))
}
_ => return None,
})
}
pub fn unbound_receiver_error(tp: &str, meth: &str, recv: &Value) -> Option<String> {
let rt = with_host(|h| h.type_name(recv));
if receiver_is_instance_of(recv, &rt, tp) {
return None;
}
Some(host::type_error(&if is_slot_wrapper(tp, meth) {
format!("descriptor '{meth}' requires a '{tp}' object but received a '{rt}'")
} else {
format!("descriptor '{meth}' for '{tp}' objects doesn't apply to a '{rt}' object")
}))
}
fn receiver_is_instance_of(recv: &Value, rt: &str, tp: &str) -> bool {
if rt == tp || tp == "object" {
return true;
}
let builtin_bases: &[&str] = match rt {
"bool" => &["int"],
"defaultdict" | "OrderedDict" | "Counter" => &["dict"],
_ => &[],
};
if builtin_bases.contains(&tp) {
return true;
}
with_host(|h| match h.get(recv) {
Some(PyObj::Instance(i)) => {
let class = i.class.clone();
h.mro_of(&class).iter().any(|c| c == tp) || h.builtin_base_of(&class) == Some(tp)
}
_ => false,
})
}
fn is_slot_wrapper(tp: &str, meth: &str) -> bool {
const METHOD_FORM: &[&str] = &[
"__alloc__",
"__bytes__",
"__ceil__",
"__complex__",
"__copy__",
"__enter__",
"__exit__",
"__floor__",
"__format__",
"__getnewargs__",
"__reduce__",
"__reduce_ex__",
"__reversed__",
"__round__",
"__sizeof__",
"__trunc__",
];
const MAPPINGS: &[&str] = &["dict", "defaultdict", "OrderedDict", "Counter"];
if !(meth.starts_with("__") && meth.ends_with("__")) || METHOD_FORM.contains(&meth) {
return false;
}
match meth {
"__contains__" => !MAPPINGS.contains(&tp) && !matches!(tp, "set" | "frozenset"),
"__getitem__" => !MAPPINGS.contains(&tp) && tp != "list",
_ => true,
}
}
pub const TYPE_OBJECT_METHODS: &[&str] = &[
"mro",
"__instancecheck__",
"__subclasscheck__",
"__prepare__",
"__call__",
"__or__",
"__ror__",
];
fn type_object_method(
recv: &Value,
name: &str,
args: &[Value],
kwargs: &[(String, Value)],
) -> Option<Result<Value, String>> {
if !TYPE_OBJECT_METHODS.contains(&name) {
return None;
}
let is_type = with_host(|h| match h.get(recv) {
Some(PyObj::Class(_)) => true,
Some(PyObj::Builtin(n)) => is_type_object_name(n),
_ => false,
});
if !is_type {
return None;
}
let arg0 = || args.first().cloned().unwrap_or(Value::Undef);
Some(match name {
"mro" => {
let cls = if matches!(recv, Value::Obj(_)) && !args.is_empty() {
let named =
with_host(|h| matches!(h.get(recv), Some(PyObj::Builtin(n)) if n == "type"));
if named {
arg0()
} else {
recv.clone()
}
} else {
recv.clone()
};
match with_host(|h| h.get_attr(&cls, "__mro__")).and_then(|m| host::iter_vec(&m)) {
Ok(items) => Ok(with_host(|h| h.new_list(items))),
Err(e) => Err(e),
}
}
"__instancecheck__" => {
call_builtin_function("isinstance", vec![arg0(), recv.clone()], vec![])
}
"__subclasscheck__" => {
call_builtin_function("issubclass", vec![arg0(), recv.clone()], vec![])
}
"__prepare__" => Ok(with_host(|h| h.new_dict(Default::default()))),
"__call__" => host::invoke(recv, args.to_vec(), kwargs.to_vec()),
"__or__" | "__ror__" => {
let (a, b) = if name == "__or__" {
(recv.clone(), arg0())
} else {
(arg0(), recv.clone())
};
with_host(|h| h.binop(host::binop::BITOR, &a, &b))
}
_ => return None,
})
}
pub fn instance_object_dunder(
recv: &Value,
class: &str,
name: &str,
args: &[Value],
) -> Option<Result<Value, String>> {
let arg0 = || args.first().cloned().unwrap_or(Value::Undef);
let attr_name = |v: &Value| with_host(|h| h.as_str(v));
let not_implemented = || Ok(with_host(|h| h.alloc(PyObj::NotImplemented)));
Some(match name {
"__eq__" | "__ne__" => {
if !same_object(recv, &arg0()) {
return Some(not_implemented());
}
Ok(Value::Bool(name == "__eq__"))
}
"__lt__" | "__le__" | "__gt__" | "__ge__" => return Some(not_implemented()),
"__hash__" => call_builtin_function("hash", vec![recv.clone()], vec![]),
"__repr__" | "__str__" => Ok(with_host(|h| {
let s = h.repr_of(recv);
h.new_str(s)
})),
"__format__" => match attr_name(&arg0()).as_deref() {
Some("") | None => Ok(with_host(|h| {
let s = h.str_of(recv);
h.new_str(s)
})),
Some(spec) => Err(host::type_error(&format!(
"unsupported format string passed to {class}.__format__ ({spec})"
))),
},
"__sizeof__" => Ok(Value::Int(object_sizeof(recv))),
"__dir__" => Ok(with_host(|h| {
let items: Vec<Value> = h
.dir_names(recv)
.into_iter()
.map(|n| h.new_str(n))
.collect();
h.new_list(items)
})),
"__getattribute__" => match attr_name(&arg0()) {
Some(n) => with_host(|h| h.get_attr(recv, &n)),
None => Err(host::type_error("attribute name must be string")),
},
"__setattr__" => match attr_name(&arg0()) {
Some(n) => {
with_host(|h| h.set_attr(recv, &n, args.get(1).cloned().unwrap_or(Value::Undef)))
.map(|_| Value::Undef)
}
None => Err(host::type_error("attribute name must be string")),
},
"__delattr__" => match attr_name(&arg0()) {
Some(n) => with_host(|h| h.del_attr(recv, &n)).map(|_| Value::Undef),
None => Err(host::type_error("attribute name must be string")),
},
"__init__" | "__init_subclass__" => Ok(Value::Undef),
"__subclasshook__" => not_implemented(),
"__getstate__" => object_getstate_default(recv, class, false),
"__reduce__" => builtin_base_reduce(recv, class)
.unwrap_or_else(|| instance_common_reduce(recv, class, 0)),
"__reduce_ex__" => {
let p = with_host(|h| h.as_int(&arg0())).unwrap_or(0);
instance_reduce_ex(recv, class, p)
}
_ => return None,
})
}
fn instance_state(recv: &Value) -> Result<Value, String> {
let Some((class, dict)) = with_host(|h| match h.get(recv) {
Some(PyObj::Instance(i)) => Some((i.class.clone(), i.dict.clone())),
_ => None,
}) else {
return Ok(Value::Undef);
};
let state = with_host(|h| match h.get(&dict) {
Some(PyObj::Dict(d)) if !d.is_empty() => dict.clone(),
_ => Value::Undef,
});
let mut slots = Vec::new();
for name in with_host(|h| h.slot_names(&class)) {
match get_attr_desc(recv, &name) {
Ok(v) => slots.push((name, v)),
Err(e) if is_attr_err(&e) => {}
Err(e) => return Err(e),
}
}
if slots.is_empty() {
return Ok(state);
}
Ok(with_host(|h| {
let mut pairs = IndexMap::new();
for (k, v) in slots {
let key = h.new_str(k.clone());
pairs.insert(PKey::Str(k), (key, v));
}
let slot_dict = h.new_dict(pairs);
h.new_tuple(vec![state, slot_dict])
}))
}
fn instance_reduce_ex(recv: &Value, class: &str, protocol: i64) -> Result<Value, String> {
if with_host(|h| h.class_lookup(class, "__reduce__")).is_some() {
return host::call_method(recv, "__reduce__", vec![], vec![]);
}
if let Some(r) = builtin_base_reduce(recv, class) {
return r;
}
instance_common_reduce(recv, class, protocol)
}
fn builtin_base_reduce(recv: &Value, class: &str) -> Option<Result<Value, String>> {
let base = with_host(|h| h.builtin_base_of(class))?;
if !matches!(base, "set" | "frozenset") {
return None;
}
let args = match call_builtin_function("list", vec![recv.clone()], vec![]) {
Ok(items) => vec![items],
Err(e) => return Some(Err(e)),
};
Some(object_getstate(recv, class, false).map(|state| {
with_host(|h| {
let cls = h.alloc(PyObj::Class(class.to_string()));
let args = h.new_tuple(args);
h.new_tuple(vec![cls, args, state])
})
}))
}
fn instance_common_reduce(recv: &Value, class: &str, protocol: i64) -> Result<Value, String> {
if protocol >= 2 {
instance_reduce_newobj(recv, class)
} else {
copyreg_reduce_ex(recv, class)
}
}
fn instance_reduce_newobj(recv: &Value, class: &str) -> Result<Value, String> {
let (args, kwargs) = instance_new_arguments(recv, class)?;
let copyreg = host::import_module("copyreg")?;
let base = with_host(|h| h.builtin_base_of(class));
let has_kwargs = kwargs
.as_ref()
.is_some_and(|k| with_host(|h| matches!(h.get(k), Some(PyObj::Dict(d)) if !d.is_empty())));
let (newobj, newargs) = with_host(|h| -> Result<_, String> {
let cls = h.alloc(PyObj::Class(class.to_string()));
if has_kwargs {
let args = args.clone().unwrap_or_else(|| h.new_tuple(Vec::new()));
let kwargs = kwargs.clone().unwrap_or(Value::Undef);
Ok((h.get_attr(©reg, "__newobj_ex__")?, h.new_tuple(vec![cls, args, kwargs])))
} else {
let mut items = vec![cls];
if let Some(Some(PyObj::Tuple(extra))) = args.as_ref().map(|a| h.get(a)) {
items.extend(extra.iter().cloned());
}
Ok((h.get_attr(©reg, "__newobj__")?, h.new_tuple(items)))
}
})?;
let required = !(args.is_some() || matches!(base, Some("list" | "dict")));
let state = object_getstate(recv, class, required)?;
let listitems = match base {
Some("list") => call_builtin_function("iter", vec![recv.clone()], vec![])?,
_ => Value::Undef,
};
let dictitems = match base {
Some("dict") => {
let items = host::call_method(recv, "items", vec![], vec![])?;
call_builtin_function("iter", vec![items], vec![])?
}
_ => Value::Undef,
};
Ok(with_host(|h| h.new_tuple(vec![newobj, newargs, state, listitems, dictitems])))
}
fn instance_new_arguments(recv: &Value, class: &str) -> Result<(Option<Value>, Option<Value>), String> {
let tuple_items = |v: &Value| with_host(|h| match h.get(v) {
Some(PyObj::Tuple(items)) => Some(items.clone()),
_ => None,
});
let type_name = |v: &Value| with_host(|h| h.type_name(v));
if with_host(|h| h.class_lookup(class, "__getnewargs_ex__")).is_some() {
let r = host::call_method(recv, "__getnewargs_ex__", vec![], vec![])?;
let Some(pair) = tuple_items(&r) else {
return Err(host::type_error(&format!(
"__getnewargs_ex__ should return a tuple, not '{}'",
type_name(&r)
)));
};
let [args, kwargs] = pair.as_slice() else {
return Err(format!(
"ValueError: __getnewargs_ex__ should return a tuple of length 2, not {}",
pair.len()
));
};
if tuple_items(args).is_none() {
return Err(host::type_error(&format!(
"first item of the tuple returned by __getnewargs_ex__ must be a tuple, not '{}'",
type_name(args)
)));
}
if !with_host(|h| matches!(h.get(kwargs), Some(PyObj::Dict(_)))) {
return Err(host::type_error(&format!(
"second item of the tuple returned by __getnewargs_ex__ must be a dict, not '{}'",
type_name(kwargs)
)));
}
return Ok((Some(args.clone()), Some(kwargs.clone())));
}
let own = with_host(|h| h.class_lookup(class, "__getnewargs__")).is_some();
let args = if own {
host::call_method(recv, "__getnewargs__", vec![], vec![])?
} else {
let base_args = with_host(|h| {
let base = h.builtin_base_of(class)?;
let payload = match h.get(recv) {
Some(PyObj::Instance(i)) => i.payload.clone(),
_ => return None,
};
Some((base, payload))
});
match base_args.and_then(|(base, payload)| type_getnewargs(&payload, base).ok()) {
Some(items) => with_host(|h| h.new_tuple(items)),
None => return Ok((None, None)),
}
};
if tuple_items(&args).is_none() {
return Err(host::type_error(&format!(
"__getnewargs__ should return a tuple, not '{}'",
type_name(&args)
)));
}
Ok((Some(args), None))
}
fn object_getstate(recv: &Value, class: &str, required: bool) -> Result<Value, String> {
if with_host(|h| h.class_lookup(class, "__getstate__")).is_some() {
return host::call_method(recv, "__getstate__", vec![], vec![]);
}
object_getstate_default(recv, class, required)
}
fn object_getstate_default(recv: &Value, class: &str, required: bool) -> Result<Value, String> {
if required && with_host(|h| matches!(h.builtin_base_of(class), Some("int" | "bytes" | "tuple"))) {
let name = with_host(|h| h.classes.get(class).map_or(class.to_string(), |c| c.name.clone()));
return Err(host::type_error(&format!("cannot pickle '{name}' object")));
}
instance_state(recv)
}
fn copyreg_reduce_ex(recv: &Value, class: &str) -> Result<Value, String> {
let copyreg = host::import_module("copyreg")?;
let reconstructor = with_host(|h| h.get_attr(©reg, "_reconstructor"))?;
let base = with_host(|h| h.builtin_base_of(class)).unwrap_or("object");
let state = if base == "object" {
Value::Undef
} else {
call_builtin_function(base, vec![recv.clone()], vec![])?
};
let args = with_host(|h| {
let cls = h.alloc(PyObj::Class(class.to_string()));
let base = h.builtin_object(base);
h.new_tuple(vec![cls, base, state])
});
let dict = if with_host(|h| h.class_lookup(class, "__getstate__")).is_some() {
host::call_method(recv, "__getstate__", vec![], vec![])?
} else {
if with_host(|h| h.class_lookup(class, "__slots__").is_some_and(|s| h.truthy(&s))) {
return Err(host::type_error(
"a class that defines __slots__ without defining __getstate__ cannot be pickled",
));
}
object_getstate_default(recv, class, false)?
};
Ok(with_host(|h| {
if h.truthy(&dict) {
h.new_tuple(vec![reconstructor, args, dict])
} else {
h.new_tuple(vec![reconstructor, args])
}
}))
}
fn builtin_extra_method(
recv: &Value,
tn: &str,
name: &str,
args: &[Value],
kwargs: &[(String, Value)],
) -> Option<Result<Value, String>> {
let arg0 = || args.first().cloned().unwrap_or(Value::Undef);
if type_classmethods(tn).contains(&name) {
return Some(call_builtin_function(
&format!("{tn}.{name}"),
args.to_vec(),
kwargs.to_vec(),
));
}
if !type_extra_names(tn).contains(&name) {
return None;
}
Some(match name {
"__getnewargs__" => match type_getnewargs(recv, tn) {
Ok(items) => Ok(with_host(|h| h.new_tuple(items))),
Err(_) => return None,
},
"__bytes__" if tn == "bytes" => Ok(recv.clone()),
"__complex__" if tn == "complex" => Ok(recv.clone()),
"__neg__" | "__pos__" if tn == "complex" => match with_host(|h| h.get(recv).cloned()) {
Some(PyObj::Complex(r, i)) if name == "__neg__" => {
Ok(with_host(|h| h.alloc(PyObj::Complex(-r, -i))))
}
_ => Ok(recv.clone()),
},
"__reversed__" => call_builtin_function("reversed", vec![recv.clone()], vec![]),
"__class_getitem__" => {
let ty = with_host(|h| h.alloc(PyObj::Builtin(tn.to_string())));
host::generic_alias(&ty, &arg0())
}
"copy" if tn == "bytearray" => Ok(mk_bytes(true, recv_bytes(recv))),
"resize" if tn == "bytearray" => {
let n = with_host(|h| h.as_int(&arg0()))
.ok_or_else(|| host::type_error("an integer is required"));
let n = match n {
Ok(n) => n,
Err(e) => return Some(Err(e)),
};
if n < 0 {
return Some(Err("ValueError: negative size".into()));
}
with_host(|h| {
if let Some(PyObj::Bytearray(b)) = h.get_mut(recv) {
b.resize(n as usize, 0);
}
});
Ok(Value::Undef)
}
"__buffer__" if matches!(tn, "bytes" | "bytearray") => {
call_builtin_function("memoryview", vec![recv.clone()], vec![])
}
"__release_buffer__" if tn == "bytearray" => Ok(Value::Undef),
"__alloc__" if tn == "bytearray" => Ok(Value::Int(with_host(|h| match h.get(recv) {
Some(PyObj::Bytearray(b)) => b.capacity() as i64,
_ => 0,
}))),
"__getformat__" if tn == "float" => float_getformat(args.first()),
_ => return None,
})
}
fn float_getformat(arg: Option<&Value>) -> Result<Value, String> {
match with_host(|h| arg.and_then(|v| h.as_str(v))).as_deref() {
Some("double") | Some("float") => Ok(with_host(|h| {
h.new_str(if cfg!(target_endian = "little") {
"IEEE, little-endian"
} else {
"IEEE, big-endian"
})
})),
_ => Err("ValueError: __getformat__() argument 1 must be 'double' or 'float'".to_string()),
}
}
pub fn call_type_method(
recv: &Value,
name: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
if name.starts_with("__") && name.ends_with("__") {
let missing = with_host(|h| {
if matches!(h.get(recv), Some(host::PyObj::Instance(_))) {
return false;
}
let tn = h.type_name(recv);
!type_has_method(&tn, name) && !is_object_dunder_method(&tn, name)
});
if missing && CONTAINER_DUNDERS.contains(&name) {
let tn = with_host(|h| h.type_name(recv));
return Err(format!(
"AttributeError: '{tn}' object has no attribute '{name}'"
));
}
}
if let Some(r) = context_var_method(recv, name, &args) {
return r;
}
if let Some(r) = with_host(|h| crate::stdlib::pystruct::struct_method(h, recv, name, &args)) {
return r;
}
if let Some(r) = with_host(|h| crate::stdlib::pyio::stream_method(h, recv, name, &args)) {
return r;
}
if let Some(r) = with_host(|h| crate::stdlib::pyhash::method(h, recv, name, &args)) {
return r;
}
if matches!(name, "writerow" | "writerows") {
if let Some(r) = crate::stdlib::pycsv::write_rows(recv, name, &args) {
return r;
}
}
if matches!(name, "__get__" | "__set__" | "__delete__") {
if let Some((kind, qual)) = with_host(|h| match h.get(recv) {
Some(host::PyObj::Descriptor {
kind: kind @ (host::DescKind::GetSetDescriptor | host::DescKind::MemberDescriptor),
qual,
..
}) => Some((*kind, qual.clone())),
_ => None,
}) {
let (owner, attr) = qual.rsplit_once('.').unwrap_or(("", &qual));
let obj = args.first().cloned().unwrap_or(Value::Undef);
if name == "__get__" && matches!(obj, Value::Undef) {
return Ok(recv.clone());
}
if kind == host::DescKind::MemberDescriptor {
with_host(|h| h.check_member_applies(owner, attr, &obj))?;
}
return match name {
"__get__" => with_host(|h| h.get_attr(&obj, attr)),
"__set__" => {
let v = args.get(1).cloned().unwrap_or(Value::Undef);
raw_setattr(&obj, attr, v).map(|_| Value::Undef)
}
_ => raw_delattr(&obj, attr).map(|_| Value::Undef),
};
}
}
if let Some(r) = nt_instance_method(recv, name, &args, &kwargs) {
return r;
}
if let Some(r) = type_object_method(recv, name, &args, &kwargs) {
return r;
}
if name == "__subclasses__" {
return Ok(with_host(|h| {
let cn = match h.get(recv) {
Some(PyObj::Class(n)) => n.clone(),
_ => String::new(),
};
let subs: Vec<Value> = h
.subclasses_of(&cn)
.into_iter()
.map(|n| h.alloc(PyObj::Class(n)))
.collect();
h.new_list(subs)
}));
}
let tn = with_host(|h| h.type_name(recv));
if name == "__contains__" {
let item = arg0(&args)?;
return Ok(Value::Bool(with_host(|h| h.contains(&item, recv))?));
}
if let Some(op) = comparison_dunder_op(name) {
if tn == "Counter" || tn == "OrderedDict" {
let other = arg0(&args)?;
match collections_compare(op, recv, &other) {
Some(r) => return r,
None if tn == "Counter" => {
return Ok(with_host(|h| h.alloc(PyObj::NotImplemented)))
}
None => {}
}
}
}
if OBJECT_METHOD_DUNDERS.contains(&name) {
if let Some(r) = object_dunder(recv, &tn, name, &args) {
return r;
}
}
if let Some(r) = builtin_extra_method(recv, &tn, name, &args, &kwargs) {
return r;
}
match name {
"__len__" => return Ok(Value::Int(py_len(recv)? as i64)),
"__getitem__" => {
let k = arg0(&args)?;
let k = if with_host(|h| matches!(h.get(recv), Some(PyObj::Slice { .. }))) {
k
} else if with_host(|h| matches!(h.get(&k), Some(PyObj::Slice { .. }))) {
normalize_slice_bounds(&k)?
} else {
store_index(recv, k)?
};
return with_host(|h| h.get_item(recv, &k));
}
"__setitem__" => {
let k = arg0(&args)?;
let v = args.get(1).cloned().unwrap_or(Value::Undef);
subscript_store(recv, k, v)?;
return Ok(Value::Undef);
}
"__delitem__" => {
let k = arg0(&args)?;
subscript_delete(recv, k)?;
return Ok(Value::Undef);
}
"__iter__" => return with_host(|h| h.make_iter(recv)),
"__next__" => {
return match host::iter_step(recv)? {
Some(v) => Ok(v),
None => Err("StopIteration".to_string()),
}
}
"__str__" | "__repr__" => {
if let Some(e) = with_host(|h| h.int_str_limit_exceeded(recv)) {
return Err(e);
}
return Ok(with_host(|h| {
let s = if name == "__str__" {
h.str_of(recv)
} else {
h.repr_of(recv)
};
h.new_str(s)
}));
}
"__bool__" => return Ok(Value::Bool(with_host(|h| h.truthy(recv)))),
_ => {}
}
if matches!(tn.as_str(), "dict_keys" | "dict_items") && name == "isdisjoint" {
let other = arg0(&args)?;
let mine = host::iter_vec(recv)?;
let theirs = host::iter_vec(&other)?;
let disjoint = !mine
.iter()
.any(|e| theirs.iter().any(|o| with_host(|h| h.equal(e, o))));
return Ok(Value::Bool(disjoint));
}
match tn.as_str() {
_ if op_dunders(&tn).contains(&name) => op_dunder(&tn, recv, name, &args),
"str" if name == "format" => {
let s = with_host(|h| h.as_str(recv)).unwrap_or_default();
str_dot_format(&s, &args, &kwargs)
}
"str" if name == "splitlines" && !kwargs.is_empty() => {
let a: Vec<Value> = kwargs
.iter()
.find(|(k, _)| k == "keepends")
.map(|(_, v)| v.clone())
.into_iter()
.collect();
str_method(recv, name, &a)
}
"str" if name == "encode" && !kwargs.is_empty() => {
let mut enc = args.first().cloned();
let mut err = args.get(1).cloned();
for (k, v) in &kwargs {
match k.as_str() {
"encoding" => enc = Some(v.clone()),
"errors" => err = Some(v.clone()),
_ => {}
}
}
let mut a2 = vec![enc.unwrap_or_else(|| new_str("utf-8".into()))];
if let Some(e) = err {
a2.push(e);
}
str_method(recv, name, &a2)
}
"str" => str_method(recv, name, &fold_str_kwargs(name, &args, &kwargs)?),
"bytes" | "bytearray" if name == "decode" && !kwargs.is_empty() => {
let mut enc = args.first().cloned();
let mut err = args.get(1).cloned();
for (k, v) in &kwargs {
match k.as_str() {
"encoding" => enc = Some(v.clone()),
"errors" => err = Some(v.clone()),
_ => {}
}
}
let mut a2 = vec![enc.unwrap_or_else(|| new_str("utf-8".into()))];
if let Some(e) = err {
a2.push(e);
}
decode_bytes(&recv_bytes(recv), &a2)
}
"bytes" => bytes_method(recv, name, &args),
"bytearray" => bytearray_method(recv, name, &args),
"memoryview" => memoryview_method(recv, name, &args),
"list" => list_method(recv, name, &args, &kwargs),
"dict" => dict_method(recv, name, &args, &kwargs),
"Counter" | "defaultdict" | "OrderedDict" => {
if let Some(r) = collections_dict_method(recv, name, &args, &kwargs, &tn) {
return r;
}
dict_method(recv, name, &args, &kwargs)
}
"set" | "frozenset" => set_method(recv, name, &args),
"tuple" => tuple_method(recv, name, &args),
"range" => range_method(recv, name, &args),
"slice" => slice_method(recv, name, &args),
"deque" => deque_method(recv, name, &args),
"TextIOWrapper" | "BufferedReader" | "BufferedWriter" | "BufferedRandom" => {
file_method(recv, name, &args)
}
"functools._lru_cache_wrapper" => lru_wrapper_method(recv, name),
"int" | "float" | "bool" if is_num_dunder(&tn, name) => num_dunder(recv, name, &args),
"int" | "float" | "bool" => num_method(recv, name, &args, &kwargs),
"complex" => complex_method(recv, name),
"property" => property_method(recv, name, &args),
"generator" => generator_method(recv, name, &args),
"coroutine" => coroutine_method(recv, name, &args),
"async_generator" => async_generator_method(recv, name, args),
"Future" | "Task" => crate::async_rt::future_method(recv, name, args),
"_UnixSelectorEventLoop" => crate::async_rt::loop_method(name, args),
"Event" | "Lock" | "Queue" => crate::async_rt::async_obj_method(recv, name, args),
_ if matches!(name, "split" | "subgroup" | "derive")
&& with_host(|h| excgroup::is_group(h, recv)) =>
{
let a = arg0(&args)?;
match name {
"split" => {
let (m, r) = excgroup::split(recv, &a)?;
let pair = vec![m.unwrap_or(Value::Undef), r.unwrap_or(Value::Undef)];
Ok(with_host(|h| h.new_tuple(pair)))
}
"subgroup" => Ok(excgroup::subgroup(recv, &a)?.unwrap_or(Value::Undef)),
_ => excgroup::derive(recv, &a),
}
}
_ if matches!(name, "with_traceback" | "add_note")
&& with_host(|h| is_exception_value(h, recv, &tn)) =>
{
if name == "with_traceback" {
return Ok(recv.clone());
}
exception_add_note(recv, arg0(&args)?)?;
Ok(Value::Undef)
}
"lock" | "RLock" => host::call_method(recv, name, args, kwargs),
"Context" if name == "run" => {
let f = arg0(&args)?;
host::invoke(&f, args[1..].to_vec(), kwargs)
}
"Context" if name == "copy" => Ok(recv.clone()),
"Context" => Ok(with_host(|h| h.new_list(vec![]))),
other => Err(format!(
"AttributeError: '{other}' object has no attribute '{name}'"
)),
}
}
fn is_exception_value(h: &host::PyHost, v: &Value, tn: &str) -> bool {
is_exception_class(tn)
|| matches!(h.get(v), Some(host::PyObj::Instance(i)) if h.class_is_exception(&i.class))
}
fn exception_add_note(exc: &Value, note: Value) -> Result<(), String> {
with_host(|h| {
if h.as_str(¬e).is_none() {
return Err(host::type_error(&format!(
"add_note() argument must be str, not {}",
if matches!(note, Value::Undef) { "None".to_string() } else { h.type_name(¬e) }
)));
}
let notes = match h.get_attr(exc, "__notes__") {
Ok(n) => n,
Err(e) if is_attr_err(&e) => {
let n = h.new_list(Vec::new());
h.set_attr(exc, "__notes__", n.clone())?;
n
}
Err(e) => return Err(e),
};
match h.get_mut(¬es) {
Some(host::PyObj::List(items)) => {
items.push(note);
Ok(())
}
_ => Err(host::type_error("Cannot add note: __notes__ is not a list")),
}
})
}
fn coroutine_method(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
match name {
"__await__" => Ok(recv.clone()),
_ => generator_method(recv, name, args),
}
}
fn async_generator_method(recv: &Value, name: &str, args: Vec<Value>) -> Result<Value, String> {
match name {
"__aiter__" | "__anext__" => Ok(recv.clone()),
"asend" | "athrow" | "aclose" => crate::async_rt::async_gen_method(recv, name, args),
_ => Err(format!(
"AttributeError: 'async_generator' object has no attribute '{name}'"
)),
}
}
fn exc_error_string(exc: &Value) -> String {
with_host(|h| match h.get(exc) {
Some(PyObj::Exception { class, args }) => {
let msg = h.exc_message(class, args);
if msg.is_empty() {
class.clone()
} else {
format!("{class}: {msg}")
}
}
_ => "StopIteration".into(),
})
}
fn generator_method(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
match name {
"__iter__" => Ok(recv.clone()),
"send" | "__next__" => {
let v = if name == "send" {
args.first().cloned().unwrap_or(Value::Undef)
} else {
Value::Undef
};
if name == "send" && !host::gen_started(recv) && !matches!(v, Value::Undef) {
return Err(host::type_error(
"can't send non-None value to a just-started generator",
));
}
match host::gen_resume(recv, v)? {
Some(y) => Ok(y),
None => {
let e = host::gen_stop_iteration(recv);
Err(exc_error_string(&e))
}
}
}
"throw" => {
let a0 = args.first().cloned().unwrap_or(Value::Undef);
let exc = with_host(|h| match h.get(&a0) {
Some(PyObj::Builtin(n)) if is_exception_class(n) => {
let n = n.clone();
let rest = args.get(1..).unwrap_or(&[]).to_vec();
h.alloc(PyObj::Exception {
class: n,
args: rest,
})
}
_ => a0.clone(),
});
match host::gen_throw(recv, exc)? {
Some(y) => Ok(y),
None => {
let e = host::gen_stop_iteration(recv);
Err(exc_error_string(&e))
}
}
}
"close" => {
if with_host(|h| h.close_unstarted_gen(recv)) {
return Ok(Value::Undef);
}
let ge = with_host(|h| {
h.alloc(PyObj::Exception {
class: "GeneratorExit".into(),
args: vec![],
})
});
match host::gen_throw(recv, ge) {
Ok(Some(_)) => Err("RuntimeError: generator ignored GeneratorExit".to_string()),
Ok(None) => Ok(Value::Undef),
Err(e) if e.contains("GeneratorExit") || e.contains("StopIteration") => {
with_host(|h| {
h.error = None;
h.exc = None;
});
Ok(Value::Undef)
}
Err(e) => Err(e),
}
}
_ => Err(format!(
"AttributeError: 'generator' object has no attribute '{name}'"
)),
}
}
fn property_method(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
if matches!(name, "__get__" | "__set__" | "__delete__") {
let obj = args.first().cloned().unwrap_or(Value::Undef);
if matches!(obj, Value::Undef) && name == "__get__" {
return Ok(recv.clone());
}
let pname = with_host(|h| match h.get(recv) {
Some(PyObj::Property { name, .. }) => name.clone(),
_ => String::new(),
});
return match name {
"__get__" => raw_getattr(&obj, &pname),
"__set__" => {
let v = args.get(1).cloned().unwrap_or(Value::Undef);
raw_setattr(&obj, &pname, v).map(|_| Value::Undef)
}
_ => del_attr_desc(&obj, &pname).map(|_| Value::Undef),
};
}
if name == "__set_name__" {
let n = args.get(1).and_then(|v| with_host(|h| h.as_str(v)));
if let Some(n) = n {
with_host(|h| {
if let Some(PyObj::Property { name, .. }) = h.get_mut(recv) {
*name = n;
}
});
}
return Ok(Value::Undef);
}
if name == "__isabstractmethod__" {
return Ok(Value::Bool(false));
}
let (fget, fset, fdel, pname) = with_host(|h| match h.get(recv) {
Some(PyObj::Property {
fget,
fset,
fdel,
name,
}) => (fget.clone(), fset.clone(), fdel.clone(), name.clone()),
_ => (Value::Undef, Value::Undef, Value::Undef, String::new()),
});
let f = args.first().cloned().unwrap_or(Value::Undef);
let (fget, fset, fdel) = match name {
"getter" => (f, fset, fdel),
"setter" => (fget, f, fdel),
"deleter" => (fget, fset, f),
_ => {
return Err(format!(
"AttributeError: 'property' object has no attribute '{name}'"
))
}
};
Ok(with_host(|h| {
h.alloc(PyObj::Property {
fget,
fset,
fdel,
name: pname,
})
}))
}
fn str_prefixes(v: &Value, name: &str) -> Result<Vec<String>, String> {
if let Some(s) = with_host(|h| h.as_str(v)) {
return Ok(vec![s]);
}
if !matches!(with_host(|h| h.get(v).cloned()), Some(PyObj::Tuple(_))) {
return Err(host::type_error(&format!(
"{name} first arg must be str or a tuple of str, not {}",
with_host(|h| h.type_name(v))
)));
}
let items = host::iter_vec(v)?;
let mut out = Vec::with_capacity(items.len());
for it in items {
out.push(with_host(|h| h.as_str(&it)).ok_or_else(|| {
host::type_error(&format!(
"tuple for {name} must only contain str, not {}",
with_host(|h| h.type_name(&it))
))
})?);
}
Ok(out)
}
fn split_ws_str(s: &str, maxsplit: i64, reverse: bool) -> Vec<String> {
let mut parts: Vec<String> = Vec::new();
if reverse {
let mut rest = s;
let mut splits = 0;
loop {
let trimmed = rest.trim_end_matches(is_py_space);
if trimmed.is_empty() {
break;
}
if maxsplit >= 0 && splits >= maxsplit {
parts.push(trimmed.to_string());
break;
}
match trimmed.rfind(is_py_space) {
Some(idx) => {
let after = idx + trimmed[idx..].chars().next().unwrap().len_utf8();
parts.push(trimmed[after..].to_string());
rest = &trimmed[..idx];
splits += 1;
}
None => {
parts.push(trimmed.to_string());
break;
}
}
}
parts.reverse();
} else {
let mut rest = s;
let mut splits = 0;
loop {
let trimmed = rest.trim_start_matches(is_py_space);
if trimmed.is_empty() {
break;
}
if maxsplit >= 0 && splits >= maxsplit {
parts.push(trimmed.to_string());
break;
}
match trimmed.find(is_py_space) {
Some(idx) => {
parts.push(trimmed[..idx].to_string());
rest = &trimmed[idx..];
splits += 1;
}
None => {
parts.push(trimmed.to_string());
break;
}
}
}
}
parts
}
fn is_line_boundary(c: char) -> bool {
matches!(
c,
'\n' | '\r'
| '\u{0b}'
| '\u{0c}'
| '\u{1c}'
| '\u{1d}'
| '\u{1e}'
| '\u{85}'
| '\u{2028}'
| '\u{2029}'
)
}
pub(crate) fn str_splitlines(s: &str, keepends: bool) -> Vec<String> {
let mut out = Vec::new();
let chars: Vec<char> = s.chars().collect();
let mut start = 0;
let mut i = 0;
while i < chars.len() {
if is_line_boundary(chars[i]) {
let mut end = i + 1;
if chars[i] == '\r' && end < chars.len() && chars[end] == '\n' {
end += 1;
}
let slice_end = if keepends { end } else { i };
out.push(chars[start..slice_end].iter().collect());
start = end;
i = end;
} else {
i += 1;
}
}
if start < chars.len() {
out.push(chars[start..].iter().collect());
}
out
}
fn str_kwarg_pos(method: &str, kw: &str) -> Option<usize> {
Some(match (method, kw) {
("split" | "rsplit", "sep") => 0,
("split" | "rsplit", "maxsplit") => 1,
("replace", "old") => 0,
("replace", "new") => 1,
("replace", "count") => 2,
("expandtabs", "tabsize") | ("splitlines", "keepends") => 0,
_ => return None,
})
}
fn str_accepts_kwargs(method: &str) -> bool {
matches!(
method,
"split" | "rsplit" | "replace" | "expandtabs" | "splitlines" | "encode"
)
}
fn fold_str_kwargs(
method: &str,
args: &[Value],
kwargs: &[(String, Value)],
) -> Result<Vec<Value>, String> {
if kwargs.is_empty() {
return Ok(args.to_vec());
}
if !str_accepts_kwargs(method) {
return Err(format!(
"TypeError: str.{method}() takes no keyword arguments"
));
}
let mut out: Vec<Option<Value>> = args.iter().cloned().map(Some).collect();
for (k, v) in kwargs {
match str_kwarg_pos(method, k) {
Some(pos) => {
if pos >= out.len() {
out.resize(pos + 1, None);
}
out[pos] = Some(v.clone());
}
None => {
return Err(format!(
"TypeError: {method}() got an unexpected keyword argument '{k}'"
))
}
}
}
Ok(out.into_iter().map(|o| o.unwrap_or(Value::Undef)).collect())
}
fn str_method(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
let s = with_host(|h| h.as_str(recv)).unwrap_or_default();
let sarg = |i: usize| with_host(|h| args.get(i).and_then(|v| h.as_str(v))).unwrap_or_default();
let sarg_checked = |i: usize, what: &str| -> Result<String, String> {
match args.get(i) {
None | Some(Value::Undef) => Ok(String::new()),
Some(v) => with_host(|h| h.as_str(v)).ok_or_else(|| {
host::type_error(&format!(
"{what} must be str, not {}",
with_host(|h| h.type_name(v))
))
}),
}
};
match name {
"upper" => Ok(new_str(s.to_uppercase())),
"lower" => Ok(new_str(s.to_lowercase())),
"casefold" => {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
match crate::casefold::casefold_override(c) {
Some(f) => out.push_str(f),
None => out.extend(c.to_lowercase()),
}
}
Ok(new_str(out))
}
"strip" => Ok(new_str(strip_str(&s, args, 3, "strip")?)),
"lstrip" => Ok(new_str(strip_str(&s, args, 1, "lstrip")?)),
"rstrip" => Ok(new_str(strip_str(&s, args, 2, "rstrip")?)),
"swapcase" => Ok(new_str(
s.chars()
.flat_map(|c| {
if c.is_uppercase() {
c.to_lowercase().collect::<Vec<_>>()
} else if c.is_lowercase() {
c.to_uppercase().collect::<Vec<_>>()
} else {
vec![c]
}
})
.collect::<String>(),
)),
"capitalize" => {
let mut c = s.chars();
let out = match c.next() {
Some(f) => to_titlecase(f) + &c.as_str().to_lowercase(),
None => String::new(),
};
Ok(new_str(out))
}
"title" => {
let mut out = String::new();
let mut prev_cased = false;
for ch in s.chars() {
if ch.is_alphabetic() {
if prev_cased {
out.extend(ch.to_lowercase());
} else {
out.push_str(&to_titlecase(ch));
}
prev_cased = true;
} else {
out.push(ch);
prev_cased = false;
}
}
Ok(new_str(out))
}
"split" | "rsplit" => {
let reverse = name == "rsplit";
let maxsplit = match args.get(1) {
Some(v) => ssize_arg(v)?,
None => -1,
};
let none_sep = args.is_empty() || matches!(args.first(), Some(Value::Undef));
let strs: Vec<String> = if none_sep {
split_ws_str(&s, maxsplit, reverse)
} else {
let sep = sarg_checked(0, "").map_err(|_| {
host::type_error(&format!(
"must be str or None, not {}",
with_host(|h| h.type_name(&args[0]))
))
})?;
if sep.is_empty() {
return Err("ValueError: empty separator".into());
}
let cap = if maxsplit >= 0 {
(maxsplit as usize).saturating_add(1)
} else {
usize::MAX
};
if reverse {
let mut v: Vec<String> = s
.rsplitn(cap, sep.as_str())
.map(|p| p.to_string())
.collect();
v.reverse();
v
} else {
s.splitn(cap, sep.as_str()).map(|p| p.to_string()).collect()
}
};
let parts: Vec<Value> = strs.into_iter().map(new_str).collect();
Ok(with_host(|h| h.new_list(parts)))
}
"splitlines" => {
let keepends = args
.first()
.map(|v| py_bool(v).unwrap_or(false))
.unwrap_or(false);
let parts: Vec<Value> = str_splitlines(&s, keepends)
.into_iter()
.map(new_str)
.collect();
Ok(with_host(|h| h.new_list(parts)))
}
"join" => {
let seq = args.first().ok_or_else(|| {
host::type_error(&format!(
"str.join() takes exactly one argument ({} given)",
args.len()
))
})?;
let items = host::iter_vec(seq)?;
let mut strs = Vec::new();
for (n, it) in items.iter().enumerate() {
strs.push(with_host(|h| h.as_str(it)).ok_or_else(|| {
host::type_error(&format!(
"sequence item {n}: expected str instance, {} found",
with_host(|h| h.type_name(it))
))
})?);
}
Ok(new_str(strs.join(&s)))
}
"replace" => {
let from = sarg_checked(0, "replace() argument 1")?;
let to = sarg_checked(1, "replace() argument 2")?;
let count = match args.get(2) {
Some(v) => Some(ssize_arg(v)?),
None => None,
};
let out = match count {
Some(n) if n >= 0 => s.replacen(&from, &to, n as usize),
_ => s.replace(&from, &to),
};
Ok(new_str(out))
}
"startswith" | "endswith" => {
let chars: Vec<char> = s.chars().collect();
let (start, end) = resolve_start_end(chars.len(), args, 1);
if start > chars.len() {
return Ok(Value::Bool(false));
}
let region: String = chars[start..end.max(start).min(chars.len())]
.iter()
.collect();
let prefixes = match args.first() {
Some(v) if !matches!(v, Value::Undef) => str_prefixes(v, name)?,
_ => {
return Err(host::type_error(&format!(
"{name} first arg must be str or a tuple of str, not NoneType"
)))
}
};
let hit = prefixes.iter().any(|p| {
if name == "startswith" {
region.starts_with(p.as_str())
} else {
region.ends_with(p.as_str())
}
});
Ok(Value::Bool(hit))
}
"find" | "rfind" => {
let needle: Vec<char> = sarg_checked(0, &format!("{name}() argument 1"))?
.chars()
.collect();
let chars: Vec<char> = s.chars().collect();
let (start, end) = resolve_start_end(chars.len(), args, 1);
let p = slice_find(&chars, &needle, start, end, name == "rfind");
Ok(Value::Int(p.map(|x| x as i64).unwrap_or(-1)))
}
"index" | "rindex" => {
let needle: Vec<char> = sarg_checked(0, &format!("{name}() argument 1"))?
.chars()
.collect();
let chars: Vec<char> = s.chars().collect();
let (start, end) = resolve_start_end(chars.len(), args, 1);
match slice_find(&chars, &needle, start, end, name == "rindex") {
Some(p) => Ok(Value::Int(p as i64)),
None => Err("ValueError: substring not found".into()),
}
}
"count" => {
let sub: Vec<char> = sarg_checked(0, "count() argument 1")?.chars().collect();
let chars: Vec<char> = s.chars().collect();
let (start, end) = resolve_start_end(chars.len(), args, 1);
Ok(Value::Int(count_range(&chars, &sub, start, end) as i64))
}
"isdigit" => Ok(Value::Bool(!s.is_empty() && s.chars().all(is_digit_char))),
"isalpha" => Ok(Value::Bool(
!s.is_empty() && s.chars().all(|c| c.is_alphabetic()),
)),
"isalnum" => Ok(Value::Bool(
!s.is_empty() && s.chars().all(|c| c.is_alphanumeric()),
)),
"isspace" => Ok(Value::Bool(!s.is_empty() && s.chars().all(is_py_space))),
"isupper" => Ok(Value::Bool(
s.chars().any(|c| c.is_alphabetic())
&& s.chars()
.filter(|c| c.is_alphabetic())
.all(|c| c.is_uppercase()),
)),
"islower" => Ok(Value::Bool(
s.chars().any(|c| c.is_alphabetic())
&& s.chars()
.filter(|c| c.is_alphabetic())
.all(|c| c.is_lowercase()),
)),
"zfill" => {
let wv = args.first().ok_or_else(|| {
host::type_error(&format!(
"str.zfill() takes exactly one argument ({} given)",
args.len()
))
})?;
let w = ssize_arg(wv)?.max(0) as usize;
let n = s.chars().count();
let out = if n < w {
let pad = "0".repeat(w - n);
match s.strip_prefix(['+', '-']) {
Some(rest) => format!("{}{pad}{rest}", &s[..1]),
None => format!("{pad}{s}"),
}
} else {
s.clone()
};
Ok(new_str(out))
}
"center" => Ok(new_str(pad_str(&s, args, 'c', name)?)),
"ljust" => Ok(new_str(pad_str(&s, args, 'l', name)?)),
"rjust" => Ok(new_str(pad_str(&s, args, 'r', name)?)),
"removeprefix" => {
let p = sarg_checked(0, "removeprefix() argument")?;
Ok(new_str(
s.strip_prefix(&p).map(|r| r.to_string()).unwrap_or(s),
))
}
"removesuffix" => {
let p = sarg_checked(0, "removesuffix() argument")?;
Ok(new_str(
s.strip_suffix(&p).map(|r| r.to_string()).unwrap_or(s),
))
}
"partition" => {
let sep = sarg(0);
let (a, b, c) = match s.find(&sep) {
Some(p) => (
s[..p].to_string(),
sep.clone(),
s[p + sep.len()..].to_string(),
),
None => (s.clone(), String::new(), String::new()),
};
Ok(with_host(|h| {
let t = vec![h.new_str(a), h.new_str(b), h.new_str(c)];
h.new_tuple(t)
}))
}
"rpartition" => {
let sep = sarg(0);
let (a, b, c) = match s.rfind(&sep) {
Some(p) => (
s[..p].to_string(),
sep.clone(),
s[p + sep.len()..].to_string(),
),
None => (String::new(), String::new(), s.clone()),
};
Ok(with_host(|h| {
let t = vec![h.new_str(a), h.new_str(b), h.new_str(c)];
h.new_tuple(t)
}))
}
"isnumeric" => Ok(Value::Bool(!s.is_empty() && s.chars().all(is_numeric_char))),
"isdecimal" => Ok(Value::Bool(!s.is_empty() && s.chars().all(is_decimal_char))),
"isidentifier" => Ok(Value::Bool(is_identifier(&s))),
"istitle" => Ok(Value::Bool(is_titlecased(&s))),
"isprintable" => Ok(Value::Bool(s.chars().all(host::is_printable_char))),
"expandtabs" => {
let tabsize = match args.first() {
Some(v) => match with_host(|h| h.index_fit(v)) {
host::IndexFit::Fits(n) => n,
host::IndexFit::TooLarge(_) => {
return Err(
"OverflowError: Python int too large to convert to C int".to_string()
)
}
host::IndexFit::NotInt => 8,
},
None => 8,
};
if tabsize > i32::MAX as i64 || tabsize < i32::MIN as i64 {
return Err("OverflowError: Python int too large to convert to C int".to_string());
}
Ok(new_str(expand_tabs(&s, tabsize.max(0) as usize)))
}
"translate" => {
let table = arg0(args)?;
Ok(new_str(str_translate(&s, &table)?))
}
"maketrans" => str_maketrans(args),
"format_map" => {
let mapping = arg0(args)?;
str_format_map(&s, &mapping)
}
"isascii" => Ok(Value::Bool(s.chars().all(|c| (c as u32) < 0x80))),
"encode" => {
let enc = args
.first()
.and_then(|v| with_host(|h| h.as_str(v)))
.unwrap_or_else(|| "utf-8".into());
let errors = args
.get(1)
.and_then(|v| with_host(|h| h.as_str(v)))
.unwrap_or_else(|| "strict".into());
let bytes = encode_str(&s, &enc, &errors)?;
Ok(with_host(|h| h.alloc(PyObj::Bytes(bytes))))
}
"format" => str_dot_format(&s, args, &[]),
_ => Err(format!(
"AttributeError: 'str' object has no attribute '{name}'"
)),
}
}
fn strip_str(s: &str, args: &[Value], mode: u8, name: &str) -> Result<String, String> {
if let Some(v) = args.first() {
let is_none = matches!(v, Value::Undef);
if !is_none && with_host(|h| h.as_str(v)).is_none() {
return Err(host::type_error(&format!("{name} arg must be None or str")));
}
}
let chars: Option<String> = with_host(|h| args.first().and_then(|v| h.as_str(v)));
let pred = |c: char| match &chars {
Some(set) => set.contains(c),
None => is_py_space(c),
};
let out = match mode {
1 => s.trim_start_matches(pred),
2 => s.trim_end_matches(pred),
_ => s.trim_matches(pred),
};
Ok(out.to_string())
}
fn ssize_arg(v: &Value) -> Result<i64, String> {
match with_host(|h| h.index_fit(v)) {
host::IndexFit::Fits(n) => Ok(n),
host::IndexFit::TooLarge(_) => {
Err("OverflowError: Python int too large to convert to C ssize_t".to_string())
}
host::IndexFit::NotInt => Err(host::type_error(&format!(
"'{}' object cannot be interpreted as an integer",
with_host(|h| h.type_name(v))
))),
}
}
fn pad_str(s: &str, args: &[Value], mode: char, name: &str) -> Result<String, String> {
let wv = args.first().ok_or_else(|| {
host::type_error(&format!(
"{name} expected at least 1 argument, got {}",
args.len()
))
})?;
let w = ssize_arg(wv)?.max(0) as usize;
let fill = with_host(|h| args.get(1).and_then(|v| h.as_str(v)))
.and_then(|f| f.chars().next())
.unwrap_or(' ');
let len = s.chars().count();
if len >= w {
return Ok(s.to_string());
}
let total = w - len;
Ok(match mode {
'l' => format!("{s}{}", fill.to_string().repeat(total)),
'r' => format!("{}{s}", fill.to_string().repeat(total)),
_ => {
let left = total / 2 + (total & w & 1);
let right = total - left;
format!(
"{}{s}{}",
fill.to_string().repeat(left),
fill.to_string().repeat(right)
)
}
})
}
fn type_new(name: &Value, bases: &Value, ns: &Value) -> Result<Value, String> {
type_new_meta(name, bases, ns, "type", vec![])
}
pub fn type_new_meta(
name: &Value,
bases: &Value,
ns: &Value,
metaclass: &str,
class_kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
let cname = with_host(|h| h.as_str(name))
.ok_or_else(|| host::type_error("type() argument 1 must be str"))?;
let base_names: Vec<String> = with_host(|h| match h.get(bases) {
Some(PyObj::Tuple(items)) | Some(PyObj::List(items)) => items
.iter()
.filter_map(|b| match h.get(b) {
Some(PyObj::Class(n)) => Some(n.clone()),
Some(PyObj::Builtin(n)) if n != "object" => Some(n.clone()),
_ => None,
})
.collect(),
_ => vec![],
});
let dict_handle = with_host(|h| match h.get(ns) {
Some(PyObj::Dict(_)) => Some(ns.clone()),
Some(PyObj::Instance(inst)) if !matches!(inst.payload, Value::Undef) => {
Some(inst.payload.clone())
}
_ => None,
});
let namespace: host::NameMap =
with_host(|h| match dict_handle.as_ref().and_then(|d| h.get(d)) {
Some(PyObj::Dict(d)) => d
.values()
.filter_map(|(k, v)| h.as_str(k).map(|s| (s, v.clone())))
.collect(),
_ => host::NameMap::default(),
});
let cls =
with_host(|h| h.register_class_meta(&cname, base_names, namespace.clone(), metaclass));
host::fire_set_name(&cname, &namespace)?;
host::fire_init_subclass(&cname, class_kwargs)?;
Ok(cls)
}
fn str_maketrans(args: &[Value]) -> Result<Value, String> {
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
if args.len() == 1 {
let pairs = with_host(|h| match h.get(&args[0]) {
Some(PyObj::Dict(m)) => m
.values()
.map(|(k, v)| (k.clone(), v.clone()))
.collect::<Vec<_>>(),
_ => vec![],
});
for (k, v) in pairs {
let ord = with_host(|h| {
if let Some(n) = h.as_int(&k) {
Some(n)
} else {
h.as_str(&k)
.filter(|s| s.chars().count() == 1)
.map(|s| s.chars().next().unwrap() as i64)
}
});
match ord {
Some(n) => {
host::dict_put(&mut d, PKey::Int(n), Value::Int(n), v);
}
None => {
return Err(host::type_error(
"keys in translate table must be strings of length 1",
))
}
}
}
return Ok(with_host(|h| h.new_dict(d)));
}
let x0 = args.first().ok_or_else(|| {
host::type_error(&format!(
"maketrans expected at least 1 argument, got {}",
args.len()
))
})?;
let x = with_host(|h| h.as_str(x0)).unwrap_or_default();
let y = with_host(|h| h.as_str(args.get(1).unwrap_or(&Value::Undef))).unwrap_or_default();
let xs: Vec<char> = x.chars().collect();
let ys: Vec<char> = y.chars().collect();
if xs.len() != ys.len() {
return Err(
"ValueError: the first two maketrans arguments must have equal length".to_string(),
);
}
for (a, b) in xs.iter().zip(ys.iter()) {
let ord = *a as i64;
host::dict_put(
&mut d,
PKey::Int(ord),
Value::Int(ord),
Value::Int(*b as i64),
);
}
if let Some(z) = args.get(2) {
let zs = with_host(|h| h.as_str(z)).unwrap_or_default();
for c in zs.chars() {
let ord = c as i64;
host::dict_put(&mut d, PKey::Int(ord), Value::Int(ord), Value::Undef);
}
}
Ok(with_host(|h| h.new_dict(d)))
}
fn is_numeric_char(c: char) -> bool {
c.is_numeric()
}
fn is_decimal_char(c: char) -> bool {
use unicode_general_category::{get_general_category, GeneralCategory};
get_general_category(c) == GeneralCategory::DecimalNumber
}
fn is_digit_char(c: char) -> bool {
is_decimal_char(c)
|| matches!(c,
'\u{00B2}'..='\u{00B3}' | '\u{00B9}' | '\u{1369}'..='\u{1371}' | '\u{19DA}'
| '\u{2070}' | '\u{2074}'..='\u{2079}' | '\u{2080}'..='\u{2089}'
| '\u{2460}'..='\u{2468}' | '\u{2474}'..='\u{247C}' | '\u{2488}'..='\u{2490}'
| '\u{24EA}' | '\u{24F5}'..='\u{24FD}' | '\u{24FF}' | '\u{2776}'..='\u{277E}'
| '\u{2780}'..='\u{2788}' | '\u{278A}'..='\u{2792}' | '\u{10A40}'..='\u{10A43}'
| '\u{10E60}'..='\u{10E68}' | '\u{11052}'..='\u{1105A}' | '\u{1F100}'..='\u{1F10A}')
}
fn to_titlecase(ch: char) -> String {
match ch {
'\u{01C4}' | '\u{01C5}' | '\u{01C6}' => "\u{01C5}".to_string(),
'\u{01C7}' | '\u{01C8}' | '\u{01C9}' => "\u{01C8}".to_string(),
'\u{01CA}' | '\u{01CB}' | '\u{01CC}' => "\u{01CB}".to_string(),
'\u{01F1}' | '\u{01F2}' | '\u{01F3}' => "\u{01F2}".to_string(),
_ => ch.to_uppercase().collect(),
}
}
pub(crate) fn is_py_space(c: char) -> bool {
c.is_whitespace() || ('\u{1c}'..='\u{1f}').contains(&c)
}
pub(crate) fn is_other_id_continue(c: char) -> bool {
matches!(
c,
'\u{00b7}' | '\u{0387}' | '\u{1369}'..='\u{1371}' | '\u{19da}' | '\u{200c}' | '\u{200d}'
)
}
pub(crate) fn is_identifier(s: &str) -> bool {
let mut it = s.chars();
match it.next() {
Some(c) if c == '_' || c.is_alphabetic() => {}
_ => return false,
}
it.all(|c| c == '_' || c.is_alphanumeric() || is_other_id_continue(c))
}
fn is_titlecased(s: &str) -> bool {
let mut cased = false;
let mut prev_cased = false;
for c in s.chars() {
if c.is_uppercase() {
if prev_cased {
return false;
}
prev_cased = true;
cased = true;
} else if c.is_lowercase() {
if !prev_cased {
return false;
}
prev_cased = true;
cased = true;
} else {
prev_cased = false;
}
}
cased
}
fn expand_tabs(s: &str, tabsize: usize) -> String {
let mut out = String::new();
let mut col = 0;
for c in s.chars() {
match c {
'\t' => {
if tabsize == 0 {
continue;
}
let n = tabsize - (col % tabsize);
for _ in 0..n {
out.push(' ');
}
col += n;
}
'\n' | '\r' => {
out.push(c);
col = 0;
}
other => {
out.push(other);
col += 1;
}
}
}
out
}
fn expand_tabs_bytes(bytes: &[u8], tabsize: usize) -> Vec<u8> {
let mut out = Vec::with_capacity(bytes.len());
let mut col = 0usize;
for &b in bytes {
match b {
b'\t' => {
if tabsize == 0 {
continue;
}
let n = tabsize - (col % tabsize);
out.extend(std::iter::repeat(b' ').take(n));
col += n;
}
b'\n' | b'\r' => {
out.push(b);
col = 0;
}
other => {
out.push(other);
col += 1;
}
}
}
out
}
fn str_translate(s: &str, table: &Value) -> Result<String, String> {
let mut out = String::new();
for c in s.chars() {
let key = with_host(|h| h.to_key(&Value::Int(c as i64)))?;
let mapped = with_host(|h| match h.get(table) {
Some(PyObj::Dict(d)) => d.get(&key).map(|(_, v)| v.clone()),
_ => None,
});
match mapped {
None => out.push(c),
Some(Value::Undef) => {} Some(v) => {
if let Some(n) = with_host(|h| h.as_int(&v)) {
if let Some(ch) = char::from_u32(n as u32) {
out.push(ch);
}
} else if let Some(rep) = with_host(|h| h.as_str(&v)) {
out.push_str(&rep);
}
}
}
}
Ok(out)
}
fn format_map_get(mapping: &Value, key: &str) -> Result<Value, String> {
let keyv = with_host(|h| h.new_str(key.to_string()));
if with_host(|h| matches!(h.get(mapping), Some(PyObj::Instance(_)))) {
return host::call_method(mapping, "__getitem__", vec![keyv], vec![]);
}
if let Some(meta) = host::dict_meta_of(mapping) {
let missing = with_host(|h| match h.to_key(&keyv) {
Ok(k) => matches!(h.get(mapping), Some(PyObj::Dict(d)) if !d.contains_key(&k)),
Err(_) => false,
});
if missing {
match meta.kind {
host::DictKind::Counter => return Ok(Value::Int(0)),
host::DictKind::DefaultDict => {
if let Some(factory) = meta.factory {
let default = host::invoke(&factory, vec![], vec![])?;
with_host(|h| h.set_item(mapping, &keyv, default.clone()))?;
return Ok(default);
}
}
host::DictKind::OrderedDict => {}
}
}
}
with_host(|h| h.get_item(mapping, &keyv))
}
fn str_format_map(s: &str, mapping: &Value) -> Result<Value, String> {
let mut out = String::new();
let chars: Vec<char> = s.chars().collect();
let mut i = 0;
while i < chars.len() {
match chars[i] {
'{' if chars.get(i + 1) == Some(&'{') => {
out.push('{');
i += 2;
}
'}' if chars.get(i + 1) == Some(&'}') => {
out.push('}');
i += 2;
}
'{' => {
let mut field = String::new();
i += 1;
while i < chars.len() && chars[i] != '}' {
field.push(chars[i]);
i += 1;
}
i += 1;
let (name_conv, spec) = match field.split_once(':') {
Some((a, b)) => (a.to_string(), b.to_string()),
None => (field, String::new()),
};
let (fname, conv) = match name_conv.split_once('!') {
Some((n, c)) => (
n.to_string(),
match c {
"s" => 1,
"r" => 2,
"a" => 3,
_ => 0,
},
),
None => (name_conv, 0),
};
let val = format_map_get(mapping, &fname)?;
out.push_str(&format_field(&val, conv, &spec)?);
}
c => {
out.push(c);
i += 1;
}
}
}
Ok(new_str(out))
}
fn conv_code(c: &str) -> i64 {
match c {
"s" => 1,
"r" => 2,
"a" => 3,
_ => 0,
}
}
fn split_format_field(field: &str) -> (String, i64, String) {
let fchars: Vec<char> = field.chars().collect();
let mut depth = 0i32;
let mut colon = None;
let mut bang = None;
for (i, &c) in fchars.iter().enumerate() {
match c {
'[' => depth += 1,
']' => depth -= 1,
':' if depth == 0 => {
colon = Some(i);
break;
}
'!' if depth == 0 && bang.is_none() => bang = Some(i),
_ => {}
}
}
let (head, spec) = match colon {
Some(i) => (
fchars[..i].iter().collect::<String>(),
fchars[i + 1..].iter().collect::<String>(),
),
None => (field.to_string(), String::new()),
};
match bang.filter(|&b| colon.map(|c| b < c).unwrap_or(true)) {
Some(b) => {
let name: String = fchars[..b].iter().collect();
let conv: String = fchars[b + 1..colon.unwrap_or(fchars.len())]
.iter()
.collect();
(name, conv_code(&conv), spec)
}
None => (head, 0, spec),
}
}
#[derive(Default)]
struct FieldNum {
counter: usize,
manual: Option<bool>,
}
fn resolve_format_arg(
name: &str,
args: &[Value],
kwargs: &[(String, Value)],
st: &mut FieldNum,
) -> Result<Value, String> {
let nchars: Vec<char> = name.chars().collect();
let mut i = 0;
while i < nchars.len() && nchars[i] != '.' && nchars[i] != '[' {
i += 1;
}
let base: String = nchars[..i].iter().collect();
let mut val = if base.is_empty() {
if st.manual == Some(true) {
return Err(
"ValueError: cannot switch from manual field specification to \
automatic field numbering"
.into(),
);
}
st.manual = Some(false);
let v = args.get(st.counter).cloned().ok_or_else(|| {
format!(
"IndexError: Replacement index {} out of range for positional args tuple",
st.counter
)
})?;
st.counter += 1;
v
} else if let Ok(n) = base.parse::<usize>() {
if st.manual == Some(false) {
return Err(
"ValueError: cannot switch from automatic field numbering to \
manual field specification"
.into(),
);
}
st.manual = Some(true);
args.get(n).cloned().ok_or_else(|| {
format!("IndexError: Replacement index {n} out of range for positional args tuple")
})?
} else {
kwargs
.iter()
.find(|(k, _)| *k == base)
.map(|(_, v)| v.clone())
.ok_or_else(|| {
with_host(|h| {
let k = h.new_str(base);
h.key_error(&k)
})
})?
};
while i < nchars.len() {
match nchars[i] {
'.' => {
i += 1;
let start = i;
while i < nchars.len() && nchars[i] != '.' && nchars[i] != '[' {
i += 1;
}
let attr: String = nchars[start..i].iter().collect();
val = with_host(|h| h.get_attr(&val, &attr))?;
}
'[' => {
i += 1;
let start = i;
while i < nchars.len() && nchars[i] != ']' {
i += 1;
}
let key: String = nchars[start..i].iter().collect();
if i < nchars.len() {
i += 1; }
let keyv = if let Ok(n) = key.parse::<i64>() {
Value::Int(n)
} else {
with_host(|h| h.new_str(key))
};
val = with_host(|h| h.get_item(&val, &keyv))?;
}
_ => break,
}
}
Ok(val)
}
fn substitute_nested_spec(
spec: &str,
args: &[Value],
kwargs: &[(String, Value)],
st: &mut FieldNum,
) -> Result<String, String> {
if !spec.contains('{') {
return Ok(spec.to_string());
}
let chars: Vec<char> = spec.chars().collect();
let mut out = String::new();
let mut i = 0;
while i < chars.len() {
match chars[i] {
'{' => {
i += 1;
let mut inner = String::new();
while i < chars.len() && chars[i] != '}' {
inner.push(chars[i]);
i += 1;
}
if i < chars.len() {
i += 1; }
let (fname, conv, _) = split_format_field(&inner);
let val = resolve_format_arg(&fname, args, kwargs, st)?;
out.push_str(&format_field(&val, conv, "")?);
}
c => {
out.push(c);
i += 1;
}
}
}
Ok(out)
}
fn str_dot_format(s: &str, args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
let mut out = String::new();
let chars: Vec<char> = s.chars().collect();
let mut i = 0;
let mut auto = FieldNum::default();
while i < chars.len() {
match chars[i] {
'{' if chars.get(i + 1) == Some(&'{') => {
out.push('{');
i += 2;
}
'}' if chars.get(i + 1) == Some(&'}') => {
out.push('}');
i += 2;
}
'{' => {
let mut field = String::new();
let mut depth = 1;
i += 1;
while i < chars.len() && depth > 0 {
match chars[i] {
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
break;
}
}
_ => {}
}
field.push(chars[i]);
i += 1;
}
if depth > 0 {
return Err("ValueError: Single '{' encountered in format string".to_string());
}
i += 1; let (fname, conv, spec) = split_format_field(&field);
let val = resolve_format_arg(&fname, args, kwargs, &mut auto)?;
let spec = substitute_nested_spec(&spec, args, kwargs, &mut auto)?;
out.push_str(&format_field(&val, conv, &spec)?);
}
'}' => return Err("ValueError: Single '}' encountered in format string".to_string()),
c => {
out.push(c);
i += 1;
}
}
}
Ok(new_str(out))
}
fn list_arity(name: &str) -> Option<Arity> {
Some(match name {
"append" | "extend" | "remove" | "count" => Arity::ExactlyOne,
"clear" | "reverse" | "copy" => Arity::NoArgs,
"insert" => Arity::VarExact(2),
"pop" => Arity::VarRange(0, 1),
"index" => Arity::VarRange(1, 3),
"sort" => Arity::NoPositional,
_ => return None,
})
}
fn list_method(
recv: &Value,
name: &str,
args: &[Value],
kwargs: &[(String, Value)],
) -> Result<Value, String> {
if let Some(spec) = list_arity(name) {
check_arity(name, &format!("list.{name}"), spec, args.len())?;
}
match name {
"append" => {
let v = arg0(args)?;
with_host(|h| {
if let Some(PyObj::List(l)) = h.get_mut(recv) {
l.push(v);
}
});
Ok(Value::Undef)
}
"extend" => {
let items = host::iter_vec(&arg0(args)?)?;
with_host(|h| {
if let Some(PyObj::List(l)) = h.get_mut(recv) {
l.extend(items);
}
});
Ok(Value::Undef)
}
"insert" => {
let a0 = arg0(args)?;
let idx = with_host(|h| h.as_int(&a0)).unwrap_or(0);
let v = args.get(1).cloned().unwrap_or(Value::Undef);
with_host(|h| {
if let Some(PyObj::List(l)) = h.get_mut(recv) {
let n = l.len() as i64;
let k = if idx < 0 {
(idx + n).max(0)
} else {
idx.min(n)
} as usize;
l.insert(k, v);
}
});
Ok(Value::Undef)
}
"pop" => {
let idx = args.first().and_then(|v| with_host(|h| h.as_int(v)));
with_host(|h| {
if let Some(PyObj::List(l)) = h.get_mut(recv) {
if l.is_empty() {
return Err("IndexError: pop from empty list".into());
}
let n = l.len() as i64;
let k = match idx {
Some(i) => {
let k = if i < 0 { i + n } else { i };
if k < 0 || k >= n {
return Err("IndexError: pop index out of range".into());
}
k as usize
}
None => l.len() - 1,
};
Ok(l.remove(k))
} else {
Err(host::type_error("not a list"))
}
})
}
"remove" => {
let v = arg0(args)?;
let elems = with_host(|h| match h.get(recv) {
Some(PyObj::List(l)) => l.clone(),
_ => Vec::new(),
});
let mut pos = None;
for (i, e) in elems.iter().enumerate() {
if elem_equal(e, &v)? {
pos = Some(i);
break;
}
}
match pos {
Some(p) => {
with_host(|h| {
if let Some(PyObj::List(l)) = h.get_mut(recv) {
l.remove(p);
}
});
Ok(Value::Undef)
}
None => Err("ValueError: list.remove(x): x not in list".into()),
}
}
"clear" => {
with_host(|h| {
if let Some(PyObj::List(l)) = h.get_mut(recv) {
l.clear();
}
});
Ok(Value::Undef)
}
"index" => {
let elems = with_host(|h| match h.get(recv) {
Some(PyObj::List(l)) => l.clone(),
_ => Vec::new(),
});
sequence_index(&elems, args, "list.index(x): x not in list")
}
"count" => {
let v = arg0(args)?;
let elems = with_host(|h| match h.get(recv) {
Some(PyObj::List(l)) => l.clone(),
_ => Vec::new(),
});
let mut n = 0i64;
for e in &elems {
if elem_equal(e, &v)? {
n += 1;
}
}
Ok(Value::Int(n))
}
"reverse" => {
with_host(|h| {
if let Some(PyObj::List(l)) = h.get_mut(recv) {
l.reverse();
}
});
Ok(Value::Undef)
}
"copy" => {
let items = with_host(|h| match h.get(recv) {
Some(PyObj::List(l)) => l.clone(),
_ => vec![],
});
Ok(with_host(|h| h.new_list(items)))
}
"sort" => {
let items = with_host(|h| match h.get(recv) {
Some(PyObj::List(l)) => l.clone(),
_ => vec![],
});
let tmp = with_host(|h| h.new_list(items));
let sorted = py_sorted(&[tmp], kwargs)?;
let new_items = with_host(|h| match h.get(&sorted) {
Some(PyObj::List(l)) => l.clone(),
_ => vec![],
});
with_host(|h| {
if let Some(PyObj::List(l)) = h.get_mut(recv) {
*l = new_items;
}
});
Ok(Value::Undef)
}
_ => Err(format!(
"AttributeError: 'list' object has no attribute '{name}'"
)),
}
}
fn dict_arity(name: &str) -> Option<Arity> {
Some(match name {
"keys" | "values" | "items" | "clear" | "copy" | "popitem" => Arity::NoArgs,
"get" | "pop" | "setdefault" => Arity::VarRange(1, 2),
"update" => Arity::VarRange(0, 1),
_ => return None,
})
}
fn dict_fromkeys(kind: Option<host::DictKind>, args: &[Value]) -> Result<Value, String> {
if kind == Some(host::DictKind::Counter) {
return Err(
"NotImplementedError: Counter.fromkeys() is undefined. Use Counter(iterable) instead."
.into(),
);
}
let keys = host::iter_vec(&arg0(args)?)?;
let value = args.get(1).cloned().unwrap_or(Value::Undef);
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
for k in keys {
let cands = with_host(|h| host::dict_local_candidates(h, &d));
let key = host::with_instance_key(&k, host::KeyRole::Dict, &cands, || {
with_host(|h| h.to_key(&k))
})?;
host::dict_put(&mut d, key, k, value.clone());
}
Ok(match kind {
Some(kind) => host::alloc_dict_subtype(d, kind, None),
None => with_host(|h| h.new_dict(d)),
})
}
fn dict_method(
recv: &Value,
name: &str,
args: &[Value],
kwargs: &[(String, Value)],
) -> Result<Value, String> {
if let Some(spec) = dict_arity(name) {
check_arity(name, &format!("dict.{name}"), spec, args.len())?;
}
match name {
"keys" => Ok(with_host(|h| {
h.alloc(PyObj::DictView {
dict: recv.clone(),
kind: 0,
})
})),
"values" => Ok(with_host(|h| {
h.alloc(PyObj::DictView {
dict: recv.clone(),
kind: 1,
})
})),
"items" => Ok(with_host(|h| {
h.alloc(PyObj::DictView {
dict: recv.clone(),
kind: 2,
})
})),
"fromkeys" => dict_fromkeys(host::dict_meta_of(recv).map(|m| m.kind), args),
"get" => {
let kv = arg0(args)?;
let cands = host::instance_key_candidates_for(recv, Some(&kv));
let key = host::with_instance_key(&kv, host::KeyRole::Dict, &cands, || {
with_host(|h| h.to_key(&kv))
})?;
Ok(with_host(|h| match h.get(recv) {
Some(PyObj::Dict(d)) => d.get(&key).map(|(_, v)| v.clone()),
_ => None,
})
.unwrap_or_else(|| args.get(1).cloned().unwrap_or(Value::Undef)))
}
"pop" => {
let kv = arg0(args)?;
let (empty, ordered) = with_host(|h| match h.get(recv) {
Some(PyObj::Dict(d)) => (d.is_empty(), h.type_name(recv) == "OrderedDict"),
_ => (false, false),
});
let role = if ordered {
host::KeyRole::Bare
} else {
host::KeyRole::Dict
};
if empty && !ordered {
return match args.get(1) {
Some(d) => Ok(d.clone()),
None => Err(with_host(|h| h.key_error(&kv))),
};
}
let cands = host::instance_key_candidates_for(recv, Some(&kv));
let key = host::with_instance_key(&kv, role, &cands, || with_host(|h| h.to_key(&kv)))?;
let got = with_host(|h| {
if let Some(PyObj::Dict(d)) = h.get_mut(recv) {
d.shift_remove(&key).map(|(_, v)| v)
} else {
None
}
});
match got {
Some(v) => Ok(v),
None => match args.get(1) {
Some(d) => Ok(d.clone()),
None => Err(with_host(|h| h.key_error(&kv))),
},
}
}
"setdefault" => {
let kv = arg0(args)?;
let default = args.get(1).cloned().unwrap_or(Value::Undef);
let cands = host::instance_key_candidates_for(recv, Some(&kv));
let key = host::with_instance_key(&kv, host::KeyRole::Dict, &cands, || {
with_host(|h| h.to_key(&kv))
})?;
Ok(with_host(|h| {
if let Some(PyObj::Dict(d)) = h.get(recv) {
if let Some((_, v)) = d.get(&key) {
return v.clone();
}
}
if let Some(PyObj::Dict(d)) = h.get_mut(recv) {
d.insert(key, (kv, default.clone()));
}
default
}))
}
"update" => {
dict_update(recv, args.first(), kwargs)?;
Ok(Value::Undef)
}
"clear" => {
with_host(|h| {
if let Some(PyObj::Dict(d)) = h.get_mut(recv) {
d.clear();
}
});
Ok(Value::Undef)
}
"copy" => {
let d = with_host(|h| match h.get(recv) {
Some(PyObj::Dict(d)) => d.clone(),
_ => IndexMap::new(),
});
Ok(match host::dict_meta_of(recv) {
Some(meta) => host::alloc_dict_subtype(d, meta.kind, meta.factory),
None => with_host(|h| h.new_dict(d)),
})
}
"popitem" => {
let got = with_host(|h| {
if let Some(PyObj::Dict(d)) = h.get_mut(recv) {
d.pop().map(|(_, (k, v))| (k, v))
} else {
None
}
});
match got {
Some((k, v)) => Ok(with_host(|h| h.new_tuple(vec![k, v]))),
None => Err(with_host(|h| {
let s = h.new_str("popitem(): dictionary is empty");
h.key_error(&s)
})),
}
}
_ => Err(format!(
"AttributeError: 'dict' object has no attribute '{name}'"
)),
}
}
fn dict_update_seq(v: &Value) -> Result<Vec<(Value, Value)>, String> {
let mut out = Vec::new();
for (n, item) in host::iter_vec(v)?.into_iter().enumerate() {
let elems =
host::iter_vec(&item).map_err(|_| host::type_error("object is not iterable"))?;
if elems.len() != 2 {
return Err(format!(
"ValueError: dictionary update sequence element #{n} has length {}; 2 is required",
elems.len()
));
}
let mut it = elems.into_iter();
out.push((
it.next().unwrap_or(Value::Undef),
it.next().unwrap_or(Value::Undef),
));
}
Ok(out)
}
fn dict_update(
recv: &Value,
other: Option<&Value>,
kwargs: &[(String, Value)],
) -> Result<(), String> {
let mut pairs: Vec<(Value, Value)> = Vec::new();
if let Some(o) = other {
match mapping_pairs(o)? {
Some(p) => pairs.extend(p),
None => pairs.extend(dict_update_seq(o)?),
}
}
for (k, v) in kwargs {
let kv = with_host(|h| h.new_str(k.clone()));
pairs.push((kv, v.clone()));
}
let mut cands = host::instance_key_candidates(recv);
let mut triples: Vec<(PKey, Value, Value)> = Vec::with_capacity(pairs.len());
for (kv, v) in pairs {
let k = host::with_instance_key(&kv, host::KeyRole::Dict, &cands, || {
with_host(|h| h.to_key(&kv))
})?;
if any_value_key(&k) && !cands.iter().any(|(ck, _)| *ck == k) {
cands.push((k.clone(), kv.clone()));
}
triples.push((k, kv, v));
}
with_host(|h| {
if let Some(PyObj::Dict(d)) = h.get_mut(recv) {
for (k, kv, v) in triples {
host::dict_put(d, k, kv, v);
}
}
});
Ok(())
}
fn any_value_key(k: &PKey) -> bool {
matches!(k, PKey::Instance { .. } | PKey::Foreign { .. })
}
fn set_arity(name: &str) -> Option<Arity> {
Some(match name {
"add" | "discard" | "remove" => Arity::ExactlyOne,
"clear" | "copy" | "pop" => Arity::NoArgs,
_ => return None,
})
}
fn set_method(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
if !with_host(|h| h.is_frozenset(recv)) {
if let Some(spec) = set_arity(name) {
check_arity(name, &format!("set.{name}"), spec, args.len())?;
}
}
if with_host(|h| h.is_frozenset(recv))
&& matches!(
name,
"add"
| "discard"
| "remove"
| "clear"
| "update"
| "pop"
| "intersection_update"
| "difference_update"
| "symmetric_difference_update"
)
{
return Err(format!(
"AttributeError: 'frozenset' object has no attribute '{name}'"
));
}
match name {
"add" => {
let v = arg0(args)?;
let cands = host::instance_key_candidates_for(recv, Some(&v));
let k = host::with_instance_key(&v, host::KeyRole::Set, &cands, || {
with_host(|h| h.to_key(&v))
})?;
with_host(|h| {
if let Some(PyObj::Set(s)) = h.get_mut(recv) {
host::set_put(s, k, v);
}
});
Ok(Value::Undef)
}
"discard" | "remove" => {
let v = arg0(args)?;
let cands = host::instance_key_candidates_for(recv, Some(&v));
let k = host::with_instance_key(&v, host::KeyRole::Set, &cands, || {
with_host(|h| h.to_key(&v))
})?;
let removed = with_host(|h| {
if let Some(PyObj::Set(s)) = h.get_mut(recv) {
s.shift_remove(&k).is_some()
} else {
false
}
});
if name == "remove" && !removed {
return Err(with_host(|h| h.key_error(&v)));
}
Ok(Value::Undef)
}
"clear" => {
with_host(|h| {
if let Some(PyObj::Set(s)) = h.get_mut(recv) {
s.clear();
}
h.set_forget_layout(recv);
});
Ok(Value::Undef)
}
"union" => set_variadic(recv, args, host::binop::BITOR),
"intersection" => set_variadic(recv, args, host::binop::BITAND),
"symmetric_difference" => set_binop(recv, args, host::binop::BITXOR),
"difference" => {
if args.is_empty() {
return set_method(recv, "copy", &[]);
}
let mut acc = recv.clone();
for a in args {
let other_set = if with_host(|h| h.setlike(a).is_some()) {
a.clone()
} else {
call_builtin_function("set", vec![a.clone()], vec![])?
};
let aligned = host::align_operand(&acc, &other_set)?;
let other_set = aligned.unwrap_or(other_set);
acc = with_host(|h| h.arith(NumOp::Sub, &acc, &other_set))?;
}
Ok(acc)
}
"issubset" => {
let other = iter_keys_against(&arg0(args)?, recv)?;
Ok(Value::Bool(with_host(|h| {
set_keys(h, recv).iter().all(|k| other.contains(k))
})))
}
"issuperset" => {
let other = iter_keys_against(&arg0(args)?, recv)?;
Ok(Value::Bool(with_host(|h| {
other.iter().all(|k| set_keys(h, recv).contains(k))
})))
}
"isdisjoint" => {
let other = iter_keys_against(&arg0(args)?, recv)?;
Ok(Value::Bool(with_host(|h| {
set_keys(h, recv).iter().all(|k| !other.contains(k))
})))
}
"copy" => {
let (s, frozen) = with_host(|h| match h.get(recv) {
Some(PyObj::Set(s)) => (s.clone(), false),
Some(PyObj::Frozenset(s)) => (s.clone(), true),
_ => (IndexMap::new(), false),
});
let src = set_merge_source(recv);
Ok(with_host(|h| {
let out = h.new_setlike(s, frozen);
if let Some((incoming, src)) = src {
let empty = IndexMap::new();
h.set_relayout_after_merge(&out, (&empty, host::SET_MINSIZE), incoming, src_ref(&src));
}
out
}))
}
"update" => {
for a in args {
let merge = set_merge_source(a).map(|m| (m, with_host(|h| h.set_layout_of(recv))));
let items = host::iter_vec(a)?;
for it in items {
let cands = host::instance_key_candidates_for(recv, Some(&it));
let k = host::with_instance_key(&it, host::KeyRole::Set, &cands, || {
with_host(|h| h.to_key(&it))
})?;
with_host(|h| {
if let Some(PyObj::Set(s)) = h.get_mut(recv) {
host::set_put(s, k, it);
}
});
}
if let Some(((incoming, src), Some(before))) = merge {
with_host(|h| {
h.set_relayout_after_merge(recv, (&before.0, before.1), incoming, src_ref(&src))
});
}
}
Ok(Value::Undef)
}
"pop" => with_host(|h| {
let first = h.set_entries_in_order(recv).and_then(|e| e.into_iter().next());
if let Some(PyObj::Set(s)) = h.get_mut(recv) {
match first {
Some((k, v)) => {
s.shift_remove(&k);
Ok(v)
}
None => {
let s = h.new_str("pop from an empty set");
Err(h.key_error(&s))
}
}
} else {
Err(host::type_error("not a set"))
}
}),
"intersection_update" => {
for a in args {
let other = iter_keys_against(a, recv)?;
with_host(|h| {
if let Some(PyObj::Set(s)) = h.get_mut(recv) {
s.retain(|k, _| other.contains(k));
}
});
}
Ok(Value::Undef)
}
"difference_update" => {
for a in args {
let other = iter_keys_against(a, recv)?;
with_host(|h| {
if let Some(PyObj::Set(s)) = h.get_mut(recv) {
s.retain(|k, _| !other.contains(k));
}
});
}
Ok(Value::Undef)
}
"symmetric_difference_update" => {
let other = arg0(args)?;
if same_object(recv, &other) {
return set_method(recv, "clear", &[]);
}
let walked = with_host(|h| {
h.setlike(&other).is_some()
|| (matches!(h.get(&other), Some(PyObj::Dict(_))) && h.type_name(&other) == "dict")
});
let other = if walked {
other
} else {
call_builtin_function("set", vec![other], vec![])?
};
let items = match with_host(|h| h.set_entries_in_order(&other)) {
Some(entries) => entries.into_iter().map(|(_, v)| v).collect(),
None => host::iter_vec(&other)?,
};
for it in items {
let cands = host::instance_key_candidates_for(recv, Some(&it));
let k = host::with_instance_key(&it, host::KeyRole::Set, &cands, || {
with_host(|h| h.to_key(&it))
})?;
with_host(|h| {
if let Some(PyObj::Set(s)) = h.get_mut(recv) {
if s.shift_remove(&k).is_none() {
host::set_put(s, k, it);
}
}
});
}
Ok(Value::Undef)
}
_ => Err(format!(
"AttributeError: 'set' object has no attribute '{name}'"
)),
}
}
fn set_keys(h: &host::PyHost, v: &Value) -> Vec<PKey> {
match h.setlike(v) {
Some(s) => s.keys().cloned().collect(),
None => vec![],
}
}
fn iter_keys_against(v: &Value, recv: &Value) -> Result<Vec<PKey>, String> {
let cands = host::instance_key_candidates(recv);
if cands.is_empty() {
if let Some(ks) = with_host(|h| h.setlike(v).map(|s| s.keys().cloned().collect::<Vec<_>>()))
{
return Ok(ks);
}
}
let items = host::iter_vec(v)?;
let mut ks = Vec::with_capacity(items.len());
for it in items {
ks.push(host::with_instance_key(
&it,
host::KeyRole::Set,
&cands,
|| with_host(|h| h.to_key(&it)),
)?);
}
Ok(ks)
}
fn set_binop(recv: &Value, args: &[Value], tag: i64) -> Result<Value, String> {
let other = arg0(args)?;
let other_set = if with_host(|h| h.setlike(&other).is_some()) {
other
} else {
call_builtin_function("set", vec![other], vec![])?
};
let aligned = host::align_operand(recv, &other_set)?;
let other_set = aligned.unwrap_or(other_set);
with_host(|h| h.binop(tag, recv, &other_set))
}
#[derive(Clone, Copy)]
pub enum ViewSetOp {
Sub,
And,
Or,
Xor,
}
fn view_setop_for_tag(tag: i64) -> Option<ViewSetOp> {
match tag {
host::binop::BITAND => Some(ViewSetOp::And),
host::binop::BITOR => Some(ViewSetOp::Or),
host::binop::BITXOR => Some(ViewSetOp::Xor),
_ => None,
}
}
fn set_view_kind(v: &Value) -> Option<bool> {
with_host(|h| match h.get(v) {
Some(PyObj::DictView { kind: 0, .. }) => Some(false),
Some(PyObj::DictView { kind: 2, .. }) => Some(true),
_ => None,
})
}
fn view_len(v: &Value) -> usize {
with_host(|h| match h.get(v) {
Some(PyObj::DictView { dict, .. }) => match h.get(dict) {
Some(PyObj::Dict(d)) => d.len(),
_ => 0,
},
_ => 0,
})
}
fn dictviews_to_set(v: &Value) -> Result<Value, String> {
let src = with_host(|h| match h.get(v) {
Some(PyObj::DictView { dict, kind: 0 }) if h.type_name(dict) == "dict" => dict.clone(),
_ => v.clone(),
});
call_builtin_function("set", vec![src], vec![])
}
pub fn dictview_setop(op: ViewSetOp, a: &Value, b: &Value) -> Option<Result<Value, String>> {
if set_view_kind(a).is_none() && set_view_kind(b).is_none() {
return None;
}
Some(match op {
ViewSetOp::And => dictview_intersect(a, b),
ViewSetOp::Or => dictviews_to_set(a).and_then(|r| {
set_method(&r, "update", std::slice::from_ref(b))?;
Ok(r)
}),
ViewSetOp::Sub => dictviews_to_set(a).and_then(|r| {
set_method(&r, "difference_update", std::slice::from_ref(b))?;
Ok(r)
}),
ViewSetOp::Xor if set_view_kind(a) == Some(true) && set_view_kind(b) == Some(true) => {
dictitems_xor(a, b)
}
ViewSetOp::Xor => dictviews_to_set(a).and_then(|r| {
set_method(&r, "symmetric_difference_update", std::slice::from_ref(b))?;
Ok(r)
}),
})
}
fn dictview_intersect(a: &Value, b: &Value) -> Result<Value, String> {
let (mut view, mut other) = if set_view_kind(a).is_some() {
(a.clone(), b.clone())
} else {
(b.clone(), a.clone())
};
let len_self = view_len(&view);
let other_set_len = with_host(|h| match h.get(&other) {
Some(PyObj::Set(s)) if h.type_name(&other) == "set" => Some(s.len()),
_ => None,
});
if other_set_len.is_some_and(|n| len_self <= n) {
return set_method(&other, "intersection", &[view]);
}
if set_view_kind(&other).is_some() && view_len(&other) > len_self {
std::mem::swap(&mut view, &mut other);
}
let result = call_builtin_function("set", vec![], vec![])?;
for key in host::iter_vec(&other)? {
if contains_value(view.clone(), key.clone())? {
set_method(&result, "add", &[key])?;
}
}
Ok(result)
}
fn dictitems_xor(a: &Value, b: &Value) -> Result<Value, String> {
let dict_of = |v: &Value| {
with_host(|h| match h.get(v) {
Some(PyObj::DictView { dict, .. }) => dict.clone(),
_ => Value::Undef,
})
};
let (d1, d2) = (dict_of(a), dict_of(b));
let temp = call_builtin_function("dict", vec![d1], vec![])?;
let result = call_builtin_function("set", vec![], vec![])?;
let pairs = host::iter_vec(&host::call_method(&d2, "items", vec![], vec![])?)?;
for pair in pairs {
let (key, val2) = with_host(|h| match h.get(&pair) {
Some(PyObj::Tuple(t)) => (t[0].clone(), t[1].clone()),
_ => (Value::Undef, Value::Undef),
});
let held = contains_value(temp.clone(), key.clone())?;
let cancels = held && {
let val1 = dict_get(&temp, &key)?;
elem_equal(&val1, &val2)?
};
if cancels {
let cands = host::instance_key_candidates_for(&temp, Some(&key));
host::with_instance_key(&key, host::KeyRole::Dict, &cands, || {
with_host(|h| h.del_item(&temp, &key))
})?;
} else {
set_method(&result, "add", &[pair])?;
}
}
let remaining = host::call_method(&temp, "items", vec![], vec![])?;
set_method(&result, "update", &[remaining])?;
Ok(result)
}
type SetLayout = (IndexMap<PKey, Value>, usize);
fn set_merge_source(v: &Value) -> Option<(usize, Option<SetLayout>)> {
with_host(|h| match h.get(v) {
Some(PyObj::Set(s) | PyObj::Frozenset(s)) => Some((s.len(), h.set_layout_of(v))),
Some(PyObj::Dict(d)) if h.type_name(v) == "dict" => Some((d.len(), None)),
_ => None,
})
}
fn src_ref(src: &Option<SetLayout>) -> Option<(&IndexMap<PKey, Value>, usize)> {
src.as_ref().map(|(m, start)| (m, *start))
}
fn set_variadic(recv: &Value, args: &[Value], tag: i64) -> Result<Value, String> {
if args.is_empty() {
return set_method(recv, "copy", &[]);
}
let mut acc = recv.clone();
for a in args {
let other_set = if with_host(|h| h.setlike(a).is_some()) {
a.clone()
} else {
call_builtin_function("set", vec![a.clone()], vec![])?
};
let aligned = host::align_operand(&acc, &other_set)?;
let other_set = aligned.unwrap_or(other_set);
acc = with_host(|h| h.binop(tag, &acc, &other_set))?;
}
Ok(acc)
}
fn resolve_slice_bound(v: &Value) -> Result<Value, String> {
match index_dunder(v)? {
Some(iv) => Ok(iv),
None => Ok(v.clone()),
}
}
fn normalize_slice_bounds(idx: &Value) -> Result<Value, String> {
let parts = with_host(|h| match h.get(idx) {
Some(PyObj::Slice { lo, hi, step }) => Some((lo.clone(), hi.clone(), step.clone())),
_ => None,
});
let (lo, hi, step) = match parts {
Some(t) => t,
None => return Ok(idx.clone()),
};
let needs = with_host(|h| {
[&lo, &hi, &step].iter().any(|b| {
!matches!(b, Value::Undef | Value::Int(_) | Value::Bool(_))
&& !matches!(h.get(b), Some(PyObj::BigInt(_)))
})
});
if !needs {
return Ok(idx.clone());
}
let lo = resolve_slice_bound(&lo)?;
let hi = resolve_slice_bound(&hi)?;
let step = resolve_slice_bound(&step)?;
for b in [&lo, &hi, &step] {
let ok = with_host(|h| {
matches!(b, Value::Undef | Value::Int(_) | Value::Bool(_))
|| matches!(h.get(b), Some(PyObj::BigInt(_)))
});
if !ok {
return Err(host::type_error(
"slice indices must be integers or None or have an __index__ method",
));
}
}
Ok(with_host(|h| h.alloc(PyObj::Slice { lo, hi, step })))
}
fn slice_method(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
if name != "indices" {
return Err(host::type_error(&format!(
"'slice' object has no attribute '{name}'"
)));
}
if args.len() != 1 {
return Err(host::type_error(&format!(
"slice.indices() takes exactly one argument ({} given)",
args.len()
)));
}
let (lo, hi, step) = with_host(|h| match h.get(recv) {
Some(PyObj::Slice { lo, hi, step }) => (lo.clone(), hi.clone(), step.clone()),
_ => (Value::Undef, Value::Undef, Value::Undef),
});
let nv = resolve_slice_bound(&args[0])?;
let n = with_host(|h| h.as_int(&nv))
.ok_or_else(|| host::type_error("'slice' object cannot be interpreted as an integer"))?;
if n < 0 {
return Err("ValueError: length should not be negative".into());
}
let step_i = if matches!(step, Value::Undef) {
1
} else {
let sv = resolve_slice_bound(&step)?;
let s = with_host(|h| h.as_int(&sv))
.ok_or_else(|| host::type_error("slice indices must be integers or None"))?;
if s == 0 {
return Err("ValueError: slice step cannot be zero".into());
}
s
};
let lo = resolve_slice_bound(&lo)?;
let hi = resolve_slice_bound(&hi)?;
let (start, stop) = with_host(|h| h.slice_adjust(&lo, &hi, step_i, n));
Ok(with_host(|h| {
h.new_tuple(vec![
Value::Int(start),
Value::Int(stop),
Value::Int(step_i),
])
}))
}
fn range_method(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
let (start, stop, step) = with_host(|h| match h.get(recv) {
Some(PyObj::Range { start, stop, step }) => (*start, *stop, *step),
_ => (0, 0, 1),
});
let len = host::range_len(start, stop, step);
let arg = arg0(args)?;
let target = with_host(|h| h.as_int(&arg));
let pos = target.and_then(|t| {
if (t - start) % step == 0 {
let k = (t - start) / step;
(k >= 0 && k < len).then_some(k)
} else {
None
}
});
match name {
"count" => Ok(Value::Int(if pos.is_some() { 1 } else { 0 })),
"index" => match pos {
Some(k) => Ok(Value::Int(k)),
None => {
let _ = with_host(|h| h.repr_of(&arg));
Err("ValueError: range.index(x): x not in range".to_string())
}
},
_ => Err(format!(
"AttributeError: 'range' object has no attribute '{name}'"
)),
}
}
fn sequence_index(elems: &[Value], args: &[Value], not_found: &str) -> Result<Value, String> {
let v = arg0(args)?;
let n = elems.len() as i64;
let bound = |a: Option<&Value>, default: i64| -> Result<i64, String> {
let Some(a) = a else { return Ok(default) };
let a = index_dunder(a)?.unwrap_or_else(|| a.clone());
with_host(|h| h.as_slice_index(&a)).ok_or_else(|| {
host::type_error("slice indices must be integers or have an __index__ method")
})
};
let mut start = bound(args.get(1), 0)?;
let mut stop = bound(args.get(2), n)?;
if start < 0 {
start = start.saturating_add(n).max(0);
}
if stop < 0 {
stop = stop.saturating_add(n).max(0);
}
for i in start..stop.min(n) {
if elem_equal(&elems[i as usize], &v)? {
return Ok(Value::Int(i));
}
}
Err(format!("ValueError: {not_found}"))
}
fn tuple_method(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
match name {
"count" if args.len() != 1 => Err(host::type_error(&format!(
"tuple.count() takes exactly one argument ({} given)",
args.len()
))),
"index" if args.len() > 3 => Err(host::type_error(&format!(
"index expected at most 3 arguments, got {}",
args.len()
))),
"index" if args.is_empty() => Err(host::type_error(
"index expected at least 1 argument, got 0",
)),
"count" => {
let v = arg0(args)?;
let elems = with_host(|h| match h.get(recv) {
Some(PyObj::Tuple(l)) => l.clone(),
_ => Vec::new(),
});
let mut n = 0i64;
for e in &elems {
if elem_equal(e, &v)? {
n += 1;
}
}
Ok(Value::Int(n))
}
"index" => {
let elems = with_host(|h| match h.get(recv) {
Some(PyObj::Tuple(l)) => Ok(l.clone()),
_ => Err(host::type_error("not a tuple")),
})?;
sequence_index(&elems, args, "tuple.index(x): x not in tuple")
}
_ => Err(format!(
"AttributeError: 'tuple' object has no attribute '{name}'"
)),
}
}
fn is_int_like(v: &Value) -> bool {
matches!(v, Value::Int(_) | Value::Bool(_))
|| with_host(|h| matches!(h.get(v), Some(PyObj::BigInt(_))))
}
fn is_num_like(v: &Value) -> bool {
is_int_like(v) || matches!(v, Value::Float(_))
}
fn to_int_value(recv: &Value) -> Value {
match recv {
Value::Bool(b) => Value::Int(*b as i64),
other => other.clone(),
}
}
fn num_dunder(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
use host::binop as bo;
let recv_float = matches!(recv, Value::Float(_));
let ni = || Ok(with_host(|h| h.alloc(PyObj::NotImplemented)));
let b = args.first().cloned().unwrap_or(Value::Undef);
let accepts = |v: &Value| {
if recv_float {
is_num_like(v)
} else {
is_int_like(v)
}
};
match name {
"__abs__" => call_builtin_function("abs", vec![recv.clone()], vec![]),
"__neg__" => with_host(|h| h.arith(NumOp::Sub, &Value::Int(0), recv)),
"__pos__" => Ok(to_int_value(recv)),
"__invert__" => {
let x = with_host(|h| h.big_val(recv)).unwrap_or_default();
Ok(with_host(|h| {
h.norm_big(-(x + num_bigint::BigInt::from(1)))
}))
}
"__index__" => Ok(to_int_value(recv)),
"__int__" | "__trunc__" => call_builtin_function("int", vec![recv.clone()], vec![]),
"__float__" => call_builtin_function("float", vec![recv.clone()], vec![]),
"__floor__" => match recv {
Value::Float(f) => call_builtin_function("math.floor", vec![Value::Float(*f)], vec![]),
_ => Ok(to_int_value(recv)),
},
"__ceil__" => match recv {
Value::Float(f) => call_builtin_function("math.ceil", vec![Value::Float(*f)], vec![]),
_ => Ok(to_int_value(recv)),
},
"__round__" => {
let mut a = vec![recv.clone()];
if let Some(nd) = args.first() {
if !matches!(nd, Value::Undef) {
a.push(nd.clone());
}
}
call_builtin_function("round", a, vec![])
}
"__bool__" => Ok(Value::Bool(with_host(|h| h.truthy(recv)))),
"__repr__" => Ok(with_host(|h| {
let s = h.repr_of(recv);
h.new_str(s)
})),
"__str__" => Ok(with_host(|h| {
let s = h.str_of(recv);
h.new_str(s)
})),
"__hash__" => {
let k = with_host(|h| h.to_key(recv))?;
Ok(Value::Int(hash_key(&k)))
}
"__eq__" | "__ne__" => {
if !accepts(&b) {
return ni();
}
let eq = with_host(|h| h.equal(recv, &b));
Ok(Value::Bool(if name == "__eq__" { eq } else { !eq }))
}
"__lt__" | "__le__" | "__gt__" | "__ge__" => {
if !accepts(&b) {
return ni();
}
let op = match name {
"__lt__" => NumOp::Lt,
"__le__" => NumOp::Le,
"__gt__" => NumOp::Gt,
_ => NumOp::Ge,
};
with_host(|h| h.compare(op, recv, &b))
}
"__add__" | "__sub__" | "__mul__" => {
if !accepts(&b) {
return ni();
}
let op = match name {
"__add__" => NumOp::Add,
"__sub__" => NumOp::Sub,
_ => NumOp::Mul,
};
with_host(|h| h.arith(op, recv, &b))
}
"__truediv__" | "__floordiv__" | "__mod__" | "__pow__" | "__and__" | "__or__"
| "__xor__" | "__lshift__" | "__rshift__" => {
if !accepts(&b) {
return ni();
}
with_host(|h| h.binop(binop_dunder_tag(name), recv, &b))
}
"__divmod__" => {
if !accepts(&b) {
return ni();
}
let q = with_host(|h| h.binop(bo::FLOORDIV, recv, &b))?;
let r = with_host(|h| h.binop(bo::MOD, recv, &b))?;
Ok(with_host(|h| h.new_tuple(vec![q, r])))
}
"__radd__" | "__rsub__" | "__rmul__" => {
if !accepts(&b) {
return ni();
}
let op = match name {
"__radd__" => NumOp::Add,
"__rsub__" => NumOp::Sub,
_ => NumOp::Mul,
};
with_host(|h| h.arith(op, &b, recv))
}
"__rtruediv__" | "__rfloordiv__" | "__rmod__" | "__rpow__" | "__rand__" | "__ror__"
| "__rxor__" | "__rlshift__" | "__rrshift__" => {
if !accepts(&b) {
return ni();
}
let fwd = format!("__{}", &name[3..]);
with_host(|h| h.binop(binop_dunder_tag(&fwd), &b, recv))
}
"__rdivmod__" => {
if !accepts(&b) {
return ni();
}
let q = with_host(|h| h.binop(bo::FLOORDIV, &b, recv))?;
let r = with_host(|h| h.binop(bo::MOD, &b, recv))?;
Ok(with_host(|h| h.new_tuple(vec![q, r])))
}
_ => Err(with_host(|h| {
format!(
"AttributeError: '{}' object has no attribute '{name}'",
h.type_name(recv)
)
})),
}
}
fn deque_subclass_op(op: NumOp, a: &Value, b: &Value) -> Option<Result<Value, String>> {
let is_deque_sub = |v: &Value, dunder: &str| {
host::collections_subclass_of(v).is_some_and(|(base, _, class)| {
base == "collections.deque"
&& with_host(|h| h.class_lookup(&class, dunder).is_none())
})
};
match op {
NumOp::Add if is_deque_sub(a, "__add__") => Some((|| {
let other_is_deque = with_host(|h| matches!(h.get(b), Some(PyObj::Deque { .. })))
|| host::collections_subclass_of(b).is_some_and(|(base, ..)| base == "collections.deque");
if !other_is_deque {
return Err(with_host(|h| {
host::type_error(&format!(
"can only concatenate deque (not \"{}\") to deque",
h.tp_name(b)
))
}));
}
let new = host::collections_subclass_copy(a).unwrap()?;
if let Some((_, payload, _)) = host::collections_subclass_of(&new) {
call_type_method(&payload, "extend", vec![b.clone()], vec![])?;
}
Ok(new)
})()),
NumOp::Mul if is_deque_sub(a, "__mul__") || is_deque_sub(b, "__rmul__") => {
let (d, n) = if is_deque_sub(a, "__mul__") { (a, b) } else { (b, a) };
let has_index = with_host(|h| {
matches!(h.get(n), Some(PyObj::Instance(i)) if instance_has(h, i, "__index__"))
});
if !is_int_like(n) && !has_index {
return None;
}
Some((|| {
let new = host::collections_subclass_copy(d).unwrap()?;
if let Some((_, payload, _)) = host::collections_subclass_of(&new) {
call_type_method(&payload, "__imul__", vec![n.clone()], vec![])?;
}
Ok(new)
})())
}
_ => None,
}
}
fn mapping_subclass_or(a: &Value, b: &Value) -> Option<Result<Value, String>> {
let subclass = |v: &Value, dunder: &str| {
host::collections_subclass_of(v).filter(|(base, _, class)| {
matches!(*base, "collections.OrderedDict" | "collections.defaultdict")
&& with_host(|h| h.class_lookup(class, dunder).is_none())
})
};
let is_dict = |v: &Value| {
with_host(|h| matches!(h.get(v), Some(PyObj::Dict(_))))
|| host::subclass_payload(v, "__getitem__")
.is_some_and(|p| with_host(|h| matches!(h.get(&p), Some(PyObj::Dict(_)))))
};
let (this, other) = match (subclass(a, "__or__"), subclass(b, "__ror__")) {
(Some(s), _) => (s, b),
(None, Some(s)) => (s, a),
_ => return None,
};
if !is_dict(other) {
return None;
}
let (base, payload, class) = this;
Some((|| {
let cls = with_host(|h| h.alloc(PyObj::Class(class)));
let mut args = Vec::new();
if base == "collections.defaultdict" {
args.push(host::dict_meta_of(&payload).and_then(|m| m.factory).unwrap_or(Value::Undef));
}
args.push(a.clone());
let new = host::invoke(&cls, args, vec![])?;
host::call_method(&new, "update", vec![b.clone()], vec![])?;
Ok(new)
})())
}
fn binop_dunder_tag(name: &str) -> i64 {
use host::binop as bo;
match name {
"__truediv__" => bo::DIV,
"__floordiv__" => bo::FLOORDIV,
"__mod__" => bo::MOD,
"__pow__" => bo::POW,
"__and__" => bo::BITAND,
"__or__" => bo::BITOR,
"__xor__" => bo::BITXOR,
"__lshift__" => bo::SHL,
"__rshift__" => bo::SHR,
_ => bo::DIV,
}
}
fn num_method(
recv: &Value,
name: &str,
args: &[Value],
kwargs: &[(String, Value)],
) -> Result<Value, String> {
match name {
"bit_length" => {
let bits = with_host(|h| h.big_val(recv))
.map(|b| b.bits())
.unwrap_or(0);
Ok(Value::Int(bits as i64))
}
"bit_count" => {
let ones: u32 = with_host(|h| h.big_val(recv))
.map(|b| b.to_bytes_le().1.iter().map(|byte| byte.count_ones()).sum())
.unwrap_or(0);
Ok(Value::Int(ones as i64))
}
"to_bytes" => int_to_bytes(recv, args, kwargs),
"is_integer" => Ok(Value::Bool(match recv {
Value::Float(f) => f.fract() == 0.0,
Value::Int(_) | Value::Bool(_) => true,
_ => false,
})),
"as_integer_ratio" => {
let (num, den) = match recv {
Value::Float(f) => float_as_integer_ratio(*f)?,
_ => (
with_host(|h| h.big_val(recv)).unwrap_or_default(),
num_bigint::BigInt::from(1),
),
};
let n = with_host(|h| h.norm_big(num));
let d = with_host(|h| h.norm_big(den));
Ok(with_host(|h| h.new_tuple(vec![n, d])))
}
"hex" => match recv {
Value::Float(f) => Ok(new_str(float_hex(*f))),
_ => Err(with_host(|h| {
format!(
"AttributeError: '{}' object has no attribute '{name}'",
h.type_name(recv)
)
})),
},
"conjugate" => Ok(match recv {
Value::Bool(b) => Value::Int(*b as i64),
_ => recv.clone(),
}),
_ => Err(with_host(|h| {
format!(
"AttributeError: '{}' object has no attribute '{name}'",
h.type_name(recv)
)
})),
}
}
fn float_hex(f: f64) -> String {
if f.is_nan() {
return "nan".into();
}
if f.is_infinite() {
return if f < 0.0 { "-inf".into() } else { "inf".into() };
}
let bits = f.to_bits();
let sign = if bits >> 63 == 1 { "-" } else { "" };
let raw_exp = ((bits >> 52) & 0x7ff) as i64;
let frac = bits & 0x000f_ffff_ffff_ffff;
if raw_exp == 0 && frac == 0 {
return format!("{sign}0x0.0p+0");
}
let (lead, exp) = if raw_exp == 0 {
(0, -1022) } else {
(1, raw_exp - 1023)
};
let digits = format!("{frac:013x}");
let sign_char = if exp >= 0 { '+' } else { '-' };
format!("{sign}0x{lead}.{digits}p{sign_char}{}", exp.abs())
}
fn int_to_bytes(recv: &Value, args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
let length = match args.first().cloned().or_else(|| kw_get(kwargs, "length")) {
Some(v) => {
let n = match with_host(|h| h.index_fit(&v)) {
host::IndexFit::Fits(n) => n,
host::IndexFit::TooLarge(_) => {
return Err(
"OverflowError: Python int too large to convert to C ssize_t".to_string(),
)
}
host::IndexFit::NotInt => {
return Err(host::type_error("'length' must be an integer"))
}
};
if n < 0 {
return Err("ValueError: length argument must be non-negative".into());
}
n as usize
}
None => 1,
};
let byteorder = match args.get(1).cloned().or_else(|| kw_get(kwargs, "byteorder")) {
Some(v) => with_host(|h| h.as_str(&v)).unwrap_or_default(),
None => "big".into(),
};
let little = match byteorder.as_str() {
"big" => false,
"little" => true,
_ => return Err("ValueError: byteorder must be either 'little' or 'big'".into()),
};
let signed = kw_get(kwargs, "signed")
.map(|v| py_bool(&v).unwrap_or(false))
.unwrap_or(false);
let val = with_host(|h| h.big_val(recv)).unwrap_or_default();
let mut be: Vec<u8> = if signed {
let minimal = val.to_signed_bytes_be();
if minimal.len() > length {
return Err("OverflowError: int too big to convert".into());
}
let pad = if val.sign() == num_bigint::Sign::Minus {
0xFF
} else {
0x00
};
let mut v = vec![pad; length - minimal.len()];
v.extend_from_slice(&minimal);
v
} else {
if val.sign() == num_bigint::Sign::Minus {
return Err("OverflowError: can't convert negative int to unsigned".into());
}
let minimal = val.to_bytes_be().1;
let minimal: &[u8] = if minimal == [0] { &[] } else { &minimal };
if minimal.len() > length {
return Err("OverflowError: int too big to convert".into());
}
let mut v = vec![0u8; length - minimal.len()];
v.extend_from_slice(minimal);
v
};
if little {
be.reverse();
}
Ok(with_host(|h| h.alloc(PyObj::Bytes(be))))
}
fn float_fromhex(s: &str) -> Result<f64, String> {
let err = || "ValueError: invalid hexadecimal floating-point string".to_string();
let t = s.trim();
let low = t.to_ascii_lowercase();
let (neg, rest) = match low.strip_prefix('-') {
Some(r) => (true, r),
None => (false, low.strip_prefix('+').unwrap_or(low.as_str())),
};
match rest {
"inf" | "infinity" => {
return Ok(if neg {
f64::NEG_INFINITY
} else {
f64::INFINITY
})
}
"nan" => return Ok(host::fresh_nan(neg)),
_ => {}
}
let body = rest.strip_prefix("0x").unwrap_or(rest);
let (mant, exp_str) = match body.split_once('p') {
Some((m, e)) => (m, Some(e)),
None => (body, None),
};
let (int_part, frac_part) = match mant.split_once('.') {
Some((i, f)) => (i, f),
None => (mant, ""),
};
if int_part.is_empty() && frac_part.is_empty() {
return Err(err());
}
let mut m = num_bigint::BigInt::from(0);
for c in int_part.chars().chain(frac_part.chars()) {
let d = c.to_digit(16).ok_or_else(err)?;
m = m * 16 + d;
}
let p: i64 = match exp_str {
Some(e) => e.parse().map_err(|_| err())?,
None => 0,
};
let e = p - 4 * frac_part.len() as i64;
let val = big_scaled_to_f64(&m, e);
Ok(if neg { -val } else { val })
}
fn big_scaled_to_f64(m: &num_bigint::BigInt, e: i64) -> f64 {
use num_traits::ToPrimitive;
if m.sign() == num_bigint::Sign::NoSign {
return 0.0;
}
let bits = m.bits() as i64;
let mut m = m.clone();
let mut e = e;
let keep = 54;
if bits > keep {
let drop = bits - keep;
let mask = (num_bigint::BigInt::from(1) << (drop as usize)) - 1;
let low: num_bigint::BigInt = &m & &mask;
m >>= drop as usize;
if low.sign() != num_bigint::Sign::NoSign
&& (&m & num_bigint::BigInt::from(1)).sign() == num_bigint::Sign::NoSign
{
m += 1;
}
e += drop;
}
let base = m.to_f64().unwrap_or(f64::INFINITY);
ldexp(base, e)
}
fn ldexp(base: f64, exp: i64) -> f64 {
if base == 0.0 || !base.is_finite() {
return base;
}
let mut b = base;
let mut e = exp;
while e > 1000 {
b *= 2f64.powi(1000);
e -= 1000;
}
while e < -1000 {
b *= 2f64.powi(-1000);
e += 1000;
}
b * 2f64.powi(e as i32)
}
fn int_from_bytes(args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
let src = args
.first()
.ok_or_else(|| host::type_error("from_bytes() missing required argument 'bytes'"))?;
let mut bytes: Vec<u8> = match with_host(|h| match h.get(src) {
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => Some(b.clone()),
_ => None,
}) {
Some(b) => b,
None => {
let items = host::iter_vec(src)?;
let mut v = Vec::with_capacity(items.len());
for it in items {
let n = with_host(|h| h.as_int(&it))
.ok_or_else(|| host::type_error("'bytes' must be an iterable of integers"))?;
if !(0..=255).contains(&n) {
return Err("ValueError: bytes must be in range(0, 256)".into());
}
v.push(n as u8);
}
v
}
};
let byteorder = match args.get(1).cloned().or_else(|| kw_get(kwargs, "byteorder")) {
Some(v) => with_host(|h| h.as_str(&v)).unwrap_or_default(),
None => "big".into(),
};
let little = match byteorder.as_str() {
"big" => false,
"little" => true,
_ => return Err("ValueError: byteorder must be either 'little' or 'big'".into()),
};
let signed = kw_get(kwargs, "signed")
.map(|v| py_bool(&v).unwrap_or(false))
.unwrap_or(false);
if little {
bytes.reverse(); }
let big = if signed {
num_bigint::BigInt::from_signed_bytes_be(&bytes)
} else {
num_bigint::BigInt::from_bytes_be(num_bigint::Sign::Plus, &bytes)
};
Ok(with_host(|h| h.norm_big(big)))
}
fn float_as_integer_ratio(f: f64) -> Result<(num_bigint::BigInt, num_bigint::BigInt), String> {
use num_bigint::BigInt;
use num_integer::Integer;
if f.is_nan() {
return Err("ValueError: cannot convert NaN to integer ratio".into());
}
if f.is_infinite() {
return Err("OverflowError: cannot convert Infinity to integer ratio".into());
}
if f == 0.0 {
return Ok((BigInt::from(0), BigInt::from(1)));
}
let bits = f.to_bits();
let sign: i8 = if bits >> 63 == 1 { -1 } else { 1 };
let raw_exp = ((bits >> 52) & 0x7ff) as i64;
let raw_mant = bits & 0x000f_ffff_ffff_ffff;
let (mant, exp) = if raw_exp == 0 {
(raw_mant, -1074) } else {
(raw_mant | 0x0010_0000_0000_0000, raw_exp - 1075)
};
let mut num = BigInt::from(mant) * sign;
let mut den = BigInt::from(1);
if exp >= 0 {
num <<= exp as usize;
} else {
den <<= (-exp) as usize;
}
let g = num.gcd(&den);
Ok((num / &g, den / &g))
}
fn complex_method(recv: &Value, name: &str) -> Result<Value, String> {
match name {
"conjugate" => with_host(|h| match h.get(recv) {
Some(PyObj::Complex(r, i)) => {
let (r, i) = (*r, *i);
Ok(h.alloc(PyObj::Complex(r, -i)))
}
_ => Err(host::type_error(
"descriptor 'conjugate' requires a 'complex' object",
)),
}),
"__abs__" => with_host(|h| match h.get(recv) {
Some(PyObj::Complex(r, i)) => Ok(Value::Float((r * r + i * i).sqrt())),
_ => Err(host::type_error(
"descriptor '__abs__' requires a 'complex'",
)),
}),
_ => Err(format!(
"AttributeError: 'complex' object has no attribute '{name}'"
)),
}
}
fn recv_bytes(recv: &Value) -> Vec<u8> {
with_host(|h| match h.get(recv) {
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => b.clone(),
_ => vec![],
})
}
fn arg_bytes_like(v: &Value) -> Option<Vec<u8>> {
let obj = with_host(|h| match h.get(v) {
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => Some(b.clone()),
_ => None,
});
if obj.is_some() {
return obj;
}
match v {
Value::Int(n) if (0..=255).contains(n) => Some(vec![*n as u8]),
_ => None,
}
}
pub(crate) fn as_bytes_object(v: &Value) -> Result<Option<Vec<u8>>, String> {
with_host(|h| match h.get(v) {
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => Ok(Some(b.clone())),
Some(PyObj::Memoryview { .. }) => h.mv_bytes(v).map(Some),
_ => Ok(None),
})
}
fn is_ascii_ws(b: u8) -> bool {
matches!(b, b' ' | b'\t' | b'\n' | b'\r' | 0x0b | 0x0c)
}
fn mk_bytes(is_ba: bool, v: Vec<u8>) -> Value {
with_host(|h| {
if is_ba {
h.alloc(PyObj::Bytearray(v))
} else {
h.alloc(PyObj::Bytes(v))
}
})
}
fn resolve_start_end(len: usize, args: &[Value], from: usize) -> (usize, usize) {
let n = len as i64;
let norm = |v: &Value, default: i64, cap: i64| -> i64 {
match with_host(|h| h.as_int(v)) {
Some(i) => {
let k = if i < 0 { i + n } else { i };
k.clamp(0, cap)
}
None => default,
}
};
let start = args.get(from).map(|v| norm(v, 0, i64::MAX)).unwrap_or(0);
let end = args.get(from + 1).map(|v| norm(v, n, n)).unwrap_or(n);
(start as usize, end as usize)
}
fn slice_find<T: PartialEq>(
hay: &[T],
needle: &[T],
start: usize,
end: usize,
reverse: bool,
) -> Option<usize> {
let end = end.min(hay.len());
if start > end {
return None;
}
let region = &hay[start..end];
if needle.is_empty() {
return Some(if reverse { end } else { start });
}
if needle.len() > region.len() {
return None;
}
if reverse {
region
.windows(needle.len())
.rposition(|w| w == needle)
.map(|p| p + start)
} else {
region
.windows(needle.len())
.position(|w| w == needle)
.map(|p| p + start)
}
}
fn count_range<T: PartialEq>(hay: &[T], needle: &[T], start: usize, end: usize) -> usize {
let end = end.min(hay.len());
if start > end {
return 0;
}
let region = &hay[start..end];
if needle.is_empty() {
return region.len() + 1;
}
let mut c = 0;
let mut i = 0;
while i + needle.len() <= region.len() {
if ®ion[i..i + needle.len()] == needle {
c += 1;
i += needle.len();
} else {
i += 1;
}
}
c
}
fn split_on_sep(hay: &[u8], sep: &[u8], maxsplit: i64, reverse: bool) -> Vec<Vec<u8>> {
let mut parts: Vec<Vec<u8>> = Vec::new();
if reverse {
let mut end = hay.len();
let mut splits = 0;
loop {
if maxsplit >= 0 && splits >= maxsplit {
break;
}
match slice_find(hay, sep, 0, end, true) {
Some(p) => {
parts.push(hay[p + sep.len()..end].to_vec());
end = p;
splits += 1;
}
None => break,
}
}
parts.push(hay[..end].to_vec());
parts.reverse();
} else {
let mut start = 0;
let mut splits = 0;
loop {
if maxsplit >= 0 && splits >= maxsplit {
break;
}
match slice_find(hay, sep, start, hay.len(), false) {
Some(p) => {
parts.push(hay[start..p].to_vec());
start = p + sep.len();
splits += 1;
}
None => break,
}
}
parts.push(hay[start..].to_vec());
}
parts
}
fn split_ws(hay: &[u8], maxsplit: i64, reverse: bool) -> Vec<Vec<u8>> {
let n = hay.len();
let mut parts: Vec<Vec<u8>> = Vec::new();
if reverse {
let mut end = n;
let mut splits = 0;
loop {
while end > 0 && is_ascii_ws(hay[end - 1]) {
end -= 1;
}
if end == 0 {
break;
}
if maxsplit >= 0 && splits >= maxsplit {
parts.push(hay[..end].to_vec());
break;
}
let mut start = end;
while start > 0 && !is_ascii_ws(hay[start - 1]) {
start -= 1;
}
parts.push(hay[start..end].to_vec());
end = start;
splits += 1;
}
parts.reverse();
} else {
let mut i = 0;
let mut splits = 0;
loop {
while i < n && is_ascii_ws(hay[i]) {
i += 1;
}
if i >= n {
break;
}
if maxsplit >= 0 && splits >= maxsplit {
parts.push(hay[i..].to_vec());
break;
}
let start = i;
while i < n && !is_ascii_ws(hay[i]) {
i += 1;
}
parts.push(hay[start..i].to_vec());
splits += 1;
}
}
parts
}
fn bytes_fromhex(args: &[Value]) -> Result<Vec<u8>, String> {
let s = args
.first()
.and_then(|v| with_host(|h| h.as_str(v)))
.ok_or_else(|| host::type_error("fromhex() argument must be str"))?;
let chars: Vec<char> = s.chars().collect();
let mut out = Vec::new();
let mut i = 0;
let hexval = |c: char| -> Option<u8> { c.to_digit(16).map(|d| d as u8) };
while i < chars.len() {
if chars[i].is_ascii_whitespace() {
i += 1;
continue;
}
let hi = hexval(chars[i]).ok_or_else(|| {
format!("ValueError: non-hexadecimal number found in fromhex() arg at position {i}")
})?;
if i + 1 >= chars.len() || chars[i + 1].is_ascii_whitespace() {
return Err(
"ValueError: fromhex() arg must contain an even number of hexadecimal digits"
.into(),
);
}
let lo = hexval(chars[i + 1]).ok_or_else(|| {
format!(
"ValueError: non-hexadecimal number found in fromhex() arg at position {}",
i + 1
)
})?;
out.push((hi << 4) | lo);
i += 2;
}
Ok(out)
}
fn norm_codec(enc: &str) -> String {
enc.to_lowercase().replace(['-', '_', ' '], "")
}
fn encode_error(
out: &mut Vec<u8>,
chars: &[char],
(start, end): (usize, usize),
errors: &str,
codec: &str,
limit: u32,
) -> Result<(), String> {
let run = &chars[start..end];
match errors {
"ignore" => Ok(()),
"replace" => {
out.extend(std::iter::repeat(b'?').take(run.len()));
Ok(())
}
"backslashreplace" => {
for &c in run {
out.extend_from_slice(unicode_char_escape(c).as_bytes());
}
Ok(())
}
"xmlcharrefreplace" => {
for &c in run {
out.extend_from_slice(format!("&#{};", c as u32).as_bytes());
}
Ok(())
}
"namereplace" => {
for &c in run {
match unicode_names2::name(c) {
Some(name) => out.extend_from_slice(format!("\\N{{{name}}}").as_bytes()),
None => out.extend_from_slice(unicode_char_escape(c).as_bytes()),
}
}
Ok(())
}
_ => Err(crate::excunicode::raise(
crate::excunicode::UnicodeErrorArgs {
class: "UnicodeEncodeError",
encoding: codec.to_string(),
object: crate::excunicode::CodecInput::Str(chars.iter().collect()),
start,
end,
reason: format!("ordinal not in range({limit})"),
},
)),
}
}
fn unicode_char_escape(c: char) -> String {
let n = c as u32;
if n <= 0xff {
format!("\\x{n:02x}")
} else if n <= 0xffff {
format!("\\u{n:04x}")
} else {
format!("\\U{n:08x}")
}
}
fn decode_error(
out: &mut String,
input: &[u8],
(start, end): (usize, usize),
errors: &str,
codec: &str,
reason: &str,
) -> Result<(), String> {
let bad = &input[start..end];
match errors {
"ignore" => Ok(()),
"replace" => {
out.push('\u{fffd}');
Ok(())
}
"backslashreplace" => {
for &b in bad {
out.push_str(&format!("\\x{b:02x}"));
}
Ok(())
}
"strict" => Err(crate::excunicode::raise(
crate::excunicode::UnicodeErrorArgs {
class: "UnicodeDecodeError",
encoding: codec.to_string(),
object: crate::excunicode::CodecInput::Bytes(input.to_vec()),
start,
end,
reason: reason.to_string(),
},
)),
"namereplace" | "xmlcharrefreplace" => Err(
"TypeError: don't know how to handle UnicodeDecodeError in error callback".to_string(),
),
_ => Err(format!(
"LookupError: unknown error handler name '{errors}'"
)),
}
}
pub(crate) fn encode_narrow(
s: &str,
errors: &str,
codec: &str,
limit: u32,
) -> Result<Vec<u8>, String> {
let chars: Vec<char> = s.chars().collect();
let mut out = Vec::with_capacity(chars.len());
let mut i = 0;
while i < chars.len() {
if (chars[i] as u32) < limit {
out.push(chars[i] as u8);
i += 1;
continue;
}
let start = i;
while i < chars.len() && (chars[i] as u32) >= limit {
i += 1;
}
encode_error(&mut out, &chars, (start, i), errors, codec, limit)?;
}
Ok(out)
}
fn encode_str(s: &str, encoding: &str, errors: &str) -> Result<Vec<u8>, String> {
let norm = norm_codec(encoding);
match norm.as_str() {
"utf8" | "u8" | "utf" | "cp65001" => Ok(s.as_bytes().to_vec()),
"ascii" | "usascii" | "646" => encode_narrow(s, errors, "ascii", 128),
"latin1" | "latin" | "iso88591" | "8859" | "cp819" | "l1" => {
encode_narrow(s, errors, "latin-1", 256)
}
"utf16"
| "utf16le"
| "utf16be"
| "u16"
| "unicodelittleunmarked"
| "unicodebigunmarked" => {
let be = norm.ends_with("be") || norm == "unicodebigunmarked";
let bom = matches!(norm.as_str(), "utf16" | "u16");
let mut out = Vec::with_capacity(s.len() * 2 + 2);
let push = |u: u16, out: &mut Vec<u8>| {
let b = if be { u.to_be_bytes() } else { u.to_le_bytes() };
out.extend_from_slice(&b);
};
if bom {
push(0xfeff, &mut out);
}
let mut buf = [0u16; 2];
for c in s.chars() {
for &u in c.encode_utf16(&mut buf).iter() {
push(u, &mut out);
}
}
Ok(out)
}
"utf32" | "utf32le" | "utf32be" | "u32" => {
let be = norm.ends_with("be");
let bom = matches!(norm.as_str(), "utf32" | "u32");
let mut out = Vec::with_capacity(s.len() * 4 + 4);
let push = |u: u32, out: &mut Vec<u8>| {
let b = if be { u.to_be_bytes() } else { u.to_le_bytes() };
out.extend_from_slice(&b);
};
if bom {
push(0xfeff, &mut out);
}
for c in s.chars() {
push(c as u32, &mut out);
}
Ok(out)
}
_ => Err(format!("LookupError: unknown encoding: {encoding}")),
}
}
fn decode_bytes(bytes: &[u8], args: &[Value]) -> Result<Value, String> {
let enc = args
.first()
.and_then(|v| with_host(|h| h.as_str(v)))
.unwrap_or_else(|| "utf-8".into());
let errors = args
.get(1)
.and_then(|v| with_host(|h| h.as_str(v)))
.unwrap_or_else(|| "strict".into());
let norm = norm_codec(&enc);
let s = match norm.as_str() {
"latin1" | "latin" | "iso88591" | "l1" | "cp1252" | "8859" | "cp819" => {
bytes.iter().map(|&b| b as char).collect::<String>()
}
"utf16" | "utf16le" | "utf16be" | "u16" => decode_utf16(bytes, &norm, &errors)?,
"utf32" | "utf32le" | "utf32be" | "u32" => decode_utf32(bytes, &norm, &errors)?,
"ascii" | "usascii" | "646" => {
let mut out = String::with_capacity(bytes.len());
for (pos, &b) in bytes.iter().enumerate() {
if b < 0x80 {
out.push(b as char);
} else {
decode_error(
&mut out,
bytes,
(pos, pos + 1),
&errors,
"ascii",
"ordinal not in range(128)",
)?;
}
}
out
}
"utf8" | "u8" | "utf" | "cp65001" => utf8_decode_errors(bytes, &errors)?,
_ => return Err(format!("LookupError: unknown encoding: {enc}")),
};
Ok(new_str(s))
}
fn decode_utf16(bytes: &[u8], norm: &str, errors: &str) -> Result<String, String> {
let mut be = norm.ends_with("be");
let mut start = 0;
if norm == "utf16" || norm == "u16" {
if bytes.starts_with(&[0xff, 0xfe]) {
start = 2;
} else if bytes.starts_with(&[0xfe, 0xff]) {
be = true;
start = 2;
}
}
let body = &bytes[start..];
let unit = |i: usize| {
let pair = [body[i], body[i + 1]];
if be {
u16::from_be_bytes(pair)
} else {
u16::from_le_bytes(pair)
}
};
let mut out = String::with_capacity(body.len() / 2);
let label = if be { "utf-16-be" } else { "utf-16-le" };
let mut i = 0;
while i + 1 < body.len() {
let (u, pos) = (unit(i), start + i);
match u {
0xd800..=0xdbff if i + 3 >= body.len() => {
let end = bytes.len();
decode_error(
&mut out,
bytes,
(pos, end),
errors,
label,
"unexpected end of data",
)?;
return Ok(out);
}
0xd800..=0xdbff => {
let lo = unit(i + 2);
if (0xdc00..=0xdfff).contains(&lo) {
let cp = 0x10000 + ((u32::from(u) - 0xd800) << 10) + (u32::from(lo) - 0xdc00);
out.extend(char::from_u32(cp));
i += 4;
} else {
let reason = "illegal UTF-16 surrogate";
decode_error(&mut out, bytes, (pos, pos + 2), errors, label, reason)?;
i += 2;
}
}
0xdc00..=0xdfff => {
decode_error(
&mut out,
bytes,
(pos, pos + 2),
errors,
label,
"illegal encoding",
)?;
i += 2;
}
_ => {
out.extend(char::from_u32(u32::from(u)));
i += 2;
}
}
}
if i < body.len() {
let end = bytes.len();
decode_error(
&mut out,
bytes,
(end - 1, end),
errors,
label,
"truncated data",
)?;
}
Ok(out)
}
fn decode_utf32(bytes: &[u8], norm: &str, errors: &str) -> Result<String, String> {
let mut be = norm.ends_with("be");
let mut start = 0;
if norm == "utf32" || norm == "u32" {
if bytes.starts_with(&[0xff, 0xfe, 0x00, 0x00]) {
start = 4;
} else if bytes.starts_with(&[0x00, 0x00, 0xfe, 0xff]) {
be = true;
start = 4;
}
}
let body = &bytes[start..];
let mut out = String::with_capacity(body.len() / 4);
let label = if be { "utf-32-be" } else { "utf-32-le" };
for (n, c) in body.chunks(4).enumerate() {
let pos = start + n * 4;
if c.len() < 4 {
decode_error(
&mut out,
bytes,
(pos, bytes.len()),
errors,
label,
"truncated data",
)?;
break;
}
let word = if be {
u32::from_be_bytes([c[0], c[1], c[2], c[3]])
} else {
u32::from_le_bytes([c[0], c[1], c[2], c[3]])
};
let reason = if word > 0x10ffff {
"code point not in range(0x110000)"
} else if (0xd800..0xe000).contains(&word) {
"code point in surrogate code point range(0xd800, 0xe000)"
} else {
out.extend(char::from_u32(word));
continue;
};
decode_error(&mut out, bytes, (pos, pos + 4), errors, label, reason)?;
}
Ok(out)
}
fn is_utf8_lead(b: u8) -> bool {
(0xc2..=0xf4).contains(&b)
}
fn utf8_decode_errors(bytes: &[u8], errors: &str) -> Result<String, String> {
let mut out = String::with_capacity(bytes.len());
let mut rest = bytes;
loop {
match std::str::from_utf8(rest) {
Ok(s) => {
out.push_str(s);
break;
}
Err(e) => {
let valid = e.valid_up_to();
out.push_str(std::str::from_utf8(&rest[..valid]).unwrap());
let skip = e.error_len().unwrap_or(rest.len() - valid);
let reason = if e.error_len().is_none() {
"unexpected end of data"
} else if is_utf8_lead(rest[valid]) {
"invalid continuation byte"
} else {
"invalid start byte"
};
let pos = bytes.len() - rest.len() + valid;
decode_error(&mut out, bytes, (pos, pos + skip), errors, "utf-8", reason)?;
rest = &rest[valid + skip..];
if rest.is_empty() {
break;
}
}
}
}
Ok(out)
}
fn bytes_method(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
bytes_common_method(recv, false, name, args)
}
fn memoryview_method(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
if matches!(name, "release" | "__exit__") {
with_host(|h| {
if let Some(PyObj::Memoryview { released, .. }) = h.get_mut(recv) {
*released = true;
}
});
return Ok(Value::Undef);
}
let bytes = with_host(|h| h.mv_bytes(recv))?;
match name {
"__enter__" => Ok(recv.clone()),
"tobytes" => Ok(with_host(|h| h.alloc(PyObj::Bytes(bytes)))),
"tolist" => Ok(with_host(|h| {
let items = bytes.iter().map(|&b| Value::Int(b as i64)).collect();
h.new_list(items)
})),
"hex" => {
let tmp = with_host(|h| h.alloc(PyObj::Bytes(bytes)));
bytes_method(&tmp, "hex", args)
}
_ => Err(format!(
"AttributeError: 'memoryview' object has no attribute '{name}'"
)),
}
}
fn bytearray_method(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
match name {
"append" => {
let a0 = arg0(args)?;
let n = with_host(|h| h.as_int(&a0))
.ok_or_else(|| host::type_error("an integer is required"))?;
if !(0..=255).contains(&n) {
return Err("ValueError: byte must be in range(0, 256)".into());
}
with_host(|h| {
if let Some(PyObj::Bytearray(b)) = h.get_mut(recv) {
b.push(n as u8);
}
});
Ok(Value::Undef)
}
"extend" => {
let add = arg0(args)?;
let extra = collect_bytes(&add)?;
with_host(|h| {
if let Some(PyObj::Bytearray(b)) = h.get_mut(recv) {
b.extend_from_slice(&extra);
}
});
Ok(Value::Undef)
}
"insert" => {
let idx_v = arg0(args)?;
let val_v = args.get(1).cloned().unwrap_or(Value::Undef);
let idx = with_host(|h| h.as_int(&idx_v)).unwrap_or(0);
let n = with_host(|h| h.as_int(&val_v))
.ok_or_else(|| host::type_error("an integer is required"))?;
if !(0..=255).contains(&n) {
return Err("ValueError: byte must be in range(0, 256)".into());
}
with_host(|h| {
if let Some(PyObj::Bytearray(b)) = h.get_mut(recv) {
let len = b.len() as i64;
let i = (if idx < 0 { idx + len } else { idx }).clamp(0, len) as usize;
b.insert(i, n as u8);
}
});
Ok(Value::Undef)
}
"pop" => {
let got = with_host(|h| match h.get_mut(recv) {
Some(PyObj::Bytearray(b)) => b.pop(),
_ => None,
});
got.map(|x| Value::Int(x as i64))
.ok_or_else(|| "IndexError: pop from empty bytearray".into())
}
"reverse" => {
with_host(|h| {
if let Some(PyObj::Bytearray(b)) = h.get_mut(recv) {
b.reverse();
}
});
Ok(Value::Undef)
}
"remove" => {
let a0 = arg0(args)?;
let n = with_host(|h| h.as_int(&a0)).unwrap_or(-1);
let removed = with_host(|h| {
if let Some(PyObj::Bytearray(b)) = h.get_mut(recv) {
if let Some(pos) = b.iter().position(|&x| x as i64 == n) {
b.remove(pos);
return true;
}
}
false
});
if removed {
Ok(Value::Undef)
} else {
Err("ValueError: value not found in bytearray".into())
}
}
"clear" => {
with_host(|h| {
if let Some(PyObj::Bytearray(b)) = h.get_mut(recv) {
b.clear();
}
});
Ok(Value::Undef)
}
_ => bytes_common_method(recv, true, name, args),
}
}
fn bytes_common_method(
recv: &Value,
is_ba: bool,
name: &str,
args: &[Value],
) -> Result<Value, String> {
let bytes = recv_bytes(recv);
let tname = if is_ba { "bytearray" } else { "bytes" };
let need_sub = |i: usize| -> Result<Vec<u8>, String> {
args.get(i)
.map(as_bytes_object)
.transpose()?
.flatten()
.ok_or_else(|| host::type_error("a bytes-like object is required"))
};
let find_needle = || -> Result<Vec<u8>, String> {
args.first()
.and_then(arg_bytes_like)
.ok_or_else(|| host::type_error("argument should be integer or bytes-like object"))
};
match name {
"decode" => decode_bytes(&bytes, args),
"hex" => {
match args.first() {
None => Ok(new_str(bytes.iter().map(|b| format!("{b:02x}")).collect())),
Some(sep_v) => {
let sep = with_host(|h| h.as_str(sep_v))
.ok_or_else(|| host::type_error("sep must be str or bytes"))?;
let group = args
.get(1)
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(1);
if group == 0 {
return Err("ValueError: bytes_per_sep must not be zero".into());
}
let g = group.unsigned_abs() as usize;
let n = bytes.len();
let mut out = String::with_capacity(n * 2 + n);
for (i, b) in bytes.iter().enumerate() {
if i > 0 {
let boundary = if group > 0 {
(n - i) % g == 0
} else {
i % g == 0
};
if boundary {
out.push_str(&sep);
}
}
out.push_str(&format!("{b:02x}"));
}
Ok(new_str(out))
}
}
}
"fromhex" => Ok(mk_bytes(is_ba, bytes_fromhex(args)?)),
"upper" => Ok(mk_bytes(is_ba, bytes.to_ascii_uppercase())),
"lower" => Ok(mk_bytes(is_ba, bytes.to_ascii_lowercase())),
"find" | "rfind" => {
let sub = find_needle()?;
let (start, end) = resolve_start_end(bytes.len(), args, 1);
let p = slice_find(&bytes, &sub, start, end, name == "rfind");
Ok(Value::Int(p.map(|x| x as i64).unwrap_or(-1)))
}
"index" | "rindex" => {
let sub = find_needle()?;
let (start, end) = resolve_start_end(bytes.len(), args, 1);
match slice_find(&bytes, &sub, start, end, name == "rindex") {
Some(p) => Ok(Value::Int(p as i64)),
None => Err("ValueError: subsection not found".into()),
}
}
"count" => {
let sub = find_needle()?;
let (start, end) = resolve_start_end(bytes.len(), args, 1);
Ok(Value::Int(count_range(&bytes, &sub, start, end) as i64))
}
"startswith" | "endswith" => {
let (start, end) = resolve_start_end(bytes.len(), args, 1);
if start > bytes.len() {
return Ok(Value::Bool(false));
}
let region = &bytes[start..end.max(start).min(bytes.len())];
let prefixes = match args.first() {
Some(v) => bytes_prefix_tuple(v)?,
None => return Err(host::type_error("startswith first arg must be bytes-like")),
};
let hit = prefixes.iter().any(|p| {
if name == "startswith" {
region.starts_with(p)
} else {
region.ends_with(p)
}
});
Ok(Value::Bool(hit))
}
"split" | "rsplit" => {
let reverse = name == "rsplit";
let maxsplit = args
.get(1)
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(-1);
let sep_arg = args.first().filter(|v| !matches!(v, Value::Undef));
let parts = match sep_arg {
None => split_ws(&bytes, maxsplit, reverse),
Some(v) => {
let sep = as_bytes_object(v)?
.ok_or_else(|| host::type_error("must be str or None, not int"))?;
if sep.is_empty() {
return Err("ValueError: empty separator".into());
}
split_on_sep(&bytes, &sep, maxsplit, reverse)
}
};
let items: Vec<Value> = parts.into_iter().map(|p| mk_bytes(is_ba, p)).collect();
Ok(with_host(|h| h.new_list(items)))
}
"splitlines" => {
let keepends = args
.first()
.map(|v| with_host(|h| h.truthy(v)))
.unwrap_or(false);
let items: Vec<Value> = split_lines(&bytes, keepends)
.into_iter()
.map(|p| mk_bytes(is_ba, p))
.collect();
Ok(with_host(|h| h.new_list(items)))
}
"join" => {
let sep = bytes.clone();
let items = host::iter_vec(&arg0(args)?)?;
let mut out = Vec::new();
for (i, it) in items.iter().enumerate() {
if i > 0 {
out.extend_from_slice(&sep);
}
let piece = as_bytes_object(it)?.ok_or_else(|| {
host::type_error(&format!(
"sequence item {i}: expected a bytes-like object, {} found",
with_host(|h| h.type_name(it))
))
})?;
out.extend_from_slice(&piece);
}
Ok(mk_bytes(is_ba, out))
}
"replace" => {
let old = need_sub(0)?;
let new = need_sub(1)?;
let count = args
.get(2)
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(-1);
Ok(mk_bytes(is_ba, replace_bytes(&bytes, &old, &new, count)))
}
"strip" | "lstrip" | "rstrip" => {
let chars = args.first().filter(|v| !matches!(v, Value::Undef));
let out = strip_bytes(&bytes, chars, name)?;
Ok(mk_bytes(is_ba, out))
}
"partition" | "rpartition" => {
let sep = need_sub(0)?;
if sep.is_empty() {
return Err("ValueError: empty separator".into());
}
let reverse = name == "rpartition";
let (head, mid, tail) = match slice_find(&bytes, &sep, 0, bytes.len(), reverse) {
Some(p) => (
bytes[..p].to_vec(),
sep.clone(),
bytes[p + sep.len()..].to_vec(),
),
None if reverse => (Vec::new(), Vec::new(), bytes.clone()),
None => (bytes.clone(), Vec::new(), Vec::new()),
};
let t = vec![
mk_bytes(is_ba, head),
mk_bytes(is_ba, mid),
mk_bytes(is_ba, tail),
];
Ok(with_host(|h| h.new_tuple(t)))
}
"removeprefix" => {
let pre = need_sub(0)?;
let out = bytes
.strip_prefix(pre.as_slice())
.map(|s| s.to_vec())
.unwrap_or(bytes);
Ok(mk_bytes(is_ba, out))
}
"removesuffix" => {
let suf = need_sub(0)?;
let out = if suf.is_empty() {
bytes
} else {
bytes
.strip_suffix(suf.as_slice())
.map(|s| s.to_vec())
.unwrap_or(bytes)
};
Ok(mk_bytes(is_ba, out))
}
"swapcase" => {
let out: Vec<u8> = bytes
.iter()
.map(|&b| {
if b.is_ascii_uppercase() {
b.to_ascii_lowercase()
} else if b.is_ascii_lowercase() {
b.to_ascii_uppercase()
} else {
b
}
})
.collect();
Ok(mk_bytes(is_ba, out))
}
"title" => {
let mut out = Vec::with_capacity(bytes.len());
let mut prev_alpha = false;
for &b in &bytes {
if b.is_ascii_alphabetic() {
out.push(if prev_alpha {
b.to_ascii_lowercase()
} else {
b.to_ascii_uppercase()
});
prev_alpha = true;
} else {
out.push(b);
prev_alpha = false;
}
}
Ok(mk_bytes(is_ba, out))
}
"capitalize" => {
let out: Vec<u8> = bytes
.iter()
.enumerate()
.map(|(i, &b)| {
if i == 0 {
b.to_ascii_uppercase()
} else {
b.to_ascii_lowercase()
}
})
.collect();
Ok(mk_bytes(is_ba, out))
}
"zfill" => {
let w = args
.first()
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(0)
.max(0) as usize;
let out = if bytes.len() < w {
let padn = w - bytes.len();
let mut out = Vec::with_capacity(w);
let mut rest = bytes.as_slice();
if let Some((&first, tail)) = bytes.split_first() {
if first == b'+' || first == b'-' {
out.push(first);
rest = tail;
}
}
out.extend(std::iter::repeat(b'0').take(padn));
out.extend_from_slice(rest);
out
} else {
bytes.clone()
};
Ok(mk_bytes(is_ba, out))
}
"expandtabs" => {
let tabsize = args
.first()
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(8)
.max(0) as usize;
Ok(mk_bytes(is_ba, expand_tabs_bytes(&bytes, tabsize)))
}
"center" => Ok(mk_bytes(is_ba, pad_bytes(&bytes, args, 'c')?)),
"ljust" => Ok(mk_bytes(is_ba, pad_bytes(&bytes, args, 'l')?)),
"rjust" => Ok(mk_bytes(is_ba, pad_bytes(&bytes, args, 'r')?)),
"translate" => Ok(mk_bytes(is_ba, bytes_translate(&bytes, args)?)),
"maketrans" => bytes_maketrans(args),
"isalpha" => Ok(Value::Bool(
!bytes.is_empty() && bytes.iter().all(|b| b.is_ascii_alphabetic()),
)),
"isdigit" => Ok(Value::Bool(
!bytes.is_empty() && bytes.iter().all(|b| b.is_ascii_digit()),
)),
"isalnum" => Ok(Value::Bool(
!bytes.is_empty() && bytes.iter().all(|b| b.is_ascii_alphanumeric()),
)),
"isspace" => Ok(Value::Bool(
!bytes.is_empty() && bytes.iter().all(|&b| is_ascii_ws(b)),
)),
"isupper" => Ok(Value::Bool(
bytes.iter().any(|b| b.is_ascii_alphabetic())
&& !bytes.iter().any(|b| b.is_ascii_lowercase()),
)),
"islower" => Ok(Value::Bool(
bytes.iter().any(|b| b.is_ascii_alphabetic())
&& !bytes.iter().any(|b| b.is_ascii_uppercase()),
)),
"istitle" => Ok(Value::Bool(is_bytes_titlecased(&bytes))),
"isascii" => Ok(Value::Bool(bytes.iter().all(|&b| b < 0x80))),
_ => Err(format!(
"AttributeError: '{tname}' object has no attribute '{name}'"
)),
}
}
fn pad_bytes(bytes: &[u8], args: &[Value], mode: char) -> Result<Vec<u8>, String> {
let w = args
.first()
.and_then(|v| with_host(|h| h.as_int(v)))
.unwrap_or(0)
.max(0) as usize;
let fill: u8 = match args.get(1) {
None | Some(Value::Undef) => b' ',
Some(v) => {
let fb = as_bytes_object(v)?.ok_or_else(|| {
host::type_error(&format!(
"{}() argument 2 must be a byte string of length 1, not {}",
match mode {
'l' => "ljust",
'r' => "rjust",
_ => "center",
},
with_host(|h| h.type_name(v))
))
})?;
if fb.len() != 1 {
return Err(host::type_error(&format!(
"{}(): argument 2 must be a byte string of length 1, not a bytes object of length {}",
match mode {
'l' => "ljust",
'r' => "rjust",
_ => "center",
},
fb.len()
)));
}
fb[0]
}
};
if bytes.len() >= w {
return Ok(bytes.to_vec());
}
let total = w - bytes.len();
let mut out = Vec::with_capacity(w);
match mode {
'l' => {
out.extend_from_slice(bytes);
out.extend(std::iter::repeat(fill).take(total));
}
'r' => {
out.extend(std::iter::repeat(fill).take(total));
out.extend_from_slice(bytes);
}
_ => {
let left = total / 2 + (total & w & 1);
let right = total - left;
out.extend(std::iter::repeat(fill).take(left));
out.extend_from_slice(bytes);
out.extend(std::iter::repeat(fill).take(right));
}
}
Ok(out)
}
fn bytes_translate(bytes: &[u8], args: &[Value]) -> Result<Vec<u8>, String> {
let table: Option<Vec<u8>> = match args.first() {
None | Some(Value::Undef) => None,
Some(v) => {
let t = as_bytes_object(v)?
.ok_or_else(|| host::type_error("a bytes-like object is required"))?;
if t.len() != 256 {
return Err("ValueError: translation table must be 256 characters long".into());
}
Some(t)
}
};
let delete: Vec<u8> = match args.get(1) {
None | Some(Value::Undef) => Vec::new(),
Some(v) => {
as_bytes_object(v)?.ok_or_else(|| host::type_error("a bytes-like object is required"))?
}
};
let mut out = Vec::with_capacity(bytes.len());
for &b in bytes {
if delete.contains(&b) {
continue;
}
out.push(match &table {
Some(t) => t[b as usize],
None => b,
});
}
Ok(out)
}
fn bytes_maketrans(args: &[Value]) -> Result<Value, String> {
let frm = args
.first()
.map(as_bytes_object)
.transpose()?
.flatten()
.ok_or_else(|| host::type_error("a bytes-like object is required"))?;
let to = args
.get(1)
.map(as_bytes_object)
.transpose()?
.flatten()
.ok_or_else(|| host::type_error("a bytes-like object is required"))?;
if frm.len() != to.len() {
return Err("ValueError: maketrans arguments must have same length".into());
}
let mut table: Vec<u8> = (0..=255).collect();
for (f, t) in frm.iter().zip(to.iter()) {
table[*f as usize] = *t;
}
Ok(with_host(|h| h.alloc(PyObj::Bytes(table))))
}
fn is_bytes_titlecased(bytes: &[u8]) -> bool {
let mut cased = false;
let mut prev_cased = false;
for &b in bytes {
if b.is_ascii_uppercase() {
if prev_cased {
return false;
}
prev_cased = true;
cased = true;
} else if b.is_ascii_lowercase() {
if !prev_cased {
return false;
}
prev_cased = true;
cased = true;
} else {
prev_cased = false;
}
}
cased
}
fn bytes_prefix_tuple(v: &Value) -> Result<Vec<Vec<u8>>, String> {
if let Some(b) = as_bytes_object(v)? {
return Ok(vec![b]);
}
let is_tuple = with_host(|h| matches!(h.get(v), Some(PyObj::Tuple(_))));
if is_tuple {
let items = host::iter_vec(v)?;
let mut out = Vec::with_capacity(items.len());
for it in items {
let b = as_bytes_object(&it)?
.ok_or_else(|| host::type_error("a bytes-like object is required"))?;
out.push(b);
}
return Ok(out);
}
Err(host::type_error(
"startswith first arg must be bytes or a tuple of bytes",
))
}
fn replace_bytes(hay: &[u8], old: &[u8], new: &[u8], count: i64) -> Vec<u8> {
if count == 0 {
return hay.to_vec();
}
if old.is_empty() {
let slots = hay.len() + 1;
let limit = if count < 0 {
slots
} else {
(count as usize).min(slots)
};
let mut out = Vec::new();
for (i, &b) in hay.iter().enumerate() {
if i < limit {
out.extend_from_slice(new);
}
out.push(b);
}
if hay.len() < limit {
out.extend_from_slice(new);
}
return out;
}
let mut out = Vec::new();
let mut i = 0;
let mut done = 0i64;
while i < hay.len() {
if (count < 0 || done < count) && hay[i..].starts_with(old) {
out.extend_from_slice(new);
i += old.len();
done += 1;
} else {
out.push(hay[i]);
i += 1;
}
}
out
}
fn strip_bytes(bytes: &[u8], chars: Option<&Value>, which: &str) -> Result<Vec<u8>, String> {
let set: Option<Vec<u8>> = match chars {
Some(v) => Some(
as_bytes_object(v)?
.ok_or_else(|| host::type_error("a bytes-like object is required"))?,
),
None => None,
};
let strip_c = |b: u8| -> bool {
match &set {
Some(s) => s.contains(&b),
None => is_ascii_ws(b),
}
};
let mut start = 0;
let mut end = bytes.len();
if which != "rstrip" {
while start < end && strip_c(bytes[start]) {
start += 1;
}
}
if which != "lstrip" {
while end > start && strip_c(bytes[end - 1]) {
end -= 1;
}
}
Ok(bytes[start..end].to_vec())
}
fn split_lines(bytes: &[u8], keepends: bool) -> Vec<Vec<u8>> {
let mut out = Vec::new();
let n = bytes.len();
let mut i = 0;
let mut start = 0;
while i < n {
let c = bytes[i];
if c == b'\n' || c == b'\r' {
let mut brk = i + 1;
if c == b'\r' && brk < n && bytes[brk] == b'\n' {
brk += 1;
}
let end = if keepends { brk } else { i };
out.push(bytes[start..end].to_vec());
i = brk;
start = brk;
} else {
i += 1;
}
}
if start < n {
out.push(bytes[start..].to_vec());
}
out
}
fn build_bytes(args: &[Value]) -> Result<Vec<u8>, String> {
let v = match args.first() {
None => return Ok(vec![]),
Some(v) => v,
};
let v = &indexed(v)?;
if matches!(with_host(|h| h.index_fit(v)), host::IndexFit::TooLarge(_)) {
return Err(format!("OverflowError: {}", host::INDEX_OVERFLOW));
}
if let Value::Int(n) = v {
if *n < 0 {
return Err("ValueError: negative count".into());
}
let mut out: Vec<u8> = Vec::new();
out.try_reserve_exact(*n as usize)
.map_err(|_| "MemoryError".to_string())?;
out.resize(*n as usize, 0u8);
return Ok(out);
}
if let Some(b) = as_bytes_object(v)? {
return Ok(b);
}
if let Some(s) = with_host(|h| h.as_str(v)) {
let enc = args
.get(1)
.and_then(|e| with_host(|h| h.as_str(e)))
.map(|e| e.to_lowercase().replace(['-', '_'], ""));
return match enc.as_deref() {
Some("latin1") | Some("latin") | Some("iso88591") | Some("l1") => {
Ok(s.chars().map(|c| c as u32 as u8).collect())
}
_ => Ok(s.into_bytes()),
};
}
collect_bytes(v)
}
fn collect_bytes(v: &Value) -> Result<Vec<u8>, String> {
if let Some(b) = as_bytes_object(v)? {
return Ok(b);
}
let items = host::iter_vec(v)?;
let mut out = Vec::with_capacity(items.len());
for it in items {
let n = with_host(|h| h.as_int(&it))
.ok_or_else(|| host::type_error("'int' object is required"))?;
if !(0..=255).contains(&n) {
return Err("ValueError: bytes must be in range(0, 256)".into());
}
out.push(n as u8);
}
Ok(out)
}
const SSIZE_OVERFLOW: &str = "OverflowError: Python int too large to convert to C ssize_t";
fn deque_method(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
match name {
"copy" | "__copy__" => with_host(|h| match h.get(recv) {
Some(PyObj::Deque { items, maxlen }) => {
let (items, maxlen) = (items.clone(), *maxlen);
Ok(h.alloc(PyObj::Deque { items, maxlen }))
}
_ => Err(host::type_error("descriptor requires a 'collections.deque' object")),
}),
"reverse" => {
with_host(|h| {
if let Some(PyObj::Deque { items, .. }) = h.get_mut(recv) {
items.make_contiguous().reverse();
}
});
Ok(Value::Undef)
}
"append" => {
let v = arg0(args)?;
with_host(|h| {
if let Some(PyObj::Deque { items, maxlen }) = h.get_mut(recv) {
items.push_back(v);
if let Some(m) = *maxlen {
while items.len() > m {
items.pop_front();
}
}
}
});
Ok(Value::Undef)
}
"appendleft" => {
let v = arg0(args)?;
with_host(|h| {
if let Some(PyObj::Deque { items, maxlen }) = h.get_mut(recv) {
items.push_front(v);
if let Some(m) = *maxlen {
while items.len() > m {
items.pop_back();
}
}
}
});
Ok(Value::Undef)
}
"pop" => with_host(|h| match h.get_mut(recv) {
Some(PyObj::Deque { items, .. }) => items.pop_back(),
_ => None,
})
.ok_or_else(|| "IndexError: pop from an empty deque".into()),
"popleft" => with_host(|h| match h.get_mut(recv) {
Some(PyObj::Deque { items, .. }) => items.pop_front(),
_ => None,
})
.ok_or_else(|| "IndexError: pop from an empty deque".into()),
"extend" => {
let add = host::iter_vec(&arg0(args)?)?;
with_host(|h| {
if let Some(PyObj::Deque { items, maxlen }) = h.get_mut(recv) {
for v in add {
items.push_back(v);
if let Some(m) = *maxlen {
while items.len() > m {
items.pop_front();
}
}
}
}
});
Ok(Value::Undef)
}
"extendleft" => {
let add = host::iter_vec(&arg0(args)?)?;
with_host(|h| {
if let Some(PyObj::Deque { items, maxlen }) = h.get_mut(recv) {
for v in add {
items.push_front(v);
if let Some(m) = *maxlen {
while items.len() > m {
items.pop_back();
}
}
}
}
});
Ok(Value::Undef)
}
"rotate" => {
let n = match args.first() {
None => 1,
Some(a) => {
let a = index_dunder(a)?.unwrap_or_else(|| a.clone());
match with_host(|h| h.index_fit(&a)) {
host::IndexFit::Fits(n) => n,
host::IndexFit::TooLarge(_) => return Err(SSIZE_OVERFLOW.into()),
host::IndexFit::NotInt => {
return Err(with_host(|h| {
host::type_error(&format!(
"'{}' object cannot be interpreted as an integer",
h.tp_name(&a)
))
}));
}
}
}
};
with_host(|h| {
if let Some(PyObj::Deque { items, .. }) = h.get_mut(recv) {
if !items.is_empty() {
let len = items.len() as i64;
let k = ((n % len) + len) % len;
for _ in 0..k {
if let Some(x) = items.pop_back() {
items.push_front(x);
}
}
}
}
});
Ok(Value::Undef)
}
"clear" => {
with_host(|h| {
if let Some(PyObj::Deque { items, .. }) = h.get_mut(recv) {
items.clear();
}
});
Ok(Value::Undef)
}
"count" => {
let v = arg0(args)?;
Ok(Value::Int(with_host(|h| match h.get(recv) {
Some(PyObj::Deque { items, .. }) => {
items.iter().filter(|x| h.equal(x, &v)).count() as i64
}
_ => 0,
})))
}
"index" => {
let elems: Vec<Value> = with_host(|h| match h.get(recv) {
Some(PyObj::Deque { items, .. }) => Ok(items.iter().cloned().collect()),
_ => Err(host::type_error("not a deque")),
})?;
sequence_index(&elems, args, "deque.index(x): x not in deque")
}
"remove" => {
let v = arg0(args)?;
let pos = with_host(|h| match h.get(recv) {
Some(PyObj::Deque { items, .. }) => items.iter().position(|x| h.equal(x, &v)),
_ => None,
});
match pos {
Some(p) => {
with_host(|h| {
if let Some(PyObj::Deque { items, .. }) = h.get_mut(recv) {
items.remove(p);
}
});
Ok(Value::Undef)
}
None => Err("ValueError: deque.remove(x): x not in deque".into()),
}
}
"insert" => {
let idx = args
.first()
.and_then(|v| with_host(|h| h.as_int(v)))
.ok_or_else(|| host::type_error("insert() argument 1 must be an integer"))?;
let v = args
.get(1)
.cloned()
.ok_or_else(|| host::type_error("insert expected 2 arguments, got 1"))?;
with_host(|h| match h.get_mut(recv) {
Some(PyObj::Deque { items, maxlen }) => {
if maxlen.is_some_and(|m| items.len() >= m) {
return Err("IndexError: deque already at its maximum size".to_string());
}
let len = items.len() as i64;
let at = if idx < 0 {
(len + idx).max(0)
} else {
idx.min(len)
};
items.insert(at as usize, v);
Ok(Value::Undef)
}
_ => Err(host::type_error("not a deque")),
})
}
_ => Err(format!(
"AttributeError: 'collections.deque' object has no attribute '{name}'"
)),
}
}
fn collections_dict_method(
recv: &Value,
name: &str,
args: &[Value],
kwargs: &[(String, Value)],
tn: &str,
) -> Option<Result<Value, String>> {
match (tn, name) {
("Counter", "most_common") => Some(counter_most_common(recv, args)),
("Counter", "elements") => Some(counter_elements(recv)),
("Counter", "total") => Some(counter_total(recv)),
("Counter", "subtract") => Some(counter_add(recv, args, kwargs, -1)),
("Counter", "update") => Some(counter_add(recv, args, kwargs, 1)),
("OrderedDict", "move_to_end") => Some(ordered_move_to_end(recv, args, kwargs)),
("OrderedDict", "popitem") => Some(ordered_popitem(recv, args, kwargs)),
("defaultdict", "__missing__") => Some(arg0(args).and_then(|key| defdict_missing(recv, key))),
("defaultdict", "__copy__") => Some(dict_method(recv, "copy", &[], &[])),
("Counter", "__missing__") => Some(arg0(args).map(|_| Value::Int(0))),
("Counter", _) => counter_dunder(recv, name, args),
_ => None,
}
}
const DEFAULTDICT_EXTRA: &[&str] = &["__missing__", "__copy__"];
const COUNTER_EXTRA: &[&str] = &[
"most_common",
"elements",
"subtract",
"update",
"total",
"__missing__",
"__add__",
"__sub__",
"__or__",
"__and__",
"__pos__",
"__neg__",
"__iadd__",
"__isub__",
"__ior__",
"__iand__",
];
fn defdict_missing(recv: &Value, key: Value) -> Result<Value, String> {
let factory = host::dict_meta_of(recv).and_then(|m| m.factory);
match factory {
Some(f) if !matches!(f, Value::Undef) => {
let value = host::invoke(&f, vec![], vec![])?;
subscript_store(recv, key, value.clone())?;
Ok(value)
}
_ => Err(with_host(|h| h.key_error(&key))),
}
}
fn counter_dunder(recv: &Value, name: &str, args: &[Value]) -> Option<Result<Value, String>> {
use host::iop;
let binary = |sym: char| -> Result<Value, String> {
let other = arg0(args)?;
match counter_binop(recv, &other, sym) {
Some(r) => r,
None => Ok(with_host(|h| h.alloc(PyObj::NotImplemented))),
}
};
let inplace = |tag: i64| -> Result<Value, String> {
let other = arg0(args)?;
counter_inplace(tag, recv, &other).unwrap_or_else(|| Ok(recv.clone()))
};
Some(match name {
"__add__" => binary('+'),
"__sub__" => binary('-'),
"__or__" => binary('|'),
"__and__" => binary('&'),
"__pos__" => with_host(|h| h.unary(host::unop::POS, recv)),
"__neg__" => numeric_hook(NumOp::Neg, recv, &Value::Undef),
"__iadd__" => inplace(iop::ADD),
"__isub__" => inplace(iop::SUB),
"__ior__" => inplace(iop::BITOR),
"__iand__" => inplace(iop::BITAND),
_ => return None,
})
}
fn counter_total(recv: &Value) -> Result<Value, String> {
let counts: Vec<Value> = with_host(|h| match h.get(recv) {
Some(PyObj::Dict(d)) => d.values().map(|(_, v)| v.clone()).collect(),
_ => vec![],
});
let mut acc = Value::Int(0);
for c in &counts {
acc = with_host(|h| h.arith(NumOp::Add, &acc, c))?;
}
Ok(acc)
}
fn ordered_popitem(
recv: &Value,
args: &[Value],
kwargs: &[(String, Value)],
) -> Result<Value, String> {
let last = args
.first()
.or_else(|| kwargs.iter().find(|(k, _)| k == "last").map(|(_, v)| v))
.map(|v| with_host(|h| h.truthy(v)))
.unwrap_or(true);
let got = with_host(|h| match h.get_mut(recv) {
Some(PyObj::Dict(d)) => {
if last {
d.pop().map(|(_, (k, v))| (k, v))
} else {
d.shift_remove_index(0).map(|(_, (k, v))| (k, v))
}
}
_ => None,
});
match got {
Some((k, v)) => Ok(with_host(|h| h.new_tuple(vec![k, v]))),
None => Err(with_host(|h| {
let s = h.new_str("dictionary is empty");
h.key_error(&s)
})),
}
}
fn counter_most_common(recv: &Value, args: &[Value]) -> Result<Value, String> {
let mut pairs: Vec<(Value, Value)> = with_host(|h| match h.get(recv) {
Some(PyObj::Dict(d)) => d.values().map(|(k, v)| (k.clone(), v.clone())).collect(),
_ => vec![],
});
with_host(|h| {
pairs.sort_by(|a, b| match (h.num_val(&b.1), h.num_val(&a.1)) {
(Some(x), Some(y)) => x.partial_cmp(&y).unwrap_or(std::cmp::Ordering::Equal),
_ => std::cmp::Ordering::Equal,
})
});
let n = args.first().and_then(|v| with_host(|h| h.as_int(v)));
if let Some(n) = n {
pairs.truncate(n.max(0) as usize);
}
let tuples: Vec<Value> = with_host(|h| {
pairs
.into_iter()
.map(|(k, c)| h.new_tuple(vec![k, c]))
.collect()
});
Ok(with_host(|h| h.new_list(tuples)))
}
fn counter_elements(recv: &Value) -> Result<Value, String> {
let pairs: Vec<(Value, i64)> = with_host(|h| match h.get(recv) {
Some(PyObj::Dict(d)) => d
.values()
.map(|(k, v)| (k.clone(), h.as_int(v).unwrap_or(0)))
.collect(),
_ => vec![],
});
let mut out = Vec::new();
for (k, c) in pairs {
for _ in 0..c.max(0) {
out.push(k.clone());
}
}
Ok(with_host(|h| h.new_list(out)))
}
fn mapping_items(m: &Value) -> Result<Vec<(Value, Value)>, String> {
let view = host::call_method(m, "items", vec![], vec![])?;
let mut out = Vec::new();
for pair in host::iter_vec(&view)? {
let kv = with_host(|h| match h.get(&pair) {
Some(PyObj::Tuple(t)) if t.len() == 2 => Some((t[0].clone(), t[1].clone())),
_ => None,
});
out.push(kv.ok_or_else(|| host::type_error("items() must yield (key, value) pairs"))?);
}
Ok(out)
}
fn counter_inplace(tag: i64, a: &Value, b: &Value) -> Option<Result<Value, String>> {
use host::iop;
if host::dict_meta_of(a)?.kind != host::DictKind::Counter
|| !matches!(tag, iop::ADD | iop::SUB | iop::BITOR | iop::BITAND)
{
return None;
}
let is_true = |op: NumOp, x: &Value, y: &Value| py_bool(&numeric_hook(op, x, y)?);
let run = || -> Result<Value, String> {
if tag == iop::BITAND {
for (elem, count) in mapping_items(a)? {
let other_count = getitem_value(b.clone(), elem.clone())?;
if is_true(NumOp::Lt, &other_count, &count)? {
subscript_store(a, elem, other_count)?;
}
}
} else {
for (elem, count) in mapping_items(b)? {
let current = getitem_value(a.clone(), elem.clone())?;
match tag {
iop::ADD => subscript_store(a, elem, numeric_hook(NumOp::Add, ¤t, &count)?)?,
iop::SUB => subscript_store(a, elem, numeric_hook(NumOp::Sub, ¤t, &count)?)?,
_ => {
if is_true(NumOp::Gt, &count, ¤t)? {
subscript_store(a, elem, count)?;
}
}
}
}
}
counter_keep_positive(a)?;
Ok(a.clone())
};
Some(run())
}
fn counter_keep_positive(c: &Value) -> Result<(), String> {
let mut nonpositive = Vec::new();
for (elem, count) in mapping_items(c)? {
if !py_bool(&numeric_hook(NumOp::Gt, &count, &Value::Int(0))?)? {
nonpositive.push(elem);
}
}
for elem in nonpositive {
subscript_delete(c, elem)?;
}
Ok(())
}
fn comparison_dunder_op(name: &str) -> Option<NumOp> {
Some(match name {
"__eq__" => NumOp::Eq,
"__ne__" => NumOp::Ne,
"__lt__" => NumOp::Lt,
"__le__" => NumOp::Le,
"__gt__" => NumOp::Gt,
"__ge__" => NumOp::Ge,
_ => return None,
})
}
fn collections_compare(op: NumOp, a: &Value, b: &Value) -> Option<Result<Value, String>> {
use host::DictKind;
if !matches!(op, NumOp::Eq | NumOp::Ne | NumOp::Lt | NumOp::Le | NumOp::Gt | NumOp::Ge) {
return None;
}
let kind_a = host::dict_meta_of(a)?.kind;
let kind_b = host::dict_meta_of(b)?.kind;
match (kind_a, kind_b) {
(DictKind::Counter, DictKind::Counter) => {
Some(counter_compare(op, a, b).map(Value::Bool))
}
(DictKind::OrderedDict, DictKind::OrderedDict) if matches!(op, NumOp::Eq | NumOp::Ne) => {
let same_order = with_host(|h| match (h.get(a), h.get(b)) {
(Some(PyObj::Dict(x)), Some(PyObj::Dict(y))) => {
x.len() != y.len() || x.keys().eq(y.keys())
}
_ => true,
});
(!same_order).then(|| Ok(Value::Bool(matches!(op, NumOp::Ne))))
}
_ => None,
}
}
fn counter_compare(op: NumOp, a: &Value, b: &Value) -> Result<bool, String> {
let all = |cmp: NumOp| -> Result<bool, String> {
for c in [a, b] {
let keys = with_host(|h| match h.get(c) {
Some(PyObj::Dict(d)) => d.values().map(|(k, _)| k.clone()).collect(),
_ => Vec::new(),
});
for e in keys {
let (x, y) = (counter_count(a, &e)?, counter_count(b, &e)?);
let r = numeric_hook(cmp, &x, &y)?;
if !py_bool(&r)? {
return Ok(false);
}
}
}
Ok(true)
};
match op {
NumOp::Eq => all(NumOp::Eq),
NumOp::Ne => all(NumOp::Eq).map(|eq| !eq),
NumOp::Le => all(NumOp::Le),
NumOp::Ge => all(NumOp::Ge),
NumOp::Lt => Ok(all(NumOp::Le)? && !all(NumOp::Eq)?),
_ => Ok(all(NumOp::Ge)? && !all(NumOp::Eq)?),
}
}
fn counter_count(counter: &Value, e: &Value) -> Result<Value, String> {
if contains_value(counter.clone(), e.clone())? {
dict_get(counter, e)
} else {
Ok(Value::Int(0))
}
}
fn counter_binop(a: &Value, b: &Value, op: char) -> Option<Result<Value, String>> {
let is_counter =
|v: &Value| host::dict_meta_of(v).map(|m| m.kind) == Some(host::DictKind::Counter);
if !(is_counter(a) && is_counter(b)) {
return None;
}
let dump = |v: &Value| -> Vec<(PKey, Value, Value)> {
with_host(|h| match h.get(v) {
Some(PyObj::Dict(m)) => m
.iter()
.map(|(k, (kv, cnt))| (k.clone(), kv.clone(), cnt.clone()))
.collect(),
_ => Vec::new(),
})
};
let da = dump(a);
let db = dump(b);
let get = |d: &[(PKey, Value, Value)], k: &PKey| {
d.iter()
.find(|(kk, _, _)| kk == k)
.map(|(_, _, c)| c.clone())
.unwrap_or(Value::Int(0))
};
let mut keys: Vec<(PKey, Value)> = da
.iter()
.map(|(k, kv, _)| (k.clone(), kv.clone()))
.collect();
for (k, kv, _) in &db {
if !da.iter().any(|(kk, _, _)| kk == k) {
keys.push((k.clone(), kv.clone()));
}
}
let mut out: IndexMap<PKey, (Value, Value)> = IndexMap::new();
for (k, kv) in keys {
let (x, y) = (get(&da, &k), get(&db, &k));
let val = match op {
'+' => with_host(|h| h.arith(NumOp::Add, &x, &y)),
'-' => with_host(|h| h.arith(NumOp::Sub, &x, &y)),
'&' | '|' => {
let lt = with_host(|h| h.num_val(&x) < h.num_val(&y));
Ok(if (op == '&') == lt { x } else { y })
}
_ => return None,
};
let val = match val {
Ok(v) => v,
Err(e) => return Some(Err(e)),
};
if with_host(|h| h.num_val(&val)).is_some_and(|n| n > 0.0) {
out.insert(k, (kv, val));
}
}
Some(Ok(host::alloc_dict_subtype(
out,
host::DictKind::Counter,
None,
)))
}
fn counter_add(
recv: &Value,
args: &[Value],
kwargs: &[(String, Value)],
sign: i64,
) -> Result<Value, String> {
let mut deltas: Vec<(PKey, Value, Value)> = match args.first() {
None | Some(Value::Undef) => Vec::new(),
Some(other) => {
let other = other.clone();
let other = host::subclass_payload(&other, "__getitem__")
.filter(|p| with_host(|h| matches!(h.get(p), Some(PyObj::Dict(_)))))
.unwrap_or(other);
let is_dict = with_host(|h| matches!(h.get(&other), Some(PyObj::Dict(_))));
if is_dict {
with_host(|h| match h.get(&other) {
Some(PyObj::Dict(d)) => d
.values()
.map(|(k, v)| {
let key = h.to_key(k).unwrap_or(PKey::None);
(key, k.clone(), v.clone())
})
.collect(),
_ => vec![],
})
} else {
let items = host::iter_vec(&other)?;
with_host(|h| {
items
.into_iter()
.map(|it| {
let key = h.to_key(&it).unwrap_or(PKey::None);
(key, it, Value::Int(1))
})
.collect()
})
}
}
};
for (k, v) in kwargs {
let kv = new_str(k.clone());
deltas.push((PKey::Str(k.clone()), kv, v.clone()));
}
let op = if sign < 0 { NumOp::Sub } else { NumOp::Add };
for (key, kv, delta) in deltas {
let cur = with_host(|h| match h.get(recv) {
Some(PyObj::Dict(d)) => d.get(&key).map(|(_, v)| v.clone()),
_ => None,
})
.unwrap_or(Value::Int(0));
let next = with_host(|h| h.arith(op, &cur, &delta))?;
with_host(|h| {
if let Some(PyObj::Dict(d)) = h.get_mut(recv) {
d.insert(key, (kv, next));
}
});
}
Ok(Value::Undef)
}
fn ordered_move_to_end(
recv: &Value,
args: &[Value],
kwargs: &[(String, Value)],
) -> Result<Value, String> {
let kv = arg0(args)?;
let key = with_host(|h| h.to_key(&kv))?;
let last = args
.get(1)
.or_else(|| kwargs.iter().find(|(k, _)| k == "last").map(|(_, v)| v))
.map(|v| with_host(|h| h.truthy(v)))
.unwrap_or(true);
let found = with_host(|h| {
if let Some(PyObj::Dict(d)) = h.get_mut(recv) {
if let Some((k, entry)) = d.shift_remove_entry(&key) {
if last {
d.insert(k, entry);
} else {
d.shift_insert(0, k, entry);
}
return true;
}
}
false
});
if !found {
return Err(with_host(|h| h.key_error(&kv)));
}
Ok(Value::Undef)
}
fn construct_collection(
kind: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
match kind {
"deque" => {
let kw = |name: &str| {
kwargs
.iter()
.find(|(k, _)| k == name)
.map(|(_, v)| v.clone())
};
let iterable = match args.first() {
Some(v) if !matches!(v, Value::Undef) => Some(v.clone()),
_ => kw("iterable"),
};
let maxlen_val = match args.get(1) {
Some(v) if !matches!(v, Value::Undef) => Some(v.clone()),
_ => kw("maxlen"),
};
let maxlen = match maxlen_val {
Some(v) if !matches!(v, Value::Undef) => match with_host(|h| h.index_fit(&v)) {
host::IndexFit::Fits(n) if n < 0 => {
return Err("ValueError: maxlen must be non-negative".into());
}
host::IndexFit::Fits(n) => Some(n as usize),
host::IndexFit::TooLarge(_) => {
return Err(SSIZE_OVERFLOW.into());
}
host::IndexFit::NotInt => {
return Err(host::type_error("an integer is required"));
}
},
_ => None,
};
let mut items: std::collections::VecDeque<Value> = match iterable {
Some(v) if !matches!(v, Value::Undef) => {
std::collections::VecDeque::from(host::iter_vec(&v)?)
}
_ => std::collections::VecDeque::new(),
};
if let Some(m) = maxlen {
while items.len() > m {
items.pop_front();
}
}
Ok(host::alloc_deque(items, maxlen))
}
"Counter" => {
let c = host::alloc_dict_subtype(IndexMap::new(), host::DictKind::Counter, None);
counter_add(&c, &args, &kwargs, 1)?;
Ok(c)
}
"defaultdict" => {
let factory = match args.first() {
None | Some(Value::Undef) => None,
Some(v) if is_callable(v)? => Some(v.clone()),
Some(_) => return Err(host::type_error("first argument must be callable or None")),
};
let rest = args.get(1..).unwrap_or(&[]);
if rest.len() > 1 {
return Err(host::type_error(&format!(
"dict expected at most 1 argument, got {}",
rest.len()
)));
}
let dd =
host::alloc_dict_subtype(IndexMap::new(), host::DictKind::DefaultDict, factory);
dict_method(&dd, "update", rest, &kwargs)?;
Ok(dd)
}
"OrderedDict" => {
if args.len() > 1 {
return Err(host::type_error(&format!(
"expected at most 1 argument, got {}",
args.len()
)));
}
let od = host::alloc_dict_subtype(IndexMap::new(), host::DictKind::OrderedDict, None);
dict_method(&od, "update", &args, &kwargs)?;
Ok(od)
}
"namedtuple" => {
let tname = args
.first()
.and_then(|v| with_host(|h| h.as_str(v)))
.ok_or_else(|| {
host::type_error(
"namedtuple() missing 1 required positional argument: 'typename'",
)
})?;
let fields: Vec<String> = match args.get(1) {
Some(v) => {
if let Some(s) = with_host(|h| h.as_str(v)) {
s.replace(',', " ")
.split_whitespace()
.map(|x| x.to_string())
.collect()
} else {
host::iter_vec(v)?
.iter()
.filter_map(|it| with_host(|h| h.as_str(it)))
.collect()
}
}
None => vec![],
};
Ok(host::make_namedtuple_type(&tname, fields))
}
_ => Err(host::name_error(&format!("collections.{kind}"))),
}
}
fn lru_wrapper_method(recv: &Value, name: &str) -> Result<Value, String> {
match name {
"cache_info" => {
let (hits, misses, maxsize, currsize) =
host::lru_cache_info(recv).unwrap_or((0, 0, None, 0));
let ms = maxsize
.map(|n| Value::Int(n as i64))
.unwrap_or(Value::Undef);
Ok(with_host(|h| {
h.new_tuple(vec![
Value::Int(hits as i64),
Value::Int(misses as i64),
ms,
Value::Int(currsize as i64),
])
}))
}
"cache_clear" => {
host::lru_cache_clear(recv);
Ok(Value::Undef)
}
_ => Err(format!(
"AttributeError: 'functools._lru_cache_wrapper' object has no attribute '{name}'"
)),
}
}
fn file_method(recv: &Value, name: &str, args: &[Value]) -> Result<Value, String> {
let id = match with_host(|h| match h.get(recv) {
Some(PyObj::File { id }) => Some(*id),
_ => None,
}) {
Some(id) => id,
None => return Err(host::type_error("not a file")),
};
match name {
"read" => {
let n = match args.first() {
None | Some(Value::Undef) => None,
Some(v) => Some(
with_host(|h| h.as_int(v))
.ok_or_else(|| host::type_error("an integer is required"))?,
),
};
if with_host(|h| h.io_is_binary(id)) {
let b = with_host(|h| h.io_read_n_bytes(id, n))?;
return Ok(with_host(|h| h.alloc(PyObj::Bytes(b))));
}
let s = with_host(|h| h.io_read_n(id, n))?;
Ok(new_str(s))
}
"readline" => {
if with_host(|h| h.io_is_binary(id)) {
let b = with_host(|h| h.io_readline_bytes(id))?;
return Ok(with_host(|h| h.alloc(PyObj::Bytes(b))));
}
let s = with_host(|h| h.io_readline(id))?;
Ok(new_str(s))
}
"readlines" => {
if with_host(|h| h.io_is_binary(id)) {
let lines = with_host(|h| h.io_read_lines_bytes(id))?;
let vals: Vec<Value> = with_host(|h| {
lines
.into_iter()
.map(|l| h.alloc(PyObj::Bytes(l)))
.collect()
});
return Ok(with_host(|h| h.new_list(vals)));
}
let lines = with_host(|h| h.io_read_lines(id))?;
let vals: Vec<Value> = with_host(|h| lines.into_iter().map(|l| h.new_str(l)).collect());
Ok(with_host(|h| h.new_list(vals)))
}
"write" => {
let s = arg0(args)?;
let binary = with_host(|h| h.io_is_binary(id));
match as_bytes_object(&s)? {
Some(bytes) => {
if !binary {
return Err(host::type_error(&format!(
"write() argument must be str, not {}",
with_host(|h| h.type_name(&s))
)));
}
with_host(|h| h.io_write_bytes(id, &bytes))
}
None => {
if binary {
return Err(host::type_error(&format!(
"a bytes-like object is required, not '{}'",
with_host(|h| h.type_name(&s))
)));
}
let txt = with_host(|h| h.str_of(&s));
with_host(|h| h.io_write(id, &txt))
}
}
}
"writelines" => {
let items = host::iter_vec(&arg0(args)?)?;
for it in items {
match as_bytes_object(&it)? {
Some(bytes) => {
with_host(|h| h.io_write_bytes(id, &bytes))?;
}
None => {
let txt = with_host(|h| h.str_of(&it));
with_host(|h| h.io_write(id, &txt))?;
}
}
}
Ok(Value::Undef)
}
"close" => {
with_host(|h| h.io_close(id));
Ok(Value::Undef)
}
"flush" => {
with_host(|h| h.io_flush(id))?;
Ok(Value::Undef)
}
"readable" => Ok(Value::Bool(with_host(|h| h.io_dirs(id).0))),
"writable" => Ok(Value::Bool(with_host(|h| h.io_dirs(id).1))),
"seekable" => Ok(Value::Bool(with_host(|h| h.io_tell(id).is_ok()))),
"tell" => Ok(Value::Int(with_host(|h| h.io_tell(id))?)),
"seek" => {
let off = {
let a0 = arg0(args)?;
with_host(|h| h.as_int(&a0))
.ok_or_else(|| host::type_error("an integer is required"))?
};
let whence = match args.get(1) {
None | Some(Value::Undef) => 0,
Some(v) => with_host(|h| h.as_int(v))
.ok_or_else(|| host::type_error("an integer is required"))?,
};
Ok(Value::Int(with_host(|h| h.io_seek(id, off, whence))?))
}
"truncate" => {
let size = match args.first() {
None | Some(Value::Undef) => None,
Some(v) => Some(
with_host(|h| h.as_int(v))
.ok_or_else(|| host::type_error("an integer is required"))?,
),
};
Ok(Value::Int(with_host(|h| h.io_truncate(id, size))?))
}
"fileno" => Ok(Value::Int(with_host(|h| h.io_fileno(id))?)),
"isatty" => Ok(Value::Bool(match id {
0 => std::io::IsTerminal::is_terminal(&std::io::stdout()),
1 => std::io::IsTerminal::is_terminal(&std::io::stderr()),
2 => std::io::IsTerminal::is_terminal(&std::io::stdin()),
_ => false,
})),
"__enter__" => Ok(recv.clone()),
"__exit__" => {
with_host(|h| h.io_close(id));
Ok(Value::Bool(false))
}
"__iter__" => Ok(recv.clone()),
"__next__" => {
let s = with_host(|h| h.io_readline(id))?;
if s.is_empty() {
Err("StopIteration".to_string())
} else {
Ok(new_str(s))
}
}
_ => Err(format!(
"AttributeError: '_io.{}' object has no attribute '{name}'",
with_host(|h| h.file_class_name(id))
)),
}
}
fn new_str(s: String) -> Value {
with_host(|h| h.new_str(s))
}
fn fmt_nonfinite(f: f64, upper: bool) -> Option<String> {
if f.is_nan() {
Some(if upper { "NAN" } else { "nan" }.to_string())
} else if f.is_infinite() {
let s = if upper { "INF" } else { "inf" };
Some(if f < 0.0 {
format!("-{s}")
} else {
s.to_string()
})
} else {
None
}
}
pub fn apply_format_spec(s: &str, v: &Value, spec: &str) -> Result<String, String> {
if spec.is_empty() {
return Ok(s.to_string());
}
let chars: Vec<char> = spec.chars().collect();
let mut i = 0;
let mut fill = ' ';
let mut align = '\0';
let mut align_explicit = false;
let mut fill_explicit = false;
if chars.len() >= 2 && matches!(chars[1], '<' | '>' | '^' | '=') {
fill = chars[0];
align = chars[1];
align_explicit = true;
fill_explicit = true;
i = 2;
} else if !chars.is_empty() && matches!(chars[0], '<' | '>' | '^' | '=') {
align = chars[0];
align_explicit = true;
i = 1;
}
let mut sign = '\0';
if i < chars.len() && matches!(chars[i], '+' | '-' | ' ') {
sign = chars[i];
i += 1;
}
let mut alt = false;
if i < chars.len() && chars[i] == '#' {
alt = true;
i += 1;
}
if i < chars.len() && chars[i] == '0' && !fill_explicit {
fill = '0';
if !align_explicit {
align = '=';
}
i += 1;
}
let mut width = 0usize;
while i < chars.len() && chars[i].is_ascii_digit() {
width = accumulate_spec_digit(width, chars[i])?;
i += 1;
}
let group: Option<char> = match chars.get(i).copied() {
Some(c @ (',' | '_')) => {
i += 1;
Some(c)
}
_ => None,
};
let mut prec: Option<usize> = None;
if i < chars.len() && chars[i] == '.' {
i += 1;
let mut p = 0usize;
while i < chars.len() && chars[i].is_ascii_digit() {
p = accumulate_spec_digit(p, chars[i])?;
i += 1;
}
if p > i32::MAX as usize {
return Err("ValueError: precision too big".to_string());
}
prec = Some(p);
}
let ty = chars.get(i).copied().unwrap_or('\0');
let ty = if ty == '\0' && is_int_like(v) {
'd'
} else {
ty
};
validate_format_spec(v, ty, sign, alt, group, prec, align, align_explicit)?;
let numeric = as_f(v).is_some() || is_int_like(v);
let body =
match ty {
'd' => match with_host(|h| h.big_val(v)) {
Some(n) => n.to_string(),
None => s.to_string(),
},
'f' | 'F' => {
let f = as_f_wide(v)?;
fmt_nonfinite(f, ty == 'F')
.unwrap_or_else(|| format!("{:.*}", prec.unwrap_or(6), f))
}
'e' | 'E' => {
let f = as_f_wide(v)?;
fmt_nonfinite(f, ty == 'E')
.unwrap_or_else(|| crate::host::fmt_sci(f, prec.unwrap_or(6), ty == 'E'))
}
'g' | 'G' => {
let f = as_f_wide(v)?;
fmt_nonfinite(f, ty == 'G')
.unwrap_or_else(|| crate::host::fmt_g(f, prec.unwrap_or(6), ty == 'G', alt))
}
'c' => match as_i(v) {
Some(n) => char::from_u32(n as u32)
.map(|c| c.to_string())
.unwrap_or_default(),
None => s.to_string(),
},
'x' => fmt_int_radix(v, 16, if alt { "0x" } else { "" }, false)
.unwrap_or_else(|| s.to_string()),
'X' => fmt_int_radix(v, 16, if alt { "0X" } else { "" }, true)
.unwrap_or_else(|| s.to_string()),
'o' => fmt_int_radix(v, 8, if alt { "0o" } else { "" }, false)
.unwrap_or_else(|| s.to_string()),
'b' => fmt_int_radix(v, 2, if alt { "0b" } else { "" }, false)
.unwrap_or_else(|| s.to_string()),
'%' => {
let f = as_f_wide(v)?;
match fmt_nonfinite(f, false) {
Some(s) => format!("{s}%"),
None => format!("{:.*}%", prec.unwrap_or(6), f * 100.0),
}
}
'n' if is_int_like(v) => match with_host(|h| h.big_val(v)) {
Some(n) => n.to_string(),
None => s.to_string(),
},
'n' => {
let f = as_f_wide(v)?;
fmt_nonfinite(f, false)
.unwrap_or_else(|| crate::host::fmt_g(f, prec.unwrap_or(6), false, alt))
}
_ => {
let mut body = s.to_string();
if let Some(p) = prec {
if matches!(v, Value::Str(_)) || is_str(v) {
body = body.chars().take(p).collect();
} else if numeric {
let f = as_f_wide(v)?;
body = fmt_nonfinite(f, false)
.unwrap_or_else(|| crate::host::fmt_none_float(f, p, alt));
}
}
body
}
};
let float_render = matches!(ty, 'e' | 'E' | 'f' | 'F' | 'g' | 'G' | '%')
|| ((ty == 'n' || ty == '\0') && !is_int_like(v) && numeric);
let body = if alt && float_render {
crate::host::alt_decimal_point(&body)
} else {
body
};
let mut sign_str = String::new();
let mut rest = body.as_str();
if let Some(r) = rest.strip_prefix('-') {
sign_str.push('-');
rest = r;
} else if numeric && matches!(sign, '+' | ' ') {
sign_str.push(sign);
}
let mut prefix = String::new();
if rest.len() >= 2 && rest.as_bytes()[0] == b'0' {
let c1 = rest.as_bytes()[1] as char;
if matches!(c1, 'x' | 'X' | 'o' | 'O' | 'b' | 'B') {
prefix = rest[..2].to_string();
rest = &rest[2..];
}
}
let cut = if matches!(ty, 'e' | 'E' | 'f' | 'F' | 'g' | 'G' | 'n' | '%' | '\0') {
rest.find(|c: char| !c.is_ascii_digit())
.unwrap_or(rest.len())
} else {
rest.find('.').unwrap_or(rest.len())
};
let (intpart, tail): (&str, &str) = rest.split_at(cut);
let zero_interleave = align == '=' && fill == '0';
let groupable = !intpart.is_empty();
if numeric && ty == 'n' {
let loc = locale_numeric();
let tail = match tail.strip_prefix('.') {
Some(rest) => format!("{}{rest}", loc.decimal_point),
None => tail.to_string(),
};
let min_width = if zero_interleave && groupable {
width.saturating_sub(
sign_str.chars().count() + prefix.chars().count() + tail.chars().count(),
)
} else {
0
};
let grouped = if groupable {
insert_grouping_locale(intpart, &loc.thousands_sep, &loc.grouping, min_width)
} else {
intpart.to_string()
};
return pad_to_width(
&format!("{sign_str}{prefix}{grouped}{tail}"),
width,
fill,
align,
true,
);
}
let group_size = match (group, ty) {
(Some(_), _) if !numeric => 0,
(Some('_'), 'x' | 'X' | 'o' | 'b') => 4,
(Some(_), _) => 3,
(None, _) => 0,
};
let sep = group.unwrap_or(',');
if numeric && groupable && group_size > 0 && zero_interleave {
let fixed = sign_str.chars().count() + prefix.chars().count() + tail.chars().count();
let mut n = intpart.chars().count().max(1);
while fixed + grouped_len(n, group_size) < width {
n += 1;
}
let padded = format!("{:0>n$}", intpart, n = n);
let grouped = insert_grouping(&padded, sep, group_size);
return Ok(format!("{sign_str}{prefix}{grouped}{tail}"));
}
let intpart_g = if numeric && groupable && group_size > 0 {
insert_grouping(intpart, sep, group_size)
} else {
intpart.to_string()
};
let body = format!("{sign_str}{prefix}{intpart_g}{tail}");
pad_to_width(&body, width, fill, align, numeric)
}
fn accumulate_spec_digit(acc: usize, c: char) -> Result<usize, String> {
let d = c as i64 - '0' as i64;
i64::try_from(acc)
.ok()
.and_then(|a| a.checked_mul(10))
.and_then(|a| a.checked_add(d))
.and_then(|a| usize::try_from(a).ok())
.ok_or_else(|| "ValueError: Too many decimal digits in format string".to_string())
}
fn pad_to_width(
body: &str,
width: usize,
fill: char,
align: char,
numeric: bool,
) -> Result<String, String> {
let body = body.to_string();
let len = body.chars().count();
if len >= width {
return Ok(body);
}
let pad = width - len;
let mut probe = String::new();
probe
.try_reserve(
pad.saturating_mul(fill.len_utf8())
.saturating_add(body.len()),
)
.map_err(|_| "MemoryError".to_string())?;
drop(probe);
Ok(match align {
'<' => format!("{body}{}", fill.to_string().repeat(pad)),
'^' => {
let l = pad / 2;
let r = pad - l;
format!(
"{}{body}{}",
fill.to_string().repeat(l),
fill.to_string().repeat(r)
)
}
'=' if !numeric => format!("{body}{}", fill.to_string().repeat(pad)),
'=' => {
let cs: Vec<char> = body.chars().collect();
let mut k = 0;
if cs.first().is_some_and(|c| matches!(c, '+' | '-' | ' ')) {
k = 1;
}
if cs.len() >= k + 2
&& cs[k] == '0'
&& matches!(cs[k + 1], 'x' | 'X' | 'o' | 'O' | 'b' | 'B')
{
k += 2;
}
let head: String = cs[..k].iter().collect();
let tail: String = cs[k..].iter().collect();
format!("{head}{}{tail}", fill.to_string().repeat(pad))
}
'>' => format!("{}{body}", fill.to_string().repeat(pad)),
_ => {
if numeric {
format!("{}{body}", fill.to_string().repeat(pad))
} else {
format!("{body}{}", fill.to_string().repeat(pad))
}
}
})
}
#[allow(clippy::too_many_arguments)]
fn validate_format_spec(
v: &Value,
ty: char,
sign: char,
alt: bool,
group: Option<char>,
prec: Option<usize>,
align: char,
align_explicit: bool,
) -> Result<(), String> {
let is_float = matches!(v, Value::Float(_));
let is_int = !is_float && with_host(|h| h.big_val(v)).is_some();
let is_num = is_float || is_int;
let is_string = !is_num && is_str(v);
let tyname = || with_host(|h| h.type_name(v));
if !is_num && !is_string {
return Err(format!(
"TypeError: unsupported format string passed to {}.__format__",
tyname()
));
}
if is_string {
if ty != '\0' && ty != 's' {
return Err(format!(
"ValueError: Unknown format code '{ty}' for object of type 'str'"
));
}
if sign == ' ' {
return Err("ValueError: Space not allowed in string format specifier".into());
}
if sign != '\0' {
return Err("ValueError: Sign not allowed in string format specifier".into());
}
if alt {
return Err(
"ValueError: Alternate form (#) not allowed in string format specifier".into(),
);
}
if align_explicit && align == '=' {
return Err("ValueError: '=' alignment not allowed in string format specifier".into());
}
if let Some(g) = group {
return Err(format!("ValueError: Cannot specify '{g}' with 's'."));
}
return Ok(());
}
if ty == 's' {
return Err(format!(
"ValueError: Unknown format code 's' for object of type '{}'",
tyname()
));
}
if is_float && matches!(ty, 'b' | 'o' | 'x' | 'X' | 'c' | 'd') {
return Err(format!(
"ValueError: Unknown format code '{ty}' for object of type 'float'"
));
}
match group {
Some(',') if matches!(ty, 'x' | 'X' | 'o' | 'b' | 'c' | 'n') => {
return Err("ValueError: Cannot specify ',' with '?'.".replace('?', &ty.to_string()));
}
Some('_') if matches!(ty, 'c' | 'n') => {
return Err("ValueError: Cannot specify '_' with '?'.".replace('?', &ty.to_string()));
}
_ => {}
}
if is_int && prec.is_some() && matches!(ty, '\0' | 'b' | 'o' | 'x' | 'X' | 'c' | 'd' | 'n') {
return Err("ValueError: Precision not allowed in integer format specifier".into());
}
if ty == 'c' {
if sign != '\0' {
return Err("ValueError: Sign not allowed with integer format specifier 'c'".into());
}
if alt {
return Err(
"ValueError: Alternate form (#) not allowed with integer format specifier 'c'"
.into(),
);
}
}
if ty == 'c' && is_int {
if let Some(n) = with_host(|h| h.big_val(v)) {
use num_bigint::BigInt;
if n < BigInt::from(0) || n > BigInt::from(0x10_FFFFu32) {
return Err("OverflowError: %c arg not in range(0x110000)".into());
}
}
}
Ok(())
}
struct LocaleNumeric {
decimal_point: String,
thousands_sep: String,
grouping: Vec<u8>,
}
fn locale_numeric() -> LocaleNumeric {
unsafe {
let lc = libc::localeconv();
if lc.is_null() {
return LocaleNumeric {
decimal_point: ".".into(),
thousands_sep: String::new(),
grouping: Vec::new(),
};
}
let mut decimal_point = locale_cstr((*lc).decimal_point);
if decimal_point.is_empty() {
decimal_point.push('.');
}
LocaleNumeric {
decimal_point,
thousands_sep: locale_cstr((*lc).thousands_sep),
grouping: locale_grouping((*lc).grouping),
}
}
}
unsafe fn locale_cstr(p: *const libc::c_char) -> String {
if p.is_null() {
return String::new();
}
let bytes = std::ffi::CStr::from_ptr(p).to_bytes();
match std::str::from_utf8(bytes) {
Ok(s) => s.to_string(),
Err(_) => bytes.iter().map(|&b| b as char).collect(),
}
}
unsafe fn locale_grouping(p: *const libc::c_char) -> Vec<u8> {
if p.is_null() {
return Vec::new();
}
std::ffi::CStr::from_ptr(p).to_bytes().to_vec()
}
fn insert_grouping_locale(digits: &str, sep: &str, grouping: &[u8], min_width: usize) -> String {
fn next_width(grouping: &[u8], i: &mut usize, previous: &mut isize) -> isize {
match grouping.get(*i).copied() {
None | Some(0) => *previous,
Some(127) => 0,
Some(w) => {
*previous = w as isize;
*i += 1;
w as isize
}
}
}
let cs: Vec<char> = digits.chars().collect();
let sep_len = sep.chars().count() as isize;
let mut remaining = cs.len() as isize;
let mut min_width = min_width as isize;
let mut pos = cs.len();
let mut pieces: Vec<String> = Vec::new();
let mut use_separator = false;
let mut i = 0usize;
let mut previous = 0isize;
let mut loop_broken = false;
let emit = |len: isize, pos: &mut usize, remaining: isize, pieces: &mut Vec<String>| {
let n_zeros = (len - remaining).max(0) as usize;
let n_chars = remaining.min(len).max(0) as usize;
let taken: String = cs[*pos - n_chars..*pos].iter().collect();
*pos -= n_chars;
pieces.push(format!("{}{taken}", "0".repeat(n_zeros)));
n_chars as isize
};
loop {
let len = next_width(grouping, &mut i, &mut previous);
if len <= 0 {
break;
}
let len = len.min(remaining.max(min_width).max(1));
if use_separator {
pieces.push(sep.to_string());
}
let n_chars = emit(len, &mut pos, remaining, &mut pieces);
use_separator = true;
remaining -= n_chars;
min_width -= len;
if remaining <= 0 && min_width <= 0 {
loop_broken = true;
break;
}
min_width -= sep_len;
}
if !loop_broken {
let len = remaining.max(min_width).max(1);
if use_separator {
pieces.push(sep.to_string());
}
emit(len, &mut pos, remaining, &mut pieces);
}
pieces.reverse();
pieces.concat()
}
fn grouped_len(n: usize, size: usize) -> usize {
if n == 0 {
0
} else {
n + (n - 1) / size
}
}
fn insert_grouping(digits: &str, sep: char, size: usize) -> String {
let cs: Vec<char> = digits.chars().collect();
let n = cs.len();
if n == 0 || size == 0 {
return digits.to_string();
}
let mut out = String::with_capacity(n + n / size);
for (idx, c) in cs.iter().enumerate() {
if idx > 0 && (n - idx) % size == 0 {
out.push(sep);
}
out.push(*c);
}
out
}
fn fmt_int_radix(v: &Value, radix: u32, prefix: &str, upper: bool) -> Option<String> {
if matches!(v, Value::Float(_)) {
return None;
}
let n = with_host(|h| h.big_val(v))?;
use num_bigint::Sign;
let sign = if n.sign() == Sign::Minus { "-" } else { "" };
let mut body = n.magnitude().to_str_radix(radix);
if upper {
body = body.to_uppercase();
}
Some(format!("{sign}{prefix}{body}"))
}
fn as_i(v: &Value) -> Option<i64> {
match v {
Value::Int(n) => Some(*n),
Value::Bool(b) => Some(*b as i64),
Value::Float(f) => Some(*f as i64),
_ => None,
}
}
fn is_str(v: &Value) -> bool {
matches!(v, Value::Str(_)) || with_host(|h| h.as_str(v).is_some())
}
fn is_callable(v: &Value) -> Result<bool, String> {
let r = call_builtin_function("callable", vec![v.clone()], vec![])?;
Ok(matches!(r, Value::Bool(true)))
}