use crate::async_rt;
use crate::ast::Span;
use fusevm::{Chunk, NumOp, VMResult, Value, VM};
use indexmap::IndexMap;
use std::cell::RefCell;
use std::collections::{BTreeSet, HashMap, HashSet, VecDeque};
use std::rc::Rc;
pub mod ops {
pub const GETLOCAL: u16 = 1; pub const SETLOCAL: u16 = 2; pub const SETGLOBAL: u16 = 3; pub const DELNAME: u16 = 4; pub const GETATTR: u16 = 5; pub const SETATTR: u16 = 6; pub const DELATTR: u16 = 7; pub const GETITEM: u16 = 8; pub const SETITEM: u16 = 9; pub const DELITEM: u16 = 10; pub const MKSTR: u16 = 11; pub const MKLIST: u16 = 12; pub const MKTUPLE: u16 = 13; pub const MKSET: u16 = 14; pub const MKDICT: u16 = 15; pub const MKSLICE: u16 = 16; pub const CALL: u16 = 17; pub const CALL_KW: u16 = 18; pub const CALL_METHOD: u16 = 19; pub const CALL_METHOD_KW: u16 = 20; pub const CALL_VALUE: u16 = 21; pub const CALL_VALUE_KW: u16 = 22; pub const TRUTHY: u16 = 23; pub const TOSTR: u16 = 24; pub const FORMAT: u16 = 25; pub const MKFUNC: u16 = 26; pub const MKLAMBDA: u16 = 27; pub const BUILD_CLASS: u16 = 28; pub const GETITER: u16 = 29; pub const FORITER: u16 = 30; pub const CONTAINS: u16 = 31; pub const IS: u16 = 32; pub const RAISE: u16 = 33; pub const RERAISE: u16 = 34; pub const SIG_RETURN: u16 = 35; pub const SIG_BREAK: u16 = 36; pub const SIG_CONTINUE: u16 = 37; pub const IMPORT: u16 = 38; pub const IMPORT_FROM: u16 = 39; pub const UNPACK: u16 = 40; pub const BINOP: u16 = 41; pub const GETGLOBAL: u16 = 42; pub const GETSELF: u16 = 43; pub const ASSERT_FAIL: u16 = 44; pub const MKEXC: u16 = 45; pub const YIELDV: u16 = 46; pub const UNARY: u16 = 47; pub const DBG_LINE: u16 = 48; pub const TRY: u16 = 49; pub const DECLARE_GLOBAL: u16 = 50; pub const DECLARE_NONLOCAL: u16 = 51; pub const CALL_EX: u16 = 52; pub const CALL_VALUE_EX: u16 = 53; pub const CALL_METHOD_EX: u16 = 54; pub const BUILD_ARGS: u16 = 55; pub const BUILD_KWARGS: u16 = 56; pub const MKDICT_EX: u16 = 57; pub const MATCH_SEQ: u16 = 58; pub const MATCH_MAP_CHECK: u16 = 59; pub const MATCH_KEY: u16 = 60; pub const MATCH_MAP_REST: u16 = 61; pub const MATCH_CLASS: u16 = 62; pub const MKBYTES: u16 = 63; pub const GENRET: u16 = 64; pub const AWAIT: u16 = 65; pub const INPLACE: u16 = 66; pub const WITH_EXIT: u16 = 67; pub const YIELD_FROM: u16 = 68; pub const LOOP_BODY: u16 = 69; pub const DISPLAYHOOK: u16 = 70; pub const EXTEND_LIST: u16 = 71; pub const EXTEND_TUPLE: u16 = 72; pub const EXTEND_SET: u16 = 73; pub const EXTEND_DICT: u16 = 74; pub const EXTEND_STR: u16 = 75; pub const ELLIPSIS: u16 = 76; pub const IMPORT_STAR: u16 = 77; pub const IMPORT_RELATIVE: u16 = 78; pub const TRY_ANNOTATION: u16 = 79; pub const IS_INT: u16 = 80; pub const INTERPOLATION: u16 = 81; pub const TEMPLATE: u16 = 82; pub const CHECK_BOUND: u16 = 83; pub const UNBOUND: u16 = 84; }
pub mod iop {
pub const ADD: i64 = 0; pub const SUB: i64 = 1; pub const MUL: i64 = 2; pub const DIV: i64 = 3; pub const FLOORDIV: i64 = 4; pub const MOD: i64 = 5; pub const POW: i64 = 6; pub const MATMUL: i64 = 7; pub const BITAND: i64 = 8; pub const BITOR: i64 = 9; pub const BITXOR: i64 = 10; pub const SHL: i64 = 11; pub const SHR: i64 = 12; }
pub mod binop {
pub const DIV: i64 = 0; pub const FLOORDIV: i64 = 1; pub const MOD: i64 = 2; pub const POW: i64 = 3; pub const MATMUL: i64 = 4; pub const BITAND: i64 = 5; pub const BITOR: i64 = 6; pub const BITXOR: i64 = 7; pub const SHL: i64 = 8; pub const SHR: i64 = 9; }
pub mod unop {
pub const INVERT: i64 = 0; pub const POS: i64 = 1; }
#[derive(Clone, Copy)]
struct ConvFlags {
plus: bool,
space: bool,
hash: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum PKey {
None,
Int(i64),
Big(num_bigint::BigInt),
FloatBits(u64),
Complex(u64, u64),
Str(String),
Bytes(Vec<u8>),
Tuple(Vec<PKey>),
Frozenset(Vec<PKey>),
Instance {
hash: i64,
id: u32,
},
Foreign {
hash: i64,
id: u32,
},
Class(String),
Singleton(u8),
}
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct FuncDef {
pub name: String,
#[serde(default)]
pub qualname: String,
pub params: Vec<String>,
#[serde(default)]
pub posonly: usize,
pub ndefaults: usize,
pub star: Option<String>,
pub kwonly: Vec<String>,
pub kwonly_required: Vec<bool>,
pub kwargs: Option<String>,
pub chunk: Chunk,
#[serde(default)]
pub locals: Vec<String>,
pub is_generator: bool,
#[serde(default)]
pub is_async: bool,
#[serde(default)]
pub doc: Option<String>,
#[serde(default)]
pub freevars: Vec<String>,
}
impl FuncDef {
pub fn clone_meta(&self) -> FuncDef {
FuncDef {
name: self.name.clone(),
qualname: self.qualname.clone(),
params: self.params.clone(),
posonly: self.posonly,
ndefaults: self.ndefaults,
star: self.star.clone(),
kwonly: self.kwonly.clone(),
kwonly_required: self.kwonly_required.clone(),
kwargs: self.kwargs.clone(),
chunk: Chunk::default(),
locals: self.locals.clone(),
is_generator: self.is_generator,
is_async: self.is_async,
doc: self.doc.clone(),
freevars: self.freevars.clone(),
}
}
}
pub type ProcDef = FuncDef;
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct TryDef {
pub body: Chunk,
pub handlers: Vec<(Option<Chunk>, Option<String>, Chunk)>,
pub orelse: Option<Chunk>,
pub finalbody: Option<Chunk>,
}
#[derive(Clone, Default)]
pub struct ClassDef {
pub name: String,
pub qualname: String,
pub bases: Vec<String>,
pub ns: IndexMap<String, Value>,
pub mro: Vec<String>,
pub metaclass: String,
pub module: String,
}
#[derive(Clone)]
pub struct FuncVal {
pub def_id: usize,
pub module: usize,
pub env: Option<Env>,
pub defaults: Vec<Value>,
pub kwonly_defaults: Vec<Value>,
pub bound: Option<Value>,
pub owner: Option<String>,
pub annotations: Value,
}
#[derive(Clone)]
pub struct Instance {
pub class: String,
pub dict: Value,
pub payload: Value,
}
const OBJECT_SLOT_WRAPPERS: &[&str] = &[
"__init__",
"__str__",
"__repr__",
"__eq__",
"__ne__",
"__lt__",
"__le__",
"__gt__",
"__ge__",
"__hash__",
"__delattr__",
"__setattr__",
"__getattribute__",
"__format__",
"__sizeof__",
"__reduce__",
"__reduce_ex__",
"__dir__",
"__init_subclass__",
"__subclasshook__",
];
#[derive(Clone)]
pub struct MtState {
mt: [u32; 624],
index: usize,
}
impl Default for MtState {
fn default() -> Self {
let mut s = MtState {
mt: [0; 624],
index: 624,
};
s.init_by_array(&[19650218u32.wrapping_add(0)]); s
}
}
impl MtState {
fn init_genrand(&mut self, seed: u32) {
self.mt[0] = seed;
for i in 1..624 {
self.mt[i] = 1812433253u32
.wrapping_mul(self.mt[i - 1] ^ (self.mt[i - 1] >> 30))
.wrapping_add(i as u32);
}
self.index = 624;
}
pub fn init_by_array(&mut self, key: &[u32]) {
self.init_genrand(19650218);
let (mut i, mut j) = (1usize, 0usize);
let mut k = 624.max(key.len());
while k > 0 {
self.mt[i] = (self.mt[i]
^ ((self.mt[i - 1] ^ (self.mt[i - 1] >> 30)).wrapping_mul(1664525)))
.wrapping_add(key[j])
.wrapping_add(j as u32);
i += 1;
j += 1;
if i >= 624 {
self.mt[0] = self.mt[623];
i = 1;
}
if j >= key.len() {
j = 0;
}
k -= 1;
}
k = 623;
while k > 0 {
self.mt[i] = (self.mt[i]
^ ((self.mt[i - 1] ^ (self.mt[i - 1] >> 30)).wrapping_mul(1566083941)))
.wrapping_sub(i as u32);
i += 1;
if i >= 624 {
self.mt[0] = self.mt[623];
i = 1;
}
k -= 1;
}
self.mt[0] = 0x80000000;
self.index = 624;
}
pub fn next_u32(&mut self) -> u32 {
if self.index >= 624 {
for i in 0..624 {
let y = (self.mt[i] & 0x80000000) | (self.mt[(i + 1) % 624] & 0x7fffffff);
let mut next = self.mt[(i + 397) % 624] ^ (y >> 1);
if y & 1 != 0 {
next ^= 0x9908b0df;
}
self.mt[i] = next;
}
self.index = 0;
}
let mut y = self.mt[self.index];
self.index += 1;
y ^= y >> 11;
y ^= (y << 7) & 0x9d2c5680;
y ^= (y << 15) & 0xefc60000;
y ^= y >> 18;
y
}
pub fn random(&mut self) -> f64 {
let a = (self.next_u32() >> 5) as f64;
let b = (self.next_u32() >> 6) as f64;
(a * 67108864.0 + b) * (1.0 / 9007199254740992.0)
}
pub fn state(&self) -> Vec<u32> {
let mut v = self.mt.to_vec();
v.push(self.index as u32);
v
}
pub fn set_state(&mut self, s: &[u32]) {
if s.len() >= 625 {
self.mt.copy_from_slice(&s[..624]);
self.index = s[624] as usize;
}
}
}
#[derive(Clone, Copy, PartialEq)]
pub enum ItKind {
Count,
Repeat,
Cycle,
Chain,
Accumulate,
StarMap,
Compress,
DropWhile,
TakeWhile,
FilterFalse,
ISlice,
ZipLongest,
Pairwise,
}
impl ItKind {
fn type_name(self) -> &'static str {
match self {
ItKind::Count => "itertools.count",
ItKind::Repeat => "itertools.repeat",
ItKind::Cycle => "itertools.cycle",
ItKind::Chain => "itertools.chain",
ItKind::Accumulate => "itertools.accumulate",
ItKind::StarMap => "itertools.starmap",
ItKind::Compress => "itertools.compress",
ItKind::DropWhile => "itertools.dropwhile",
ItKind::TakeWhile => "itertools.takewhile",
ItKind::FilterFalse => "itertools.filterfalse",
ItKind::ISlice => "itertools.islice",
ItKind::ZipLongest => "itertools.zip_longest",
ItKind::Pairwise => "itertools.pairwise",
}
}
}
#[derive(Clone, Copy, PartialEq)]
pub enum DescKind {
WrapperDescriptor,
MethodWrapper,
ClassMethodDescriptor,
GetSetDescriptor,
MemberDescriptor,
}
pub const STRUCT_TIME_FIELDS: &[&str] = &[
"tm_year", "tm_mon", "tm_mday", "tm_hour", "tm_min", "tm_sec", "tm_wday", "tm_yday",
"tm_isdst", "tm_gmtoff", "tm_zone",
];
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum TypeVarKind {
TypeVar,
ParamSpec,
TypeVarTuple,
}
impl TypeVarKind {
fn type_name(self) -> &'static str {
match self {
TypeVarKind::TypeVar => "TypeVar",
TypeVarKind::ParamSpec => "ParamSpec",
TypeVarKind::TypeVarTuple => "TypeVarTuple",
}
}
}
impl DescKind {
pub fn type_name(self) -> &'static str {
match self {
DescKind::WrapperDescriptor => "wrapper_descriptor",
DescKind::MethodWrapper => "method-wrapper",
DescKind::ClassMethodDescriptor => "classmethod_descriptor",
DescKind::GetSetDescriptor => "getset_descriptor",
DescKind::MemberDescriptor => "member_descriptor",
}
}
}
#[derive(Clone)]
pub enum PyObj {
Str(String),
Bytes(Vec<u8>),
List(Vec<Value>),
Tuple(Vec<Value>),
Dict(IndexMap<PKey, (Value, Value)>),
Set(IndexMap<PKey, Value>),
Frozenset(IndexMap<PKey, Value>),
DictView {
dict: Value,
kind: u8,
},
Range {
start: i64,
stop: i64,
step: i64,
},
BigRange {
start: num_bigint::BigInt,
stop: num_bigint::BigInt,
step: num_bigint::BigInt,
},
Slice {
lo: Value,
hi: Value,
step: Value,
},
Func(Rc<FuncVal>),
Code {
def_id: usize,
},
Union {
args: Vec<Value>,
},
GenericAlias {
origin: Value,
args: Vec<Value>,
},
Pattern {
id: usize,
pattern: String,
flags: i64,
groups: usize,
},
Match {
text: String,
spans: Vec<Option<(usize, usize)>>,
named: Vec<(String, usize)>,
},
StructTime {
fields: Vec<Value>,
},
TypeVarLike {
kind: TypeVarKind,
name: String,
attrs: Value,
},
Namespace {
attrs: IndexMap<String, Value>,
},
MappingProxy {
dict: Value,
},
Descriptor {
kind: DescKind,
qual: String,
},
Traceback {
frames: Vec<(String, u32)>,
idx: usize,
},
PyFrame {
name: String,
lineno: u32,
},
FrameCode {
name: String,
lineno: u32,
},
Cell {
value: Value,
},
Lock {
count: u32,
reentrant: bool,
},
StructFmt(String),
ContextVar {
name: String,
default: Option<Box<Value>>,
value: Option<Box<Value>>,
},
ContextToken {
var: Box<Value>,
old: Option<Box<Value>>,
},
Hasher {
algo: crate::stdlib::pyhash::Algo,
data: Vec<u8>,
out_len: usize,
},
CsvWriter {
stream: Value,
dialect: Box<crate::stdlib::pycsv::Dialect>,
},
CsvDialect(Box<crate::stdlib::pycsv::Dialect>),
CsvReader {
rows: Vec<Value>,
idx: usize,
dialect: Box<crate::stdlib::pycsv::Dialect>,
},
Unbound,
ContextObj,
ItertoolsIter {
kind: ItKind,
sources: Vec<Value>,
func: Value,
nums: Vec<i64>,
buf: Vec<Value>,
flag: bool,
done: bool,
},
Builtin(String),
Class(String),
Instance(Instance),
BoundMethod {
recv: Value,
func: Value,
},
Exception {
class: String,
args: Vec<Value>,
},
Iter(IterState),
Zip {
sources: Vec<Value>,
strict: bool,
done: bool,
},
MapObj {
func: Value,
sources: Vec<Value>,
done: bool,
},
FilterObj {
func: Value,
source: Value,
done: bool,
},
EnumerateObj {
source: Value,
next: i64,
done: bool,
},
CallIter {
func: Value,
sentinel: Value,
done: bool,
},
Module {
name: String,
slot: usize,
},
BytesIO {
buf: Vec<u8>,
pos: usize,
closed: bool,
},
StringIO {
buf: String,
len: usize,
pos: usize,
closed: bool,
translate: bool,
},
Template {
strings: Vec<String>,
interpolations: Vec<Value>,
},
Interpolation {
value: Value,
expression: String,
conversion: Option<char>,
format_spec: String,
},
ModuleDict {
slot: usize,
},
BigInt(num_bigint::BigInt),
Complex(f64, f64),
Generator {
id: u32,
},
Future {
id: u32,
},
EventLoop,
AsyncObj {
id: u32,
},
Bytearray(Vec<u8>),
Memoryview {
obj: Value,
start: usize,
len: usize,
readonly: bool,
},
File {
id: u32,
},
Deque {
items: VecDeque<Value>,
maxlen: Option<usize>,
},
NamedTupleType {
type_name: String,
fields: Vec<String>,
},
Partial {
func: Value,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
},
LruCache {
func: Value,
cache_id: u32,
},
Super {
owner: String,
instance: Value,
},
StaticMethod(Value),
ClassMethod(Value),
Property {
fget: Value,
fset: Value,
fdel: Value,
},
CachedProperty {
func: Value,
name: String,
},
Redirect {
stderr: bool,
target: Value,
saved: Option<Value>,
},
NotImplemented,
Ellipsis,
#[cfg(feature = "stdlib-ffi")]
Foreign(u32),
}
pub enum AttrGet {
Plain,
Property {
fget: Value,
inst: Value,
owner: Option<String>,
},
Descriptor {
desc: Value,
inst: Value,
cls: Value,
},
CachedProperty {
func: Value,
inst: Value,
name: String,
},
}
pub enum AttrSet {
Plain,
Property {
fset: Value,
inst: Value,
val: Value,
owner: Option<String>,
},
Descriptor {
desc: Value,
inst: Value,
val: Value,
},
}
pub enum AttrDel {
Plain,
Property {
fdel: Value,
inst: Value,
owner: Option<String>,
},
Descriptor {
desc: Value,
inst: Value,
has_delete: bool,
},
}
#[derive(Clone)]
pub enum IterState {
Seq { items: Vec<Value>, idx: usize },
RangeIter { cur: i64, stop: i64, step: i64 },
BigRangeIter {
cur: num_bigint::BigInt,
stop: num_bigint::BigInt,
step: num_bigint::BigInt,
},
DictKeys { keys: Vec<Value>, idx: usize },
}
pub enum IoCell {
Stdout,
Stderr,
Stdin,
File {
file: Option<std::fs::File>,
path: String,
readable: bool,
writable: bool,
},
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum DictKind {
Counter,
DefaultDict,
OrderedDict,
}
#[derive(Clone)]
pub struct DictMeta {
pub kind: DictKind,
pub factory: Option<Value>,
}
#[derive(Clone)]
pub struct NtMeta {
pub type_name: String,
pub fields: Vec<String>,
}
pub struct LruData {
pub map: IndexMap<PKey, Value>,
pub order: VecDeque<PKey>,
pub maxsize: Option<usize>,
pub hits: u64,
pub misses: u64,
}
pub struct EnvData {
pub vars: IndexMap<String, Value>,
pub parent: Option<Env>,
}
pub type Env = Rc<RefCell<EnvData>>;
fn new_env(parent: Option<Env>) -> Env {
Rc::new(RefCell::new(EnvData {
vars: IndexMap::new(),
parent,
}))
}
pub struct Frame {
pub env: Env,
pub globals_decl: HashSet<String>,
pub nonlocals_decl: HashSet<String>,
pub locals_set: Rc<HashSet<String>>,
pub is_class_body: bool,
pub self_obj: Option<Value>,
pub owner: Option<String>,
pub name: Rc<str>,
pub line: u32,
pub span: Span,
}
#[derive(Clone)]
pub enum Signal {
Return(Value),
Break,
Continue,
}
pub enum AttrCompletion {
BuiltinType(&'static str),
Names(Vec<String>),
}
pub struct PyHost {
heap: Vec<PyObj>,
pub funcs: Vec<Rc<FuncDef>>,
pub func_locals: Vec<Rc<HashSet<String>>>,
pub func_names: Vec<Rc<str>>,
pub classes: IndexMap<String, ClassDef>,
mro_cache: std::cell::RefCell<HashMap<String, std::rc::Rc<Vec<String>>>>,
pub tries: Vec<TryDef>,
module_globals: Vec<IndexMap<String, Value>>,
module_dicts: HashMap<usize, Value>,
cur_module: usize,
frames: Vec<Frame>,
pub error: Option<String>,
pub exc: Option<Value>,
pub signal: Option<Signal>,
pub pending_kw_dup: Option<String>,
generators: Vec<GenCell>,
io_handles: Vec<IoCell>,
pub dict_meta: HashMap<u32, DictMeta>,
pub nt_meta: HashMap<u32, NtMeta>,
lru_caches: Vec<LruData>,
total_ordering: HashSet<String>,
pub exc_links: HashMap<u32, (Value, Value)>,
pub exc_tb: HashMap<u32, Vec<(String, u32, Span)>>,
pub func_attrs: HashMap<u32, IndexMap<String, Value>>,
pub codec_search: Vec<Value>,
pub codec_cache: HashMap<String, Value>,
pub codec_errors: HashMap<String, Value>,
pub suppress_context: HashSet<u32>,
pub foreign_exc_bases: HashMap<String, Vec<String>>,
pub argv: Vec<String>,
pub main_file: Option<String>,
pub prog_source: String,
pub tb_filename: String,
pub tb_show_source: bool,
pub traceback: Vec<(String, u32, Span)>,
pub stdout_target: Option<Value>,
pub stderr_target: Option<Value>,
modules: IndexMap<String, Value>,
sys_modules: Option<Value>,
pub mt_states: HashMap<u32, MtState>,
pub atexit_callbacks: Vec<(Value, Vec<Value>, Vec<(String, Value)>)>,
pub regexes: Vec<crate::regexpr::PyRegex>,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum GenKind {
Generator,
Coroutine,
AsyncGen,
}
#[derive(Clone)]
pub enum AGenOp {
Send(Value),
Throw(Value),
Close,
}
struct GenCell {
kind: GenKind,
coro: Option<corosensei::Coroutine<Value, Value, Result<Value, String>>>,
yielder: *const (),
ctx: GenContext,
done: bool,
started: bool,
pending_throw: Option<Value>,
ret_value: Value,
awaiting: bool,
agen_op: Option<AGenOp>,
func_name: String,
}
#[derive(Default)]
struct GenContext {
frames: Vec<Frame>,
error: Option<String>,
exc: Option<Value>,
signal: Option<Signal>,
module: usize,
}
thread_local! {
static CUR_GEN: std::cell::Cell<Option<u32>> = const { std::cell::Cell::new(None) };
}
thread_local! {
static HOST: RefCell<PyHost> = RefCell::new(PyHost::new());
}
thread_local! {
static POSITIONS: RefCell<HashMap<u64, Vec<Span>>> = RefCell::new(HashMap::new());
}
pub fn register_positions(op_hash: u64, table: Vec<Span>) {
POSITIONS.with(|p| {
p.borrow_mut().insert(op_hash, table);
});
}
pub fn lookup_position(op_hash: u64, idx: usize) -> Span {
POSITIONS.with(|p| {
p.borrow()
.get(&op_hash)
.and_then(|t| t.get(idx))
.copied()
.unwrap_or(Span::NONE)
})
}
thread_local! {
static PENDING_KEY: RefCell<HashMap<u32, PKey>> = RefCell::new(HashMap::new());
}
fn pending_key_set(id: u32, key: PKey) {
PENDING_KEY.with(|p| p.borrow_mut().insert(id, key));
}
fn pending_key_get(id: u32) -> Option<PKey> {
PENDING_KEY.with(|p| p.borrow().get(&id).cloned())
}
fn pending_key_clear() {
PENDING_KEY.with(|p| p.borrow_mut().clear());
}
#[cfg(feature = "stdlib-ffi")]
thread_local! {
static PENDING_DISPLAY: RefCell<HashMap<u32, (String, String)>> =
RefCell::new(HashMap::new());
}
#[cfg(feature = "stdlib-ffi")]
fn pending_display_get(id: u32) -> Option<(String, String)> {
PENDING_DISPLAY.with(|p| p.borrow().get(&id).cloned())
}
#[cfg(feature = "stdlib-ffi")]
fn prefetch_foreign_display(v: &Value) {
fn walk(v: &Value, seen: &mut HashSet<u32>) {
let Value::Obj(id) = v else { return };
if !seen.insert(*id) {
return;
}
enum Kind {
Foreign(u32),
Children(Vec<Value>),
None,
}
let kind = with_host(|h| match h.get(v) {
Some(PyObj::Foreign(f)) => Kind::Foreign(*f),
Some(PyObj::List(l)) | Some(PyObj::Tuple(l)) => Kind::Children(l.clone()),
Some(PyObj::Set(s)) | Some(PyObj::Frozenset(s)) => {
Kind::Children(s.values().cloned().collect())
}
Some(PyObj::Deque { items, .. }) => Kind::Children(items.iter().cloned().collect()),
Some(PyObj::Dict(d)) => Kind::Children(
d.values()
.flat_map(|(k, val)| [k.clone(), val.clone()])
.collect(),
),
_ => Kind::None,
});
match kind {
Kind::Foreign(fid) => {
let s = crate::ffi::str_of(fid);
let r = crate::ffi::repr_of(fid);
PENDING_DISPLAY.with(|p| p.borrow_mut().insert(fid, (s, r)));
}
Kind::Children(children) => {
for c in &children {
walk(c, seen);
}
}
Kind::None => {}
}
}
let mut seen = HashSet::new();
walk(v, &mut seen);
}
pub fn str_of(v: &Value) -> String {
#[cfg(feature = "stdlib-ffi")]
prefetch_foreign_display(v);
let s = with_host(|h| h.str_of(v));
#[cfg(feature = "stdlib-ffi")]
PENDING_DISPLAY.with(|p| p.borrow_mut().clear());
s
}
pub fn repr_of(v: &Value) -> String {
#[cfg(feature = "stdlib-ffi")]
prefetch_foreign_display(v);
let s = with_host(|h| h.repr_of(v));
#[cfg(feature = "stdlib-ffi")]
PENDING_DISPLAY.with(|p| p.borrow_mut().clear());
s
}
pub fn with_host<R>(f: impl FnOnce(&mut PyHost) -> R) -> R {
HOST.with(|h| f(&mut h.borrow_mut()))
}
pub fn reset_host() {
with_host(|h| *h = PyHost::new());
VM_POOL.with(|p| p.borrow_mut().clear());
async_rt::reset();
}
pub fn init_runtime(
argv: Vec<String>,
main_file: Option<String>,
source: &str,
tb_filename: &str,
tb_show_source: bool,
) {
with_host(|h| {
h.argv = argv;
h.main_file = main_file.clone();
h.prog_source = source.to_string();
h.tb_filename = tb_filename.to_string();
h.tb_show_source = tb_show_source;
h.traceback.clear();
let name = h.new_str("__main__");
h.set_global("__name__", name);
if let Some(path) = main_file {
let f = h.new_str(path);
h.set_global("__file__", f);
}
});
}
impl Default for PyHost {
fn default() -> Self {
Self::new()
}
}
impl PyHost {
pub fn new() -> PyHost {
let module_env = new_env(None);
PyHost {
heap: Vec::new(),
funcs: Vec::new(),
func_locals: Vec::new(),
func_names: Vec::new(),
classes: IndexMap::new(),
mro_cache: std::cell::RefCell::new(HashMap::new()),
tries: Vec::new(),
module_globals: vec![IndexMap::new()],
module_dicts: HashMap::new(),
cur_module: 0,
frames: vec![Frame {
env: module_env,
globals_decl: HashSet::new(),
nonlocals_decl: HashSet::new(),
locals_set: Rc::new(HashSet::new()),
is_class_body: false,
self_obj: None,
owner: None,
name: Rc::from("<module>"),
line: 0,
span: Span::NONE,
}],
error: None,
exc: None,
signal: None,
pending_kw_dup: None,
generators: Vec::new(),
io_handles: vec![IoCell::Stdout, IoCell::Stderr, IoCell::Stdin],
dict_meta: HashMap::new(),
nt_meta: HashMap::new(),
lru_caches: Vec::new(),
total_ordering: HashSet::new(),
exc_links: HashMap::new(),
exc_tb: HashMap::new(),
func_attrs: HashMap::new(),
codec_search: Vec::new(),
codec_cache: HashMap::new(),
codec_errors: HashMap::new(),
suppress_context: HashSet::new(),
foreign_exc_bases: HashMap::new(),
argv: vec![String::new()],
main_file: None,
prog_source: String::new(),
tb_filename: "<string>".to_string(),
tb_show_source: true,
traceback: Vec::new(),
stdout_target: None,
stderr_target: None,
modules: IndexMap::new(),
sys_modules: None,
mt_states: HashMap::new(),
atexit_callbacks: Vec::new(),
regexes: Vec::new(),
}
}
pub fn cached_module(&self, name: &str) -> Option<Value> {
if let Some(m) = self.modules.get(name) {
return Some(m.clone());
}
if let Some(sm) = &self.sys_modules {
if let Some(PyObj::Dict(d)) = self.get(sm) {
if let Some((_, v)) = d.get(&PKey::Str(name.to_string())) {
return Some(v.clone());
}
}
}
None
}
pub fn cache_module(&mut self, name: &str, module: Value) {
self.modules.insert(name.to_string(), module.clone());
if let Some(sm) = self.sys_modules.clone() {
let kv = self.new_str(name.to_string());
if let Some(PyObj::Dict(d)) = self.get_mut(&sm) {
d.insert(PKey::Str(name.to_string()), (kv, module));
}
}
}
pub fn sys_module_entry(&self, name: &str) -> Option<Value> {
let sm = self.sys_modules.as_ref()?;
match self.get(sm) {
Some(PyObj::Dict(d)) => d.get(&PKey::Str(name.to_string())).map(|(_, v)| v.clone()),
_ => None,
}
}
pub fn uncache_module(&mut self, name: &str) {
self.modules.remove(name);
if let Some(sm) = self.sys_modules.clone() {
if let Some(PyObj::Dict(d)) = self.get_mut(&sm) {
d.shift_remove(&PKey::Str(name.to_string()));
}
}
}
pub fn set_exc_link(&mut self, exc: &Value, cause: Value, context: Value) {
if let Value::Obj(i) = exc {
let slot = self
.exc_links
.entry(*i)
.or_insert((Value::Undef, Value::Undef));
if !matches!(cause, Value::Undef) {
slot.0 = cause;
}
if !matches!(context, Value::Undef) {
slot.1 = context;
}
}
}
pub fn exc_link(&self, exc: &Value) -> (Value, Value) {
if let Value::Obj(i) = exc {
if let Some(links) = self.exc_links.get(i) {
return links.clone();
}
}
(Value::Undef, Value::Undef)
}
pub fn program_offsets(&self) -> (usize, usize) {
(self.funcs.len(), self.tries.len())
}
pub fn load_program(&mut self, funcs: Vec<FuncDef>, tries: Vec<TryDef>) {
for f in funcs {
self.func_locals
.push(Rc::new(f.locals.iter().cloned().collect()));
self.func_names.push(Rc::from(f.name.as_str()));
self.funcs.push(Rc::new(f));
}
self.tries.extend(tries);
}
pub fn try_def(&self, id: usize) -> Option<TryDef> {
self.tries.get(id).cloned()
}
pub fn gen_return_value(&self, id: u32) -> Value {
self.generators
.get(id as usize)
.map(|g| g.ret_value.clone())
.unwrap_or(Value::Undef)
}
pub fn alloc(&mut self, obj: PyObj) -> Value {
self.heap.push(obj);
Value::Obj((self.heap.len() - 1) as u32)
}
pub fn addr_of(&self, v: &Value) -> u64 {
match v {
Value::Obj(i) => *i as u64,
_ => 0,
}
}
pub fn get(&self, v: &Value) -> Option<&PyObj> {
if let Value::Obj(i) = v {
self.heap.get(*i as usize)
} else {
None
}
}
pub fn get_mut(&mut self, v: &Value) -> Option<&mut PyObj> {
if let Value::Obj(i) = v {
self.heap.get_mut(*i as usize)
} else {
None
}
}
#[cfg(feature = "stdlib-ffi")]
pub fn foreign_id(&self, v: &Value) -> Option<u32> {
match self.get(v) {
Some(PyObj::Foreign(id)) => Some(*id),
_ => None,
}
}
#[cfg(not(feature = "stdlib-ffi"))]
pub fn foreign_id(&self, _v: &Value) -> Option<u32> {
None
}
pub fn new_str(&mut self, s: impl Into<String>) -> Value {
self.alloc(PyObj::Str(s.into()))
}
pub fn new_list(&mut self, items: Vec<Value>) -> Value {
self.alloc(PyObj::List(items))
}
pub fn new_tuple(&mut self, items: Vec<Value>) -> Value {
self.alloc(PyObj::Tuple(items))
}
pub fn new_iter_seq(&mut self, items: Vec<Value>) -> Value {
self.alloc(PyObj::Iter(IterState::Seq { items, idx: 0 }))
}
pub fn new_dict(&mut self, pairs: IndexMap<PKey, (Value, Value)>) -> Value {
self.alloc(PyObj::Dict(pairs))
}
pub fn new_instance(&mut self, class: String, attrs: IndexMap<String, Value>) -> Value {
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::with_capacity(attrs.len());
for (k, v) in attrs {
let kv = self.new_str(k.clone());
d.insert(PKey::Str(k), (kv, v));
}
let dict = self.alloc(PyObj::Dict(d));
self.alloc(PyObj::Instance(Instance {
class,
dict,
payload: Value::Undef,
}))
}
pub fn new_instance_payload(&mut self, class: String, payload: Value) -> Value {
let dict = self.alloc(PyObj::Dict(IndexMap::new()));
self.alloc(PyObj::Instance(Instance {
class,
dict,
payload,
}))
}
pub fn builtin_base_of(&self, class: &str) -> Option<&'static str> {
for c in self.mro_of(class) {
match c.as_str() {
"list" => return Some("list"),
"dict" => return Some("dict"),
"str" => return Some("str"),
"int" => return Some("int"),
"float" => return Some("float"),
"tuple" => return Some("tuple"),
"set" => return Some("set"),
"frozenset" => return Some("frozenset"),
_ => {}
}
}
None
}
pub fn inst_attr(&self, dict: &Value, name: &str) -> Option<Value> {
match self.get(dict) {
Some(PyObj::Dict(m)) => m.get(&PKey::Str(name.to_string())).map(|(_, v)| v.clone()),
_ => None,
}
}
pub fn inst_has(&self, dict: &Value, name: &str) -> bool {
matches!(self.get(dict), Some(PyObj::Dict(m)) if m.contains_key(&PKey::Str(name.to_string())))
}
pub fn inst_attr_set(&mut self, dict: &Value, name: &str, val: Value) {
let key = PKey::Str(name.to_string());
if let Some(PyObj::Dict(m)) = self.get(dict) {
if m.contains_key(&key) {
if let Some(PyObj::Dict(m)) = self.get_mut(dict) {
if let Some(slot) = m.get_mut(&key) {
slot.1 = val;
}
}
return;
}
}
let kv = self.new_str(name.to_string());
if let Some(PyObj::Dict(m)) = self.get_mut(dict) {
m.insert(key, (kv, val));
}
}
pub fn inst_attr_del(&mut self, dict: &Value, name: &str) -> bool {
match self.get_mut(dict) {
Some(PyObj::Dict(m)) => m.shift_remove(&PKey::Str(name.to_string())).is_some(),
_ => false,
}
}
pub fn inst_attr_names(&self, dict: &Value) -> Vec<String> {
match self.get(dict) {
Some(PyObj::Dict(m)) => m
.keys()
.filter_map(|k| match k {
PKey::Str(s) => Some(s.clone()),
_ => None,
})
.collect(),
_ => Vec::new(),
}
}
pub fn new_set(&mut self, items: IndexMap<PKey, Value>) -> Value {
self.alloc(PyObj::Set(items))
}
pub fn set_ordered_values(&self, s: &IndexMap<PKey, Value>) -> Vec<Value> {
let mut hashes = Vec::with_capacity(s.len());
for k in s.keys() {
match k {
PKey::Int(n) => hashes.push(cpython_int_hash(*n)),
_ => return s.values().cloned().collect(),
}
}
let vals: Vec<&Value> = s.values().collect();
cpython_set_order(&hashes)
.into_iter()
.map(|i| vals[i].clone())
.collect()
}
pub fn new_frozenset(&mut self, items: IndexMap<PKey, Value>) -> Value {
self.alloc(PyObj::Frozenset(items))
}
pub fn new_setlike(&mut self, items: IndexMap<PKey, Value>, frozen: bool) -> Value {
if frozen {
self.alloc(PyObj::Frozenset(items))
} else {
self.alloc(PyObj::Set(items))
}
}
pub fn setlike(&self, v: &Value) -> Option<&IndexMap<PKey, Value>> {
match self.get(v) {
Some(PyObj::Set(s)) | Some(PyObj::Frozenset(s)) => Some(s),
_ => None,
}
}
pub fn is_frozenset(&self, v: &Value) -> bool {
matches!(self.get(v), Some(PyObj::Frozenset(_)))
}
fn union_members(&self, v: &Value) -> Option<Vec<Value>> {
match self.get(v) {
Some(PyObj::Union { args }) => Some(args.clone()),
Some(PyObj::Class(_)) | Some(PyObj::GenericAlias { .. }) => Some(vec![v.clone()]),
Some(PyObj::Builtin(n)) if crate::builtins::is_type_object_name(n) => {
Some(vec![v.clone()])
}
None if matches!(v, Value::Undef) => Some(vec![v.clone()]),
_ => None,
}
}
fn union_member_name(&self, v: &Value) -> String {
match self.get(v) {
Some(PyObj::Builtin(n)) if n == "NoneType" => "None".to_string(),
Some(PyObj::Builtin(n)) => n.clone(),
Some(PyObj::Class(n)) => n.clone(),
None if matches!(v, Value::Undef) => "None".to_string(),
_ => self.repr_of(v),
}
}
fn generic_arg_name(&self, v: &Value) -> String {
match self.get(v) {
Some(PyObj::Builtin(n)) => n.clone(),
Some(PyObj::Class(n)) => n.clone(),
Some(PyObj::GenericAlias { .. }) | Some(PyObj::Union { .. }) => self.repr_of(v),
None if matches!(v, Value::Undef) => "None".to_string(),
_ => self.repr_of(v),
}
}
pub fn view_items(&mut self, v: &Value) -> Option<Vec<Value>> {
let (dict, kind) = match self.get(v) {
Some(PyObj::DictView { dict, kind }) => (dict.clone(), *kind),
_ => return None,
};
let pairs: Vec<(Value, Value)> = match self.get(&dict) {
Some(PyObj::Dict(d)) => d.values().map(|(k, v)| (k.clone(), v.clone())).collect(),
_ => vec![],
};
Some(
pairs
.into_iter()
.map(|(k, v)| match kind {
0 => k,
1 => v,
_ => self.new_tuple(vec![k, v]),
})
.collect(),
)
}
pub fn setmap_of(&mut self, v: &Value) -> Option<IndexMap<PKey, Value>> {
self.setmap_operand(v, false)
}
pub fn setmap_operand(&mut self, v: &Value, other_is_set: bool) -> Option<IndexMap<PKey, Value>> {
if let Some(PyObj::Set(s)) | Some(PyObj::Frozenset(s)) = self.get(v) {
return Some(s.clone());
}
if other_is_set {
if let Some(PyObj::List(items)) | Some(PyObj::Tuple(items)) = self.get(v) {
let items = items.clone();
let mut out: IndexMap<PKey, Value> = IndexMap::new();
for it in items {
if let Ok(k) = self.to_key(&it) {
out.insert(k, it);
}
}
return Some(out);
}
}
let kind = match self.get(v) {
Some(PyObj::DictView { kind, .. }) if *kind == 0 || *kind == 2 => *kind,
_ => return None,
};
let items = self.view_items(v)?;
let mut out: IndexMap<PKey, Value> = IndexMap::new();
for it in items {
if let Ok(k) = self.to_key(&it) {
out.insert(k, it);
}
}
let _ = kind;
Some(out)
}
pub fn as_str(&self, v: &Value) -> Option<String> {
match self.get(v) {
Some(PyObj::Str(s)) => Some(s.clone()),
Some(PyObj::Instance(_)) => self.base_payload_any(v).and_then(|p| self.as_str(&p)),
_ => None,
}
}
fn frame(&self) -> &Frame {
self.frames.last().unwrap()
}
fn cur_env(&self) -> Env {
self.frame().env.clone()
}
pub fn enter_module_scope(&mut self) -> Vec<Frame> {
self.frames.split_off(1)
}
pub fn restore_scope(&mut self, parked: Vec<Frame>) {
self.frames.truncate(1);
self.frames.extend(parked);
}
pub fn caller_locals(&self) -> IndexMap<String, Value> {
let mut out: IndexMap<String, Value> = IndexMap::new();
if self.frames.len() <= 1 {
return out;
}
let mut chain: Vec<Env> = Vec::new();
let mut cur = Some(self.frame().env.clone());
while let Some(e) = cur {
cur = e.borrow().parent.clone();
chain.push(e);
}
for e in chain.into_iter().rev() {
for (k, v) in e.borrow().vars.iter() {
out.insert(k.clone(), v.clone());
}
}
out
}
pub fn frame_depth(&self) -> usize {
self.frames.len()
}
pub fn set_cur_line(&mut self, line: u32) {
if let Some(f) = self.frames.last_mut() {
f.line = line;
}
}
pub fn set_cur_line_span(&mut self, line: u32, span: Span) {
if let Some(f) = self.frames.last_mut() {
f.line = line;
f.span = span;
}
}
pub fn push_tb_frame(&mut self) {
if let Some(f) = self.frames.last() {
self.traceback.push((f.name.to_string(), f.line, f.span));
}
}
pub fn capture_exc_tb(&mut self, exc: &Value) {
let Value::Obj(id) = exc else { return };
let mut tb: Vec<(String, u32, Span)> = Vec::new();
if let Some(f) = self.frames.last() {
tb.push((f.name.to_string(), f.line, f.span));
}
for f in self.traceback.iter().rev() {
tb.push(f.clone());
}
self.exc_tb.insert(*id, tb);
}
pub fn dbg_stack(&self) -> Vec<(String, u32)> {
self.frames
.iter()
.rev()
.map(|f| (f.name.to_string(), f.line))
.collect()
}
pub fn dbg_locals(&self) -> Vec<(String, String)> {
let env = self.cur_env();
let names: Vec<String> = env
.borrow()
.vars
.keys()
.filter(|k| !k.starts_with("__"))
.cloned()
.collect();
names
.into_iter()
.map(|n| {
let v = self.read_name(&n).unwrap_or(Value::Undef);
(n, self.repr_of(&v))
})
.collect()
}
pub fn read_name(&self, name: &str) -> Option<Value> {
let mut env = Some(self.cur_env());
while let Some(e) = env {
if let Some(v) = e.borrow().vars.get(name) {
return Some(v.clone());
}
env = e.borrow().parent.clone();
}
self.globals().get(name).cloned()
}
#[inline]
fn globals(&self) -> &IndexMap<String, Value> {
&self.module_globals[self.cur_module]
}
#[inline]
fn globals_mut(&mut self) -> &mut IndexMap<String, Value> {
&mut self.module_globals[self.cur_module]
}
pub fn current_package(&self) -> String {
let g = self.globals();
if let Some(v) = g.get("__package__") {
if let Some(s) = self.as_str(v) {
return s;
}
}
if let Some(v) = g.get("__name__") {
if let Some(n) = self.as_str(v) {
return match n.rsplit_once('.') {
Some((parent, _)) => parent.to_string(),
None => String::new(),
};
}
}
String::new()
}
#[inline]
pub fn module_slot(&self, v: &Value) -> Option<usize> {
match self.get(v) {
Some(PyObj::Module { slot, .. }) => Some(*slot),
_ => None,
}
}
#[inline]
pub fn module_ns(&self, slot: usize) -> &IndexMap<String, Value> {
&self.module_globals[slot]
}
#[inline]
pub fn module_ns_mut(&mut self, slot: usize) -> &mut IndexMap<String, Value> {
&mut self.module_globals[slot]
}
pub fn set_module_attr(&mut self, module: &Value, name: &str, val: Value) {
if let Some(slot) = self.module_slot(module) {
self.module_globals[slot].insert(name.to_string(), val);
}
}
#[inline]
pub fn cur_module(&self) -> usize {
self.cur_module
}
#[inline]
pub fn swap_module(&mut self, m: usize) -> usize {
std::mem::replace(&mut self.cur_module, m)
}
pub fn new_module_slot(&mut self, ns: IndexMap<String, Value>) -> usize {
let id = self.module_globals.len();
self.module_globals.push(ns);
id
}
pub fn module_globals_pairs(&self, id: usize) -> Vec<(String, Value)> {
self.module_globals[id]
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect()
}
pub fn read_global(&self, name: &str) -> Option<Value> {
self.globals().get(name).cloned()
}
pub fn module_dict(&mut self, slot: usize) -> Value {
if let Some(v) = self.module_dicts.get(&slot) {
return v.clone();
}
let v = self.alloc(PyObj::ModuleDict { slot });
self.module_dicts.insert(slot, v.clone());
v
}
pub fn module_dict_snapshot(&mut self, v: &Value) -> Option<Value> {
let slot = match self.get(v) {
Some(PyObj::ModuleDict { slot }) => *slot,
_ => return None,
};
let pairs = self.module_globals_pairs(slot);
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::with_capacity(pairs.len());
for (k, val) in pairs {
let kv = self.new_str(k.clone());
d.insert(PKey::Str(k), (kv, val));
}
Some(self.new_dict(d))
}
#[inline]
pub fn module_dict_slot(&self, v: &Value) -> Option<usize> {
match self.get(v) {
Some(PyObj::ModuleDict { slot }) => Some(*slot),
_ => None,
}
}
fn env_lookup(&self, env: &Env, name: &str) -> Option<Value> {
let mut cur = Some(env.clone());
while let Some(e) = cur {
if let Some(v) = e.borrow().vars.get(name) {
return Some(v.clone());
}
cur = e.borrow().parent.clone();
}
None
}
pub fn read_name_checked(&self, name: &str) -> NameRead {
let f = self.frame();
if f.locals_set.contains(name)
&& !f.globals_decl.contains(name)
&& !f.nonlocals_decl.contains(name)
{
return match self.cur_env().borrow().vars.get(name) {
Some(v) => NameRead::Value(v.clone()),
None => NameRead::Unbound,
};
}
match self.read_name(name) {
Some(v) => NameRead::Value(v),
None => NameRead::Missing,
}
}
pub fn call_display_name(&self, name: &str) -> String {
match self.read_name(name).and_then(|v| self.get(&v).cloned()) {
Some(PyObj::Func(fv)) => {
let q = self.funcs.get(fv.def_id).map_or(name, |d| d.name.as_str());
format!("__main__.{q}")
}
Some(PyObj::Class(_)) => format!("__main__.{name}"),
_ => name.to_string(),
}
}
pub fn set_name(&mut self, name: &str, val: Value) {
if self.frame().globals_decl.contains(name) {
self.globals_mut().insert(name.to_string(), val);
return;
}
if self.frame().nonlocals_decl.contains(name) {
let cur = self.cur_env();
let mut env = cur.borrow().parent.clone();
while let Some(e) = env {
if e.borrow().vars.contains_key(name) {
e.borrow_mut().vars.insert(name.to_string(), val);
return;
}
let parent = e.borrow().parent.clone();
env = parent;
}
let parent = cur.borrow().parent.clone();
if let Some(p) = parent {
p.borrow_mut().vars.insert(name.to_string(), val);
return;
}
}
let cur = self.cur_env();
if cur.borrow().parent.is_none() {
self.globals_mut().insert(name.to_string(), val);
} else {
cur.borrow_mut().vars.insert(name.to_string(), val);
}
}
pub fn set_global(&mut self, name: &str, val: Value) {
self.globals_mut().insert(name.to_string(), val);
}
fn str_sequence(&self, v: &Value) -> Result<Vec<String>, String> {
match self.get(v) {
Some(PyObj::List(items)) | Some(PyObj::Tuple(items)) => items
.iter()
.map(|it| {
self.as_str(it)
.ok_or_else(|| type_error("__all__ must contain only strings"))
})
.collect(),
_ => Err(type_error("__all__ must be a list or tuple of str")),
}
}
pub fn import_star_bindings(&mut self, module: &Value) -> Result<Vec<(String, Value)>, String> {
let foreign = self.foreign_id(module);
match self.module_slot(module).map(|s| self.module_globals[s].clone()) {
Some(ns) => {
if let Some(all) = ns.get("__all__").cloned() {
let mut out = Vec::new();
for n in self.str_sequence(&all)? {
if let Some(v) = ns.get(&n) {
out.push((n, v.clone()));
}
}
Ok(out)
} else {
Ok(ns
.iter()
.filter(|(k, _)| !k.starts_with('_'))
.map(|(k, v)| (k.clone(), v.clone()))
.collect())
}
}
_ => match foreign {
Some(id) => {
let all = crate::ffi::get_attr(self, id, "__all__").map_err(|_| {
type_error("'import *' from this module needs an __all__ it does not define")
})?;
let mut out = Vec::new();
for n in self.str_sequence(&all)? {
if let Ok(v) = crate::ffi::get_attr(self, id, &n) {
out.push((n, v));
}
}
Ok(out)
}
None => Err(type_error("'import *' requires a module object")),
},
}
}
pub fn del_global(&mut self, name: &str) -> Option<Value> {
self.globals_mut().shift_remove(name)
}
pub fn snapshot_globals(&self) -> IndexMap<String, Value> {
self.globals().clone()
}
pub fn replace_globals(&mut self, g: IndexMap<String, Value>) {
*self.globals_mut() = g;
}
pub fn globals_pairs(&self) -> Vec<(String, Value)> {
self.globals()
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect()
}
pub fn global_names(&self) -> Vec<String> {
let mut names: Vec<String> = self.globals().keys().cloned().collect();
names.extend(self.classes.keys().cloned());
names.sort();
names.dedup();
names
}
pub fn attr_completions(&self, base: &str) -> Option<AttrCompletion> {
let v = self.read_global(base)?;
match &v {
Value::Bool(_) | Value::Int(_) => return Some(AttrCompletion::BuiltinType("int")),
Value::Float(_) => return Some(AttrCompletion::BuiltinType("float")),
Value::Str(_) => return Some(AttrCompletion::BuiltinType("str")),
Value::Obj(_) => {}
_ => return None,
}
match self.get(&v)? {
PyObj::Str(_) => Some(AttrCompletion::BuiltinType("str")),
PyObj::Bytes(_) | PyObj::Bytearray(_) => Some(AttrCompletion::BuiltinType("bytes")),
PyObj::List(_) => Some(AttrCompletion::BuiltinType("list")),
PyObj::Tuple(_) => Some(AttrCompletion::BuiltinType("tuple")),
PyObj::Dict(_) => Some(AttrCompletion::BuiltinType("dict")),
PyObj::Set(_) => Some(AttrCompletion::BuiltinType("set")),
PyObj::Frozenset(_) => Some(AttrCompletion::BuiltinType("frozenset")),
PyObj::BigInt(_) => Some(AttrCompletion::BuiltinType("int")),
PyObj::Module { slot, .. } => Some(AttrCompletion::Names(
self.module_globals[*slot].keys().cloned().collect(),
)),
PyObj::Instance(i) => {
let class = i.class.clone();
let dict = i.dict.clone();
let mut names = self.inst_attr_names(&dict);
if let Some(c) = self.classes.get(&class) {
for cls in &c.mro {
if let Some(cd) = self.classes.get(cls) {
names.extend(cd.ns.keys().cloned());
}
}
}
names.sort();
names.dedup();
Some(AttrCompletion::Names(names))
}
_ => None,
}
}
pub fn del_name(&mut self, name: &str) -> Result<(), String> {
if self
.cur_env()
.borrow_mut()
.vars
.shift_remove(name)
.is_some()
{
return Ok(());
}
if self.globals_mut().shift_remove(name).is_some() {
return Ok(());
}
Err(name_error(name))
}
pub fn declare_global(&mut self, name: &str) {
self.frames
.last_mut()
.unwrap()
.globals_decl
.insert(name.to_string());
}
pub fn declare_nonlocal(&mut self, name: &str) {
self.frames
.last_mut()
.unwrap()
.nonlocals_decl
.insert(name.to_string());
}
pub fn current_self(&self) -> Option<Value> {
self.frame().self_obj.clone()
}
pub fn current_owner(&self) -> Option<String> {
self.frame().owner.clone()
}
pub fn take_error(&mut self) -> Option<String> {
self.error.take()
}
pub fn has_signal(&self) -> bool {
self.signal.is_some() || self.error.is_some()
}
pub fn raise_str(&mut self, class: &str, msg: &str) -> String {
let s = if msg.is_empty() {
class.to_string()
} else {
format!("{class}: {msg}")
};
self.error = Some(s.clone());
s
}
}
pub fn name_error(name: &str) -> String {
format!("NameError: name '{name}' is not defined")
}
pub fn unbound_local_error(name: &str) -> String {
format!(
"UnboundLocalError: cannot access local variable '{name}' where it is not associated with a value"
)
}
pub enum NameRead {
Value(Value),
Unbound,
Missing,
}
pub fn type_error(msg: &str) -> String {
format!("TypeError: {msg}")
}
pub fn callable_display_name(callable: &Value) -> String {
with_host(|h| match h.get(callable) {
Some(PyObj::Func(fv)) => {
let q = h
.funcs
.get(fv.def_id)
.map_or("<callable>", |d| d.name.as_str());
format!("__main__.{q}")
}
_ => "<callable>".to_string(),
})
}
pub const PY_MAJOR: i64 = 3;
pub const PY_MINOR: i64 = 14;
pub const PY_MICRO: i64 = 6;
pub fn py_platform() -> &'static str {
match std::env::consts::OS {
"macos" => "darwin",
"windows" => "win32",
other => other,
}
}
thread_local! {
static DEBUG_MODE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
pub fn set_debug_mode(on: bool) {
DEBUG_MODE.with(|d| d.set(on));
}
thread_local! {
static REPR_GUARD: std::cell::RefCell<Vec<u32>> = const { std::cell::RefCell::new(Vec::new()) };
}
pub fn repr_guard_enter(id: u32) -> bool {
REPR_GUARD.with(|g| {
let mut g = g.borrow_mut();
if g.contains(&id) {
true
} else {
g.push(id);
false
}
})
}
pub fn repr_guard_leave(id: u32) {
REPR_GUARD.with(|g| {
let mut g = g.borrow_mut();
if let Some(pos) = g.iter().rposition(|&x| x == id) {
g.remove(pos);
}
});
}
thread_local! {
static VM_POOL: RefCell<HashMap<u64, Vec<Box<VM>>>> = RefCell::new(HashMap::new());
}
pub fn run_chunk_on(chunk: Chunk) -> Result<Value, String> {
finish_run(Box::new(new_configured_vm(chunk)), None)
}
fn new_configured_vm(chunk: Chunk) -> VM {
let mut vm = VM::new(chunk);
crate::builtins::install(&mut vm);
vm.set_numeric_hook(std::sync::Arc::new(|op, a, b| {
crate::builtins::numeric_hook(op, a, b)
}));
if DEBUG_MODE.with(|d| d.get()) {
vm.set_extension_handler(Box::new(|vm, id, _| {
crate::dap::on_ext(vm, id);
}));
} else {
vm.enable_tracing_jit();
}
vm
}
fn finish_run(mut vm: Box<VM>, pool_key: Option<u64>) -> Result<Value, String> {
let outcome = vm.run();
let halted_top = matches!(outcome, VMResult::Halted)
.then(|| vm.stack.last().cloned().unwrap_or(Value::Undef));
if let VMResult::Error(_) = &outcome {
crate::builtins::record_err_line(&vm);
}
if let Some(key) = pool_key {
VM_POOL.with(|p| p.borrow_mut().entry(key).or_default().push(vm));
}
if let Some(e) = with_host(|h| h.take_error()) {
return Err(e);
}
match outcome {
VMResult::Ok(v) => Ok(v),
VMResult::Halted => Ok(halted_top.unwrap_or(Value::Undef)),
VMResult::Error(e) => Err(e),
}
}
pub fn run_chunk_cached(key: u64, make: impl FnOnce() -> Chunk) -> Result<Value, String> {
let pooled = VM_POOL.with(|p| p.borrow_mut().get_mut(&key).and_then(|v| v.pop()));
let vm = match pooled {
Some(mut vm) => {
let own = std::mem::take(&mut vm.chunk);
vm.reset(own);
vm
}
None => Box::new(new_configured_vm(make())),
};
finish_run(vm, Some(key))
}
pub fn run_main(chunk: Chunk) -> Result<Value, String> {
let r = run_chunk_on(chunk);
with_host(|h| h.signal = None);
r
}
fn type_object_class_name(n: &str) -> Option<String> {
let qualified = match n {
"Counter" => Some("collections.Counter"),
"defaultdict" => Some("collections.defaultdict"),
"OrderedDict" => Some("collections.OrderedDict"),
"deque" => Some("collections.deque"),
"partial" => Some("functools.partial"),
"TextIOWrapper" => Some("_io.TextIOWrapper"),
"functools._lru_cache_wrapper" => Some("functools._lru_cache_wrapper"),
"re.Pattern" => Some("re.Pattern"),
"os.stat_result" => Some("os.stat_result"),
_ if n.strip_prefix("_io.")
.is_some_and(|t| crate::stdlib::pyio::STREAM_TYPES.contains(&t)) =>
{
Some(n)
}
"re.Match" => Some("re.Match"),
"string.templatelib.Template" => Some("string.templatelib.Template"),
"string.templatelib.Interpolation" => Some("string.templatelib.Interpolation"),
_ => None,
};
if let Some(q) = qualified {
return Some(q.to_string());
}
if crate::builtins::is_exception_class(n) {
return Some(n.to_string());
}
let unqualified = matches!(
n,
"int"
| "float"
| "str"
| "bool"
| "list"
| "tuple"
| "dict"
| "set"
| "frozenset"
| "bytes"
| "bytearray"
| "memoryview"
| "complex"
| "object"
| "type"
| "range"
| "slice"
| "NoneType"
| "NotImplementedType"
| "ellipsis"
| "function"
| "builtin_function_or_method"
| "method"
| "module"
| "property"
| "staticmethod"
| "classmethod"
| "super"
| "iterator"
| "callable_iterator"
| "zip"
| "map"
| "filter"
| "enumerate"
| "generator"
| "coroutine"
| "async_generator"
| "dict_keys"
| "dict_values"
| "dict_items"
);
unqualified.then(|| n.to_string())
}
#[cfg(feature = "stdlib-ffi")]
fn module_ffi_fallback(
host: &mut PyHost,
mname: &str,
name: &str,
) -> Option<Result<Value, String>> {
if !matches!(mname, "math" | "collections" | "functools" | "contextlib") {
return None;
}
match crate::ffi::import(mname) {
Ok(id) => Some(crate::ffi::get_attr(host, id, name)),
Err(e) => Some(Err(e)),
}
}
#[cfg(not(feature = "stdlib-ffi"))]
fn module_ffi_fallback(
_host: &mut PyHost,
_mname: &str,
_name: &str,
) -> Option<Result<Value, String>> {
None
}
impl PyHost {
pub fn type_name(&self, v: &Value) -> String {
match v {
Value::Undef => "NoneType".into(),
Value::Bool(_) => "bool".into(),
Value::Int(_) => "int".into(),
Value::Float(_) => "float".into(),
Value::Str(_) => "str".into(),
Value::Obj(_) => match self.get(v) {
Some(PyObj::StructFmt(_)) => "Struct".into(),
Some(PyObj::ContextVar { .. }) => "ContextVar".into(),
Some(PyObj::ContextToken { .. }) => "Token".into(),
Some(PyObj::ContextObj) => "Context".into(),
Some(PyObj::Unbound) => "unbound".into(),
Some(PyObj::CsvWriter { .. }) => "_csv.writer".into(),
Some(PyObj::CsvDialect(_)) => "Dialect".into(),
Some(PyObj::CsvReader { .. }) => "_csv.reader".into(),
Some(PyObj::Hasher { algo, .. }) => (*algo).name().into(),
Some(PyObj::Str(_)) => "str".into(),
Some(PyObj::Bytes(_)) => "bytes".into(),
Some(PyObj::Bytearray(_)) => "bytearray".into(),
Some(PyObj::Memoryview { .. }) => "memoryview".into(),
Some(PyObj::List(_)) => "list".into(),
Some(PyObj::Tuple(_)) => match v {
Value::Obj(i) => match self.nt_meta.get(i) {
Some(m) => m.type_name.clone(),
None => "tuple".into(),
},
_ => "tuple".into(),
},
Some(PyObj::Dict(_)) => match v {
Value::Obj(i) => match self.dict_meta.get(i).map(|m| m.kind) {
Some(DictKind::Counter) => "Counter".into(),
Some(DictKind::DefaultDict) => "defaultdict".into(),
Some(DictKind::OrderedDict) => "OrderedDict".into(),
None => "dict".into(),
},
_ => "dict".into(),
},
Some(PyObj::Set(_)) => "set".into(),
Some(PyObj::Frozenset(_)) => "frozenset".into(),
Some(PyObj::DictView { kind, .. }) => match kind {
0 => "dict_keys".into(),
1 => "dict_values".into(),
_ => "dict_items".into(),
},
Some(PyObj::Range { .. }) | Some(PyObj::BigRange { .. }) => "range".into(),
Some(PyObj::Slice { .. }) => "slice".into(),
Some(PyObj::Func(_)) => "function".into(),
Some(PyObj::Builtin(n)) => {
if crate::builtins::is_type_like_builtin(n) {
"type".into()
} else {
"builtin_function_or_method".into()
}
}
Some(PyObj::Class(n)) => self
.classes
.get(n)
.map(|c| c.metaclass.clone())
.unwrap_or_else(|| "type".into()),
Some(PyObj::Instance(i)) => i.class.clone(),
Some(PyObj::BoundMethod { .. }) => "method".into(),
Some(PyObj::Exception { class, .. }) => class.clone(),
Some(PyObj::Iter(_)) => "iterator".into(),
Some(PyObj::Zip { .. }) => "zip".into(),
Some(PyObj::MapObj { .. }) => "map".into(),
Some(PyObj::FilterObj { .. }) => "filter".into(),
Some(PyObj::EnumerateObj { .. }) => "enumerate".into(),
Some(PyObj::CallIter { .. }) => "callable_iterator".into(),
Some(PyObj::Module { .. }) => "module".into(),
Some(PyObj::ModuleDict { .. }) => "dict".into(),
Some(PyObj::BytesIO { .. }) => "_io.BytesIO".into(),
Some(PyObj::StringIO { .. }) => "_io.StringIO".into(),
Some(PyObj::Template { .. }) => "string.templatelib.Template".into(),
Some(PyObj::Interpolation { .. }) => "string.templatelib.Interpolation".into(),
Some(PyObj::BigInt(_)) => "int".into(),
Some(PyObj::Complex(..)) => "complex".into(),
Some(PyObj::Generator { id }) => match self.generators[*id as usize].kind {
GenKind::Coroutine => "coroutine".into(),
GenKind::Generator => "generator".into(),
GenKind::AsyncGen => "async_generator".into(),
},
Some(PyObj::Future { id }) => async_rt::future_type_name(*id).into(),
Some(PyObj::EventLoop) => "_UnixSelectorEventLoop".into(),
Some(PyObj::AsyncObj { id }) => async_rt::async_obj_type_name(*id).into(),
Some(PyObj::File { .. }) => "TextIOWrapper".into(),
Some(PyObj::Deque { .. }) => "deque".into(),
Some(PyObj::NamedTupleType { .. }) => "type".into(),
Some(PyObj::Partial { .. }) => "partial".into(),
Some(PyObj::Code { .. }) => "code".into(),
Some(PyObj::Union { .. }) => "UnionType".into(),
Some(PyObj::GenericAlias { .. }) => "GenericAlias".into(),
Some(PyObj::TypeVarLike { kind, .. }) => kind.type_name().into(),
Some(PyObj::StructTime { .. }) => "struct_time".into(),
Some(PyObj::Pattern { .. }) => "re.Pattern".into(),
Some(PyObj::Match { .. }) => "re.Match".into(),
Some(PyObj::Namespace { .. }) => "SimpleNamespace".into(),
Some(PyObj::MappingProxy { .. }) => "mappingproxy".into(),
Some(PyObj::Descriptor { kind, .. }) => kind.type_name().into(),
Some(PyObj::Traceback { .. }) => "traceback".into(),
Some(PyObj::PyFrame { .. }) => "frame".into(),
Some(PyObj::FrameCode { .. }) => "code".into(),
Some(PyObj::Cell { .. }) => "cell".into(),
Some(PyObj::Lock { reentrant, .. }) => {
if *reentrant { "RLock" } else { "lock" }.into()
}
Some(PyObj::ItertoolsIter { kind, .. }) => kind.type_name().into(),
Some(PyObj::LruCache { .. }) => "functools._lru_cache_wrapper".into(),
Some(PyObj::Super { .. }) => "super".into(),
Some(PyObj::StaticMethod(_)) => "staticmethod".into(),
Some(PyObj::ClassMethod(_)) => "classmethod".into(),
Some(PyObj::Property { .. }) => "property".into(),
Some(PyObj::CachedProperty { .. }) => "cached_property".into(),
Some(PyObj::Redirect { stderr, .. }) => {
if *stderr {
"redirect_stderr".into()
} else {
"redirect_stdout".into()
}
}
Some(PyObj::NotImplemented) => "NotImplementedType".into(),
Some(PyObj::Ellipsis) => "ellipsis".into(),
#[cfg(feature = "stdlib-ffi")]
Some(PyObj::Foreign(id)) => crate::ffi::type_name(*id),
None => "object".into(),
},
_ => "object".into(),
}
}
pub fn truthy(&self, v: &Value) -> bool {
match v {
Value::Undef => false,
Value::Bool(b) => *b,
Value::Int(n) => *n != 0,
Value::Float(f) => *f != 0.0,
Value::Str(s) => !s.is_empty(),
Value::Obj(_) => match self.get(v) {
Some(PyObj::Str(s)) => !s.is_empty(),
Some(PyObj::Bytes(b)) => !b.is_empty(),
Some(PyObj::Bytearray(b)) => !b.is_empty(),
Some(PyObj::Memoryview { len, .. }) => *len != 0,
Some(PyObj::Deque { items, .. }) => !items.is_empty(),
Some(PyObj::List(l)) => !l.is_empty(),
Some(PyObj::Tuple(l)) => !l.is_empty(),
Some(PyObj::Dict(d)) => !d.is_empty(),
Some(PyObj::Set(s)) => !s.is_empty(),
Some(PyObj::Frozenset(s)) => !s.is_empty(),
Some(PyObj::DictView { dict, .. }) => {
matches!(self.get(dict), Some(PyObj::Dict(d)) if !d.is_empty())
}
Some(PyObj::Range { start, stop, step }) => range_len(*start, *stop, *step) != 0,
Some(PyObj::BigRange { start, stop, step }) => {
use num_traits::Zero;
!big_range_len(start, stop, step).is_zero()
}
Some(PyObj::BigInt(b)) => *b != num_bigint::BigInt::from(0),
Some(PyObj::Complex(r, i)) => *r != 0.0 || *i != 0.0,
Some(PyObj::Instance(_)) => true, #[cfg(feature = "stdlib-ffi")]
Some(PyObj::Foreign(id)) => crate::ffi::truthy(*id),
_ => true,
},
_ => true,
}
}
pub fn str_of(&self, v: &Value) -> String {
match v {
Value::Undef => "None".into(),
Value::Bool(b) => if *b { "True" } else { "False" }.into(),
Value::Int(n) => n.to_string(),
Value::Float(f) => fmt_float(*f),
Value::Str(s) => (**s).clone(),
Value::Obj(_) => match self.get(v) {
Some(PyObj::StructFmt(f)) => format!("<_struct.Struct object, format '{f}'>"),
Some(PyObj::BytesIO { .. }) => "<_io.BytesIO object>".to_string(),
Some(PyObj::StringIO { .. }) => "<_io.StringIO object>".to_string(),
Some(PyObj::ContextVar { name, .. }) => format!("<ContextVar name='{name}'>"),
Some(PyObj::ContextToken { .. }) => "<Token>".to_string(),
Some(PyObj::ContextObj) => "<Context>".to_string(),
Some(PyObj::Unbound) => "<unbound local>".to_string(),
Some(PyObj::CsvWriter { .. }) => "<_csv.writer object>".to_string(),
Some(PyObj::CsvDialect(_)) => "<_csv.Dialect object>".to_string(),
Some(PyObj::CsvReader { .. }) => "<_csv.reader object>".to_string(),
Some(PyObj::Hasher { algo, .. }) => {
format!("<{} _hashlib.HASH object>", algo.name())
}
Some(PyObj::Str(s)) => s.clone(),
Some(PyObj::BigInt(b)) => b.to_string(),
Some(PyObj::Complex(r, i)) => fmt_complex(*r, *i),
Some(PyObj::Bytes(b)) => format!("b{}", quote_bytes(b, false)),
Some(PyObj::Instance(inst)) => {
if self.class_is_exception(&inst.class) {
let a = self.exc_instance_args(&inst.dict);
self.exc_message(&inst.class, &a)
} else if !matches!(inst.payload, Value::Undef)
&& self.builtin_base_of(&inst.class).is_some()
&& self.class_lookup(&inst.class, "__str__").is_none()
&& self.class_lookup(&inst.class, "__repr__").is_none()
{
self.str_of(&inst.payload)
} else {
format!(
"<__main__.{} object at 0x{:012x}>",
inst.class,
self.addr_of(v)
)
}
}
Some(PyObj::Class(n)) => format!("<class '__main__.{n}'>"),
Some(PyObj::Func(f)) => {
let name = self
.funcs
.get(f.def_id)
.map(|d| d.name.clone())
.unwrap_or_default();
format!("<function {name}>")
}
Some(PyObj::Code { def_id }) => {
let name = self
.funcs
.get(*def_id)
.map(|d| d.name.clone())
.unwrap_or_default();
format!("<code object {name} at 0x0000000000000000, file \"<string>\", line 1>")
}
Some(PyObj::TypeVarLike { name, .. }) => name.clone(),
Some(PyObj::StructTime { fields }) => {
let fields = fields.clone();
let parts: Vec<String> = STRUCT_TIME_FIELDS
.iter()
.zip(fields.iter())
.filter(|(_, v)| !matches!(v, Value::Undef))
.map(|(name, v)| format!("{name}={}", self.repr_of(v)))
.collect();
format!("time.struct_time({})", parts.join(", "))
}
Some(PyObj::Pattern { pattern, .. }) => {
let shown: String = pattern.chars().take(200).collect();
let q = shown.replace('\\', "\\\\").replace('\'', "\\'");
format!("re.compile('{q}')")
}
Some(PyObj::Match { text, spans, .. }) => {
let (s, e) = spans.first().copied().flatten().unwrap_or((0, 0));
let matched = text.get(s..e).unwrap_or("");
let q = matched.replace('\\', "\\\\").replace('\'', "\\'");
format!("<re.Match object; span=({s}, {e}), match='{q}'>")
}
Some(PyObj::Union { args }) => {
let args = args.clone();
args.iter()
.map(|a| self.union_member_name(a))
.collect::<Vec<_>>()
.join(" | ")
}
Some(PyObj::GenericAlias { origin, args }) => {
let (origin, args) = (origin.clone(), args.clone());
let inner = args
.iter()
.map(|a| self.generic_arg_name(a))
.collect::<Vec<_>>()
.join(", ");
format!("{}[{inner}]", self.generic_arg_name(&origin))
}
Some(PyObj::Namespace { attrs }) => {
let attrs: Vec<(String, Value)> =
attrs.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
let inner = attrs
.iter()
.map(|(k, v)| format!("{k}={}", self.repr_of(v)))
.collect::<Vec<_>>()
.join(", ");
format!("namespace({inner})")
}
Some(PyObj::MappingProxy { dict }) => {
let dict = dict.clone();
format!("mappingproxy({})", self.repr_of(&dict))
}
Some(PyObj::Descriptor { kind, qual }) => {
let (kind, qual) = (*kind, qual.clone());
let (owner, name) = qual.split_once('.').unwrap_or(("", qual.as_str()));
match kind {
DescKind::MethodWrapper => {
format!("<method-wrapper '{name}' of object>")
}
DescKind::GetSetDescriptor | DescKind::MemberDescriptor => {
format!("<attribute '{name}' of '{owner}' objects>")
}
DescKind::WrapperDescriptor => {
format!("<slot wrapper '{name}' of '{owner}' objects>")
}
_ => format!("<{} '{name}' of '{owner}' objects>", kind.type_name()),
}
}
Some(PyObj::Traceback { .. }) => {
format!("<traceback object at 0x{:012x}>", self.addr_of(v))
}
Some(PyObj::FrameCode { name, .. }) => format!("<code object {name}>"),
Some(PyObj::PyFrame { name, lineno }) => {
format!(
"<frame at 0x{:012x}, file '<string>', line {lineno}, code {name}>",
self.addr_of(v)
)
}
Some(PyObj::Cell { value }) => {
let value = value.clone();
if matches!(value, Value::Undef) {
format!("<cell at 0x{:012x}: empty>", self.addr_of(v))
} else {
format!(
"<cell at 0x{:012x}: {} object at 0x{:012x}>",
self.addr_of(v),
self.type_name(&value),
self.addr_of(&value)
)
}
}
Some(PyObj::Builtin(n)) => {
if let Some((tp, meth)) = n.split_once('.') {
if crate::builtins::type_has_method(tp, meth) {
return format!("<method '{meth}' of '{tp}' objects>");
}
}
match type_object_class_name(n) {
Some(cls) => format!("<class '{cls}'>"),
None => format!("<built-in function {n}>"),
}
}
Some(PyObj::BoundMethod { .. }) => "<bound method>".into(),
Some(PyObj::Exception { class, args }) => self.exc_str(class, args),
Some(PyObj::Module { name, .. }) => format!("<module '{name}'>"),
Some(PyObj::Template {
strings,
interpolations,
}) => {
let ss: Vec<String> = strings.iter().map(|s| quote_str(s)).collect();
let is: Vec<String> = interpolations.iter().map(|v| self.repr_of(v)).collect();
format!(
"Template(strings=({}), interpolations=({}))",
tuple_body(&ss),
tuple_body(&is)
)
}
Some(PyObj::Interpolation {
value,
expression,
conversion,
format_spec,
}) => format!(
"Interpolation({}, {}, {}, {})",
self.repr_of(value),
quote_str(expression),
match conversion {
Some(c) => quote_str(&c.to_string()),
None => "None".to_string(),
},
quote_str(format_spec)
),
Some(PyObj::ModuleDict { slot }) => {
let items: Vec<String> = self.module_globals[*slot]
.iter()
.map(|(k, v)| format!("{}: {}", quote_str(k), self.repr_of(v)))
.collect();
format!("{{{}}}", items.join(", "))
}
Some(PyObj::Range { start, stop, step }) => {
if *step == 1 {
format!("range({start}, {stop})")
} else {
format!("range({start}, {stop}, {step})")
}
}
Some(PyObj::BigRange { start, stop, step }) => {
use num_traits::One;
if step.is_one() {
format!("range({start}, {stop})")
} else {
format!("range({start}, {stop}, {step})")
}
}
Some(PyObj::Iter(_)) => "<iterator>".into(),
Some(PyObj::Zip { .. }) => format!("<zip object at 0x{:012x}>", self.addr_of(v)),
Some(PyObj::ItertoolsIter { kind, .. }) => {
format!("<{} object at 0x{:012x}>", kind.type_name(), self.addr_of(v))
}
Some(PyObj::Lock { count, reentrant }) => {
let state = if *count > 0 { "locked" } else { "unlocked" };
let kind = if *reentrant { "_thread.RLock" } else { "_thread.lock" };
format!("<{state} {kind} object at 0x{:012x}>", self.addr_of(v))
}
Some(PyObj::MapObj { .. }) => format!("<map object at 0x{:012x}>", self.addr_of(v)),
Some(PyObj::FilterObj { .. }) => {
format!("<filter object at 0x{:012x}>", self.addr_of(v))
}
Some(PyObj::EnumerateObj { .. }) => {
format!("<enumerate object at 0x{:012x}>", self.addr_of(v))
}
Some(PyObj::CallIter { .. }) => {
format!("<callable_iterator object at 0x{:012x}>", self.addr_of(v))
}
Some(PyObj::Generator { id }) => {
let g = &self.generators[*id as usize];
let nm = g
.ctx
.frames
.first()
.map(|f| f.name.clone())
.unwrap_or_default();
match g.kind {
GenKind::Coroutine => {
format!("<coroutine object {nm} at 0x{:012x}>", self.addr_of(v))
}
GenKind::Generator => {
format!("<generator object {nm} at 0x{:012x}>", self.addr_of(v))
}
GenKind::AsyncGen => {
format!(
"<async_generator object {nm} at 0x{:012x}>",
self.addr_of(v)
)
}
}
}
Some(PyObj::Future { id }) => async_rt::future_repr(*id),
Some(PyObj::EventLoop) => {
"<_UnixSelectorEventLoop running=False closed=False debug=False>".into()
}
Some(PyObj::AsyncObj { id }) => async_rt::async_obj_repr(*id),
Some(PyObj::Bytearray(b)) => format!("bytearray(b{})", quote_bytes(b, true)),
Some(PyObj::Memoryview { .. }) => {
format!("<memory at 0x{:012x}>", self.addr_of(v))
}
Some(PyObj::File { id }) => self.file_repr(*id),
Some(PyObj::Deque { items, maxlen }) => {
let inner: Vec<String> = items.iter().map(|x| self.repr_of(x)).collect();
match maxlen {
Some(m) => format!("deque([{}], maxlen={m})", inner.join(", ")),
None => format!("deque([{}])", inner.join(", ")),
}
}
Some(PyObj::NamedTupleType { type_name, .. }) => format!("<class '{type_name}'>"),
Some(PyObj::Partial { func, .. }) => {
format!("functools.partial({})", self.repr_of(func))
}
Some(PyObj::LruCache { func, .. }) => {
format!("<functools._lru_cache_wrapper {}>", self.str_of(func))
}
Some(PyObj::Super { owner, instance }) => {
let icls = match self.get(instance) {
Some(PyObj::Instance(i)) => i.class.clone(),
_ => owner.clone(),
};
format!("<super: <class '{owner}'>, <{icls} object>>")
}
Some(PyObj::StaticMethod(f)) => {
format!("<staticmethod({})>", self.str_of(f))
}
Some(PyObj::ClassMethod(f)) => {
format!("<classmethod({})>", self.str_of(f))
}
Some(PyObj::Property { .. }) => "<property object>".into(),
Some(PyObj::CachedProperty { .. }) => "<functools.cached_property object>".into(),
Some(PyObj::Redirect { stderr, .. }) => {
let nm = if *stderr {
"redirect_stderr"
} else {
"redirect_stdout"
};
format!("<contextlib.{nm} object at 0x{:012x}>", self.addr_of(v))
}
Some(PyObj::NotImplemented) => "NotImplemented".into(),
Some(PyObj::Ellipsis) => "Ellipsis".into(),
#[cfg(feature = "stdlib-ffi")]
Some(PyObj::Foreign(id)) => pending_display_get(*id)
.map(|(s, _)| s)
.unwrap_or_else(|| crate::ffi::str_of(*id)),
Some(PyObj::Slice { .. })
| Some(PyObj::List(_))
| Some(PyObj::Tuple(_))
| Some(PyObj::Dict(_))
| Some(PyObj::Set(_))
| Some(PyObj::Frozenset(_))
| Some(PyObj::DictView { .. }) => self.repr_of(v),
None => "<object>".into(),
},
_ => "<object>".into(),
}
}
fn exc_str(&self, class: &str, args: &[Value]) -> String {
self.exc_message(class, args)
}
pub fn repr_of(&self, v: &Value) -> String {
match v {
Value::Str(s) => quote_str(s),
Value::Obj(_) => match self.get(v) {
Some(PyObj::Str(s)) => quote_str(s),
Some(PyObj::List(l)) => {
let id = if let Value::Obj(i) = v { *i } else { 0 };
if repr_guard_enter(id) {
return "[...]".into();
}
let inner: Vec<String> = l.iter().map(|x| self.repr_of(x)).collect();
repr_guard_leave(id);
format!("[{}]", inner.join(", "))
}
Some(PyObj::Tuple(l)) => {
let id = if let Value::Obj(i) = v { *i } else { 0 };
if repr_guard_enter(id) {
return "(...)".into();
}
let nt = match v {
Value::Obj(i) => self.nt_meta.get(i),
_ => None,
};
let out = if let Some(m) = nt {
let inner: Vec<String> = m
.fields
.iter()
.zip(l.iter())
.map(|(f, x)| format!("{f}={}", self.repr_of(x)))
.collect();
format!("{}({})", m.type_name, inner.join(", "))
} else {
let inner: Vec<String> = l.iter().map(|x| self.repr_of(x)).collect();
if l.len() == 1 {
format!("({},)", inner[0])
} else {
format!("({})", inner.join(", "))
}
};
repr_guard_leave(id);
out
}
Some(PyObj::Dict(d)) => {
let id = if let Value::Obj(i) = v { *i } else { 0 };
if repr_guard_enter(id) {
return "{...}".into();
}
let body: Vec<String> = d
.values()
.map(|(k, val)| format!("{}: {}", self.repr_of(k), self.repr_of(val)))
.collect();
let dict_repr = format!("{{{}}}", body.join(", "));
let meta = match v {
Value::Obj(i) => self.dict_meta.get(i),
_ => None,
};
let empty = d.is_empty();
let out = match meta.map(|m| (m.kind, m.factory.clone())) {
Some((DictKind::Counter, _)) if empty => "Counter()".into(),
Some((DictKind::Counter, _)) => format!("Counter({dict_repr})"),
Some((DictKind::DefaultDict, factory)) => {
let f = factory
.map(|fv| self.repr_of(&fv))
.unwrap_or_else(|| "None".into());
format!("defaultdict({f}, {dict_repr})")
}
Some((DictKind::OrderedDict, _)) if empty => "OrderedDict()".into(),
Some((DictKind::OrderedDict, _)) => format!("OrderedDict({dict_repr})"),
None => dict_repr,
};
repr_guard_leave(id);
out
}
Some(PyObj::Set(s)) => {
if s.is_empty() {
"set()".into()
} else {
let id = if let Value::Obj(i) = v { *i } else { 0 };
if repr_guard_enter(id) {
return "{...}".into();
}
let inner: Vec<String> = self
.set_ordered_values(s)
.iter()
.map(|x| self.repr_of(x))
.collect();
repr_guard_leave(id);
format!("{{{}}}", inner.join(", "))
}
}
Some(PyObj::Frozenset(s)) => {
if s.is_empty() {
"frozenset()".into()
} else {
let id = if let Value::Obj(i) = v { *i } else { 0 };
if repr_guard_enter(id) {
return "frozenset(...)".into();
}
let inner: Vec<String> = self
.set_ordered_values(s)
.iter()
.map(|x| self.repr_of(x))
.collect();
repr_guard_leave(id);
format!("frozenset({{{}}})", inner.join(", "))
}
}
Some(PyObj::DictView { dict, kind }) => {
let (kind, dict) = (*kind, dict.clone());
let label = match kind {
0 => "dict_keys",
1 => "dict_values",
_ => "dict_items",
};
let inner: Vec<String> = match self.get(&dict) {
Some(PyObj::Dict(d)) => d
.values()
.map(|(k, v)| match kind {
0 => self.repr_of(k),
1 => self.repr_of(v),
_ => format!("({}, {})", self.repr_of(k), self.repr_of(v)),
})
.collect(),
_ => vec![],
};
format!("{label}([{}])", inner.join(", "))
}
Some(PyObj::Exception { class, args }) => {
let inner: Vec<String> = args.iter().map(|a| self.repr_of(a)).collect();
format!("{class}({})", inner.join(", "))
}
Some(PyObj::Instance(inst)) if self.class_is_exception(&inst.class) => {
let a = self.exc_instance_args(&inst.dict);
let inner: Vec<String> = a.iter().map(|x| self.repr_of(x)).collect();
format!("{}({})", inst.class, inner.join(", "))
}
Some(PyObj::Instance(inst))
if !matches!(inst.payload, Value::Undef)
&& self.builtin_base_of(&inst.class).is_some()
&& self.class_lookup(&inst.class, "__repr__").is_none() =>
{
self.repr_of(&inst.payload)
}
Some(PyObj::Slice { lo, hi, step }) => format!(
"slice({}, {}, {})",
self.repr_of(lo),
self.repr_of(hi),
self.repr_of(step)
),
#[cfg(feature = "stdlib-ffi")]
Some(PyObj::Foreign(id)) => pending_display_get(*id)
.map(|(_, r)| r)
.unwrap_or_else(|| crate::ffi::repr_of(*id)),
_ => self.str_of(v),
},
_ => self.str_of(v),
}
}
pub fn to_key(&self, v: &Value) -> Result<PKey, String> {
Ok(match v {
Value::Undef => PKey::None,
Value::Bool(b) => PKey::Int(*b as i64),
Value::Int(n) => PKey::Int(*n),
Value::Float(f) => float_pkey(*f),
Value::Str(s) => PKey::Str((**s).clone()),
Value::Obj(_) => match self.get(v) {
Some(PyObj::Str(s)) => PKey::Str(s.clone()),
Some(PyObj::Bytes(b)) => PKey::Bytes(b.clone()),
Some(PyObj::BigInt(b)) => PKey::Big(b.clone()),
Some(PyObj::Complex(r, i)) => {
if *i == 0.0 {
float_pkey(*r)
} else {
PKey::Complex(r.to_bits(), i.to_bits())
}
}
Some(PyObj::Tuple(items)) => {
let mut ks = Vec::with_capacity(items.len());
for it in items {
ks.push(self.to_key(it)?);
}
PKey::Tuple(ks)
}
Some(PyObj::Frozenset(s)) => {
let mut ks: Vec<PKey> = s.keys().cloned().collect();
ks.sort();
ks.dedup();
PKey::Frozenset(ks)
}
Some(PyObj::Class(n)) => PKey::Class(n.clone()),
Some(PyObj::Builtin(n)) => PKey::Class(n.clone()),
Some(PyObj::Descriptor { kind, qual }) => {
PKey::Class(format!("{}:{qual}", kind.type_name()))
}
Some(
PyObj::Func(_)
| PyObj::BoundMethod { .. }
| PyObj::StaticMethod(_)
| PyObj::ClassMethod(_)
| PyObj::Module { .. }
| PyObj::Code { .. }
| PyObj::Lock { .. }
| PyObj::TypeVarLike { .. },
) => {
let id = match v {
Value::Obj(i) => *i,
_ => 0,
};
PKey::Instance {
hash: id as i64,
id,
}
}
Some(PyObj::Ellipsis) => PKey::Singleton(0),
Some(PyObj::NotImplemented) => PKey::Singleton(1),
Some(PyObj::Instance(inst)) => {
let id = match v {
Value::Obj(i) => *i,
_ => 0,
};
let class = inst.class.clone();
if let Some(k) = pending_key_get(id) {
k
} else {
match self.class_lookup(&class, "__hash__") {
Some(Value::Undef) => {
return Err(type_error(&format!("unhashable type: '{class}'")))
}
None if self.class_lookup(&class, "__eq__").is_some() => {
return Err(type_error(&format!("unhashable type: '{class}'")))
}
None if !matches!(inst.payload, Value::Undef) => {
return self.to_key(&inst.payload);
}
None => PKey::Instance {
hash: id as i64,
id,
},
Some(_) => {
return Err(type_error(&format!("unhashable type: '{class}'")))
}
}
}
}
#[cfg(feature = "stdlib-ffi")]
Some(PyObj::Foreign(fid)) => {
let id = match v {
Value::Obj(i) => *i,
_ => 0,
};
if let Some(k) = pending_key_get(id) {
k
} else {
let hash = crate::ffi::foreign_hash(*fid)?;
PKey::Foreign { hash, id }
}
}
Some(other) => {
return Err(type_error(&format!(
"unhashable type: '{}'",
self.type_name_obj(other)
)))
}
None => PKey::None,
},
_ => return Err(type_error("unhashable type")),
})
}
fn type_name_obj(&self, o: &PyObj) -> &'static str {
match o {
PyObj::List(_) => "list",
PyObj::Dict(_) => "dict",
PyObj::Set(_) => "set",
PyObj::Frozenset(_) => "frozenset",
_ => "object",
}
}
pub fn equal(&self, a: &Value, b: &Value) -> bool {
let ua;
let a = if self.class_lookup_eq_free(a) {
ua = self.base_payload_any(a).unwrap();
&ua
} else {
a
};
let ub;
let b = if self.class_lookup_eq_free(b) {
ub = self.base_payload_any(b).unwrap();
&ub
} else {
b
};
match (a, b) {
(Value::Undef, Value::Undef) => true,
(Value::Bool(x), Value::Bool(y)) => x == y,
(Value::Int(x), Value::Int(y)) => x == y,
_ => {
if self.is_bignum(a) || self.is_bignum(b) {
if let (Some(x), Some(y)) = (self.big_val(a), self.big_val(b)) {
return x == y;
}
if let (Some(x), Some(f)) = (self.big_val(a), self.float_val(b)) {
return exact_big_eq_float(&x, f);
}
if let (Some(f), Some(y)) = (self.float_val(a), self.big_val(b)) {
return exact_big_eq_float(&y, f);
}
}
if let (Some(x), Some(y)) = (self.num_val(a), self.num_val(b)) {
return x == y;
}
if self.is_complex(a) || self.is_complex(b) {
if let (Some((ar, ai)), Some((br, bi))) =
(self.complex_val(a), self.complex_val(b))
{
return ar == br && ai == bi;
}
}
match (self.get(a), self.get(b)) {
(Some(PyObj::Str(x)), Some(PyObj::Str(y))) => x == y,
(Some(PyObj::List(x)), Some(PyObj::List(y)))
| (Some(PyObj::Tuple(x)), Some(PyObj::Tuple(y))) => {
x.len() == y.len() && x.iter().zip(y).all(|(p, q)| self.equal(p, q))
}
(Some(PyObj::Dict(x)), Some(PyObj::Dict(y))) => {
x.len() == y.len()
&& x.iter().all(|(k, (_, xv))| {
y.get(k).map(|(_, yv)| self.equal(xv, yv)).unwrap_or(false)
})
}
(Some(PyObj::Set(x)), Some(PyObj::Set(y)))
| (Some(PyObj::Set(x)), Some(PyObj::Frozenset(y)))
| (Some(PyObj::Frozenset(x)), Some(PyObj::Set(y)))
| (Some(PyObj::Frozenset(x)), Some(PyObj::Frozenset(y))) => {
x.len() == y.len() && x.keys().all(|k| y.contains_key(k))
}
(Some(PyObj::Deque { items: x, .. }), Some(PyObj::Deque { items: y, .. })) => {
x.len() == y.len() && x.iter().zip(y).all(|(p, q)| self.equal(p, q))
}
(Some(PyObj::Union { args: x }), Some(PyObj::Union { args: y })) => {
x.len() == y.len()
&& x.iter().all(|p| y.iter().any(|q| self.equal(p, q)))
}
(
Some(PyObj::Range {
start: s1,
stop: e1,
step: t1,
}),
Some(PyObj::Range {
start: s2,
stop: e2,
step: t2,
}),
) => {
let (l1, l2) = (range_len(*s1, *e1, *t1), range_len(*s2, *e2, *t2));
l1 == l2 && (l1 == 0 || (s1 == s2 && (l1 == 1 || t1 == t2)))
}
(Some(PyObj::Bytes(x)), Some(PyObj::Bytes(y)))
| (Some(PyObj::Bytes(x)), Some(PyObj::Bytearray(y)))
| (Some(PyObj::Bytearray(x)), Some(PyObj::Bytes(y)))
| (Some(PyObj::Bytearray(x)), Some(PyObj::Bytearray(y))) => x == y,
(Some(PyObj::Memoryview { .. }), _)
if matches!(
self.get(b),
Some(PyObj::Memoryview { .. })
| Some(PyObj::Bytes(_))
| Some(PyObj::Bytearray(_))
) =>
{
let yb = match self.get(b) {
Some(PyObj::Bytes(y)) | Some(PyObj::Bytearray(y)) => y.clone(),
_ => self.mv_bytes(b),
};
self.mv_bytes(a) == yb
}
(_, Some(PyObj::Memoryview { .. }))
if matches!(
self.get(a),
Some(PyObj::Bytes(_)) | Some(PyObj::Bytearray(_))
) =>
{
let xb = match self.get(a) {
Some(PyObj::Bytes(x)) | Some(PyObj::Bytearray(x)) => x.clone(),
_ => Vec::new(),
};
xb == self.mv_bytes(b)
}
(Some(PyObj::Builtin(x)), Some(PyObj::Builtin(y))) => x == y,
(Some(PyObj::Class(x)), Some(PyObj::Class(y))) => x == y,
(Some(PyObj::Ellipsis), Some(PyObj::Ellipsis))
| (Some(PyObj::NotImplemented), Some(PyObj::NotImplemented)) => true,
#[cfg(feature = "stdlib-ffi")]
(Some(PyObj::Foreign(x)), Some(PyObj::Foreign(y))) => {
crate::ffi::foreign_eq(*x, *y)
}
#[cfg(feature = "stdlib-ffi")]
(Some(PyObj::Foreign(f)), _) => self.foreign_eq_native(*f, b),
#[cfg(feature = "stdlib-ffi")]
(_, Some(PyObj::Foreign(f))) => self.foreign_eq_native(*f, a),
_ => match (a, b) {
(Value::Str(x), Value::Str(y)) => x == y,
_ => a == b,
},
}
}
}
}
#[cfg(feature = "stdlib-ffi")]
fn foreign_eq_native(&self, fid: u32, other: &Value) -> bool {
if let Some(n) = self.as_int(other) {
crate::ffi::foreign_eq_prim(fid, crate::ffi::Prim::Int(n))
} else if let Some(f) = self.num_val(other) {
crate::ffi::foreign_eq_prim(fid, crate::ffi::Prim::Float(f))
} else if let Some(s) = self.as_str(other) {
crate::ffi::foreign_eq_prim(fid, crate::ffi::Prim::Str(&s))
} else {
false
}
}
pub fn complex_val(&self, v: &Value) -> Option<(f64, f64)> {
if let Some(PyObj::Complex(r, i)) = self.get(v) {
return Some((*r, *i));
}
self.num_val(v).map(|r| (r, 0.0))
}
pub fn is_complex(&self, v: &Value) -> bool {
matches!(self.get(v), Some(PyObj::Complex(..)))
}
pub fn num_val(&self, 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),
Value::Obj(_) => match self.get(v) {
Some(PyObj::BigInt(b)) => Some(bigint_to_f64(b)),
Some(PyObj::Instance(_)) => self.base_payload_num(v).and_then(|p| self.num_val(&p)),
_ => None,
},
_ => None,
}
}
pub fn as_int(&self, v: &Value) -> Option<i64> {
match v {
Value::Int(n) => Some(*n),
Value::Bool(b) => Some(*b as i64),
Value::Obj(_) => self.base_payload_num(v).and_then(|p| self.as_int(&p)),
_ => None,
}
}
fn base_payload_num(&self, v: &Value) -> Option<Value> {
match self.get(v) {
Some(PyObj::Instance(i)) if !matches!(i.payload, Value::Undef) => {
match self.builtin_base_of(&i.class) {
Some("int") | Some("float") => Some(i.payload.clone()),
_ => None,
}
}
_ => None,
}
}
fn class_lookup_eq_free(&self, v: &Value) -> bool {
match self.get(v) {
Some(PyObj::Instance(i)) if !matches!(i.payload, Value::Undef) => {
self.builtin_base_of(&i.class).is_some()
&& self.class_lookup(&i.class, "__eq__").is_none()
}
_ => false,
}
}
fn base_payload_any(&self, v: &Value) -> Option<Value> {
match self.get(v) {
Some(PyObj::Instance(i))
if !matches!(i.payload, Value::Undef)
&& self.builtin_base_of(&i.class).is_some() =>
{
Some(i.payload.clone())
}
_ => None,
}
}
}
pub fn dict_put(d: &mut IndexMap<PKey, (Value, Value)>, key: PKey, kv: Value, val: Value) {
use indexmap::map::Entry;
match d.entry(key) {
Entry::Occupied(mut e) => e.get_mut().1 = val,
Entry::Vacant(e) => {
e.insert((kv, val));
}
}
}
pub fn set_put(s: &mut IndexMap<PKey, Value>, key: PKey, item: Value) {
s.entry(key).or_insert(item);
}
const SET_MINSIZE: usize = 8;
const SET_LINEAR_PROBES: usize = 9;
const SET_PERTURB_SHIFT: u32 = 5;
fn cpython_int_hash(n: i64) -> i64 {
if n == -1 {
-2
} else {
n
}
}
struct SetTable {
slots: Vec<Option<(i64, usize)>>,
mask: usize,
fill: usize,
used: usize,
}
impl SetTable {
fn new() -> SetTable {
SetTable {
slots: vec![None; SET_MINSIZE],
mask: SET_MINSIZE - 1,
fill: 0,
used: 0,
}
}
fn find_empty(slots: &[Option<(i64, usize)>], mask: usize, hash: i64) -> usize {
let mut perturb = hash as u64;
let mut i = (hash as u64 as usize) & mask;
loop {
let probes = if i + SET_LINEAR_PROBES <= mask {
SET_LINEAR_PROBES
} else {
0
};
for (entry, slot) in slots.iter().enumerate().skip(i).take(probes + 1) {
if slot.is_none() {
return entry;
}
}
perturb >>= SET_PERTURB_SHIFT;
i = (i
.wrapping_mul(5)
.wrapping_add(1)
.wrapping_add(perturb as usize))
& mask;
}
}
fn add(&mut self, hash: i64, idx: usize) {
let slot = Self::find_empty(&self.slots, self.mask, hash);
self.slots[slot] = Some((hash, idx));
self.fill += 1;
self.used += 1;
if self.fill * 5 >= self.mask * 3 {
let minused = if self.used > 50000 {
self.used * 2
} else {
self.used * 4
};
self.resize(minused);
}
}
fn resize(&mut self, minused: usize) {
let mut newsize = SET_MINSIZE;
while newsize <= minused {
newsize <<= 1;
}
let old = std::mem::replace(&mut self.slots, vec![None; newsize]);
self.mask = newsize - 1;
self.fill = self.used;
for entry in old.into_iter().flatten() {
let (hash, idx) = entry;
let slot = Self::find_empty(&self.slots, self.mask, hash);
self.slots[slot] = Some((hash, idx));
}
}
}
fn cpython_set_order(hashes: &[i64]) -> Vec<usize> {
let mut t = SetTable::new();
for (idx, &h) in hashes.iter().enumerate() {
t.add(h, idx);
}
t.slots
.iter()
.filter_map(|s| s.map(|(_, idx)| idx))
.collect()
}
fn hash_int_of(v: &Value) -> Result<i64, String> {
match v {
Value::Bool(b) => Ok(*b as i64),
Value::Int(n) => Ok(*n),
_ => with_host(|h| match h.get(v) {
Some(PyObj::BigInt(b)) => Ok(bigint_pyhash(b)),
_ => Err(type_error("__hash__ method should return an integer")),
}),
}
}
fn bigint_pyhash(b: &num_bigint::BigInt) -> i64 {
use num_bigint::BigInt;
use num_traits::ToPrimitive;
let modulus = BigInt::from((1i64 << 61) - 1);
let mut r = b % &modulus;
if r.sign() == num_bigint::Sign::Minus {
r += &modulus;
}
let mut h = r.to_i64().unwrap_or(0);
if b.sign() == num_bigint::Sign::Minus {
h = -h;
}
if h == -1 {
-2
} else {
h
}
}
pub fn prepare_key(v: &Value, candidates: &[(PKey, Value)]) -> Result<(), String> {
#[cfg(feature = "stdlib-ffi")]
if let Some((id, fid)) = with_host(|h| match (v, h.get(v)) {
(Value::Obj(i), Some(PyObj::Foreign(f))) => Some((*i, *f)),
_ => None,
}) {
let hash = crate::ffi::foreign_hash(fid)?;
let mut canonical = PKey::Foreign { hash, id };
for (pk, kobj) in candidates {
if let PKey::Foreign { hash: ch, .. } = pk {
if *ch == hash {
if let Some(cf) = with_host(|h| h.foreign_id(kobj)) {
if crate::ffi::foreign_eq(fid, cf) {
canonical = pk.clone();
break;
}
}
}
}
}
pending_key_set(id, canonical);
return Ok(());
}
let (id, class) = match with_host(|h| match v {
Value::Obj(i) => match h.get(v) {
Some(PyObj::Instance(inst)) => Some((*i, inst.class.clone())),
_ => None,
},
_ => None,
}) {
Some(t) => t,
None => return Ok(()),
};
let hashf = with_host(|h| h.class_lookup(&class, "__hash__"));
match &hashf {
Some(Value::Undef) => return Err(type_error(&format!("unhashable type: '{class}'"))),
None => {
if with_host(|h| h.class_lookup(&class, "__eq__").is_some()) {
return Err(type_error(&format!("unhashable type: '{class}'")));
}
return Ok(());
}
Some(_) => {}
}
let hres = call_method(v, "__hash__", vec![], vec![])?;
let hash = hash_int_of(&hres)?;
let mut canonical = PKey::Instance { hash, id };
for (pk, kobj) in candidates {
if let PKey::Instance { hash: ch, .. } = pk {
if *ch == hash {
let eqr = call_method(v, "__eq__", vec![kobj.clone()], vec![])?;
if with_host(|h| h.truthy(&eqr)) {
canonical = pk.clone();
break;
}
}
}
}
pending_key_set(id, canonical);
Ok(())
}
pub fn instance_hash_value(v: &Value) -> Result<i64, String> {
let (id, class) = match with_host(|h| match v {
Value::Obj(i) => match h.get(v) {
Some(PyObj::Instance(inst)) => Some((*i, inst.class.clone())),
_ => None,
},
_ => None,
}) {
Some(t) => t,
None => return Err(type_error("unhashable type")),
};
match with_host(|h| h.class_lookup(&class, "__hash__")) {
Some(Value::Undef) => Err(type_error(&format!("unhashable type: '{class}'"))),
None => {
if with_host(|h| h.class_lookup(&class, "__eq__").is_some()) {
return Err(type_error(&format!("unhashable type: '{class}'")));
}
if let Some((base, payload)) = with_host(|h| match h.get(v) {
Some(PyObj::Instance(i)) if !matches!(i.payload, Value::Undef) => {
h.builtin_base_of(&i.class).map(|b| (b, i.payload.clone()))
}
_ => None,
}) {
if base_provides(base, "__hash__") {
let k = with_host(|h| h.to_key(&payload))?;
return Ok(crate::builtins::hash_key(&k));
}
return Err(type_error(&format!("unhashable type: '{class}'")));
}
Ok(id as i64)
}
Some(_) => {
let r = call_method(v, "__hash__", vec![], vec![])?;
hash_int_of(&r)
}
}
}
pub fn set_local_candidates(s: &IndexMap<PKey, Value>) -> Vec<(PKey, Value)> {
s.iter()
.filter(|(k, _)| matches!(k, PKey::Instance { .. } | PKey::Foreign { .. }))
.map(|(k, v)| (k.clone(), v.clone()))
.collect()
}
pub fn dict_local_candidates(d: &IndexMap<PKey, (Value, Value)>) -> Vec<(PKey, Value)> {
d.iter()
.filter(|(k, _)| matches!(k, PKey::Instance { .. } | PKey::Foreign { .. }))
.map(|(k, (kv, _))| (k.clone(), kv.clone()))
.collect()
}
pub fn instance_key_candidates(container: &Value) -> Vec<(PKey, Value)> {
instance_key_candidates_for(container, None)
}
pub fn instance_key_candidates_for(container: &Value, key: Option<&Value>) -> Vec<(PKey, Value)> {
if let Some(k) = key {
let key_can_collapse = with_host(|h| {
matches!(k, Value::Obj(_))
&& !matches!(
h.get(k),
Some(
PyObj::Str(_)
| PyObj::Bytes(_)
| PyObj::Tuple(_)
| PyObj::BigInt(_)
| PyObj::Frozenset(_)
)
)
});
if !key_can_collapse {
return Vec::new();
}
}
with_host(|h| match h.get(container) {
Some(PyObj::Dict(d)) => d
.iter()
.filter(|(k, _)| matches!(k, PKey::Instance { .. } | PKey::Foreign { .. }))
.map(|(k, (kv, _))| (k.clone(), kv.clone()))
.collect(),
Some(PyObj::Set(s)) | Some(PyObj::Frozenset(s)) => s
.iter()
.filter(|(k, _)| matches!(k, PKey::Instance { .. } | PKey::Foreign { .. }))
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
_ => vec![],
})
}
pub fn with_instance_key<R>(
v: &Value,
candidates: &[(PKey, Value)],
f: impl FnOnce() -> Result<R, String>,
) -> Result<R, String> {
let prep = prepare_key(v, candidates);
let r = prep.and_then(|()| f());
pending_key_clear();
r
}
fn float_pkey(f: f64) -> PKey {
if f.is_finite() && f.fract() == 0.0 {
if f >= i64::MIN as f64 && f <= i64::MAX as f64 {
return PKey::Int(f as i64);
}
use num_traits::FromPrimitive;
if let Some(b) = num_bigint::BigInt::from_f64(f) {
return PKey::Big(b);
}
}
PKey::FloatBits(f.to_bits())
}
fn bigint_floordiv(x: &num_bigint::BigInt, y: &num_bigint::BigInt) -> num_bigint::BigInt {
let q = x / y;
let r = x % y;
let zero = num_bigint::BigInt::from(0);
if r != zero && (r < zero) != (*y < zero) {
q - num_bigint::BigInt::from(1)
} else {
q
}
}
fn bigint_mod(x: &num_bigint::BigInt, y: &num_bigint::BigInt) -> num_bigint::BigInt {
let r = x % y;
let zero = num_bigint::BigInt::from(0);
if r != zero && (r < zero) != (*y < zero) {
r + y
} else {
r
}
}
pub fn fmt_float(f: f64) -> String {
if f.is_infinite() {
return if f < 0.0 { "-inf".into() } else { "inf".into() };
}
if f.is_nan() {
return "nan".into();
}
let sci = format!("{f:e}"); let epos = sci
.rfind('e')
.expect("scientific format carries an exponent");
let exp: i32 = sci[epos + 1..].parse().expect("valid exponent");
if (-4..16).contains(&exp) {
let mut s = format!("{f}");
if !s.contains('.') {
s.push_str(".0"); }
s
} else {
let mantissa = &sci[..epos];
let sign = if exp < 0 { '-' } else { '+' };
format!("{mantissa}e{sign}{:02}", exp.abs())
}
}
fn float_divmod(vx: f64, wx: f64) -> (f64, f64) {
let mut mod_ = vx % wx; let div = (vx - mod_) / wx;
let mut div = div;
if mod_ != 0.0 {
if (wx < 0.0) != (mod_ < 0.0) {
mod_ += wx;
div -= 1.0;
}
} else {
mod_ = 0.0_f64.copysign(wx);
}
let floordiv = if div != 0.0 {
let fd = div.floor();
if div - fd > 0.5 {
fd + 1.0
} else {
fd
}
} else {
0.0_f64.copysign(vx / wx)
};
(floordiv, mod_)
}
fn c_quot(ar: f64, ai: f64, br: f64, bi: f64) -> (f64, f64) {
let abs_br = br.abs();
let abs_bi = bi.abs();
if abs_br >= abs_bi {
if abs_br == 0.0 {
(0.0, 0.0)
} else {
let ratio = bi / br;
let denom = bi.mul_add(ratio, br); (
ai.mul_add(ratio, ar) / denom, (-ar).mul_add(ratio, ai) / denom, )
}
} else if abs_bi >= abs_br {
let ratio = br / bi;
let denom = br.mul_add(ratio, bi); (
ar.mul_add(ratio, ai) / denom, ai.mul_add(ratio, -ar) / denom, )
} else {
(f64::NAN, f64::NAN)
}
}
fn fmt_complex(r: f64, i: f64) -> String {
if r == 0.0 && r.is_sign_positive() {
format!("{}j", fmt_complex_part(i))
} else {
let sign = if i >= 0.0 || i.is_nan() { "+" } else { "-" };
format!(
"({}{}{}j)",
fmt_complex_part(r),
sign,
fmt_complex_part(i.abs())
)
}
}
fn fmt_complex_part(f: f64) -> String {
let s = fmt_float(f);
match s.strip_suffix(".0") {
Some(t) => t.to_string(),
None => s,
}
}
fn c_pow(a: (f64, f64), b: (f64, f64)) -> (f64, f64) {
if b.1 == 0.0 && b.0 == b.0.floor() && b.0.abs() <= 100.0 {
return c_powi(a, b.0 as i64);
}
c_pow_polar(a, b)
}
fn c_pow_polar(a: (f64, f64), b: (f64, f64)) -> (f64, f64) {
if b.0 == 0.0 && b.1 == 0.0 {
return (1.0, 0.0);
}
if a.0 == 0.0 && a.1 == 0.0 {
return (0.0, 0.0);
}
let vabs = a.0.hypot(a.1);
let mut len = vabs.powf(b.0);
let at = a.1.atan2(a.0);
let mut phase = at * b.0;
if b.1 != 0.0 {
len /= (at * b.1).exp();
phase += b.1 * vabs.ln();
}
(len * phase.cos(), len * phase.sin())
}
fn c_prod(a: (f64, f64), b: (f64, f64)) -> (f64, f64) {
(a.0 * b.0 - a.1 * b.1, a.0 * b.1 + a.1 * b.0)
}
fn c_powi(x: (f64, f64), n: i64) -> (f64, f64) {
if n < 0 {
let p = c_powu(x, -n);
let d = p.0 * p.0 + p.1 * p.1;
return (p.0 / d, -p.1 / d);
}
c_powu(x, n)
}
fn c_powu(x: (f64, f64), n: i64) -> (f64, f64) {
let mut r = (1.0, 0.0);
let mut p = x;
let mut mask = 1i64;
while mask > 0 && n >= mask {
if n & mask != 0 {
r = c_prod(r, p);
}
mask <<= 1;
p = c_prod(p, p);
}
r
}
pub fn is_printable_char(c: char) -> bool {
if c == ' ' {
return true;
}
use unicode_general_category::{get_general_category, GeneralCategory as G};
!matches!(
get_general_category(c),
G::Control
| G::Format
| G::Surrogate
| G::PrivateUse
| G::Unassigned
| G::LineSeparator
| G::ParagraphSeparator
| G::SpaceSeparator
)
}
fn quote_str(s: &str) -> String {
let has_single = s.contains('\'');
let has_double = s.contains('"');
let (q, esc_q) = if has_single && !has_double {
('"', '"')
} else {
('\'', '\'')
};
let mut out = String::with_capacity(s.len() + 2);
out.push(q);
for c in s.chars() {
match c {
'\\' => out.push_str("\\\\"),
'\n' => out.push_str("\\n"),
'\t' => out.push_str("\\t"),
'\r' => out.push_str("\\r"),
c if c == esc_q => {
out.push('\\');
out.push(c);
}
c if !is_printable_char(c) => {
let n = c as u32;
if n <= 0xff {
out.push_str(&format!("\\x{n:02x}"));
} else if n <= 0xffff {
out.push_str(&format!("\\u{n:04x}"));
} else {
out.push_str(&format!("\\U{n:08x}"));
}
}
c => out.push(c),
}
}
out.push(q);
out
}
fn quote_bytes(b: &[u8], is_bytearray: bool) -> String {
let has_single = b.contains(&b'\'');
let has_double = b.contains(&b'"');
let quote = if has_single && !has_double {
b'"'
} else {
b'\''
};
let mut out = String::new();
out.push(quote as char);
for &c in b {
match c {
b'\\' => out.push_str("\\\\"),
b'\t' => out.push_str("\\t"),
b'\n' => out.push_str("\\n"),
b'\r' => out.push_str("\\r"),
_ if c == quote => {
out.push('\\');
out.push(quote as char);
}
b'\'' if is_bytearray => out.push_str("\\'"),
0x20..=0x7e => out.push(c as char),
_ => out.push_str(&format!("\\x{c:02x}")),
}
}
out.push(quote as char);
out
}
pub fn range_len(start: i64, stop: i64, step: i64) -> i64 {
if step == 0 {
return 0;
}
if step > 0 {
if stop > start {
(stop - start + step - 1) / step
} else {
0
}
} else if start > stop {
(start - stop - step - 1) / (-step)
} else {
0
}
}
pub fn big_range_len(
start: &num_bigint::BigInt,
stop: &num_bigint::BigInt,
step: &num_bigint::BigInt,
) -> num_bigint::BigInt {
use num_bigint::BigInt;
use num_traits::{One, Zero};
let zero = BigInt::zero();
if *step > zero {
if stop > start {
(stop - start + step - BigInt::one()) / step
} else {
zero
}
} else if start > stop {
(start - stop - step - BigInt::one()) / (-step)
} else {
zero
}
}
fn exact_big_eq_float(b: &num_bigint::BigInt, f: f64) -> bool {
use num_traits::FromPrimitive;
if !f.is_finite() || f.fract() != 0.0 {
return false;
}
num_bigint::BigInt::from_f64(f).is_some_and(|x| &x == b)
}
fn tuple_body(items: &[String]) -> String {
match items {
[one] => format!("{one},"),
_ => items.join(", "),
}
}
fn bigint_to_f64(b: &num_bigint::BigInt) -> f64 {
use num_traits::ToPrimitive;
b.to_f64().unwrap_or(f64::INFINITY)
}
impl PyHost {
pub fn arith(&mut self, op: NumOp, a: &Value, b: &Value) -> Result<Value, String> {
use NumOp::*;
#[cfg(feature = "stdlib-ffi")]
{
if matches!(op, Neg) {
if self.foreign_id(a).is_some() {
return crate::ffi::unary_op(self, "neg", a);
}
} else if self.foreign_id(a).is_some() || self.foreign_id(b).is_some() {
let func = 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 => unreachable!(),
};
return crate::ffi::binary_op(self, func, a, b);
}
}
let ai = self.as_int(a);
let bi = self.as_int(b);
match op {
Add => {
if let (Some(x), Some(y)) = (ai, bi) {
return Ok(self.int_result(x as i128 + y as i128));
}
if let (Some(x), Some(y)) = (self.big_val(a), self.big_val(b)) {
return Ok(self.norm_big(x + y));
}
if let (Some(x), Some(y)) = (self.num_val(a), self.num_val(b)) {
return Ok(Value::Float(x + y));
}
if self.is_complex(a) || self.is_complex(b) {
if let (Some((ar, ai)), Some((br, bi))) =
(self.complex_val(a), self.complex_val(b))
{
return Ok(self.alloc(PyObj::Complex(ar + br, ai + bi)));
}
}
if let (
Some(PyObj::Template {
strings: ls,
interpolations: li,
}),
Some(PyObj::Template {
strings: rs,
interpolations: ri,
}),
) = (self.get(a), self.get(b))
{
let mut strings = ls.clone();
let mut interpolations = li.clone();
let (rs, ri) = (rs.clone(), ri.clone());
let seam = strings.pop().unwrap_or_default();
let mut rs = rs.into_iter();
strings.push(seam + &rs.next().unwrap_or_default());
strings.extend(rs);
interpolations.extend(ri);
return Ok(self.alloc(PyObj::Template {
strings,
interpolations,
}));
}
match (self.get(a), self.get(b)) {
(Some(PyObj::Str(x)), Some(PyObj::Str(y))) => {
let s = format!("{x}{y}");
Ok(self.new_str(s))
}
(Some(PyObj::List(x)), Some(PyObj::List(y))) => {
let mut v = x.clone();
v.extend(y.clone());
Ok(self.new_list(v))
}
(Some(PyObj::Tuple(x)), Some(PyObj::Tuple(y))) => {
let mut v = x.clone();
v.extend(y.clone());
Ok(self.new_tuple(v))
}
(Some(PyObj::Bytes(x)), Some(PyObj::Bytes(y)))
| (Some(PyObj::Bytes(x)), Some(PyObj::Bytearray(y))) => {
let mut v = x.clone();
v.extend_from_slice(y);
Ok(self.alloc(PyObj::Bytes(v)))
}
(Some(PyObj::Bytearray(x)), Some(PyObj::Bytes(y)))
| (Some(PyObj::Bytearray(x)), Some(PyObj::Bytearray(y))) => {
let mut v = x.clone();
v.extend_from_slice(y);
Ok(self.alloc(PyObj::Bytearray(v)))
}
_ => {
let rt = self.type_name(b);
Err(match self.get(a) {
Some(PyObj::Str(_)) => type_error(&format!(
"can only concatenate str (not \"{rt}\") to str"
)),
Some(PyObj::List(_)) => type_error(&format!(
"can only concatenate list (not \"{rt}\") to list"
)),
Some(PyObj::Tuple(_)) => type_error(&format!(
"can only concatenate tuple (not \"{rt}\") to tuple"
)),
Some(PyObj::Bytes(_)) => {
type_error(&format!("can't concat {rt} to bytes"))
}
Some(PyObj::Bytearray(_)) => {
type_error(&format!("can't concat {rt} to bytearray"))
}
_ => self.optype_err("+", a, b),
})
}
}
}
Sub => {
if let (Some(x), Some(y)) = (self.big_val(a), self.big_val(b)) {
return Ok(self.norm_big(x - y));
}
if let (Some(x), Some(y)) = (self.num_val(a), self.num_val(b)) {
return Ok(Value::Float(x - y));
}
if self.is_complex(a) || self.is_complex(b) {
if let (Some((ar, ai)), Some((br, bi))) =
(self.complex_val(a), self.complex_val(b))
{
return Ok(self.alloc(PyObj::Complex(ar - br, ai - bi)));
}
}
let a_set = self.setmap_of(a).is_some();
if let (Some(mut out), Some(y)) =
(self.setmap_of(a), self.setmap_operand(b, a_set))
{
for k in y.keys() {
out.shift_remove(k);
}
let frozen = self.is_frozenset(a);
return Ok(self.new_setlike(out, frozen));
}
Err(self.optype_err("-", a, b))
}
Mul => {
if let (Some(x), Some(y)) = (self.big_val(a), self.big_val(b)) {
return Ok(self.norm_big(x * y));
}
if let Some(r) = self.repeat_seq(a, b)? {
return Ok(r);
}
if let (Some(x), Some(y)) = (self.num_val(a), self.num_val(b)) {
return Ok(Value::Float(x * y));
}
if self.is_complex(a) || self.is_complex(b) {
if let (Some((ar, ai)), Some((br, bi))) =
(self.complex_val(a), self.complex_val(b))
{
return Ok(self.alloc(PyObj::Complex(ar * br - ai * bi, ar * bi + ai * br)));
}
}
Err(self.optype_err("*", a, b))
}
Div => self.binop(binop::DIV, a, b),
Mod => self.binop(binop::MOD, a, b),
Pow => self.binop(binop::POW, a, b),
Neg => {
if let Some(x) = self.big_val(a) {
return Ok(self.norm_big(-x));
}
if let Some(PyObj::Complex(r, i)) = self.get(a) {
let (r, i) = (*r, *i);
return Ok(self.alloc(PyObj::Complex(-r, -i)));
}
Err(type_error(&format!(
"bad operand type for unary -: '{}'",
self.type_name(a)
)))
}
Eq => Ok(Value::Bool(self.equal(a, b))),
Ne => Ok(Value::Bool(!self.equal(a, b))),
Lt | Gt | Le | Ge => self.compare(op, a, b),
}
}
fn optype_err(&self, op: &str, a: &Value, b: &Value) -> String {
type_error(&format!(
"unsupported operand type(s) for {op}: '{}' and '{}'",
self.type_name(a),
self.type_name(b)
))
}
fn int_result(&mut self, n: i128) -> Value {
if let Ok(v) = i64::try_from(n) {
Value::Int(v)
} else {
self.alloc(PyObj::BigInt(num_bigint::BigInt::from(n)))
}
}
#[inline]
pub fn is_bignum(&self, v: &Value) -> bool {
matches!(v, Value::Obj(_)) && matches!(self.get(v), Some(PyObj::BigInt(_)))
}
#[inline]
pub fn float_val(&self, v: &Value) -> Option<f64> {
match v {
Value::Float(f) => Some(*f),
_ => None,
}
}
pub fn big_val(&self, v: &Value) -> Option<num_bigint::BigInt> {
match v {
Value::Int(n) => Some(num_bigint::BigInt::from(*n)),
Value::Bool(b) => Some(num_bigint::BigInt::from(*b as i64)),
Value::Obj(_) => match self.get(v) {
Some(PyObj::BigInt(b)) => Some(b.clone()),
_ => None,
},
_ => None,
}
}
pub fn norm_big(&mut self, b: num_bigint::BigInt) -> Value {
use num_traits::ToPrimitive;
if let Some(n) = b.to_i64() {
Value::Int(n)
} else {
self.alloc(PyObj::BigInt(b))
}
}
fn repeat_seq(&mut self, a: &Value, b: &Value) -> Result<Option<Value>, String> {
let (seq, count) = if let Some(n) = self.as_int(b) {
(a.clone(), n)
} else if let Some(n) = self.as_int(a) {
(b.clone(), n)
} else {
return Ok(None);
};
let n = count.max(0) as usize;
match self.get(&seq) {
Some(PyObj::Str(s)) => {
let r = s.repeat(n);
Ok(Some(self.new_str(r)))
}
Some(PyObj::List(l)) => {
let mut out = Vec::with_capacity(l.len() * n);
let base = l.clone();
for _ in 0..n {
out.extend(base.clone());
}
Ok(Some(self.new_list(out)))
}
Some(PyObj::Tuple(l)) => {
let base = l.clone();
let mut out = Vec::with_capacity(base.len() * n);
for _ in 0..n {
out.extend(base.clone());
}
Ok(Some(self.new_tuple(out)))
}
Some(PyObj::Bytes(s)) => {
let base = s.clone();
let mut out = Vec::with_capacity(base.len() * n);
for _ in 0..n {
out.extend_from_slice(&base);
}
Ok(Some(self.alloc(PyObj::Bytes(out))))
}
Some(PyObj::Bytearray(s)) => {
let base = s.clone();
let mut out = Vec::with_capacity(base.len() * n);
for _ in 0..n {
out.extend_from_slice(&base);
}
Ok(Some(self.alloc(PyObj::Bytearray(out))))
}
_ => Ok(None),
}
}
pub fn compare(&mut self, op: NumOp, a: &Value, b: &Value) -> Result<Value, String> {
use std::cmp::Ordering;
if let (Some(x), Some(y)) = (self.setlike(a), self.setlike(b)) {
let a_sub_b = x.keys().all(|k| y.contains_key(k)); let b_sub_a = y.keys().all(|k| x.contains_key(k)); let (la, lb) = (x.len(), y.len());
let res = match op {
NumOp::Le => a_sub_b,
NumOp::Lt => a_sub_b && la < lb,
NumOp::Ge => b_sub_a,
NumOp::Gt => b_sub_a && lb < la,
_ => unreachable!(),
};
return Ok(Value::Bool(res));
}
let sym = match op {
NumOp::Lt => "<",
NumOp::Le => "<=",
NumOp::Gt => ">",
NumOp::Ge => ">=",
_ => "<",
};
let ord = self.order(a, b, sym)?;
let res = match op {
NumOp::Lt => ord == Ordering::Less,
NumOp::Le => ord != Ordering::Greater,
NumOp::Gt => ord == Ordering::Greater,
NumOp::Ge => ord != Ordering::Less,
_ => unreachable!(),
};
Ok(Value::Bool(res))
}
fn order(&self, a: &Value, b: &Value, sym: &str) -> Result<std::cmp::Ordering, String> {
use std::cmp::Ordering;
let a_is_float = matches!(a, Value::Float(_));
let b_is_float = matches!(b, Value::Float(_));
if !a_is_float && !b_is_float {
if let (Some(x), Some(y)) = (self.big_val(a), self.big_val(b)) {
return Ok(x.cmp(&y));
}
}
if let (Some(x), Some(y)) = (self.num_val(a), self.num_val(b)) {
return Ok(x.partial_cmp(&y).unwrap_or(Ordering::Equal));
}
match (self.get(a), self.get(b)) {
(Some(PyObj::Str(x)), Some(PyObj::Str(y))) => Ok(x.cmp(y)),
(Some(PyObj::Bytes(x)), Some(PyObj::Bytes(y)))
| (Some(PyObj::Bytes(x)), Some(PyObj::Bytearray(y)))
| (Some(PyObj::Bytearray(x)), Some(PyObj::Bytes(y)))
| (Some(PyObj::Bytearray(x)), Some(PyObj::Bytearray(y))) => Ok(x.cmp(y)),
(Some(PyObj::List(x)), Some(PyObj::List(y)))
| (Some(PyObj::Tuple(x)), Some(PyObj::Tuple(y))) => {
for (p, q) in x.iter().zip(y.iter()) {
let o = self.order(p, q, sym)?;
if o != Ordering::Equal {
return Ok(o);
}
}
Ok(x.len().cmp(&y.len()))
}
#[cfg(feature = "stdlib-ffi")]
(Some(PyObj::Foreign(x)), Some(PyObj::Foreign(y))) => crate::ffi::foreign_cmp(*x, *y),
_ => Err(type_error(&format!(
"'{sym}' not supported between instances of '{}' and '{}'",
self.type_name(a),
self.type_name(b)
))),
}
}
pub fn binop(&mut self, tag: i64, a: &Value, b: &Value) -> Result<Value, String> {
#[cfg(feature = "stdlib-ffi")]
if self.foreign_id(a).is_some() || self.foreign_id(b).is_some() {
let func = match tag {
binop::DIV => "truediv",
binop::FLOORDIV => "floordiv",
binop::MOD => "mod",
binop::POW => "pow",
binop::MATMUL => "matmul",
binop::BITAND => "and_",
binop::BITOR => "or_",
binop::BITXOR => "xor",
binop::SHL => "lshift",
binop::SHR => "rshift",
_ => return Err(type_error("unknown binop")),
};
return crate::ffi::binary_op(self, func, a, b);
}
let ai = self.as_int(a);
let bi = self.as_int(b);
let af = self.num_val(a);
let bf = self.num_val(b);
match tag {
binop::DIV => match (af, bf) {
(Some(_), Some(0.0)) => Err("ZeroDivisionError: division by zero".into()),
(Some(x), Some(y)) => Ok(Value::Float(x / y)),
_ if self.is_complex(a) || self.is_complex(b) => {
match (self.complex_val(a), self.complex_val(b)) {
(Some((ar, ai)), Some((br, bi))) => {
if br == 0.0 && bi == 0.0 {
return Err("ZeroDivisionError: division by zero".into());
}
let (rr, ri) = c_quot(ar, ai, br, bi);
Ok(self.alloc(PyObj::Complex(rr, ri)))
}
_ => Err(self.optype_err("/", a, b)),
}
}
_ => Err(self.optype_err("/", a, b)),
},
binop::FLOORDIV => {
if let (Some(x), Some(y)) = (ai, bi) {
if y == 0 {
return Err("ZeroDivisionError: division by zero".into());
}
let (x, y) = (x as i128, y as i128);
let q = x / y;
let r = x % y;
let q = if r != 0 && (r < 0) != (y < 0) {
q - 1
} else {
q
};
return Ok(self.int_result(q));
}
if let (Some(x), Some(y)) = (self.big_val(a), self.big_val(b)) {
if y == num_bigint::BigInt::from(0) {
return Err("ZeroDivisionError: division by zero".into());
}
return Ok(self.norm_big(bigint_floordiv(&x, &y)));
}
match (af, bf) {
(Some(_), Some(0.0)) => Err("ZeroDivisionError: division by zero".into()),
(Some(x), Some(y)) => Ok(Value::Float(float_divmod(x, y).0)),
_ => Err(self.optype_err("//", a, b)),
}
}
binop::MOD => {
if let Some(PyObj::Str(fmt)) = self.get(a) {
let fmt = fmt.clone();
return self.str_format_percent(&fmt, b, &HashMap::new());
}
if let (Some(x), Some(y)) = (ai, bi) {
if y == 0 {
return Err("ZeroDivisionError: division by zero".into());
}
let r = x % y;
let r = if r != 0 && (r < 0) != (y < 0) {
r + y
} else {
r
};
return Ok(Value::Int(r));
}
if let (Some(x), Some(y)) = (self.big_val(a), self.big_val(b)) {
if y == num_bigint::BigInt::from(0) {
return Err("ZeroDivisionError: division by zero".into());
}
return Ok(self.norm_big(bigint_mod(&x, &y)));
}
match (af, bf) {
(Some(_), Some(0.0)) => Err("ZeroDivisionError: division by zero".into()),
(Some(x), Some(y)) => Ok(Value::Float(float_divmod(x, y).1)),
_ => Err(self.optype_err("%", a, b)),
}
}
binop::POW => match (ai, bi) {
(Some(x), Some(y)) if y >= 0 => {
let mut acc = num_bigint::BigInt::from(1);
let base = num_bigint::BigInt::from(x);
for _ in 0..y {
acc *= &base;
}
Ok(self.norm_big(acc))
}
_ if self.is_complex(a) || self.is_complex(b) => {
match (self.complex_val(a), self.complex_val(b)) {
(Some(x), Some(y)) => {
let (r, i) = c_pow(x, y);
Ok(self.alloc(PyObj::Complex(r, i)))
}
_ => Err(self.optype_err("**", a, b)),
}
}
_ => match (af, bf) {
(Some(x), Some(y)) if x == 0.0 && y < 0.0 => {
Err("ZeroDivisionError: zero to a negative power".into())
}
(Some(x), Some(y)) if x < 0.0 && y.fract() != 0.0 => {
let (r, i) = c_pow((x, 0.0), (y, 0.0));
Ok(self.alloc(PyObj::Complex(r, i)))
}
(Some(x), Some(y)) => Ok(Value::Float(x.powf(y))),
_ => Err(self.optype_err("**", a, b)),
},
},
binop::BITAND | binop::BITOR | binop::BITXOR => {
if tag == binop::BITOR {
if let (Some(PyObj::Dict(x)), Some(PyObj::Dict(y))) = (self.get(a), self.get(b))
{
let mut out = x.clone();
for (k, (kv, vv)) in y.clone() {
dict_put(&mut out, k, kv, vv);
}
return Ok(self.new_dict(out));
}
}
let a_set = self.setmap_of(a).is_some();
let b_set = self.setmap_of(b).is_some();
if let (Some(x), Some(y)) = (
self.setmap_operand(a, b_set),
self.setmap_operand(b, a_set),
) {
let mut out = IndexMap::new();
match tag {
binop::BITAND => {
for (k, v) in &x {
if y.contains_key(k) {
out.insert(k.clone(), v.clone());
}
}
}
binop::BITOR => {
out = x.clone();
for (k, v) in &y {
out.entry(k.clone()).or_insert_with(|| v.clone());
}
}
_ => {
for (k, v) in &x {
if !y.contains_key(k) {
out.insert(k.clone(), v.clone());
}
}
for (k, v) in &y {
if !x.contains_key(k) {
out.insert(k.clone(), v.clone());
}
}
}
}
let frozen = self.is_frozenset(a);
return Ok(self.new_setlike(out, frozen));
}
if let (Some(x), Some(y)) = (self.big_val(a), self.big_val(b)) {
let res = match tag {
binop::BITAND => x & y,
binop::BITOR => x | y,
_ => x ^ y,
};
if matches!(a, Value::Bool(_)) && matches!(b, Value::Bool(_)) {
use num_traits::Zero;
return Ok(Value::Bool(!res.is_zero()));
}
return Ok(self.norm_big(res));
}
if tag == binop::BITOR {
if let (Some(mut xs), Some(ys)) =
(self.union_members(a), self.union_members(b))
{
xs.extend(ys);
return Ok(self.build_union(xs));
}
}
Err(self.optype_err("bitop", a, b))
}
binop::SHL | binop::SHR => {
if let (Some(x), Some(y)) = (self.big_val(a), self.big_val(b)) {
use num_bigint::Sign;
use num_traits::ToPrimitive;
if y.sign() == Sign::Minus {
return Err("ValueError: negative shift count".into());
}
let sh = match y.to_usize() {
Some(s) => s,
None => return Err("OverflowError: too many digits in integer".into()),
};
let res = if tag == binop::SHL { x << sh } else { x >> sh };
return Ok(self.norm_big(res));
}
Err(self.optype_err("shift", a, b))
}
binop::MATMUL => Err(self.optype_err("@", a, b)),
_ => Err(type_error("unknown binop")),
}
}
pub fn unary(&mut self, tag: i64, v: &Value) -> Result<Value, String> {
#[cfg(feature = "stdlib-ffi")]
if self.foreign_id(v).is_some() {
let func = match tag {
unop::INVERT => "invert",
unop::POS => "pos",
_ => return Err(type_error("unknown unary op")),
};
return crate::ffi::unary_op(self, func, v);
}
match tag {
unop::INVERT => match self.big_val(v) {
Some(n) => Ok(self.norm_big(-(n + num_bigint::BigInt::from(1)))),
None => Err(type_error(&format!(
"bad operand type for unary ~: '{}'",
self.type_name(v)
))),
},
unop::POS => match v {
Value::Int(_) | Value::Float(_) | Value::Bool(_) => Ok(v.clone()),
_ if matches!(self.get(v), Some(PyObj::Complex(..))) => Ok(v.clone()),
_ if self.num_val(v).is_some() => Ok(v.clone()),
_ => Err(type_error(&format!(
"bad operand type for unary +: '{}'",
self.type_name(v)
))),
},
_ => Err(type_error("unknown unary op")),
}
}
pub fn str_format_percent(
&mut self,
fmt: &str,
args: &Value,
premap: &HashMap<u32, (String, String, String)>,
) -> Result<Value, String> {
let is_mapping = matches!(self.get(args), Some(PyObj::Dict(_)));
let arglist: Vec<Value> = if is_mapping {
vec![]
} else {
match self.get(args) {
Some(PyObj::Tuple(t)) => t.clone(),
_ => vec![args.clone()],
}
};
let chars: Vec<char> = fmt.chars().collect();
let n = chars.len();
let mut out = String::new();
let mut ai = 0usize;
let mut i = 0usize;
while i < n {
if chars[i] != '%' {
out.push(chars[i]);
i += 1;
continue;
}
i += 1;
if i >= n {
return Err("ValueError: incomplete format".into());
}
if chars[i] == '%' {
out.push('%');
i += 1;
continue;
}
let mut mapping_key: Option<String> = None;
if chars[i] == '(' {
i += 1;
let mut key = String::new();
let mut depth = 1;
while i < n && depth > 0 {
match chars[i] {
'(' => {
depth += 1;
key.push('(');
}
')' => {
depth -= 1;
if depth > 0 {
key.push(')');
}
}
c => key.push(c),
}
i += 1;
}
mapping_key = Some(key);
}
let (mut f_minus, mut f_plus, mut f_space, mut f_zero, mut f_hash) =
(false, false, false, false, false);
while i < n {
match chars[i] {
'-' => f_minus = true,
'+' => f_plus = true,
' ' => f_space = true,
'0' => f_zero = true,
'#' => f_hash = true,
_ => break,
}
i += 1;
}
let mut width: Option<usize> = None;
if i < n && chars[i] == '*' {
i += 1;
let w = self.next_arg_int(&arglist, &mut ai);
if w < 0 {
f_minus = true;
width = Some((-w) as usize);
} else {
width = Some(w as usize);
}
} else {
let mut wd = String::new();
while i < n && chars[i].is_ascii_digit() {
wd.push(chars[i]);
i += 1;
}
if !wd.is_empty() {
width = wd.parse().ok();
}
}
let mut prec: Option<usize> = None;
if i < n && chars[i] == '.' {
i += 1;
if i < n && chars[i] == '*' {
i += 1;
prec = Some(self.next_arg_int(&arglist, &mut ai).max(0) as usize);
} else {
let mut pd = String::new();
while i < n && chars[i].is_ascii_digit() {
pd.push(chars[i]);
i += 1;
}
prec = Some(pd.parse().unwrap_or(0));
}
}
while i < n && matches!(chars[i], 'h' | 'l' | 'L') {
i += 1;
}
if i >= n {
return Err("ValueError: incomplete format".into());
}
let conv = chars[i];
i += 1;
let val = if let Some(key) = &mapping_key {
let kv = self.new_str(key.clone());
let k = self.to_key(&kv)?;
let found = match self.get(args) {
Some(PyObj::Dict(d)) => d.get(&k).map(|(_, v)| v.clone()),
_ => return Err("TypeError: format requires a mapping".into()),
};
match found {
Some(v) => v,
None => return Err(self.key_error(&kv)),
}
} else {
let v = arglist.get(ai).cloned().ok_or_else(|| {
"TypeError: not enough arguments for format string".to_string()
})?;
ai += 1;
v
};
let core = self.format_conv(
conv,
&val,
ConvFlags {
plus: f_plus,
space: f_space,
hash: f_hash,
},
prec,
premap,
)?;
out.push_str(&pad_conv(
&core,
width,
f_minus,
f_zero,
is_numeric_conv(conv),
));
}
if !is_mapping && ai < arglist.len() {
return Err("TypeError: not all arguments converted during string formatting".into());
}
Ok(self.new_str(out))
}
pub fn bytes_format_percent(
&mut self,
fmt: &[u8],
args: &Value,
is_ba: bool,
premap: &std::collections::HashMap<u32, Vec<u8>>,
) -> Result<Value, String> {
let is_mapping = matches!(self.get(args), Some(PyObj::Dict(_)));
let arglist: Vec<Value> = if is_mapping {
vec![]
} else {
match self.get(args) {
Some(PyObj::Tuple(t)) => t.clone(),
_ => vec![args.clone()],
}
};
let n = fmt.len();
let mut out: Vec<u8> = Vec::with_capacity(n);
let mut ai = 0usize;
let mut i = 0usize;
while i < n {
if fmt[i] != b'%' {
out.push(fmt[i]);
i += 1;
continue;
}
i += 1;
if i >= n {
return Err("ValueError: incomplete format".into());
}
if fmt[i] == b'%' {
out.push(b'%');
i += 1;
continue;
}
let mut mapping_key: Option<Vec<u8>> = None;
if fmt[i] == b'(' {
i += 1;
let mut key = Vec::new();
let mut depth = 1;
while i < n && depth > 0 {
match fmt[i] {
b'(' => {
depth += 1;
key.push(b'(');
}
b')' => {
depth -= 1;
if depth > 0 {
key.push(b')');
}
}
c => key.push(c),
}
i += 1;
}
mapping_key = Some(key);
}
let (mut f_minus, mut f_plus, mut f_space, mut f_zero, mut f_hash) =
(false, false, false, false, false);
while i < n {
match fmt[i] {
b'-' => f_minus = true,
b'+' => f_plus = true,
b' ' => f_space = true,
b'0' => f_zero = true,
b'#' => f_hash = true,
_ => break,
}
i += 1;
}
let mut width: Option<usize> = None;
if i < n && fmt[i] == b'*' {
i += 1;
let w = self.next_arg_int(&arglist, &mut ai);
if w < 0 {
f_minus = true;
width = Some((-w) as usize);
} else {
width = Some(w as usize);
}
} else {
let mut wd = String::new();
while i < n && fmt[i].is_ascii_digit() {
wd.push(fmt[i] as char);
i += 1;
}
if !wd.is_empty() {
width = wd.parse().ok();
}
}
let mut prec: Option<usize> = None;
if i < n && fmt[i] == b'.' {
i += 1;
if i < n && fmt[i] == b'*' {
i += 1;
prec = Some(self.next_arg_int(&arglist, &mut ai).max(0) as usize);
} else {
let mut pd = String::new();
while i < n && fmt[i].is_ascii_digit() {
pd.push(fmt[i] as char);
i += 1;
}
prec = Some(pd.parse().unwrap_or(0));
}
}
while i < n && matches!(fmt[i], b'h' | b'l' | b'L') {
i += 1;
}
if i >= n {
return Err("ValueError: incomplete format".into());
}
let conv = fmt[i] as char;
i += 1;
let val = if let Some(key) = &mapping_key {
let kv = self.alloc(PyObj::Bytes(key.clone()));
let k = self.to_key(&kv)?;
let found = match self.get(args) {
Some(PyObj::Dict(d)) => d.get(&k).map(|(_, v)| v.clone()),
_ => return Err("TypeError: format requires a mapping".into()),
};
match found {
Some(v) => v,
None => return Err(self.key_error(&kv)),
}
} else {
let v = arglist.get(ai).cloned().ok_or_else(|| {
"TypeError: not enough arguments for format string".to_string()
})?;
ai += 1;
v
};
let (core, numeric): (Vec<u8>, bool) = match conv {
'b' | 's' => {
let mut raw = match self.get(&val) {
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => b.clone(),
_ => match &val {
Value::Obj(id) if premap.contains_key(id) => premap[id].clone(),
_ => {
return Err(type_error(&format!(
"%b requires a bytes-like object, or an object that implements __bytes__, not '{}'",
self.type_name(&val)
)))
}
},
};
if let Some(p) = prec {
raw.truncate(p);
}
(raw, false)
}
'a' | 'r' => {
let mut s = ascii_of(&self.repr_of(&val));
if let Some(p) = prec {
s = s.chars().take(p).collect();
}
(s.into_bytes(), false)
}
'c' => {
if let Some(cp) = self.as_int(&val) {
if !(0..=255).contains(&cp) {
return Err(
"TypeError: %c requires an integer in range(256) or a single byte"
.into(),
);
}
(vec![cp as u8], false)
} else {
let raw = match self.get(&val) {
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => Some(b.clone()),
_ => None,
};
match raw {
Some(b) if b.len() == 1 => (vec![b[0]], false),
Some(b) => return Err(format!(
"TypeError: %c requires an integer in range(256) or a single byte, not a bytes object of length {}",
b.len()
)),
None => return Err(type_error(&format!(
"%c requires an integer in range(256) or a single byte, not {}",
self.type_name(&val)
))),
}
}
}
'd' | 'i' | 'u' | 'x' | 'X' | 'o' | 'f' | 'F' | 'e' | 'E' | 'g' | 'G' => {
let s = self.format_conv(
conv,
&val,
ConvFlags {
plus: f_plus,
space: f_space,
hash: f_hash,
},
prec,
&HashMap::new(),
)?;
(s.into_bytes(), true)
}
other => {
return Err(format!(
"ValueError: unsupported format character '{other}'"
))
}
};
out.extend_from_slice(&pad_conv_bytes(&core, width, f_minus, f_zero, numeric));
}
if !is_mapping && ai < arglist.len() {
return Err("TypeError: not all arguments converted during bytes formatting".into());
}
Ok(if is_ba {
self.alloc(PyObj::Bytearray(out))
} else {
self.alloc(PyObj::Bytes(out))
})
}
fn next_arg_int(&self, arglist: &[Value], ai: &mut usize) -> i64 {
let v = arglist.get(*ai).cloned().unwrap_or(Value::Int(0));
*ai += 1;
self.as_int(&v).unwrap_or(0)
}
fn format_conv(
&mut self,
conv: char,
val: &Value,
flags: ConvFlags,
prec: Option<usize>,
premap: &HashMap<u32, (String, String, String)>,
) -> Result<String, String> {
let ConvFlags { plus, space, hash } = flags;
let sign_str = |neg: bool| -> &'static str {
if neg {
"-"
} else if plus {
"+"
} else if space {
" "
} else {
""
}
};
match conv {
's' | 'r' | 'a' => {
let mut s = match val {
Value::Obj(id) if premap.contains_key(id) => {
let (sr, rp, asc) = &premap[id];
match conv {
's' => sr.clone(),
'r' => rp.clone(),
_ => asc.clone(),
}
}
_ => match conv {
's' => self.str_of(val),
'r' => self.repr_of(val),
_ => ascii_of(&self.repr_of(val)),
},
};
if let Some(p) = prec {
s = s.chars().take(p).collect();
}
Ok(s)
}
'c' => {
if let Some(cp) = self.as_int(val) {
let ch = char::from_u32(cp as u32)
.ok_or_else(|| "OverflowError: %c arg not in range".to_string())?;
Ok(ch.to_string())
} else if let Some(s) = self.as_str(val) {
if s.chars().count() == 1 {
Ok(s)
} else {
Err("TypeError: %c requires int or char".into())
}
} else {
Err("TypeError: %c requires int or char".into())
}
}
'd' | 'i' | 'u' | 'x' | 'X' | 'o' => {
use num_traits::Signed;
let b = match self.big_val(val) {
Some(b) => b,
None if matches!(conv, 'd' | 'i' | 'u') => match self.num_val(val) {
Some(f) => num_bigint::BigInt::from(f.trunc() as i64),
None => {
return Err(type_error(&format!(
"%{conv} format: a real number is required, not {}",
self.type_name(val)
)))
}
},
None => {
return Err(type_error(&format!(
"%{conv} format: an integer is required, not {}",
self.type_name(val)
)))
}
};
let neg = b.is_negative();
let abs = b.abs();
let (mut digits, prefix) = match conv {
'x' => (abs.to_str_radix(16), if hash { "0x" } else { "" }),
'X' => (
abs.to_str_radix(16).to_uppercase(),
if hash { "0X" } else { "" },
),
'o' => (abs.to_str_radix(8), if hash { "0o" } else { "" }),
_ => (abs.to_str_radix(10), ""),
};
if let Some(p) = prec {
while digits.len() < p {
digits.insert(0, '0');
}
}
Ok(format!("{}{}{}", sign_str(neg), prefix, digits))
}
'f' | 'F' | 'e' | 'E' | 'g' | 'G' => {
let x = self.num_val(val).ok_or_else(|| {
type_error(&format!(
"%{conv} format: a real number is required, not {}",
self.type_name(val)
))
})?;
let neg = x.is_sign_negative();
if x.is_nan() {
return Ok(format!(
"{}{}",
sign_str(false),
if conv.is_uppercase() { "NAN" } else { "nan" }
));
}
if x.is_infinite() {
return Ok(format!(
"{}{}",
sign_str(neg),
if conv.is_uppercase() { "INF" } else { "inf" }
));
}
let mag = x.abs();
let core = match conv {
'f' | 'F' => format!("{:.*}", prec.unwrap_or(6), mag),
'e' => fmt_sci(mag, prec.unwrap_or(6), false),
'E' => fmt_sci(mag, prec.unwrap_or(6), true),
'g' => fmt_g(mag, prec.unwrap_or(6), false, hash),
_ => fmt_g(mag, prec.unwrap_or(6), true, hash),
};
Ok(format!("{}{}", sign_str(neg), core))
}
other => Err(format!(
"ValueError: unsupported format character '{other}'"
)),
}
}
}
fn is_numeric_conv(c: char) -> bool {
matches!(
c,
'd' | 'i' | 'u' | 'x' | 'X' | 'o' | 'f' | 'F' | 'e' | 'E' | 'g' | 'G'
)
}
fn pad_conv(core: &str, width: Option<usize>, minus: bool, zero: bool, numeric: bool) -> String {
let w = match width {
Some(w) => w,
None => return core.to_string(),
};
let len = core.chars().count();
if len >= w {
return core.to_string();
}
let pad = w - len;
if minus {
format!("{core}{}", " ".repeat(pad))
} else if zero && numeric {
let (prefix, rest) = split_sign_prefix(core);
format!("{prefix}{}{rest}", "0".repeat(pad))
} else {
format!("{}{core}", " ".repeat(pad))
}
}
fn pad_conv_bytes(
core: &[u8],
width: Option<usize>,
minus: bool,
zero: bool,
numeric: bool,
) -> Vec<u8> {
let w = match width {
Some(w) => w,
None => return core.to_vec(),
};
if core.len() >= w {
return core.to_vec();
}
let pad = w - core.len();
if minus {
let mut v = core.to_vec();
v.extend(std::iter::repeat(b' ').take(pad));
v
} else if zero && numeric {
let (prefix, rest) = split_sign_prefix_bytes(core);
let mut v = prefix.to_vec();
v.extend(std::iter::repeat(b'0').take(pad));
v.extend_from_slice(rest);
v
} else {
let mut v: Vec<u8> = std::iter::repeat(b' ').take(pad).collect();
v.extend_from_slice(core);
v
}
}
fn split_sign_prefix_bytes(s: &[u8]) -> (&[u8], &[u8]) {
let mut idx = 0;
if let Some(&c) = s.first() {
if c == b'+' || c == b'-' || c == b' ' {
idx = 1;
}
}
if s.len() >= idx + 2 && s[idx] == b'0' && matches!(s[idx + 1], b'x' | b'X' | b'o') {
idx += 2;
}
(&s[..idx], &s[idx..])
}
fn split_sign_prefix(s: &str) -> (String, &str) {
let mut idx = 0;
let bytes: Vec<char> = s.chars().collect();
let mut prefix = String::new();
if let Some(&c) = bytes.first() {
if c == '+' || c == '-' || c == ' ' {
prefix.push(c);
idx = 1;
}
}
if bytes.len() >= idx + 2 && bytes[idx] == '0' && matches!(bytes[idx + 1], 'x' | 'X' | 'o') {
prefix.push('0');
prefix.push(bytes[idx + 1]);
idx += 2;
}
let byte_off: usize = s.chars().take(idx).map(|c| c.len_utf8()).sum();
(prefix, &s[byte_off..])
}
pub fn fmt_sci(x: f64, prec: usize, upper: bool) -> String {
let s = format!("{:.*e}", prec, x);
let (mant, exp) = s.split_once('e').unwrap_or((s.as_str(), "0"));
let exp_num: i32 = exp.parse().unwrap_or(0);
let e = if upper { 'E' } else { 'e' };
format!(
"{mant}{e}{}{:02}",
if exp_num < 0 { '-' } else { '+' },
exp_num.abs()
)
}
pub fn fmt_g(x: f64, prec: usize, upper: bool, hash: bool) -> String {
let p = prec.max(1);
if x == 0.0 {
return "0".to_string();
}
let exp: i32 = format!("{:e}", x)
.split_once('e')
.and_then(|(_, e)| e.parse().ok())
.unwrap_or(0);
if exp < -4 || exp >= p as i32 {
let mut s = fmt_sci(x, p - 1, upper);
if !hash {
s = strip_g_sci(&s);
}
s
} else {
let dec = (p as i32 - 1 - exp).max(0) as usize;
let mut s = format!("{:.*}", dec, x);
if !hash && s.contains('.') {
s = s.trim_end_matches('0').trim_end_matches('.').to_string();
}
s
}
}
pub fn fmt_none_float(x: f64, prec: usize) -> String {
let p = prec.max(1);
let exp: i32 = if x == 0.0 {
0
} else {
fmt_sci(x, p - 1, false)
.split_once(['e', 'E'])
.and_then(|(_, e)| e.parse().ok())
.unwrap_or(0)
};
let mut s = if exp < -4 || exp >= p as i32 - 1 {
strip_g_sci(&fmt_sci(x, p - 1, false))
} else {
let dec = (p as i32 - 1 - exp).max(0) as usize;
let s = format!("{:.*}", dec, x);
if s.contains('.') {
s.trim_end_matches('0').trim_end_matches('.').to_string()
} else {
s
}
};
if !s.contains('.') && !s.contains(['e', 'E']) {
s.push_str(".0");
}
s
}
fn strip_g_sci(s: &str) -> String {
match s.find(['e', 'E']) {
Some(pos) => {
let (mant, exp) = s.split_at(pos);
let mant = if mant.contains('.') {
mant.trim_end_matches('0').trim_end_matches('.')
} else {
mant
};
format!("{mant}{exp}")
}
None => s.to_string(),
}
}
pub fn ascii_of(s: &str) -> String {
let mut o = String::new();
for c in s.chars() {
if c.is_ascii() {
o.push(c);
} else {
let u = c as u32;
if u <= 0xff {
o.push_str(&format!("\\x{u:02x}"));
} else if u <= 0xffff {
o.push_str(&format!("\\u{u:04x}"));
} else {
o.push_str(&format!("\\U{u:08x}"));
}
}
}
o
}
impl PyHost {
pub fn mv_bytes(&self, recv: &Value) -> Vec<u8> {
if let Some(PyObj::Memoryview {
obj, start, len, ..
}) = self.get(recv)
{
let (start, len) = (*start, *len);
if let Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) = self.get(obj) {
return b
.get(start..start + len)
.map(|s| s.to_vec())
.unwrap_or_default();
}
}
Vec::new()
}
pub fn get_item(&mut self, recv: &Value, idx: &Value) -> Result<Value, String> {
if let Some(d) = self.module_dict_snapshot(recv) {
return self.get_item(&d, idx);
}
#[cfg(feature = "stdlib-ffi")]
if let Some(id) = self.foreign_id(recv) {
return crate::ffi::get_item(self, id, idx);
}
if let Some(PyObj::Slice { lo, hi, step }) = self.get(idx) {
let (lo, hi, step) = (lo.clone(), hi.clone(), step.clone());
return self.get_slice(recv, &lo, &hi, &step);
}
if let Some(PyObj::StructTime { fields }) = self.get(recv) {
let seq: Vec<Value> = fields.iter().take(9).cloned().collect();
let t = self.new_tuple(seq);
return self.get_item(&t, idx);
}
match self.get(recv) {
Some(PyObj::List(l)) | Some(PyObj::Tuple(l)) => {
let is_tuple = matches!(self.get(recv), Some(PyObj::Tuple(_)));
let n = l.len() as i64;
let i = self.as_int(idx).ok_or_else(|| {
let ty = if is_tuple { "tuple" } else { "list" };
type_error(&format!(
"{ty} indices must be integers or slices, not {}",
self.type_name(idx)
))
})?;
let k = if i < 0 { i + n } else { i };
if k < 0 || k >= n {
let ty = if is_tuple { "tuple" } else { "list" };
return Err(format!("IndexError: {ty} index out of range"));
}
Ok(l[k as usize].clone())
}
Some(PyObj::Str(s)) => {
let chars: Vec<char> = s.chars().collect();
let n = chars.len() as i64;
let i = self
.as_int(idx)
.ok_or_else(|| type_error("string indices must be integers"))?;
let k = if i < 0 { i + n } else { i };
if k < 0 || k >= n {
return Err("IndexError: string index out of range".into());
}
let ch = chars[k as usize].to_string();
Ok(self.new_str(ch))
}
Some(PyObj::Dict(d)) => {
let key = self.to_key(idx)?;
let found = d.get(&key).map(|(_, v)| v.clone());
match found {
Some(v) => Ok(v),
None => Err(self.key_error(idx)),
}
}
Some(PyObj::MappingProxy { dict }) => {
let dict = dict.clone();
self.get_item(&dict, idx)
}
Some(PyObj::Range { start, step, .. }) => {
let (start, step) = (*start, *step);
let len = match self.get(recv) {
Some(PyObj::Range { start, stop, step }) => range_len(*start, *stop, *step),
_ => 0,
};
let i = self
.as_int(idx)
.ok_or_else(|| type_error("range indices must be integers"))?;
let k = if i < 0 { i + len } else { i };
if k < 0 || k >= len {
return Err("IndexError: range object index out of range".into());
}
Ok(Value::Int(start + k * step))
}
Some(PyObj::BigRange { start, stop, step }) => {
let (start, stop, step) = (start.clone(), stop.clone(), step.clone());
let len = big_range_len(&start, &stop, &step);
let i = self
.big_val(idx)
.ok_or_else(|| type_error("range indices must be integers"))?;
let k = if i < num_bigint::BigInt::from(0) { &i + &len } else { i };
if k < num_bigint::BigInt::from(0) || k >= len {
return Err("IndexError: range object index out of range".into());
}
Ok(self.norm_big(start + k * step))
}
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => {
let is_ba = matches!(self.get(recv), Some(PyObj::Bytearray(_)));
let n = b.len() as i64;
let i = self
.as_int(idx)
.ok_or_else(|| type_error("byte indices must be integers"))?;
let k = if i < 0 { i + n } else { i };
if k < 0 || k >= n {
return Err(if is_ba {
"IndexError: bytearray index out of range".into()
} else {
"IndexError: index out of range".into()
});
}
Ok(Value::Int(b[k as usize] as i64))
}
Some(PyObj::Deque { items, .. }) => {
let n = items.len() as i64;
let i = self
.as_int(idx)
.ok_or_else(|| type_error("deque indices must be integers"))?;
let k = if i < 0 { i + n } else { i };
if k < 0 || k >= n {
return Err("IndexError: deque index out of range".into());
}
Ok(items[k as usize].clone())
}
Some(PyObj::Memoryview { .. }) => {
let bytes = self.mv_bytes(recv);
let n = bytes.len() as i64;
let i = self
.as_int(idx)
.ok_or_else(|| type_error("memoryview: invalid slice key"))?;
let k = if i < 0 { i + n } else { i };
if k < 0 || k >= n {
return Err("IndexError: index out of bounds on dimension 1".into());
}
Ok(Value::Int(bytes[k as usize] as i64))
}
Some(PyObj::Builtin(n)) if n == "_typing.Union" => {
let items = match self.get(idx) {
Some(PyObj::Tuple(xs)) => xs.clone(),
_ => vec![idx.clone()],
};
if items.is_empty() {
return Err(type_error("Cannot take a Union of no types."));
}
Ok(self.build_union(items))
}
_ => Err(type_error(&format!(
"'{}' object is not subscriptable",
self.type_name(recv)
))),
}
}
fn build_union(&mut self, items: Vec<Value>) -> Value {
let mut args: Vec<Value> = Vec::with_capacity(items.len());
for item in items {
let members = match self.get(&item) {
Some(PyObj::Union { args }) => args.clone(),
_ => vec![item],
};
for m in members {
if !args.iter().any(|x| self.equal(x, &m)) {
args.push(m);
}
}
}
if args.len() == 1 {
return args.into_iter().next().unwrap();
}
self.alloc(PyObj::Union { args })
}
fn get_slice(
&mut self,
recv: &Value,
lo: &Value,
hi: &Value,
step: &Value,
) -> Result<Value, String> {
let step = self.as_int(step).unwrap_or(1);
if step == 0 {
return Err("ValueError: slice step cannot be zero".into());
}
if let Some(PyObj::Range {
start: rstart,
stop: rstop,
step: rstep,
}) = self.get(recv)
{
let (rstart, rstep) = (*rstart, *rstep);
let len = range_len(rstart, *rstop, rstep);
let (i, j) = slice_bounds(lo, hi, step, len, self);
return Ok(self.alloc(PyObj::Range {
start: rstart + i * rstep,
stop: rstart + j * rstep,
step: rstep * step,
}));
}
if let Some(PyObj::Memoryview {
obj,
start,
len,
readonly,
}) = self.get(recv)
{
let (obj, start, readonly) = (obj.clone(), *start, *readonly);
let n = *len as i64;
let (mut i, stop) = slice_bounds(lo, hi, step, n, self);
if step == 1 {
let lo_i = i.clamp(0, n);
let hi_i = stop.clamp(lo_i, n);
return Ok(self.alloc(PyObj::Memoryview {
obj,
start: start + lo_i as usize,
len: (hi_i - lo_i) as usize,
readonly,
}));
}
let src = self.mv_bytes(recv);
let mut out = Vec::new();
if step > 0 {
while i < stop {
if i >= 0 && i < n {
out.push(src[i as usize]);
}
i += step;
}
} else {
while i > stop {
if i >= 0 && i < n {
out.push(src[i as usize]);
}
i += step;
}
}
let len = out.len();
let buf = self.alloc(PyObj::Bytes(out));
return Ok(self.alloc(PyObj::Memoryview {
obj: buf,
start: 0,
len,
readonly: true,
}));
}
if let Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) = self.get(recv) {
let is_ba = matches!(self.get(recv), Some(PyObj::Bytearray(_)));
let src = b.clone();
let n = src.len() as i64;
let (mut i, stop) = slice_bounds(lo, hi, step, n, self);
let mut out = Vec::new();
if step > 0 {
while i < stop {
if i >= 0 && i < n {
out.push(src[i as usize]);
}
i += step;
}
} else {
while i > stop {
if i >= 0 && i < n {
out.push(src[i as usize]);
}
i += step;
}
}
return Ok(if is_ba {
self.alloc(PyObj::Bytearray(out))
} else {
self.alloc(PyObj::Bytes(out))
});
}
let is_str = matches!(self.get(recv), Some(PyObj::Str(_)));
let items: Vec<Value> = match self.get(recv) {
Some(PyObj::List(l)) | Some(PyObj::Tuple(l)) => l.clone(),
Some(PyObj::Str(s)) => s.chars().map(|c| Value::Int(c as i64)).collect(),
_ => return Err(type_error("object is not subscriptable")),
};
let n = items.len() as i64;
let (mut i, stop) = slice_bounds(lo, hi, step, n, self);
let mut out = Vec::new();
if step > 0 {
while i < stop {
if i >= 0 && i < n {
out.push(items[i as usize].clone());
}
i += step;
}
} else {
while i > stop {
if i >= 0 && i < n {
out.push(items[i as usize].clone());
}
i += step;
}
}
if is_str {
if let Some(PyObj::Str(s)) = self.get(recv) {
let chars: Vec<char> = s.chars().collect();
let mut r = String::new();
let (mut i2, stop2) = slice_bounds(lo, hi, step, n, self);
if step > 0 {
while i2 < stop2 {
if i2 >= 0 && i2 < n {
r.push(chars[i2 as usize]);
}
i2 += step;
}
} else {
while i2 > stop2 {
if i2 >= 0 && i2 < n {
r.push(chars[i2 as usize]);
}
i2 += step;
}
}
return Ok(self.new_str(r));
}
}
if matches!(self.get(recv), Some(PyObj::Tuple(_))) {
Ok(self.new_tuple(out))
} else {
Ok(self.new_list(out))
}
}
pub fn set_item(&mut self, recv: &Value, idx: &Value, val: Value) -> Result<(), String> {
if let Some(slot) = self.module_dict_slot(recv) {
let k = self
.as_str(idx)
.ok_or_else(|| type_error("module namespace keys must be strings"))?;
self.module_globals[slot].insert(k, val);
return Ok(());
}
match self.get(recv) {
Some(PyObj::List(l)) => {
let n = l.len() as i64;
let i = self
.as_int(idx)
.ok_or_else(|| type_error("list indices must be integers"))?;
let k = if i < 0 { i + n } else { i };
if k < 0 || k >= n {
return Err("IndexError: list assignment index out of range".into());
}
if let Some(PyObj::List(l)) = self.get_mut(recv) {
l[k as usize] = val;
}
Ok(())
}
Some(PyObj::Dict(_)) => {
let key = self.to_key(idx)?;
let kv = idx.clone();
if let Some(PyObj::Dict(d)) = self.get_mut(recv) {
dict_put(d, key, kv, val);
}
Ok(())
}
Some(PyObj::Bytearray(b)) => {
let n = b.len() as i64;
let i = self
.as_int(idx)
.ok_or_else(|| type_error("bytearray indices must be integers"))?;
let k = if i < 0 { i + n } else { i };
if k < 0 || k >= n {
return Err("IndexError: bytearray index out of range".into());
}
let v = self
.as_int(&val)
.ok_or_else(|| type_error("an integer is required"))?;
if !(0..=255).contains(&v) {
return Err("ValueError: byte must be in range(0, 256)".into());
}
if let Some(PyObj::Bytearray(b)) = self.get_mut(recv) {
b[k as usize] = v as u8;
}
Ok(())
}
_ => Err(type_error(&format!(
"'{}' object does not support item assignment",
self.type_name(recv)
))),
}
}
pub fn del_item(&mut self, recv: &Value, idx: &Value) -> Result<(), String> {
if let Some(slot) = self.module_dict_slot(recv) {
let k = self
.as_str(idx)
.ok_or_else(|| type_error("module namespace keys must be strings"))?;
return match self.module_globals[slot].shift_remove(&k) {
Some(_) => Ok(()),
None => Err(format!("KeyError: '{k}'")),
};
}
if let Some(PyObj::Slice { lo, hi, step }) = self.get(idx) {
let (lo, hi, step) = (lo.clone(), hi.clone(), step.clone());
return self.del_slice(recv, &lo, &hi, &step);
}
match self.get(recv) {
Some(PyObj::Dict(_)) => {
let key = self.to_key(idx)?;
let removed = match self.get_mut(recv) {
Some(PyObj::Dict(d)) => d.shift_remove(&key).is_some(),
_ => false,
};
if !removed {
return Err(self.key_error(idx));
}
Ok(())
}
Some(PyObj::List(l)) => {
let n = l.len() as i64;
let i = self
.as_int(idx)
.ok_or_else(|| type_error("list indices must be integers"))?;
let k = if i < 0 { i + n } else { i };
if k < 0 || k >= n {
return Err("IndexError: list assignment index out of range".into());
}
if let Some(PyObj::List(l)) = self.get_mut(recv) {
l.remove(k as usize);
}
Ok(())
}
Some(PyObj::Bytearray(b)) => {
let n = b.len() as i64;
let i = self
.as_int(idx)
.ok_or_else(|| type_error("bytearray indices must be integers"))?;
let k = if i < 0 { i + n } else { i };
if k < 0 || k >= n {
return Err("IndexError: bytearray index out of range".into());
}
if let Some(PyObj::Bytearray(b)) = self.get_mut(recv) {
b.remove(k as usize);
}
Ok(())
}
_ => Err(type_error("object doesn't support item deletion")),
}
}
fn slice_indices(&mut self, lo: &Value, hi: &Value, step: i64, n: i64) -> Vec<i64> {
let (mut i, stop) = slice_bounds(lo, hi, step, n, self);
let mut out = Vec::new();
if step > 0 {
while i < stop {
if i >= 0 && i < n {
out.push(i);
}
i += step;
}
} else {
while i > stop {
if i >= 0 && i < n {
out.push(i);
}
i += step;
}
}
out
}
pub fn set_slice_vals(
&mut self,
recv: &Value,
idx: &Value,
repl: Vec<Value>,
) -> Result<(), String> {
let (lo, hi, step) = match self.get(idx) {
Some(PyObj::Slice { lo, hi, step }) => (lo.clone(), hi.clone(), step.clone()),
_ => return Err(type_error("expected a slice")),
};
let (lo, hi) = (&lo, &hi);
let step = self.as_int(&step).unwrap_or(1);
if step == 0 {
return Err("ValueError: slice step cannot be zero".into());
}
if matches!(self.get(recv), Some(PyObj::Bytearray(_))) {
return self.set_bytearray_slice(recv, lo, hi, step, repl);
}
let n = match self.get(recv) {
Some(PyObj::List(l)) => l.len() as i64,
_ => {
return Err(type_error(&format!(
"'{}' object does not support item assignment",
self.type_name(recv)
)))
}
};
if step == 1 {
let (start, stop) = slice_bounds(lo, hi, 1, n, self);
let (start, stop) = (
start.clamp(0, n) as usize,
stop.clamp(0, n).max(start) as usize,
);
if let Some(PyObj::List(l)) = self.get_mut(recv) {
l.splice(start..stop, repl);
}
Ok(())
} else {
let indices = self.slice_indices(lo, hi, step, n);
if indices.len() != repl.len() {
return Err(format!(
"ValueError: attempt to assign sequence of size {} to extended slice of size {}",
repl.len(),
indices.len()
));
}
if let Some(PyObj::List(l)) = self.get_mut(recv) {
for (idx, v) in indices.into_iter().zip(repl) {
l[idx as usize] = v;
}
}
Ok(())
}
}
fn set_bytearray_slice(
&mut self,
recv: &Value,
lo: &Value,
hi: &Value,
step: i64,
repl: Vec<Value>,
) -> Result<(), String> {
let mut bytes = Vec::with_capacity(repl.len());
for v in &repl {
let n = self
.as_int(v)
.ok_or_else(|| type_error("an integer is required"))?;
if !(0..=255).contains(&n) {
return Err("ValueError: byte must be in range(0, 256)".into());
}
bytes.push(n as u8);
}
let n = match self.get(recv) {
Some(PyObj::Bytearray(b)) => b.len() as i64,
_ => return Err(type_error("expected a bytearray")),
};
if step == 1 {
let (start, stop) = slice_bounds(lo, hi, 1, n, self);
let (start, stop) = (
start.clamp(0, n) as usize,
stop.clamp(0, n).max(start) as usize,
);
if let Some(PyObj::Bytearray(b)) = self.get_mut(recv) {
b.splice(start..stop, bytes);
}
Ok(())
} else {
let indices = self.slice_indices(lo, hi, step, n);
if indices.len() != bytes.len() {
return Err(format!(
"ValueError: attempt to assign bytes of size {} to extended slice of size {}",
bytes.len(),
indices.len()
));
}
if let Some(PyObj::Bytearray(b)) = self.get_mut(recv) {
for (idx, v) in indices.into_iter().zip(bytes) {
b[idx as usize] = v;
}
}
Ok(())
}
}
fn del_slice(
&mut self,
recv: &Value,
lo: &Value,
hi: &Value,
step: &Value,
) -> Result<(), String> {
let step = self.as_int(step).unwrap_or(1);
if step == 0 {
return Err("ValueError: slice step cannot be zero".into());
}
let n = match self.get(recv) {
Some(PyObj::List(l)) => l.len() as i64,
Some(PyObj::Bytearray(b)) => b.len() as i64,
_ => {
return Err(type_error(&format!(
"'{}' object doesn't support item deletion",
self.type_name(recv)
)))
}
};
let mut indices = self.slice_indices(lo, hi, step, n);
indices.sort_unstable();
indices.dedup();
match self.get_mut(recv) {
Some(PyObj::List(l)) => {
for i in indices.into_iter().rev() {
if (i as usize) < l.len() {
l.remove(i as usize);
}
}
}
Some(PyObj::Bytearray(b)) => {
for i in indices.into_iter().rev() {
if (i as usize) < b.len() {
b.remove(i as usize);
}
}
}
_ => {}
}
Ok(())
}
pub fn iter_items(&mut self, v: &Value) -> Result<Vec<Value>, String> {
if let Some(PyObj::CsvReader { rows, idx, .. }) = self.get(v) {
let out = rows[(*idx).min(rows.len())..].to_vec();
if let Some(PyObj::CsvReader { rows, idx, .. }) = self.get_mut(v) {
*idx = rows.len();
}
return Ok(out);
}
if let Some(d) = self.module_dict_snapshot(v) {
return self.iter_items(&d);
}
if matches!(
self.get(v),
Some(PyObj::BytesIO { .. }) | Some(PyObj::StringIO { .. })
) {
if let Some(r) = crate::stdlib::pyio::stream_lines(self, v) {
return r;
}
}
if let Some(PyObj::Template {
strings,
interpolations,
}) = self.get(v)
{
let (strings, interps) = (strings.clone(), interpolations.clone());
let mut out = Vec::with_capacity(strings.len() + interps.len());
for (i, s) in strings.iter().enumerate() {
if !s.is_empty() {
let sv = self.new_str(s.clone());
out.push(sv);
}
if let Some(interp) = interps.get(i) {
out.push(interp.clone());
}
}
return Ok(out);
}
let file_id = match self.get(v) {
Some(PyObj::File { id }) => Some(*id),
_ => None,
};
if let Some(id) = file_id {
let lines = self.io_read_lines(id)?;
return Ok(lines.into_iter().map(|l| self.new_str(l)).collect());
}
if let Some(items) = self.view_items(v) {
return Ok(items);
}
#[cfg(feature = "stdlib-ffi")]
if let Some(id) = self.foreign_id(v) {
let it = crate::ffi::make_iter(self, id)?;
let mut out = Vec::new();
while let Some(x) = self.iter_next(&it)? {
out.push(x);
}
return Ok(out);
}
match self.get(v) {
Some(PyObj::List(l)) | Some(PyObj::Tuple(l)) => Ok(l.clone()),
Some(PyObj::StructTime { fields }) => Ok(fields.iter().take(9).cloned().collect()),
Some(PyObj::Deque { items, .. }) => Ok(items.iter().cloned().collect()),
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => {
Ok(b.iter().map(|&x| Value::Int(x as i64)).collect())
}
Some(PyObj::Memoryview { .. }) => Ok(self
.mv_bytes(v)
.iter()
.map(|&x| Value::Int(x as i64))
.collect()),
Some(PyObj::Str(s)) => {
let chars: Vec<Value> = s
.chars()
.collect::<Vec<_>>()
.iter()
.map(|c| c.to_string())
.collect::<Vec<_>>()
.into_iter()
.map(|s| self.new_str(s))
.collect();
Ok(chars)
}
Some(PyObj::Set(s)) | Some(PyObj::Frozenset(s)) => Ok(self.set_ordered_values(s)),
Some(PyObj::Dict(d)) => Ok(d.values().map(|(k, _)| k.clone()).collect()),
Some(PyObj::Range { start, stop, step }) => {
let (start, stop, step) = (*start, *stop, *step);
let mut out = Vec::new();
let mut c = start;
if step > 0 {
while c < stop {
out.push(Value::Int(c));
c += step;
}
} else if step < 0 {
while c > stop {
out.push(Value::Int(c));
c += step;
}
}
Ok(out)
}
Some(PyObj::BigRange { start, stop, step }) => {
use num_traits::Zero;
let (stop, step) = (stop.clone(), step.clone());
let pos = step > num_bigint::BigInt::zero();
let mut out = Vec::new();
let mut c = start.clone();
loop {
let go = if pos { c < stop } else { c > stop };
if !go {
break;
}
out.push(self.norm_big(c.clone()));
c += &step;
}
Ok(out)
}
Some(PyObj::MappingProxy { dict }) => {
let dict = dict.clone();
Ok(match self.get(&dict) {
Some(PyObj::Dict(d)) => d.values().map(|(k, _)| k.clone()).collect(),
_ => Vec::new(),
})
}
Some(PyObj::Iter(_)) => {
let mut out = Vec::new();
while let Some(x) = self.iter_next(v)? {
out.push(x);
}
Ok(out)
}
_ => {
Err(type_error(&format!(
"'{}' object is not iterable",
self.type_name(v)
)))
}
}
}
pub fn make_iter(&mut self, v: &Value) -> Result<Value, String> {
#[cfg(feature = "stdlib-ffi")]
if let Some(id) = self.foreign_id(v) {
return crate::ffi::make_iter(self, id);
}
if let Some(items) = self.view_items(v) {
return Ok(self.alloc(PyObj::Iter(IterState::Seq { items, idx: 0 })));
}
if let Some((payload, class)) = match self.get(v) {
Some(PyObj::Instance(i)) if !matches!(i.payload, Value::Undef) => {
Some((i.payload.clone(), i.class.clone()))
}
_ => None,
} {
if self.builtin_base_of(&class).is_some()
&& self.class_lookup(&class, "__iter__").is_none()
{
return self.make_iter(&payload);
}
}
let state = match self.get(v) {
Some(PyObj::List(l)) | Some(PyObj::Tuple(l)) => IterState::Seq {
items: l.clone(),
idx: 0,
},
Some(PyObj::StructTime { fields }) => IterState::Seq {
items: fields.iter().take(9).cloned().collect(),
idx: 0,
},
Some(PyObj::Str(s)) => IterState::Seq {
items: s
.chars()
.map(|c| c.to_string())
.collect::<Vec<_>>()
.into_iter()
.map(PyObj::Str)
.map(|o| self.alloc(o))
.collect(),
idx: 0,
},
Some(PyObj::Set(s)) | Some(PyObj::Frozenset(s)) => IterState::Seq {
items: self.set_ordered_values(s),
idx: 0,
},
Some(PyObj::Dict(d)) => IterState::DictKeys {
keys: d.values().map(|(k, _)| k.clone()).collect(),
idx: 0,
},
Some(PyObj::Range { start, stop, step }) => IterState::RangeIter {
cur: *start,
stop: *stop,
step: *step,
},
Some(PyObj::BigRange { start, stop, step }) => IterState::BigRangeIter {
cur: start.clone(),
stop: stop.clone(),
step: step.clone(),
},
Some(PyObj::Iter(_))
| Some(PyObj::CsvReader { .. })
| Some(PyObj::Generator { .. })
| Some(PyObj::Zip { .. })
| Some(PyObj::MapObj { .. })
| Some(PyObj::FilterObj { .. })
| Some(PyObj::EnumerateObj { .. })
| Some(PyObj::ItertoolsIter { .. })
| Some(PyObj::CallIter { .. }) => return Ok(v.clone()),
_ => {
let items = self.iter_items(v)?;
IterState::Seq { items, idx: 0 }
}
};
Ok(self.alloc(PyObj::Iter(state)))
}
pub fn iter_next(&mut self, it: &Value) -> Result<Option<Value>, String> {
if let Some(PyObj::CsvReader { rows, idx, .. }) = self.get_mut(it) {
if *idx >= rows.len() {
return Ok(None);
}
let row = rows[*idx].clone();
*idx += 1;
return Ok(Some(row));
}
#[cfg(feature = "stdlib-ffi")]
if let Some(id) = self.foreign_id(it) {
return crate::ffi::iter_next(self, id);
}
if let Some(PyObj::Iter(IterState::BigRangeIter { cur, stop, step })) = self.get(it) {
use num_traits::Zero;
let (cur, stop, step) = (cur.clone(), stop.clone(), step.clone());
let go = if step > num_bigint::BigInt::zero() {
cur < stop
} else {
cur > stop
};
if !go {
return Ok(None);
}
if let Some(PyObj::Iter(IterState::BigRangeIter { cur: c, .. })) = self.get_mut(it) {
*c = &cur + &step;
}
return Ok(Some(self.norm_big(cur)));
}
let out = match self.get_mut(it) {
Some(PyObj::Iter(IterState::Seq { items, idx })) => {
if *idx < items.len() {
let v = items[*idx].clone();
*idx += 1;
Some(v)
} else {
None
}
}
Some(PyObj::Iter(IterState::DictKeys { keys, idx })) => {
if *idx < keys.len() {
let v = keys[*idx].clone();
*idx += 1;
Some(v)
} else {
None
}
}
Some(PyObj::Iter(IterState::RangeIter { cur, stop, step })) => {
let go = if *step > 0 {
*cur < *stop
} else {
*cur > *stop
};
if go {
let v = *cur;
*cur += *step;
Some(Value::Int(v))
} else {
None
}
}
_ => return Err(type_error("not an iterator")),
};
Ok(out)
}
pub fn contains(&mut self, item: &Value, container: &Value) -> Result<bool, String> {
if let Some(d) = self.module_dict_snapshot(container) {
return self.contains(item, &d);
}
#[cfg(feature = "stdlib-ffi")]
if let Some(id) = self.foreign_id(container) {
return crate::ffi::contains(self, id, item);
}
if let Some(PyObj::DictView { dict, kind }) = self.get(container) {
let (dict, kind) = (dict.clone(), *kind);
if kind == 0 {
let key = self.to_key(item)?;
return Ok(matches!(self.get(&dict), Some(PyObj::Dict(d)) if d.contains_key(&key)));
}
let items = self.view_items(container).unwrap_or_default();
return Ok(items.iter().any(|x| self.equal(x, item)));
}
match self.get(container) {
Some(PyObj::Str(s)) => {
let needle = self
.as_str(item)
.ok_or_else(|| type_error("'in <string>' requires string as left operand"))?;
Ok(s.contains(&needle))
}
Some(PyObj::List(l)) | Some(PyObj::Tuple(l)) => {
let l = l.clone();
Ok(l.iter().any(|x| self.equal(x, item)))
}
Some(PyObj::Dict(d)) => {
let key = self.to_key(item)?;
Ok(d.contains_key(&key))
}
Some(PyObj::MappingProxy { dict }) => {
let dict = dict.clone();
self.contains(item, &dict)
}
Some(PyObj::Set(s)) | Some(PyObj::Frozenset(s)) => {
let key = self.to_key(item)?;
Ok(s.contains_key(&key))
}
Some(PyObj::Bytes(hay)) | Some(PyObj::Bytearray(hay)) => {
let hay = hay.clone();
if let Some(n) = self.as_int(item) {
if !(0..=255).contains(&n) {
return Err("ValueError: byte must be in range(0, 256)".into());
}
return Ok(hay.contains(&(n as u8)));
}
let needle = match self.get(item) {
Some(PyObj::Bytes(b)) | Some(PyObj::Bytearray(b)) => b.clone(),
_ => {
return Err(type_error(&format!(
"a bytes-like object is required, or an integer, not '{}'",
self.type_name(item)
)))
}
};
if needle.is_empty() {
return Ok(true);
}
Ok(hay.windows(needle.len()).any(|w| w == needle.as_slice()))
}
Some(PyObj::Memoryview { .. }) => {
let hay = self.mv_bytes(container);
match self.as_int(item) {
Some(n) if (0..=255).contains(&n) => Ok(hay.contains(&(n as u8))),
_ => Ok(false),
}
}
Some(PyObj::Range { start, stop, step }) => {
let (start, stop, step) = (*start, *stop, *step);
let x = match item {
Value::Int(n) => *n,
Value::Bool(b) => *b as i64,
Value::Float(f)
if f.fract() == 0.0
&& f.is_finite()
&& *f >= i64::MIN as f64
&& *f <= i64::MAX as f64 =>
{
*f as i64
}
_ => return Ok(false),
};
let in_bounds = if step > 0 {
x >= start && x < stop
} else {
x <= start && x > stop
};
Ok(in_bounds && (x - start).rem_euclid(step.abs()) == 0)
}
_ => {
let items = self.iter_items(container)?;
Ok(items.iter().any(|x| self.equal(x, item)))
}
}
}
}
impl PyHost {
pub fn slice_adjust(&self, lo: &Value, hi: &Value, step: i64, n: i64) -> (i64, i64) {
slice_bounds(lo, hi, step, n, self)
}
}
fn slice_bounds(lo: &Value, hi: &Value, step: i64, n: i64, h: &PyHost) -> (i64, i64) {
let lower = if step < 0 { -1 } else { 0 };
let upper = if step < 0 { n - 1 } else { n };
let adjust = |x: i64| -> i64 {
let x = if x < 0 { x + n } else { x };
x.clamp(lower, upper)
};
let start = match h.as_int(lo) {
Some(x) => adjust(x),
None => {
if step < 0 {
n - 1
} else {
0
}
}
};
let stop = match h.as_int(hi) {
Some(x) => adjust(x),
None => {
if step < 0 {
-1
} else {
n
}
}
};
(start, stop)
}
impl PyHost {
pub fn class_is_exception(&self, class: &str) -> bool {
self.mro_of(class)
.iter()
.any(|c| crate::builtins::is_exception_class(c))
}
fn class_or_builtin_type(&mut self, name: String) -> Value {
if self.classes.contains_key(&name) {
self.alloc(PyObj::Class(name))
} else {
self.alloc(PyObj::Builtin(name))
}
}
pub fn make_type_var(
&mut self,
kind: TypeVarKind,
name: String,
constraints: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Value {
let get = |kwargs: &[(String, Value)], k: &str| {
kwargs.iter().find(|(kk, _)| kk == k).map(|(_, v)| v.clone())
};
let name_v = self.new_str(name.clone());
let bound = get(&kwargs, "bound").unwrap_or(Value::Undef);
let covariant = get(&kwargs, "covariant").unwrap_or(Value::Bool(false));
let contravariant = get(&kwargs, "contravariant").unwrap_or(Value::Bool(false));
let infer_variance = get(&kwargs, "infer_variance").unwrap_or(Value::Bool(false));
let default = get(&kwargs, "default").unwrap_or_else(|| {
self.cached_module("_typing")
.and_then(|m| self.get_attr(&m, "NoDefault").ok())
.unwrap_or(Value::Undef)
});
let constraints_tuple = self.new_tuple(constraints);
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
for (k, v) in [
("__name__", name_v),
("__bound__", bound),
("__constraints__", constraints_tuple),
("__covariant__", covariant),
("__contravariant__", contravariant),
("__infer_variance__", infer_variance),
("__default__", default),
] {
let kv = self.new_str(k.to_string());
d.insert(PKey::Str(k.to_string()), (kv, v));
}
let attrs = self.new_dict(d);
self.alloc(PyObj::TypeVarLike { kind, name, attrs })
}
pub fn metaclass_method(&self, v: &Value, name: &str) -> Option<Value> {
let cname = match self.get(v) {
Some(PyObj::Class(c)) => c.clone(),
_ => return None,
};
let meta = self.classes.get(&cname).map(|cd| cd.metaclass.clone())?;
if meta.is_empty() || meta == "type" {
return None;
}
self.class_lookup(&meta, name)
}
pub fn mro_rc(&self, class: &str) -> std::rc::Rc<Vec<String>> {
if let Some(hit) = self.mro_cache.borrow().get(class) {
return hit.clone();
}
let computed = std::rc::Rc::new(self.compute_mro(class));
self.mro_cache
.borrow_mut()
.insert(class.to_string(), computed.clone());
computed
}
pub fn mro_of(&self, class: &str) -> Vec<String> {
(*self.mro_rc(class)).clone()
}
fn compute_mro(&self, class: &str) -> Vec<String> {
let bases: Vec<String> = self
.classes
.get(class)
.map(|cd| cd.bases.clone())
.unwrap_or_default();
if bases.is_empty() {
return vec![class.to_string()];
}
let mut seqs: Vec<Vec<String>> = bases.iter().map(|b| (*self.mro_rc(b)).clone()).collect();
seqs.push(bases);
let mut result = vec![class.to_string()];
loop {
seqs.retain(|s| !s.is_empty());
if seqs.is_empty() {
break;
}
let head = seqs.iter().find_map(|s| {
let h = &s[0];
let in_tail = seqs.iter().any(|t| t.len() > 1 && t[1..].contains(h));
if in_tail {
None
} else {
Some(h.clone())
}
});
let head = match head {
Some(h) => h,
None => break,
};
result.push(head.clone());
for s in &mut seqs {
if s.first() == Some(&head) {
s.remove(0);
}
}
}
result
}
pub fn class_lookup(&self, class: &str, name: &str) -> Option<Value> {
for c in self.mro_rc(class).iter() {
if let Some(cd) = self.classes.get(c) {
if let Some(v) = cd.ns.get(name) {
return Some(v.clone());
}
}
}
None
}
pub fn get_attr(&mut self, recv: &Value, name: &str) -> Result<Value, String> {
#[cfg(feature = "stdlib-ffi")]
if let Some(id) = self.foreign_id(recv) {
return crate::ffi::get_attr(self, id, name);
}
if !matches!(recv, Value::Obj(_)) {
match name {
"__new__" => return Ok(self.alloc(PyObj::Builtin("object.__new__".into()))),
"__class__" => {
let tn = self.type_name(recv);
return Ok(self.alloc(PyObj::Builtin(tn)));
}
_ => {}
}
}
if let Value::Obj(i) = recv {
if let Some(fields) = self.nt_meta.get(i).map(|m| m.fields.clone()) {
if name == "_fields" {
let items: Vec<Value> =
fields.iter().map(|f| self.new_str(f.clone())).collect();
return Ok(self.new_tuple(items));
}
if let Some(idx) = fields.iter().position(|f| f == name) {
if let Some(PyObj::Tuple(items)) = self.get(recv) {
if let Some(v) = items.get(idx) {
return Ok(v.clone());
}
}
}
}
}
if name == "_fields" {
if let Some(PyObj::NamedTupleType { fields, .. }) = self.get(recv) {
let fields = fields.clone();
let items: Vec<Value> = fields.iter().map(|f| self.new_str(f.clone())).collect();
return Ok(self.new_tuple(items));
}
}
if let Some(PyObj::Module { name: mname, .. }) = self.get(recv) {
if mname == "sys" {
if name == "stdout" {
if let Some(t) = self.stdout_target.clone() {
return Ok(t);
}
} else if name == "stderr" {
if let Some(t) = self.stderr_target.clone() {
return Ok(t);
}
}
}
}
if matches!(name, "__iter__" | "__next__")
&& matches!(self.get(recv), Some(PyObj::CsvReader { .. }))
{
let b = self.alloc(PyObj::Builtin(name.to_string()));
return Ok(self.alloc(PyObj::BoundMethod {
recv: recv.clone(),
func: b,
}));
}
if matches!(name, "__next__" | "__iter__")
&& matches!(
self.get(recv),
Some(PyObj::ItertoolsIter { .. })
| Some(PyObj::Zip { .. })
| Some(PyObj::MapObj { .. })
| Some(PyObj::FilterObj { .. })
| Some(PyObj::EnumerateObj { .. })
| Some(PyObj::Iter(_))
| Some(PyObj::CallIter { .. })
)
{
let b = self.alloc(PyObj::Builtin(name.to_string()));
return Ok(self.alloc(PyObj::BoundMethod {
recv: recv.clone(),
func: b,
}));
}
if matches!(
self.get(recv),
Some(PyObj::CsvWriter { .. }) | Some(PyObj::CsvDialect(_)) | Some(PyObj::CsvReader { .. })
) {
if let Some(r) = crate::stdlib::pycsv::attr(self, recv, name) {
return r;
}
}
if matches!(self.get(recv), Some(PyObj::Hasher { .. })) {
if let Some(r) = crate::stdlib::pyhash::attr(self, recv, name) {
return r;
}
}
if matches!(
self.get(recv),
Some(PyObj::BytesIO { .. }) | Some(PyObj::StringIO { .. })
) {
if let Some(r) = crate::stdlib::pyio::stream_attr(self, recv, name) {
return r;
}
}
let module_lookup = match self.get(recv) {
Some(PyObj::Module { slot, name: mname }) => Some((
self.module_globals[*slot].get(name).cloned(),
mname.clone(),
*slot,
)),
_ => None,
};
if let Some((hit, _, slot)) = &module_lookup {
if name == "__dict__" {
let slot = *slot;
return Ok(self.module_dict(slot));
}
if name == "__name__" && hit.is_none() {
let n = module_lookup.as_ref().unwrap().1.clone();
return Ok(self.new_str(n));
}
}
let module_lookup = module_lookup.map(|(hit, mname, _)| (hit, mname));
if let Some((hit, mname)) = module_lookup {
return match hit {
Some(v) => Ok(v),
None => match module_ffi_fallback(self, &mname, name) {
Some(r) => r,
None => Err(format!(
"AttributeError: module '{mname}' has no attribute '{name}'"
)),
},
};
}
match self.get(recv) {
Some(PyObj::Instance(inst)) => {
let class = inst.class.clone();
let inst_dict = inst.dict.clone();
let inst_payload = inst.payload.clone();
if let Some(v) = self.inst_attr(&inst_dict, name) {
return Ok(v);
}
if name == "args" && self.class_is_exception(&class) {
return Ok(self.alloc(PyObj::Tuple(vec![])));
}
if (name == "__cause__" || name == "__context__" || name == "__suppress_context__")
&& self.class_is_exception(&class)
{
return Ok(match name {
"__cause__" => self.exc_link(recv).0,
"__context__" => self.exc_link(recv).1,
_ => Value::Bool(!matches!(self.exc_link(recv).0, Value::Undef)),
});
}
if name == "__class__" {
return Ok(self.alloc(PyObj::Class(class)));
}
if name == "__dict__" {
if self.slots_of(&class).is_some() {
return Err(format!(
"AttributeError: '{class}' object has no attribute '__dict__'"
));
}
return Ok(inst_dict);
}
if let Some(v) = self.class_lookup(&class, name) {
match self.get(&v) {
Some(PyObj::Func(_)) => {
return Ok(self.alloc(PyObj::BoundMethod {
recv: recv.clone(),
func: v,
}));
}
Some(PyObj::StaticMethod(inner)) => return Ok(inner.clone()),
Some(PyObj::ClassMethod(inner)) => {
let inner = inner.clone();
let cls = self.alloc(PyObj::Class(class.clone()));
return Ok(self.alloc(PyObj::BoundMethod {
recv: cls,
func: inner,
}));
}
Some(PyObj::Builtin(n))
if !crate::builtins::is_type_object_name(n) =>
{
return Ok(self.alloc(PyObj::BoundMethod {
recv: recv.clone(),
func: v,
}));
}
_ => return Ok(v),
}
}
if !matches!(inst_payload, Value::Undef) {
if let Some(base) = self.builtin_base_of(&class) {
if crate::builtins::type_has_method(base, name) {
let func =
self.alloc(PyObj::Builtin(format!("__base_method__.{name}")));
return Ok(self.alloc(PyObj::BoundMethod {
recv: recv.clone(),
func,
}));
}
}
}
if OBJECT_SLOT_WRAPPERS.contains(&name) {
return Ok(self.alloc(PyObj::Descriptor {
kind: DescKind::MethodWrapper,
qual: format!("{class}.{name}"),
}));
}
Err(format!(
"AttributeError: '{}' object has no attribute '{}'",
class, name
))
}
Some(PyObj::Class(cname)) => {
let cname = cname.clone();
if name == "__name__" {
let disp = self
.classes
.get(&cname)
.map(|c| c.name.clone())
.unwrap_or_else(|| cname.clone());
return Ok(self.new_str(disp));
}
if name == "__qualname__" {
let q = self
.classes
.get(&cname)
.map(|c| c.qualname.clone())
.filter(|q| !q.is_empty())
.unwrap_or_else(|| cname.clone());
return Ok(self.new_str(q));
}
if name == "__doc__" && !self.class_has(&cname, "__doc__") {
return Ok(Value::Undef);
}
if name == "__module__" {
let m = self
.classes
.get(&cname)
.map(|c| c.module.clone())
.filter(|m| !m.is_empty())
.unwrap_or_else(|| "__main__".to_string());
return Ok(self.new_str(m));
}
if name == "__annotations__" && self.class_lookup(&cname, name).is_none() {
return Ok(self.new_dict(IndexMap::new()));
}
if name == "__class__" {
let meta = self
.classes
.get(&cname)
.map(|c| c.metaclass.clone())
.unwrap_or_else(|| "type".into());
return Ok(if self.classes.contains_key(&meta) {
self.alloc(PyObj::Class(meta))
} else {
self.alloc(PyObj::Builtin(meta))
});
}
if name == "__mro__" {
let mut mro: Vec<Value> = self
.mro_of(&cname)
.into_iter()
.map(|c| self.class_or_builtin_type(c))
.collect();
mro.push(self.alloc(PyObj::Builtin("object".into())));
return Ok(self.new_tuple(mro));
}
if name == "__bases__" {
let bases: Vec<String> = self
.classes
.get(&cname)
.map(|cd| cd.bases.clone())
.unwrap_or_default();
let vals: Vec<Value> = if bases.is_empty() {
vec![self.alloc(PyObj::Builtin("object".into()))]
} else {
bases
.into_iter()
.map(|b| self.class_or_builtin_type(b))
.collect()
};
return Ok(self.new_tuple(vals));
}
if name == "__subclasses__" {
let cls = self.alloc(PyObj::Class(cname.clone()));
let func = self.alloc(PyObj::Builtin("__subclasses__".into()));
return Ok(self.alloc(PyObj::BoundMethod { recv: cls, func }));
}
if name == "__new__" {
if let Some(f) = self.class_lookup(&cname, "__new__") {
return Ok(f);
}
return Ok(self.alloc(PyObj::Builtin("object.__new__".into())));
}
if name == "__dict__" {
let ns = self
.classes
.get(&cname)
.map(|cd| cd.ns.clone())
.unwrap_or_default();
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
for (k, val) in ns {
let kv = self.new_str(k.clone());
d.insert(PKey::Str(k), (kv, val));
}
return Ok(self.new_dict(d));
}
if let Some(v) = self.class_lookup(&cname, name) {
match self.get(&v) {
Some(PyObj::StaticMethod(inner)) => return Ok(inner.clone()),
Some(PyObj::ClassMethod(inner)) => {
let inner = inner.clone();
let cls = self.alloc(PyObj::Class(cname.clone()));
return Ok(self.alloc(PyObj::BoundMethod {
recv: cls,
func: inner,
}));
}
_ => return Ok(v),
}
}
let meta = self
.classes
.get(&cname)
.map(|c| c.metaclass.clone())
.unwrap_or_else(|| "type".into());
if meta != "type" {
if let Some(v) = self.class_lookup(&meta, name) {
if matches!(self.get(&v), Some(PyObj::Func(_))) {
let cls = self.alloc(PyObj::Class(cname.clone()));
return Ok(self.alloc(PyObj::BoundMethod { recv: cls, func: v }));
}
return Ok(v);
}
}
if OBJECT_SLOT_WRAPPERS.contains(&name) {
return Ok(self.alloc(PyObj::Descriptor {
kind: DescKind::WrapperDescriptor,
qual: format!("object.{name}"),
}));
}
Err(format!(
"AttributeError: type object '{cname}' has no attribute '{name}'"
))
}
Some(PyObj::Module { slot, name: mname }) => {
let (mname, slot) = (mname.clone(), *slot);
match self.module_globals[slot].get(name) {
Some(v) => Ok(v.clone()),
None => Err(format!(
"AttributeError: module '{mname}' has no attribute '{name}'"
)),
}
}
Some(PyObj::Exception { class, args }) => {
if let Value::Obj(id) = recv {
if let Some(v) = self.func_attrs.get(id).and_then(|m| m.get(name)) {
return Ok(v.clone());
}
}
if name == "__class__" {
let c = class.clone();
return Ok(self.alloc(PyObj::Builtin(c)));
}
if name == "args" {
let a = args.clone();
return Ok(self.new_tuple(a));
}
if name == "value" && (class == "StopIteration" || class == "StopAsyncIteration") {
return Ok(args.first().cloned().unwrap_or(Value::Undef));
}
if name == "code" && class == "SystemExit" {
return Ok(match args.len() {
0 => Value::Undef,
1 => args[0].clone(),
_ => {
let a = args.clone();
self.new_tuple(a)
}
});
}
if name == "__cause__" {
return Ok(self.exc_link(recv).0);
}
if name == "__context__" {
return Ok(self.exc_link(recv).1);
}
if name == "__traceback__" {
let frames = match recv {
Value::Obj(id) => self
.exc_tb
.get(id)
.map(|fs| fs.iter().map(|(s, l, _)| (s.clone(), *l)).collect::<Vec<_>>())
.unwrap_or_default(),
_ => Vec::new(),
};
if frames.is_empty() {
return Ok(Value::Undef);
}
return Ok(self.alloc(PyObj::Traceback { frames, idx: 0 }));
}
if name == "__suppress_context__" {
let suppressed =
matches!(recv, Value::Obj(id) if self.suppress_context.contains(id));
return Ok(Value::Bool(suppressed));
}
let class = class.clone();
Err(format!(
"AttributeError: '{class}' object has no attribute '{name}'"
))
}
Some(PyObj::Super { owner, instance }) => {
let owner = owner.clone();
let instance = instance.clone();
let inst_class = match self.get(&instance) {
Some(PyObj::Instance(i)) => i.class.clone(),
_ => owner.clone(),
};
match super_lookup(self, &owner, &inst_class, name) {
Some((v, _)) => {
if matches!(self.get(&v), Some(PyObj::Func(_))) {
Ok(self.alloc(PyObj::BoundMethod {
recv: instance,
func: v,
}))
} else {
Ok(v)
}
}
None => Err(format!(
"AttributeError: 'super' object has no attribute '{name}'"
)),
}
}
Some(PyObj::Func(_)) if name == "__get__" => {
let func = self.alloc(PyObj::Builtin("function.__get__".into()));
Ok(self.alloc(PyObj::BoundMethod {
recv: recv.clone(),
func,
}))
}
Some(PyObj::Func(fv)) if name == "__closure__" => {
let fv = fv.clone();
let freevars = self.funcs[fv.def_id].freevars.clone();
if freevars.is_empty() {
return Ok(Value::Undef);
}
let mut cells = Vec::with_capacity(freevars.len());
for fname in &freevars {
let val = fv
.env
.as_ref()
.and_then(|e| self.env_lookup(e, fname))
.unwrap_or(Value::Undef);
cells.push(self.alloc(PyObj::Cell { value: val }));
}
Ok(self.new_tuple(cells))
}
Some(PyObj::Cell { value }) if name == "cell_contents" => Ok(value.clone()),
Some(PyObj::StaticMethod(inner)) | Some(PyObj::ClassMethod(inner))
if name == "__func__" || name == "__wrapped__" =>
{
Ok(inner.clone())
}
Some(PyObj::StaticMethod(_)) | Some(PyObj::ClassMethod(_))
if name == "__isabstractmethod__" =>
{
Ok(Value::Bool(false))
}
Some(PyObj::Func(_)) if name == "__dict__" => {
let id = if let Value::Obj(i) = recv { *i } else { 0 };
let attrs = self.func_attrs.get(&id).cloned().unwrap_or_default();
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
for (k, v) in attrs {
let kv = self.new_str(k.clone());
d.insert(PKey::Str(k), (kv, v));
}
Ok(self.new_dict(d))
}
Some(PyObj::Property { .. }) | Some(PyObj::Descriptor { .. })
if name == "__doc__"
|| matches!(recv, Value::Obj(id)
if self.func_attrs.get(id).is_some_and(|m| m.contains_key(name))) =>
{
let id = if let Value::Obj(i) = recv { *i } else { 0 };
Ok(self
.func_attrs
.get(&id)
.and_then(|m| m.get(name))
.cloned()
.unwrap_or(Value::Undef))
}
Some(PyObj::Func(_))
if name == "__isabstractmethod__"
|| matches!(recv, Value::Obj(id)
if self.func_attrs.get(id).is_some_and(|m| m.contains_key(name))) =>
{
let id = if let Value::Obj(i) = recv { *i } else { 0 };
let v = self.func_attrs.get(&id).and_then(|m| m.get(name)).cloned();
Ok(v.unwrap_or(Value::Bool(false)))
}
Some(PyObj::FrameCode { name: fname, lineno }) => {
let (fname, lineno) = (fname.clone(), *lineno);
match name {
"co_name" | "co_qualname" => Ok(self.new_str(fname)),
"co_filename" => {
let f = self.tb_filename.clone();
Ok(self.new_str(f))
}
"co_firstlineno" => Ok(Value::Int(lineno as i64)),
"co_flags" => Ok(Value::Int(0)),
"co_argcount" | "co_kwonlyargcount" | "co_posonlyargcount" | "co_nlocals"
| "co_stacksize" => Ok(Value::Int(0)),
"co_varnames" | "co_names" | "co_freevars" | "co_cellvars" | "co_consts" => {
Ok(self.new_tuple(vec![]))
}
_ => Err(format!(
"AttributeError: 'code' object has no attribute '{name}'"
)),
}
}
Some(PyObj::Code { def_id }) => {
let def_id = *def_id;
self.code_attr(def_id, name)
}
Some(PyObj::Template {
strings,
interpolations,
}) => {
let (strings, interps) = (strings.clone(), interpolations.clone());
match name {
"strings" => {
let vals: Vec<Value> =
strings.into_iter().map(|s| self.new_str(s)).collect();
Ok(self.new_tuple(vals))
}
"interpolations" => Ok(self.new_tuple(interps)),
"values" => {
let vals: Vec<Value> = interps
.iter()
.map(|i| match self.get(i) {
Some(PyObj::Interpolation { value, .. }) => value.clone(),
_ => Value::Undef,
})
.collect();
Ok(self.new_tuple(vals))
}
_ => Err(format!(
"AttributeError: 'Template' object has no attribute '{name}'"
)),
}
}
Some(PyObj::Interpolation {
value,
expression,
conversion,
format_spec,
}) => {
let (value, expression) = (value.clone(), expression.clone());
let (conversion, format_spec) = (*conversion, format_spec.clone());
match name {
"value" => Ok(value),
"expression" => Ok(self.new_str(expression)),
"conversion" => Ok(match conversion {
Some(c) => self.new_str(c.to_string()),
None => Value::Undef,
}),
"format_spec" => Ok(self.new_str(format_spec)),
_ => Err(format!(
"AttributeError: 'Interpolation' object has no attribute '{name}'"
)),
}
}
Some(PyObj::Union { args }) if name == "__args__" => {
let args = args.clone();
Ok(self.new_tuple(args))
}
Some(PyObj::Union { .. }) if name == "__parameters__" => Ok(self.new_tuple(vec![])),
Some(PyObj::TypeVarLike { attrs, .. }) => {
let attrs = attrs.clone();
if let Some(PyObj::Dict(d)) = self.get(&attrs) {
if let Some((_, v)) = d.get(&PKey::Str(name.to_string())) {
return Ok(v.clone());
}
}
Err(format!(
"AttributeError: '{}' object has no attribute '{name}'",
self.type_name(recv)
))
}
Some(PyObj::StructTime { fields }) => {
if let Some(i) = STRUCT_TIME_FIELDS.iter().position(|f| *f == name) {
if let Some(v) = fields.get(i) {
return Ok(v.clone());
}
}
Err(format!(
"AttributeError: 'time.struct_time' object has no attribute '{name}'"
))
}
Some(PyObj::Pattern {
pattern,
flags,
groups,
..
}) => {
let (pattern, flags, groups) = (pattern.clone(), *flags, *groups);
match name {
"pattern" => Ok(self.new_str(pattern)),
"flags" => Ok(Value::Int(flags)),
"groups" => Ok(Value::Int(groups as i64)),
"match" | "search" | "fullmatch" | "findall" | "finditer" | "sub"
| "subn" | "split" => {
let func = self.alloc(PyObj::Builtin(format!("__base_method__.{name}")));
Ok(self.alloc(PyObj::BoundMethod {
recv: recv.clone(),
func,
}))
}
_ => Err(format!(
"AttributeError: 're.Pattern' object has no attribute '{name}'"
)),
}
}
Some(PyObj::Match { text, spans, .. }) => {
match name {
"string" => Ok(self.new_str(text.clone())),
"lastindex" => Ok(spans
.iter()
.rposition(|s| s.is_some())
.filter(|&i| i > 0)
.map(|i| Value::Int(i as i64))
.unwrap_or(Value::Undef)),
"pos" => Ok(Value::Int(0)),
"endpos" => Ok(Value::Int(text.len() as i64)),
"group" | "groups" | "groupdict" | "start" | "end" | "span" => {
let func = self.alloc(PyObj::Builtin(format!("__base_method__.{name}")));
Ok(self.alloc(PyObj::BoundMethod {
recv: recv.clone(),
func,
}))
}
_ => Err(format!(
"AttributeError: 're.Match' object has no attribute '{name}'"
)),
}
}
Some(PyObj::GenericAlias { origin, args }) => {
let (origin, args) = (origin.clone(), args.clone());
match name {
"__origin__" => Ok(origin),
"__args__" => Ok(self.new_tuple(args)),
"__parameters__" => Ok(self.new_tuple(vec![])),
_ => self.get_attr(&origin, name),
}
}
Some(PyObj::StructFmt(_)) => {
match crate::stdlib::pystruct::struct_attr_of(self, recv, name) {
Some(r) => r,
None => Err(type_error(&format!(
"'Struct' object has no attribute '{name}'"
))),
}
}
Some(PyObj::Namespace { attrs }) => match attrs.get(name) {
Some(v) => Ok(v.clone()),
None => Err(format!(
"AttributeError: 'types.SimpleNamespace' object has no attribute '{name}'"
)),
},
Some(PyObj::Traceback { frames, idx }) => {
let (frames, idx) = (frames.clone(), *idx);
match name {
"tb_frame" => {
let (n, l) = frames[idx].clone();
Ok(self.alloc(PyObj::PyFrame {
name: n,
lineno: l,
}))
}
"tb_lineno" => Ok(Value::Int(frames[idx].1 as i64)),
"tb_lasti" => Ok(Value::Int(-1)),
"tb_next" => {
if idx + 1 < frames.len() {
Ok(self.alloc(PyObj::Traceback {
frames,
idx: idx + 1,
}))
} else {
Ok(Value::Undef)
}
}
_ => Err(format!(
"AttributeError: 'traceback' object has no attribute '{name}'"
)),
}
}
Some(PyObj::PyFrame { lineno, name: fname }) => {
let lineno = *lineno;
let fname = fname.clone();
match name {
"f_lineno" => Ok(Value::Int(lineno as i64)),
"f_code" => Ok(self.alloc(PyObj::FrameCode {
name: fname,
lineno,
})),
"f_trace" | "f_trace_lines" | "f_trace_opcodes" => Ok(Value::Undef),
"f_lasti" => Ok(Value::Int(-1)),
"f_locals" => Ok(self.new_dict(IndexMap::new())),
"f_globals" | "f_builtins" => {
let pairs = self.globals_pairs();
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
for (k, v) in pairs {
let kv = self.new_str(k.clone());
d.insert(PKey::Str(k), (kv, v));
}
Ok(self.new_dict(d))
}
"f_back" => Ok(Value::Undef),
_ => Err(format!(
"AttributeError: 'frame' object has no attribute '{name}'"
)),
}
}
Some(PyObj::Func(fv)) if name == "__annotations__" => {
let ann = fv.annotations.clone();
if matches!(ann, Value::Undef) {
Ok(self.new_dict(IndexMap::new()))
} else {
Ok(ann)
}
}
Some(PyObj::BoundMethod { func, .. }) if name == "__annotations__" => {
let func = func.clone();
match self.get(&func) {
Some(PyObj::Func(fv)) if !matches!(fv.annotations, Value::Undef) => {
Ok(fv.annotations.clone())
}
_ => Ok(self.new_dict(IndexMap::new())),
}
}
Some(PyObj::Func(fv))
if matches!(
name,
"__name__"
| "__qualname__"
| "__module__"
| "__defaults__"
| "__kwdefaults__"
| "__doc__"
| "__code__"
) =>
{
let (def_id, defaults) = (fv.def_id, fv.defaults.clone());
let kwd = fv.kwonly_defaults.clone();
self.func_dunder(name, def_id, &defaults, &kwd)
}
Some(PyObj::BoundMethod { func, recv })
if matches!(
name,
"__name__"
| "__qualname__"
| "__module__"
| "__defaults__"
| "__kwdefaults__"
| "__doc__"
| "__code__"
) =>
{
let func = func.clone();
let recv = recv.clone();
match self.get(&func) {
Some(PyObj::Func(fv)) => {
let (def_id, defaults) = (fv.def_id, fv.defaults.clone());
let kwd = fv.kwonly_defaults.clone();
self.func_dunder(name, def_id, &defaults, &kwd)
}
Some(PyObj::Builtin(bn)) => {
let bare = bn.rsplit('.').next().unwrap_or(bn).to_string();
match name {
"__name__" => Ok(self.new_str(bare)),
"__qualname__" => {
let tn = self.type_name(&recv);
Ok(self.new_str(format!("{tn}.{bare}")))
}
_ => Ok(Value::Undef),
}
}
_ => Err(format!(
"AttributeError: 'method' object has no attribute '{name}'"
)),
}
}
Some(PyObj::Builtin(n)) if name == "__name__" || name == "__qualname__" => {
let n = n.clone();
let bare = match type_object_class_name(&n) {
Some(_) => n.rsplit('.').next().unwrap_or(&n).to_string(),
None => n,
};
Ok(self.new_str(bare))
}
Some(PyObj::Builtin(_)) if name == "__doc__" => Ok(Value::Undef),
Some(PyObj::Builtin(n)) if name == "__module__" => {
let n = n.clone();
let m = match type_object_class_name(&n) {
Some(q) => match q.rsplit_once('.') {
Some((module, _)) => module.to_string(),
None => "builtins".to_string(),
},
None => "builtins".to_string(),
};
Ok(self.new_str(m))
}
Some(PyObj::Builtin(n))
if (name == "__mro__" || name == "__bases__")
&& (crate::builtins::is_type_object_name(n)) =>
{
let mut mro = crate::builtins::builtin_mro(n);
if name == "__bases__" {
mro.remove(0);
}
let vals: Vec<Value> =
mro.into_iter().map(|c| self.alloc(PyObj::Builtin(c))).collect();
Ok(self.new_tuple(vals))
}
Some(PyObj::Builtin(n))
if name == "__dict__"
&& (crate::builtins::is_type_object_name(n)) =>
{
let n = n.clone();
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
{
let key = self.new_str("__new__".to_string());
let ctor = self.alloc(PyObj::Builtin("object.__new__".into()));
d.insert(PKey::Str("__new__".to_string()), (key, ctor));
}
if n == "type" {
for attr in [
"__annotations__",
"__mro__",
"__dict__",
"__bases__",
"__name__",
"__qualname__",
"__module__",
"__doc__",
] {
let key = self.new_str(attr.to_string());
let desc = self.alloc(PyObj::Descriptor {
kind: DescKind::GetSetDescriptor,
qual: format!("type.{attr}"),
});
d.insert(PKey::Str(attr.to_string()), (key, desc));
}
}
for cm in crate::builtins::type_classmethods(&n) {
let key = self.new_str((*cm).to_string());
let desc = self.alloc(PyObj::Descriptor {
kind: DescKind::ClassMethodDescriptor,
qual: format!("{n}.{cm}"),
});
d.insert(PKey::Str((*cm).to_string()), (key, desc));
}
let dict = self.alloc(PyObj::Dict(d));
Ok(self.alloc(PyObj::MappingProxy { dict }))
}
Some(PyObj::Builtin(n))
if name == "__new__" && crate::builtins::is_type_object_name(n) =>
{
let ctor = match n.as_str() {
"int" | "str" | "float" | "tuple" | "frozenset" | "list" | "dict"
| "set" => format!("{n}.__new__"),
_ => "object.__new__".to_string(),
};
Ok(self.alloc(PyObj::Builtin(ctor)))
}
Some(PyObj::Builtin(n)) if n == "function" && name == "__code__" => {
Ok(self.alloc(PyObj::Descriptor {
kind: DescKind::GetSetDescriptor,
qual: "function.__code__".into(),
}))
}
Some(PyObj::Builtin(n)) if n == "function" && name == "__globals__" => {
Ok(self.alloc(PyObj::Descriptor {
kind: DescKind::MemberDescriptor,
qual: "function.__globals__".into(),
}))
}
Some(PyObj::Builtin(n))
if crate::builtins::is_type_like_builtin(n)
&& name.starts_with("__")
&& name.ends_with("__")
&& OBJECT_SLOT_WRAPPERS.contains(&name) =>
{
Ok(self.alloc(PyObj::Descriptor {
kind: DescKind::WrapperDescriptor,
qual: format!("{n}.{name}"),
}))
}
Some(PyObj::Builtin(n)) if n == "dict" && name == "fromkeys" => {
Ok(self.alloc(PyObj::Builtin("dict.fromkeys".into())))
}
Some(PyObj::Builtin(n)) if n == "str" && name == "maketrans" => {
Ok(self.alloc(PyObj::Builtin("str.maketrans".into())))
}
Some(PyObj::Builtin(n)) if (n == "bytes" || n == "bytearray") && name == "fromhex" => {
Ok(self.alloc(PyObj::Builtin(format!("{n}.fromhex"))))
}
Some(PyObj::Builtin(n)) if n == "int" && name == "from_bytes" => {
Ok(self.alloc(PyObj::Builtin("int.from_bytes".into())))
}
Some(PyObj::Builtin(n)) if n == "float" && name == "fromhex" => {
Ok(self.alloc(PyObj::Builtin("float.fromhex".into())))
}
Some(PyObj::Builtin(n))
if (n == "bytes" || n == "bytearray") && name == "maketrans" =>
{
Ok(self.alloc(PyObj::Builtin(format!("{n}.maketrans"))))
}
Some(PyObj::Builtin(n)) if crate::builtins::type_has_method(n, name) => {
Ok(self.alloc(PyObj::Builtin(format!("{n}.{name}"))))
}
Some(PyObj::Memoryview {
obj, len, readonly, ..
}) if matches!(
name,
"obj"
| "nbytes"
| "format"
| "itemsize"
| "ndim"
| "readonly"
| "shape"
| "strides"
| "contiguous"
| "c_contiguous"
| "f_contiguous"
) =>
{
let (obj, len, readonly) = (obj.clone(), *len, *readonly);
Ok(match name {
"obj" => obj,
"nbytes" => Value::Int(len as i64),
"format" => self.new_str("B"),
"itemsize" => Value::Int(1),
"ndim" => Value::Int(1),
"readonly" => Value::Bool(readonly),
"shape" => {
let n = Value::Int(len as i64);
self.new_tuple(vec![n])
}
"strides" => {
let one = Value::Int(1);
self.new_tuple(vec![one])
}
_ => Value::Bool(true),
})
}
Some(PyObj::Slice { lo, hi, step }) if matches!(name, "start" | "stop" | "step") => {
let v = match name {
"start" => lo,
"stop" => hi,
_ => step,
};
Ok(v.clone())
}
_ => {
if name == "real" || name == "imag" {
if let Some(PyObj::Complex(r, i)) = self.get(recv) {
let (r, i) = (*r, *i);
return Ok(Value::Float(if name == "real" { r } else { i }));
}
if let Value::Int(_) | Value::Bool(_) = recv {
return Ok(if name == "real" {
recv.clone()
} else {
Value::Int(0)
});
}
if let Value::Float(f) = recv {
return Ok(Value::Float(if name == "real" { *f } else { 0.0 }));
}
if matches!(self.get(recv), Some(PyObj::BigInt(_))) {
return Ok(if name == "real" {
recv.clone()
} else {
Value::Int(0)
});
}
}
if name == "numerator" || name == "denominator" {
let is_int = matches!(recv, Value::Int(_) | Value::Bool(_))
|| matches!(self.get(recv), Some(PyObj::BigInt(_)));
if is_int {
return Ok(if name == "denominator" {
Value::Int(1)
} else if let Value::Bool(b) = recv {
Value::Int(*b as i64)
} else {
recv.clone()
});
}
}
let tn = self.type_name(recv);
if name == "__class__" {
return Ok(if self.classes.contains_key(&tn) {
self.alloc(PyObj::Class(tn))
} else {
self.alloc(PyObj::Builtin(tn))
});
}
if name == "maxlen" {
if let Some(PyObj::Deque { maxlen, .. }) = self.get(recv) {
return Ok(match maxlen {
Some(m) => Value::Int(*m as i64),
None => Value::Undef,
});
}
}
if crate::builtins::type_has_method(&tn, name)
|| crate::builtins::is_object_dunder_method(name)
{
let b = self.alloc(PyObj::Builtin(name.to_string()));
return Ok(self.alloc(PyObj::BoundMethod {
recv: recv.clone(),
func: b,
}));
}
Err(format!(
"AttributeError: '{tn}' object has no attribute '{name}'"
))
}
}
}
pub fn class_has(&self, class: &str, name: &str) -> bool {
self.class_lookup(class, name).is_some()
}
pub fn dir_names(&self, v: &Value) -> Vec<String> {
let mut set: BTreeSet<String> = BTreeSet::new();
match self.get(v) {
Some(PyObj::Instance(i)) => {
let dict = i.dict.clone();
let class = i.class.clone();
for n in self.inst_attr_names(&dict) {
set.insert(n);
}
self.collect_class_dir(&class, &mut set);
}
Some(PyObj::Class(c)) => {
let c = c.clone();
self.collect_class_dir(&c, &mut set);
}
Some(PyObj::Module { slot, .. }) => {
for k in self.module_globals[*slot].keys() {
set.insert(k.clone());
}
}
_ => {}
}
set.into_iter().collect()
}
fn collect_class_dir(&self, class: &str, set: &mut BTreeSet<String>) {
for c in self.mro_of(class) {
if let Some(cd) = self.classes.get(&c) {
for k in cd.ns.keys() {
set.insert(k.clone());
}
}
}
if let Some(slots) = self.slots_of(class) {
for s in slots {
set.insert(s);
}
}
}
fn slots_of(&self, class: &str) -> Option<HashSet<String>> {
let mut slots = HashSet::new();
let mut any = false;
for c in self.mro_of(class) {
let cd = match self.classes.get(&c) {
Some(cd) => cd,
None => continue, };
let v = cd.ns.get("__slots__")?;
any = true;
match self.get(v) {
Some(PyObj::List(items)) | Some(PyObj::Tuple(items)) => {
for it in items {
if let Some(s) = self.as_str(it) {
slots.insert(s);
}
}
}
Some(PyObj::Str(s)) => {
slots.insert(s.clone());
}
_ => {}
}
}
if any {
Some(slots)
} else {
None
}
}
pub fn plan_attr_get(&mut self, recv: &Value, name: &str) -> AttrGet {
if let Some(PyObj::Super { owner, instance }) = self.get(recv) {
let owner = owner.clone();
let instance = instance.clone();
let inst_class = match self.get(&instance) {
Some(PyObj::Instance(i)) => i.class.clone(),
_ => owner.clone(),
};
if let Some((v, found)) = super_lookup(self, &owner, &inst_class, name) {
if let Some(PyObj::Property { fget, .. }) = self.get(&v) {
return AttrGet::Property {
fget: fget.clone(),
inst: instance,
owner: Some(found),
};
}
}
return AttrGet::Plain;
}
if let Some(PyObj::Class(cname)) = self.get(recv) {
let cname = cname.clone();
if let Some(cls_attr) = self.class_lookup(&cname, name) {
if let Some(PyObj::Instance(i)) = self.get(&cls_attr) {
let c = i.class.clone();
if self.class_has(&c, "__get__") {
return AttrGet::Descriptor {
desc: cls_attr,
inst: Value::Undef,
cls: recv.clone(),
};
}
}
} else {
let meta = self
.classes
.get(&cname)
.map(|c| c.metaclass.clone())
.unwrap_or_else(|| "type".into());
if meta != "type" {
if let Some(v) = self.class_lookup(&meta, name) {
if let Some(PyObj::Property { fget, .. }) = self.get(&v) {
return AttrGet::Property {
fget: fget.clone(),
inst: recv.clone(),
owner: method_owner(self, &meta, name),
};
}
}
}
}
return AttrGet::Plain;
}
let (class, inst_dict) = match self.get(recv) {
Some(PyObj::Instance(i)) => (i.class.clone(), i.dict.clone()),
_ => return AttrGet::Plain,
};
let in_instdict = self.inst_has(&inst_dict, name);
let cls_attr = match self.class_lookup(&class, name) {
Some(v) => v,
None => return AttrGet::Plain,
};
if let Some(PyObj::Property { fget, .. }) = self.get(&cls_attr) {
return AttrGet::Property {
fget: fget.clone(),
inst: recv.clone(),
owner: method_owner(self, &class, name),
};
}
if let Some(PyObj::CachedProperty { func, .. }) = self.get(&cls_attr) {
if !in_instdict {
return AttrGet::CachedProperty {
func: func.clone(),
inst: recv.clone(),
name: name.to_string(),
};
}
}
let (has_get, is_data) = match self.get(&cls_attr) {
Some(PyObj::Instance(i)) => {
let c = i.class.clone();
(
self.class_has(&c, "__get__"),
self.class_has(&c, "__set__") || self.class_has(&c, "__delete__"),
)
}
_ => (false, false),
};
if has_get && (is_data || !in_instdict) {
let cls = self.alloc(PyObj::Class(class));
return AttrGet::Descriptor {
desc: cls_attr,
inst: recv.clone(),
cls,
};
}
AttrGet::Plain
}
pub fn plan_attr_set(&mut self, recv: &Value, name: &str, val: &Value) -> AttrSet {
let class = match self.get(recv) {
Some(PyObj::Instance(i)) => i.class.clone(),
_ => return AttrSet::Plain,
};
let cls_attr = match self.class_lookup(&class, name) {
Some(v) => v,
None => return AttrSet::Plain,
};
if let Some(PyObj::Property { fset, .. }) = self.get(&cls_attr) {
return AttrSet::Property {
fset: fset.clone(),
inst: recv.clone(),
val: val.clone(),
owner: method_owner(self, &class, name),
};
}
let has_set = match self.get(&cls_attr) {
Some(PyObj::Instance(i)) => {
let c = i.class.clone();
self.class_has(&c, "__set__")
}
_ => false,
};
if has_set {
return AttrSet::Descriptor {
desc: cls_attr,
inst: recv.clone(),
val: val.clone(),
};
}
AttrSet::Plain
}
pub fn plan_attr_del(&mut self, recv: &Value, name: &str) -> AttrDel {
let class = match self.get(recv) {
Some(PyObj::Instance(i)) => i.class.clone(),
_ => return AttrDel::Plain,
};
let cls_attr = match self.class_lookup(&class, name) {
Some(v) => v,
None => return AttrDel::Plain,
};
if let Some(PyObj::Property { fdel, .. }) = self.get(&cls_attr) {
return AttrDel::Property {
fdel: fdel.clone(),
inst: recv.clone(),
owner: method_owner(self, &class, name),
};
}
if let Some(PyObj::Instance(i)) = self.get(&cls_attr) {
let c = i.class.clone();
let has_delete = self.class_has(&c, "__delete__");
if has_delete || self.class_has(&c, "__set__") {
return AttrDel::Descriptor {
desc: cls_attr,
inst: recv.clone(),
has_delete,
};
}
}
AttrDel::Plain
}
pub fn set_attr(&mut self, recv: &Value, name: &str, val: Value) -> Result<(), String> {
#[cfg(feature = "stdlib-ffi")]
if let Some(id) = self.foreign_id(recv) {
return crate::ffi::set_attr(self, id, name, &val);
}
if let Some(PyObj::Namespace { attrs }) = self.get_mut(recv) {
attrs.insert(name.to_string(), val);
return Ok(());
}
if let Some(PyObj::Instance(inst)) = self.get(recv) {
let class = inst.class.clone();
if let Some(slots) = self.slots_of(&class) {
if !slots.contains(name) {
return Err(format!(
"AttributeError: '{class}' object has no attribute '{name}' and no \
__dict__ for setting new attributes"
));
}
}
}
if let Some(PyObj::Instance(inst)) = self.get(recv) {
let dict = inst.dict.clone();
self.inst_attr_set(&dict, name, val);
return Ok(());
}
if let Some(PyObj::Module { name: mname, .. }) = self.get(recv) {
if mname == "sys" && (name == "stdout" || name == "stderr") {
let is_stdout = name == "stdout";
let native_id = if is_stdout { 0 } else { 1 };
let target = match &val {
v if matches!(self.get(v), Some(PyObj::File { id }) if *id == native_id) => {
None
}
_ => Some(val.clone()),
};
if is_stdout {
self.stdout_target = target;
} else {
self.stderr_target = target;
}
}
}
if matches!(self.get(recv), Some(PyObj::Exception { .. })) {
if let Value::Obj(id) = recv {
let id = *id;
self.func_attrs.entry(id).or_default().insert(name.to_string(), val);
return Ok(());
}
}
if matches!(
self.get(recv),
Some(PyObj::Property { .. }) | Some(PyObj::Descriptor { .. })
) {
if let Value::Obj(id) = recv {
let id = *id;
self.func_attrs.entry(id).or_default().insert(name.to_string(), val);
return Ok(());
}
}
if matches!(self.get(recv), Some(PyObj::Func(_))) {
if let Value::Obj(id) = recv {
let id = *id;
self.func_attrs.entry(id).or_default().insert(name.to_string(), val);
return Ok(());
}
}
if let Some(slot) = self.module_slot(recv) {
self.module_globals[slot].insert(name.to_string(), val);
return Ok(());
}
match self.get_mut(recv) {
Some(PyObj::Class(cname)) => {
let cname = cname.clone();
if let Some(cd) = self.classes.get_mut(&cname) {
cd.ns.insert(name.to_string(), val);
}
Ok(())
}
_ => Err(type_error(&format!(
"'{}' object attribute assignment unsupported",
self.type_name(recv)
))),
}
}
pub fn del_attr(&mut self, recv: &Value, name: &str) -> Result<(), String> {
if let Some(PyObj::Instance(inst)) = self.get(recv) {
let dict = inst.dict.clone();
if self.inst_attr_del(&dict, name) {
return Ok(());
}
}
if let Some(PyObj::Class(cname)) = self.get(recv) {
let cname = cname.clone();
if let Some(cd) = self.classes.get_mut(&cname) {
if cd.ns.shift_remove(name).is_some() {
return Ok(());
}
}
return Err(format!(
"AttributeError: type object '{cname}' has no attribute '{name}'"
));
}
if let Some(PyObj::Namespace { attrs }) = self.get_mut(recv) {
if attrs.shift_remove(name).is_some() {
return Ok(());
}
}
Err(format!(
"AttributeError: '{}' object has no attribute '{name}'",
self.type_name(recv)
))
}
pub fn register_class(
&mut self,
name: &str,
bases: Vec<String>,
ns: IndexMap<String, Value>,
) -> Value {
self.register_class_meta(name, bases, ns, "type")
}
pub fn register_class_meta(
&mut self,
name: &str,
bases: Vec<String>,
ns: IndexMap<String, Value>,
metaclass: &str,
) -> Value {
let ancestors: Vec<String> = bases.iter().flat_map(|b| self.mro_of(b)).collect();
let key = if ancestors.iter().any(|b| b == name)
|| crate::builtins::shadows_builtin_type(name)
{
let mut n = 1usize;
loop {
let cand = format!("{name}#{n}");
if !self.classes.contains_key(&cand) {
break cand;
}
n += 1;
}
} else {
name.to_string()
};
let name_for_key = key.clone();
self.mro_cache.borrow_mut().clear();
let mro = {
let mut out = vec![name_for_key.clone()];
for b in &bases {
for m in self.mro_of(b) {
if !out.contains(&m) {
out.push(m);
}
}
}
out
};
for v in ns.values() {
if let Some(PyObj::Func(fv)) = self.get_mut(v) {
if fv.owner.is_none() {
Rc::make_mut(fv).owner = Some(name_for_key.clone());
}
}
}
let cp_names: Vec<(Value, String)> = ns
.iter()
.filter(|(_, v)| {
matches!(self.get(v), Some(PyObj::CachedProperty { name, .. }) if name.is_empty())
})
.map(|(k, v)| (v.clone(), k.clone()))
.collect();
for (val, key) in cp_names {
if let Some(PyObj::CachedProperty { name, .. }) = self.get_mut(&val) {
*name = key;
}
}
let module = ns
.get("__module__")
.and_then(|v| self.as_str(v))
.or_else(|| self.globals().get("__name__").cloned().and_then(|v| self.as_str(&v)))
.unwrap_or_else(|| "__main__".to_string());
self.classes.insert(
key.clone(),
ClassDef {
name: name.to_string(),
qualname: String::new(),
bases,
ns,
mro,
metaclass: metaclass.to_string(),
module,
},
);
self.alloc(PyObj::Class(key))
}
}
pub fn invoke(
callable: &Value,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
let obj = with_host(|h| h.get(callable).cloned());
match obj {
Some(PyObj::Builtin(name)) => crate::builtins::call_builtin_function(&name, args, kwargs),
Some(PyObj::Func(fv)) => run_user_func(&fv, None, None, args, kwargs),
Some(PyObj::StaticMethod(inner)) | Some(PyObj::ClassMethod(inner)) => {
invoke(&inner, args, kwargs)
}
Some(PyObj::BoundMethod { recv, func }) => {
let f = with_host(|h| h.get(&func).cloned());
match f {
Some(PyObj::Builtin(name)) => {
if let Some(m) = name.strip_prefix("__base_method__.") {
return call_method(&recv, m, args, kwargs);
}
if let Some(m) = name.strip_prefix("_random.Random.") {
if let Value::Obj(id) = &recv {
return crate::builtins::random_method(*id, m, &args);
}
}
if name == "function.__get__" {
let obj = args.into_iter().next().unwrap_or(Value::Undef);
return Ok(match obj {
Value::Undef => recv,
inst => with_host(|h| {
h.alloc(PyObj::BoundMethod { recv: inst, func: recv.clone() })
}),
});
}
crate::builtins::call_type_method(&recv, &name, args, kwargs)
}
Some(PyObj::Func(fv)) => run_user_func(&fv, Some(recv), None, args, kwargs),
_ => Err(type_error("bound method is not callable")),
}
}
Some(PyObj::Class(name)) => instantiate(&name, args, kwargs),
Some(PyObj::Descriptor {
kind: DescKind::WrapperDescriptor,
qual,
}) => {
let (base, method) = qual.split_once('.').unwrap_or(("", qual.as_str()));
let (base, method) = (base.to_string(), method.to_string());
let mut it = args.into_iter();
let recv = it.next().ok_or_else(|| {
type_error(&format!("unbound method {qual}() needs an argument"))
})?;
let rest: Vec<Value> = it.collect();
if crate::builtins::is_type_like_builtin(&base) {
let payload =
with_host(|h| h.base_payload_any(&recv)).unwrap_or_else(|| recv.clone());
return base_dispatch(&recv, &payload, &base, &method, rest, kwargs);
}
call_method(&recv, &method, rest, kwargs)
}
Some(PyObj::GenericAlias { origin, .. }) => invoke(&origin, args, kwargs),
Some(PyObj::NamedTupleType { type_name, fields }) => {
namedtuple_construct(&type_name, &fields, args, kwargs)
}
Some(PyObj::Partial {
func,
args: bound,
kwargs: bkw,
}) => {
let mut all_args = bound;
all_args.extend(args);
let mut all_kw = bkw;
for (k, v) in kwargs {
if let Some(slot) = all_kw.iter_mut().find(|(kk, _)| *kk == k) {
slot.1 = v;
} else {
all_kw.push((k, v));
}
}
invoke(&func, all_args, all_kw)
}
Some(PyObj::LruCache { func, cache_id }) => lru_invoke(&func, cache_id, args, kwargs),
Some(PyObj::Instance(inst))
if with_host(|h| h.class_lookup(&inst.class, "__call__").is_some()) =>
{
call_method(callable, "__call__", args, kwargs)
}
#[cfg(feature = "stdlib-ffi")]
Some(PyObj::Foreign(id)) => crate::ffi::call(id, args, kwargs),
_ => Err(type_error(&format!(
"'{}' object is not callable",
with_host(|h| h.type_name(callable))
))),
}
}
fn marshal_ffi_arg(v: &Value) -> Value {
match v {
Value::Obj(_) => match with_host(|h| h.as_str(v)) {
Some(s) => Value::str(s),
None => v.clone(),
},
_ => v.clone(),
}
}
pub fn is_generic_subscriptable(recv: &Value) -> bool {
with_host(|h| match h.get(recv) {
Some(PyObj::Class(_)) => true,
Some(PyObj::Builtin(n)) => matches!(
n.as_str(),
"list" | "dict" | "tuple" | "set" | "frozenset" | "type"
| "Generic"
| "re.Pattern" | "re.Match"
),
_ => false,
})
}
pub fn generic_alias(recv: &Value, idx: &Value) -> Result<Value, String> {
let hook = with_host(|h| match h.get(recv) {
Some(PyObj::Class(cn)) => {
let cn = cn.clone();
h.class_lookup(&cn, "__class_getitem__").map(|_| ())
}
_ => None,
});
if hook.is_some() {
let bound = with_host(|h| h.get_attr(recv, "__class_getitem__"))?;
return invoke(&bound, vec![idx.clone()], vec![]);
}
Ok(with_host(|h| {
let args = match h.get(idx) {
Some(PyObj::Tuple(items)) => items.clone(),
_ => vec![idx.clone()],
};
h.alloc(PyObj::GenericAlias {
origin: recv.clone(),
args,
})
}))
}
pub fn call_named(
name: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
if name == "__rust_compile" {
let b64 = args
.first()
.map(|v| with_host(|h| h.str_of(v)))
.unwrap_or_default();
return fusevm::ffi::compile_and_register(&b64).map(|_| Value::Undef);
}
if let Some(v) = with_host(|h| h.read_name(name)) {
return invoke(&v, args, kwargs);
}
if with_host(|h| h.classes.contains_key(name)) {
return instantiate(name, args, kwargs);
}
if crate::builtins::is_known_builtin(name) {
return crate::builtins::call_builtin_function(name, args, kwargs);
}
if fusevm::ffi::is_registered(name) {
let margs: Vec<Value> = args.iter().map(marshal_ffi_arg).collect();
if let Some(r) = fusevm::ffi::try_call(name, &margs) {
return r;
}
}
Err(name_error(name))
}
pub fn base_provides(base: &str, dunder: &str) -> bool {
match base {
"list" => matches!(
dunder,
"__len__"
| "__getitem__"
| "__setitem__"
| "__delitem__"
| "__iter__"
| "__contains__"
| "__reversed__"
| "__repr__"
| "__str__"
),
"tuple" => matches!(
dunder,
"__len__"
| "__getitem__"
| "__iter__"
| "__contains__"
| "__reversed__"
| "__repr__"
| "__str__"
| "__hash__"
),
"str" => matches!(
dunder,
"__len__"
| "__getitem__"
| "__iter__"
| "__contains__"
| "__reversed__"
| "__repr__"
| "__str__"
| "__hash__"
),
"dict" => matches!(
dunder,
"__len__"
| "__getitem__"
| "__setitem__"
| "__delitem__"
| "__iter__"
| "__contains__"
| "__repr__"
| "__str__"
),
"set" => matches!(
dunder,
"__len__" | "__iter__" | "__contains__" | "__repr__" | "__str__"
),
"frozenset" => matches!(
dunder,
"__len__" | "__iter__" | "__contains__" | "__repr__" | "__str__" | "__hash__"
),
"int" => matches!(
dunder,
"__repr__"
| "__str__"
| "__hash__"
| "__bool__"
| "__index__"
| "__int__"
| "__float__"
),
"float" => matches!(
dunder,
"__repr__" | "__str__" | "__hash__" | "__bool__" | "__int__" | "__float__"
),
_ => false,
}
}
pub fn subclass_payload(v: &Value, dunder: &str) -> Option<Value> {
with_host(|h| match h.get(v) {
Some(PyObj::Instance(i)) if !matches!(i.payload, Value::Undef) => {
let base = h.builtin_base_of(&i.class)?;
if base_provides(base, dunder) && h.class_lookup(&i.class, dunder).is_none() {
Some(i.payload.clone())
} else {
None
}
}
_ => None,
})
}
pub fn subclass_operand(v: &Value, dunder: &str) -> Value {
with_host(|h| match h.get(v) {
Some(PyObj::Instance(i)) if !matches!(i.payload, Value::Undef) => {
if h.builtin_base_of(&i.class).is_some() && h.class_lookup(&i.class, dunder).is_none() {
i.payload.clone()
} else {
v.clone()
}
}
_ => v.clone(),
})
}
fn base_dispatch(
recv: &Value,
payload: &Value,
base: &str,
name: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
match name {
"__len__" => {
let n = crate::builtins::py_len(payload)?;
Ok(Value::Int(n as i64))
}
"__bool__" => Ok(Value::Bool(with_host(|h| h.truthy(payload)))),
"__getitem__" => {
let idx = args.into_iter().next().unwrap_or(Value::Undef);
if base == "dict" {
let missing = with_host(|h| match h.to_key(&idx) {
Ok(k) => matches!(h.get(payload), Some(PyObj::Dict(d)) if !d.contains_key(&k)),
Err(_) => false,
});
if missing {
let cls = with_host(|h| match h.get(recv) {
Some(PyObj::Instance(i)) => i.class.clone(),
_ => String::new(),
});
if with_host(|h| h.class_lookup(&cls, "__missing__").is_some()) {
return call_method(recv, "__missing__", vec![idx], vec![]);
}
}
}
with_host(|h| h.get_item(payload, &idx))
}
"__setitem__" => {
let mut it = args.into_iter();
let idx = it.next().unwrap_or(Value::Undef);
let val = it.next().unwrap_or(Value::Undef);
with_host(|h| h.set_item(payload, &idx, val)).map(|_| Value::Undef)
}
"__delitem__" => {
let idx = args.into_iter().next().unwrap_or(Value::Undef);
with_host(|h| h.del_item(payload, &idx)).map(|_| Value::Undef)
}
"__contains__" => {
let item = args.into_iter().next().unwrap_or(Value::Undef);
Ok(Value::Bool(with_host(|h| h.contains(&item, payload))?))
}
"__iter__" => with_host(|h| h.make_iter(payload)),
"__repr__" => Ok(with_host(|h| {
let s = h.repr_of(payload);
h.new_str(s)
})),
"__str__" => Ok(with_host(|h| {
let s = h.str_of(payload);
h.new_str(s)
})),
"__hash__" => {
let k = with_host(|h| h.to_key(payload))?;
Ok(Value::Int(crate::builtins::hash_key(&k)))
}
"__int__" | "__index__" => {
let n = with_host(|h| h.as_int(payload));
match n {
Some(n) => Ok(Value::Int(n)),
None => crate::builtins::call_type_method(payload, name, args, kwargs),
}
}
"__float__" => {
let f = with_host(|h| h.num_val(payload));
match f {
Some(f) => Ok(Value::Float(f)),
None => crate::builtins::call_type_method(payload, name, args, kwargs),
}
}
_ => crate::builtins::call_type_method(payload, name, args, kwargs),
}
}
fn new_subclass_or_bare(cname: &str, extra: &[Value]) -> Result<Value, String> {
if let Some(base) = with_host(|h| h.builtin_base_of(cname)) {
let payload = crate::builtins::call_builtin_function(base, extra.to_vec(), vec![])?;
return Ok(with_host(|h| {
h.new_instance_payload(cname.to_string(), payload)
}));
}
Ok(with_host(|h| {
h.new_instance(cname.to_string(), IndexMap::new())
}))
}
fn base_super_init(
base: &str,
payload: &Value,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
match base {
"list" | "dict" | "set" => {
let built = crate::builtins::call_builtin_function(base, args, kwargs)?;
with_host(|h| {
if let Some(o) = h.get(&built).cloned() {
if let Some(slot) = h.get_mut(payload) {
*slot = o;
}
}
});
Ok(Value::Undef)
}
_ => Ok(Value::Undef),
}
}
fn module_dict_method(
slot: usize,
recv: &Value,
name: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
match name {
"update" => {
let mut pairs: Vec<(String, Value)> = Vec::new();
if let Some(src) = args.first() {
let items = match with_host(|h| h.get(src).cloned()) {
Some(PyObj::Dict(d)) => {
d.values().map(|(k, v)| (k.clone(), v.clone())).collect()
}
_ if with_host(|h| h.module_dict_slot(src)).is_some() => {
let s = with_host(|h| h.module_dict_slot(src)).unwrap();
with_host(|h| {
h.module_globals_pairs(s)
.into_iter()
.map(|(k, v)| (h.new_str(k), v))
.collect::<Vec<_>>()
})
}
_ => match call_method(src, "items", vec![], vec![]) {
Ok(items) => {
let mut out = Vec::new();
for it in iter_vec(&items)? {
let kv = iter_vec(&it)?;
if kv.len() != 2 {
return Err(type_error(
"dictionary update sequence element has length != 2",
));
}
out.push((kv[0].clone(), kv[1].clone()));
}
out
}
Err(_) => {
let mut out = Vec::new();
for it in iter_vec(src)? {
let kv = iter_vec(&it)?;
if kv.len() != 2 {
return Err(type_error(
"dictionary update sequence element has length != 2",
));
}
out.push((kv[0].clone(), kv[1].clone()));
}
out
}
},
};
for (k, v) in items {
let ks = with_host(|h| h.as_str(&k))
.ok_or_else(|| type_error("module namespace keys must be strings"))?;
pairs.push((ks, v));
}
}
for (k, v) in kwargs {
pairs.push((k, v));
}
with_host(|h| {
for (k, v) in pairs {
h.module_globals[slot].insert(k, v);
}
});
Ok(Value::Undef)
}
"setdefault" => {
let k = with_host(|h| args.first().and_then(|a| h.as_str(a)))
.ok_or_else(|| type_error("module namespace keys must be strings"))?;
let default = args.get(1).cloned().unwrap_or(Value::Undef);
Ok(with_host(|h| {
h.module_globals[slot].entry(k).or_insert(default).clone()
}))
}
"pop" => {
let k = with_host(|h| args.first().and_then(|a| h.as_str(a)))
.ok_or_else(|| type_error("module namespace keys must be strings"))?;
match with_host(|h| h.module_globals[slot].shift_remove(&k)) {
Some(v) => Ok(v),
None => match args.get(1) {
Some(d) => Ok(d.clone()),
None => Err(format!("KeyError: '{k}'")),
},
}
}
"popitem" => {
match with_host(|h| h.module_globals[slot].pop()) {
Some((k, v)) => Ok(with_host(|h| {
let kv = h.new_str(k);
h.new_tuple(vec![kv, v])
})),
None => Err("KeyError: 'popitem(): dictionary is empty'".into()),
}
}
"clear" => {
with_host(|h| h.module_globals[slot].clear());
Ok(Value::Undef)
}
_ => {
let snap = with_host(|h| h.module_dict_snapshot(recv))
.ok_or_else(|| type_error("not a module namespace"))?;
call_method(&snap, name, args, kwargs)
}
}
}
pub fn call_method(
recv: &Value,
name: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
if let Some(slot) = with_host(|h| h.module_dict_slot(recv)) {
return module_dict_method(slot, recv, name, args, kwargs);
}
if matches!(name, "random" | "seed" | "getrandbits" | "getstate" | "setstate") {
if let Some(id) = with_host(|h| match (recv, h.get(recv)) {
(Value::Obj(oid), Some(PyObj::Instance(i)))
if h.mro_of(&i.class).iter().any(|c| c == "_random.Random") =>
{
Some(*oid)
}
_ => None,
}) {
return crate::builtins::random_method(id, name, &args);
}
}
let needs_owned = with_host(|h| {
matches!(
h.get(recv),
Some(
PyObj::Pattern { .. }
| PyObj::Match { .. }
| PyObj::Func(_)
| PyObj::MappingProxy { .. }
| PyObj::Lock { .. }
| PyObj::Redirect { .. }
| PyObj::Instance(_)
| PyObj::Class(_)
| PyObj::Module { .. }
| PyObj::Super { .. }
| PyObj::Builtin(_)
)
)
});
if let Value::Obj(id) = recv {
let stored = with_host(|h| {
matches!(h.get(recv), Some(PyObj::Func(_)))
.then(|| h.func_attrs.get(id).and_then(|m| m.get(name)).cloned())
.flatten()
});
if let Some(f) = stored {
return invoke(&f, args, kwargs);
}
}
if !needs_owned {
return crate::builtins::call_type_method(recv, name, args, kwargs);
}
let obj = with_host(|h| h.get(recv).cloned());
match obj {
Some(PyObj::Pattern { .. }) => {
crate::builtins::re_pattern_method(recv, name, &args, &kwargs)
}
Some(PyObj::Match { .. }) => crate::builtins::re_match_method(recv, name, &args),
Some(PyObj::Func(_)) if name == "__get__" => {
let inst = args.into_iter().next().unwrap_or(Value::Undef);
Ok(match inst {
Value::Undef => recv.clone(),
inst => with_host(|h| {
h.alloc(PyObj::BoundMethod {
recv: inst,
func: recv.clone(),
})
}),
})
}
Some(PyObj::MappingProxy { dict }) => {
match name {
"get" | "keys" | "values" | "items" | "copy" | "__getitem__"
| "__contains__" | "__len__" | "__iter__" | "__or__" | "__ror__"
| "__eq__" | "__ne__" | "__reversed__" => call_method(&dict, name, args, kwargs),
_ => Err(type_error(&format!(
"'mappingproxy' object has no attribute '{name}'"
))),
}
}
Some(PyObj::Lock { reentrant, .. }) => match name {
"acquire" | "__enter__" => {
let held = with_host(|h| {
matches!(h.get(recv), Some(PyObj::Lock { count, .. }) if *count > 0)
});
let blocking = match args.first() {
Some(v) => with_host(|h| h.truthy(v)),
None => true,
};
if held && !reentrant {
if !blocking {
return Ok(Value::Bool(false));
}
return Err(
"RuntimeError: deadlock: acquiring a lock already held by this thread"
.to_string(),
);
}
with_host(|h| {
if let Some(PyObj::Lock { count, .. }) = h.get_mut(recv) {
*count += 1;
}
});
Ok(Value::Bool(true))
}
"release" | "__exit__" => {
with_host(|h| {
if let Some(PyObj::Lock { count, .. }) = h.get_mut(recv) {
if *count > 0 {
*count -= 1;
}
}
});
Ok(Value::Undef)
}
"locked" => Ok(Value::Bool(with_host(|h| {
matches!(h.get(recv), Some(PyObj::Lock { count, .. }) if *count > 0)
}))),
"_at_fork_reinit" => Ok(Value::Undef),
"_is_owned" => Ok(Value::Bool(reentrant
&& with_host(|h| {
matches!(h.get(recv), Some(PyObj::Lock { count, .. }) if *count > 0)
}))),
_ => Err(type_error(&format!("'lock' object has no method '{name}'"))),
},
Some(PyObj::Redirect { stderr, target, .. }) => match name {
"__enter__" => {
with_host(|h| {
let cur = if stderr {
h.stderr_target.clone()
} else {
h.stdout_target.clone()
};
if let Some(PyObj::Redirect { saved, .. }) = h.get_mut(recv) {
*saved = cur;
}
let new = Some(target.clone());
if stderr {
h.stderr_target = new;
} else {
h.stdout_target = new;
}
});
Ok(target)
}
"__exit__" => {
with_host(|h| {
let saved = match h.get(recv) {
Some(PyObj::Redirect { saved, .. }) => saved.clone(),
_ => None,
};
if stderr {
h.stderr_target = saved;
} else {
h.stdout_target = saved;
}
});
Ok(Value::Bool(false))
}
_ => Err(format!(
"AttributeError: 'redirect' object has no attribute '{name}'"
)),
},
Some(PyObj::Instance(inst)) => {
if let Some(v) = with_host(|h| h.inst_attr(&inst.dict, name)) {
return invoke(&v, args, kwargs);
}
let class = inst.class.clone();
if name == "__class__" {
let cls = with_host(|h| h.alloc(PyObj::Class(class.clone())));
return invoke(&cls, args, kwargs);
}
if let Some(f) = with_host(|h| h.class_lookup(&class, name)) {
let fobj = with_host(|h| h.get(&f).cloned());
match fobj {
Some(PyObj::Func(fv)) => {
let owner = with_host(|h| method_owner(h, &class, name));
return run_user_func(&fv, Some(recv.clone()), owner, args, kwargs);
}
Some(PyObj::StaticMethod(inner)) => return invoke(&inner, args, kwargs),
Some(PyObj::ClassMethod(inner)) => {
let cls = with_host(|h| h.alloc(PyObj::Class(class.clone())));
let mut a = Vec::with_capacity(args.len() + 1);
a.push(cls);
a.extend(args);
return invoke(&inner, a, kwargs);
}
Some(PyObj::Descriptor {
kind: DescKind::WrapperDescriptor,
..
}) => {
let mut a = Vec::with_capacity(args.len() + 1);
a.push(recv.clone());
a.extend(args);
return invoke(&f, a, kwargs);
}
_ => return invoke(&f, args, kwargs),
}
}
if name == "__init__" {
return Ok(Value::Undef);
}
if !matches!(inst.payload, Value::Undef) {
if let Some(base) = with_host(|h| h.builtin_base_of(&class)) {
return base_dispatch(recv, &inst.payload, base, name, args, kwargs);
}
}
if with_host(|h| h.class_has(&class, "__getattr__")) {
let key = with_host(|h| h.new_str(name.to_string()));
let attr = call_method(recv, "__getattr__", vec![key], vec![])?;
return invoke(&attr, args, kwargs);
}
Err(format!(
"AttributeError: '{class}' object has no attribute '{name}'"
))
}
Some(PyObj::Class(cname)) => {
if name == "__subclasses__" {
let cls = with_host(|h| h.alloc(PyObj::Class(cname.clone())));
return crate::builtins::call_type_method(&cls, name, args, kwargs);
}
if let Some(f) = with_host(|h| h.class_lookup(&cname, name)) {
let fobj = with_host(|h| h.get(&f).cloned());
match fobj {
Some(PyObj::Func(fv)) => {
return run_user_func(&fv, None, Some(cname.clone()), args, kwargs);
}
Some(PyObj::StaticMethod(inner)) => return invoke(&inner, args, kwargs),
Some(PyObj::ClassMethod(inner)) => {
let cls = with_host(|h| h.alloc(PyObj::Class(cname.clone())));
let mut a = Vec::with_capacity(args.len() + 1);
a.push(cls);
a.extend(args);
return invoke(&inner, a, kwargs);
}
_ => return invoke(&f, args, kwargs),
}
}
if name == "__new__" {
return crate::builtins::call_builtin_function("object.__new__", args, kwargs);
}
let meta = with_host(|h| {
h.classes
.get(&cname)
.map(|c| c.metaclass.clone())
.unwrap_or_else(|| "type".into())
});
if meta != "type" {
if let Some(f) = with_host(|h| h.class_lookup(&meta, name)) {
if let Some(PyObj::Func(fv)) = with_host(|h| h.get(&f).cloned()) {
let clsobj = with_host(|h| h.alloc(PyObj::Class(cname.clone())));
let owner = with_host(|h| method_owner(h, &meta, name));
return run_user_func(&fv, Some(clsobj), owner, args, kwargs);
}
}
}
if name == "__subclasshook__" {
return Ok(with_host(|h| h.alloc(PyObj::NotImplemented)));
}
if name == "__init_subclass__" {
return Ok(Value::Undef);
}
Err(format!(
"AttributeError: type object '{cname}' has no attribute '{name}'"
))
}
Some(PyObj::Module { slot, name: mname }) => match with_host(|h| {
h.module_globals[slot].get(name).cloned()
}) {
Some(v) => invoke(&v, args, kwargs),
None => match with_host(|h| module_ffi_fallback(h, &mname, name)) {
Some(Ok(f)) => invoke(&f, args, kwargs),
Some(Err(e)) => Err(e),
None => Err(format!(
"AttributeError: module '{mname}' has no attribute '{name}'"
)),
},
},
Some(PyObj::Super { owner, instance }) => {
let inst_class = match with_host(|h| h.get(&instance).cloned()) {
Some(PyObj::Instance(i)) => i.class,
_ => owner.clone(),
};
match with_host(|h| super_lookup(h, &owner, &inst_class, name)) {
Some((f, found)) => {
let fobj = with_host(|h| h.get(&f).cloned());
if let Some(PyObj::Func(fv)) = fobj {
let recv = if name == "__new__" { None } else { Some(instance) };
return run_user_func(&fv, recv, Some(found), args, kwargs);
}
if found == "_random.Random" {
if let Value::Obj(id) = &instance {
return crate::builtins::random_method(*id, &name, &args);
}
}
invoke(&f, args, kwargs)
}
None if with_host(|h| h.builtin_base_of(&inst_class).is_some())
&& !matches!(
with_host(|h| match h.get(&instance) {
Some(PyObj::Instance(i)) => i.payload.clone(),
_ => Value::Undef,
}),
Value::Undef
) =>
{
let base = with_host(|h| h.builtin_base_of(&inst_class)).unwrap();
let payload = with_host(|h| match h.get(&instance) {
Some(PyObj::Instance(i)) => i.payload.clone(),
_ => Value::Undef,
});
if name == "__init__" {
return base_super_init(base, &payload, args, kwargs);
}
if name == "__new__" {
return Ok(instance.clone());
}
base_dispatch(&instance, &payload, base, name, args, kwargs)
}
None if with_host(|h| class_inherits_type(h, &owner)) => {
match name {
"__new__" if args.len() >= 4 => {
let mcls_name = with_host(|h| match h.get(&args[0]) {
Some(PyObj::Class(n)) => n.clone(),
_ => owner.clone(),
});
crate::builtins::type_new_meta(
&args[1], &args[2], &args[3], &mcls_name, kwargs,
)
}
"__init__" => Ok(Value::Undef),
"__call__" => {
let cls_name = with_host(|h| match h.get(&instance) {
Some(PyObj::Class(n)) => Some(n.clone()),
_ => None,
});
match cls_name {
Some(c) => instantiate_plain(&c, args, kwargs),
None => {
Err(type_error("super().__call__: receiver is not a class"))
}
}
}
_ => Err(format!(
"AttributeError: 'super' object has no attribute '{name}'"
)),
}
}
None if name == "__new__" => {
let cls = args.first().cloned().unwrap_or(Value::Undef);
match with_host(|h| h.get(&cls).cloned()) {
Some(PyObj::Class(cname)) => Ok(new_subclass_or_bare(&cname, &args[1..])?),
_ => Err(type_error("object.__new__(X): X is not a type object")),
}
}
None if name == "__init__" => {
if with_host(|h| h.class_is_exception(&inst_class)) {
with_host(|h| {
let t = h.alloc(PyObj::Tuple(args.clone()));
let _ = h.set_attr(&instance, "args", t);
});
}
Ok(Value::Undef)
}
None if name == "__init_subclass__" => Ok(Value::Undef),
None if name == "__setattr__" => {
let mut it = args.into_iter();
let attr = it.next().unwrap_or(Value::Undef);
let val = it.next().unwrap_or(Value::Undef);
let attr_s = with_host(|h| h.as_str(&attr)).unwrap_or_default();
with_host(|h| h.set_attr(&instance, &attr_s, val)).map(|_| Value::Undef)
}
None if name == "__delattr__" => {
let attr = args.into_iter().next().unwrap_or(Value::Undef);
let attr_s = with_host(|h| h.as_str(&attr)).unwrap_or_default();
with_host(|h| h.del_attr(&instance, &attr_s)).map(|_| Value::Undef)
}
None if name == "__getattribute__" => {
let attr = args.into_iter().next().unwrap_or(Value::Undef);
let attr_s = with_host(|h| h.as_str(&attr)).unwrap_or_default();
with_host(|h| h.get_attr(&instance, &attr_s))
}
None => Err(format!(
"AttributeError: 'super' object has no attribute '{name}'"
)),
}
}
Some(PyObj::Builtin(bname)) if bname == "object" && name == "__new__" => {
let cls = args.first().cloned().unwrap_or(Value::Undef);
match with_host(|h| h.get(&cls).cloned()) {
Some(PyObj::Class(cname)) => {
Ok(with_host(|h| h.new_instance(cname, IndexMap::new())))
}
_ => Err(type_error("object.__new__(X): X is not a type object")),
}
}
Some(PyObj::Builtin(bname))
if bname == "object"
&& matches!(name, "__getattribute__" | "__setattr__" | "__delattr__") =>
{
let selfv = args.first().cloned().unwrap_or(Value::Undef);
let attr = with_host(|h| h.str_of(&args.get(1).cloned().unwrap_or(Value::Undef)));
match name {
"__getattribute__" => crate::builtins::raw_getattr(&selfv, &attr),
"__setattr__" => {
let v = args.get(2).cloned().unwrap_or(Value::Undef);
crate::builtins::raw_setattr(&selfv, &attr, v).map(|_| Value::Undef)
}
_ => crate::builtins::raw_delattr(&selfv, &attr).map(|_| Value::Undef),
}
}
#[cfg(feature = "stdlib-ffi")]
Some(PyObj::Foreign(id)) => crate::ffi::call_method(id, name, args, kwargs),
Some(PyObj::Builtin(bname))
if name == "__init__" && crate::builtins::is_exception_class(&bname) =>
{
let mut it = args.into_iter();
if let Some(inst) = it.next() {
let rest: Vec<Value> = it.collect();
with_host(|h| {
let t = h.alloc(PyObj::Tuple(rest));
let _ = h.set_attr(&inst, "args", t);
});
}
Ok(Value::Undef)
}
Some(PyObj::Builtin(_)) => match with_host(|h| h.get_attr(recv, name)) {
Ok(f) => invoke(&f, args, kwargs),
Err(_) => crate::builtins::call_type_method(recv, name, args, kwargs),
},
_ => crate::builtins::call_type_method(recv, name, args, kwargs),
}
}
pub fn metaclass_hook(cls: &Value, name: &str, arg: Value) -> Option<Result<Value, String>> {
let cname = match with_host(|h| h.get(cls).cloned()) {
Some(PyObj::Class(n)) => n,
_ => return None,
};
let meta = with_host(|h| {
h.classes
.get(&cname)
.map(|c| c.metaclass.clone())
.unwrap_or_else(|| "type".into())
});
if meta == "type" || !with_host(|h| h.class_lookup(&meta, name).is_some()) {
return None;
}
Some(call_method(cls, name, vec![arg], vec![]))
}
pub fn class_getitem(cls: &Value, item: Value) -> Option<Result<Value, String>> {
let cname = match with_host(|h| match h.get(cls) {
Some(PyObj::Class(n)) => Some(n.clone()),
_ => None,
}) {
Some(n) => n,
None => return None,
};
let f = with_host(|h| h.class_lookup(&cname, "__class_getitem__"))?;
let inner = match with_host(|h| h.get(&f).cloned()) {
Some(PyObj::ClassMethod(inner)) => inner,
_ => f,
};
Some(invoke(&inner, vec![cls.clone(), item], vec![]))
}
fn super_lookup(h: &PyHost, owner: &str, inst_class: &str, name: &str) -> Option<(Value, String)> {
let mro = h.mro_of(inst_class);
let start = mro.iter().position(|c| c == owner).map(|i| i + 1)?;
for c in &mro[start..] {
if let Some(cd) = h.classes.get(c) {
if let Some(v) = cd.ns.get(name) {
return Some((v.clone(), c.clone()));
}
}
}
None
}
fn method_owner(h: &PyHost, class: &str, name: &str) -> Option<String> {
for c in h.mro_of(class) {
if let Some(cd) = h.classes.get(&c) {
if cd.ns.contains_key(name) {
return Some(c);
}
}
}
None
}
pub fn instantiate(
class: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
if crate::builtins::is_exception_class(class) && !with_host(|h| h.classes.contains_key(class)) {
return Ok(with_host(|h| {
h.alloc(PyObj::Exception {
class: class.to_string(),
args,
})
}));
}
let meta = with_host(|h| h.classes.get(class).map(|c| c.metaclass.clone()));
if let Some(m) = &meta {
if m != "type" {
if let Some(f) = with_host(|h| h.class_lookup(m, "__call__")) {
if let Some(PyObj::Func(fv)) = with_host(|h| h.get(&f).cloned()) {
let clsobj = with_host(|h| h.alloc(PyObj::Class(class.to_string())));
let owner = with_host(|h| method_owner(h, m, "__call__"));
return run_user_func(&fv, Some(clsobj), owner, args, kwargs);
}
}
}
}
instantiate_plain(class, args, kwargs)
}
pub fn instantiate_plain(
class: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
if with_host(|h| class_inherits_type(h, class)) {
return metaclass_instantiate(class, args, kwargs);
}
let inst = if let Some(newf) = with_host(|h| h.class_lookup(class, "__new__")) {
let newf = match with_host(|h| h.get(&newf).cloned()) {
Some(PyObj::StaticMethod(inner)) => inner,
_ => newf,
};
let clsobj = with_host(|h| h.alloc(PyObj::Class(class.to_string())));
if let Some(PyObj::Func(fv)) = with_host(|h| h.get(&newf).cloned()) {
let owner = with_host(|h| method_owner(h, class, "__new__"));
run_user_func(&fv, Some(clsobj), owner, args.clone(), kwargs.clone())?
} else {
let mut a = Vec::with_capacity(args.len() + 1);
a.push(clsobj);
a.extend(args.clone());
invoke(&newf, a, kwargs.clone())?
}
} else if let Some(base) = with_host(|h| h.builtin_base_of(class)) {
let immutable = matches!(base, "int" | "float" | "str" | "tuple" | "frozenset");
let has_user_init = with_host(|h| {
matches!(
h.class_lookup(class, "__init__")
.and_then(|f| h.get(&f).cloned()),
Some(PyObj::Func(_))
)
});
let payload = if immutable || !has_user_init {
crate::builtins::call_builtin_function(base, args.clone(), kwargs.clone())?
} else {
crate::builtins::call_builtin_function(base, vec![], vec![])?
};
with_host(|h| h.new_instance_payload(class.to_string(), payload))
} else {
with_host(|h| {
let mut attrs = IndexMap::new();
if h.class_is_exception(class) {
let t = h.alloc(PyObj::Tuple(args.clone()));
attrs.insert("args".to_string(), t);
}
h.new_instance(class.to_string(), attrs)
})
};
let init_ok = with_host(|h| match h.get(&inst) {
Some(PyObj::Instance(i)) => h.mro_of(&i.class).iter().any(|c| c == class),
_ => false,
});
if init_ok {
if let Some(f) = with_host(|h| h.class_lookup(class, "__init__")) {
let fobj = with_host(|h| h.get(&f).cloned());
if let Some(PyObj::Func(fv)) = fobj {
let owner = with_host(|h| method_owner(h, class, "__init__"));
run_user_func(&fv, Some(inst.clone()), owner, args, kwargs)?;
}
}
}
Ok(inst)
}
fn metaclass_instantiate(
meta: &str,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
let newcls = if let Some(newf) = with_host(|h| h.class_lookup(meta, "__new__")) {
let newf = match with_host(|h| h.get(&newf).cloned()) {
Some(PyObj::StaticMethod(inner)) => inner,
_ => newf,
};
let metaobj = with_host(|h| h.alloc(PyObj::Class(meta.to_string())));
if let Some(PyObj::Func(fv)) = with_host(|h| h.get(&newf).cloned()) {
let owner = with_host(|h| method_owner(h, meta, "__new__"));
run_user_func(&fv, Some(metaobj), owner, args.clone(), kwargs.clone())?
} else {
let mut a = Vec::with_capacity(args.len() + 1);
a.push(metaobj);
a.extend(args.clone());
invoke(&newf, a, kwargs.clone())?
}
} else if args.len() >= 3 {
crate::builtins::type_new_meta(&args[0], &args[1], &args[2], meta, kwargs.clone())?
} else {
return Err(type_error("type() takes 1 or 3 arguments"));
};
let is_class = with_host(|h| matches!(h.get(&newcls), Some(PyObj::Class(_))));
if is_class {
if let Some(f) = with_host(|h| h.class_lookup(meta, "__init__")) {
if let Some(PyObj::Func(fv)) = with_host(|h| h.get(&f).cloned()) {
let owner = with_host(|h| method_owner(h, meta, "__init__"));
run_user_func(&fv, Some(newcls.clone()), owner, args, kwargs)?;
}
}
}
Ok(newcls)
}
const RECURSION_LIMIT: usize = 1000;
pub fn run_user_func(
fv: &FuncVal,
self_opt: Option<Value>,
owner_opt: Option<String>,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
let (def, locals_rc, frame_name) = with_host(|h| {
(
h.funcs[fv.def_id].clone(),
h.func_locals[fv.def_id].clone(),
h.func_names[fv.def_id].clone(),
)
});
let def_id = fv.def_id;
let self_val = self_opt.or_else(|| fv.bound.clone());
let mut pos = args;
if let Some(s) = &self_val {
pos.insert(0, s.clone());
}
let frame_self = self_val.clone().or_else(|| {
if def.name == "__new__" {
pos.first().cloned()
} else {
None
}
});
let env = new_env(fv.env.clone());
bind_params(&env, &def, &fv.defaults, &fv.kwonly_defaults, pos, kwargs)?;
let owner = owner_opt.or_else(|| fv.owner.clone());
if def.is_async || def.is_generator {
let saved_mod = with_host(|h| h.swap_module(fv.module));
let body = def.chunk.clone();
let obj = if def.is_async {
if def.is_generator {
make_async_generator(
body,
env,
self_val,
owner,
def.name.clone(),
def.locals.clone(),
)
} else {
make_coroutine(
body,
env,
self_val,
owner,
def.name.clone(),
def.locals.clone(),
)
}
} else {
make_generator(
body,
env,
self_val,
owner,
def.name.clone(),
def.locals.clone(),
)
};
with_host(|h| h.swap_module(saved_mod));
return Ok(obj);
}
if with_host(|h| h.frames.len() >= RECURSION_LIMIT) {
return Err("RecursionError: maximum recursion depth exceeded".to_string());
}
let saved_mod = with_host(|h| {
let saved = h.swap_module(fv.module);
h.frames.push(Frame {
env,
globals_decl: HashSet::new(),
nonlocals_decl: HashSet::new(),
locals_set: locals_rc,
is_class_body: false,
self_obj: frame_self,
owner,
name: frame_name,
line: 0,
span: Span::NONE,
});
saved
});
let r = run_chunk_cached(def_id as u64, || def.chunk.clone());
let sig = with_host(|h| {
if r.is_err() {
h.push_tb_frame();
}
h.frames.pop();
h.swap_module(saved_mod);
h.signal.take()
});
match r {
Err(e) => Err(e),
Ok(_) => Ok(match sig {
Some(Signal::Return(v)) => v,
_ => Value::Undef,
}),
}
}
fn join_names(names: &[String]) -> String {
match names.len() {
0 => String::new(),
1 => format!("'{}'", names[0]),
2 => format!("'{}' and '{}'", names[0], names[1]),
n => {
let head: Vec<String> = names[..n - 1].iter().map(|s| format!("'{s}'")).collect();
format!("{}, and '{}'", head.join(", "), names[n - 1])
}
}
}
fn bind_params(
env: &Env,
def: &FuncDef,
defaults: &[Value],
kwonly_defaults: &[Value],
pos: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<(), String> {
let np = def.params.len();
let ndef = def.ndefaults;
let posonly = def.posonly.min(np);
let fname = if def.qualname.is_empty() {
def.name.as_str()
} else {
def.qualname.as_str()
};
let has_vararg = def.star.as_deref().is_some_and(|s| !s.is_empty());
let mut vars: IndexMap<String, Value> = IndexMap::new();
let mut star_items = Vec::new();
let npos = pos.len();
for (i, val) in pos.into_iter().enumerate() {
if i < np {
vars.insert(def.params[i].clone(), val);
} else {
star_items.push(val);
}
}
let kwonly_given = kwargs
.iter()
.filter(|(k, _)| def.kwonly.contains(k))
.count();
let mut leftover: Vec<(String, Value)> = Vec::new();
for (k, v) in kwargs {
if let Some(idx) = def.params.iter().position(|p| p == &k) {
if idx < posonly {
leftover.push((k, v));
} else if vars.contains_key(&k) {
return Err(type_error(&format!(
"{}() got multiple values for argument '{}'",
fname, k
)));
} else {
vars.insert(k, v);
}
} else if def.kwonly.contains(&k) {
vars.insert(k, v);
} else {
leftover.push((k, v));
}
}
if def.kwargs.is_none() && !leftover.is_empty() {
let bad_posonly: Vec<String> = def.params[..posonly]
.iter()
.filter(|p| leftover.iter().any(|(k, _)| k == *p))
.cloned()
.collect();
if !bad_posonly.is_empty() {
return Err(type_error(&format!(
"{}() got some positional-only arguments passed as keyword arguments: '{}'",
fname,
bad_posonly.join(", ")
)));
}
return Err(type_error(&format!(
"{}() got an unexpected keyword argument '{}'",
fname, leftover[0].0
)));
}
if npos > np && !has_vararg {
return Err(type_error(&format!(
"{}() {}",
fname,
too_many_positional(np, ndef, npos, kwonly_given)
)));
}
let mut missing: Vec<String> = Vec::new();
for i in 0..np {
if !vars.contains_key(&def.params[i]) {
if i >= np - ndef {
vars.insert(def.params[i].clone(), defaults[i - (np - ndef)].clone());
} else {
missing.push(def.params[i].clone());
}
}
}
if !missing.is_empty() {
let plural = if missing.len() == 1 { "" } else { "s" };
return Err(type_error(&format!(
"{}() missing {} required positional argument{}: {}",
fname,
missing.len(),
plural,
join_names(&missing)
)));
}
if has_vararg {
let name = def.star.clone().unwrap_or_default();
let t = with_host(|h| h.new_tuple(star_items));
vars.insert(name, t);
}
let mut kwdef_cursor = 0usize;
let mut missing_kw: Vec<String> = Vec::new();
for (j, name) in def.kwonly.iter().enumerate() {
let required = def.kwonly_required.get(j).copied().unwrap_or(true);
if vars.contains_key(name) {
if !required {
kwdef_cursor += 1;
}
} else if required {
missing_kw.push(name.clone());
} else {
let d = kwonly_defaults
.get(kwdef_cursor)
.cloned()
.unwrap_or(Value::Undef);
vars.insert(name.clone(), d);
kwdef_cursor += 1;
}
}
if !missing_kw.is_empty() {
let plural = if missing_kw.len() == 1 { "" } else { "s" };
return Err(type_error(&format!(
"{}() missing {} required keyword-only argument{}: {}",
fname,
missing_kw.len(),
plural,
join_names(&missing_kw)
)));
}
if let Some(kw) = &def.kwargs {
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
for (k, v) in leftover {
let kv = with_host(|h| h.new_str(k.clone()));
d.insert(PKey::Str(k), (kv, v));
}
let dict = with_host(|h| h.new_dict(d));
vars.insert(kw.clone(), dict);
}
env.borrow_mut().vars = vars;
Ok(())
}
fn too_many_positional(np: usize, ndef: usize, posgiven: usize, kwonly_given: usize) -> String {
let takes = if ndef > 0 {
format!("from {} to {} positional arguments", np - ndef, np)
} else if np == 1 {
"1 positional argument".to_string()
} else {
format!("{np} positional arguments")
};
let given = if kwonly_given > 0 {
let ps = if posgiven == 1 { "" } else { "s" };
let ks = if kwonly_given == 1 { "" } else { "s" };
format!(
"{posgiven} positional argument{ps} (and {kwonly_given} keyword-only argument{ks}) were given"
)
} else if posgiven == 1 {
"1 was given".to_string()
} else {
format!("{posgiven} were given")
};
format!("takes {takes} but {given}")
}
impl PyHost {
fn code_attr(&mut self, def_id: usize, name: &str) -> Result<Value, String> {
const CO_OPTIMIZED: i64 = 0x0001;
const CO_NEWLOCALS: i64 = 0x0002;
const CO_VARARGS: i64 = 0x0004;
const CO_VARKEYWORDS: i64 = 0x0008;
const CO_NOFREE: i64 = 0x0040;
const CO_GENERATOR: i64 = 0x0020;
const CO_COROUTINE: i64 = 0x0080;
const CO_ASYNC_GENERATOR: i64 = 0x0200;
let (co_name, co_qualname, params, posonly, kwonly, star, kwargs, locals, flags) = {
let d = &self.funcs[def_id];
let q = if d.qualname.is_empty() {
d.name.clone()
} else {
d.qualname.clone()
};
let mut f = CO_OPTIMIZED | CO_NEWLOCALS;
if d.freevars.is_empty() {
f |= CO_NOFREE;
}
if d.star.is_some() {
f |= CO_VARARGS;
}
if d.kwargs.is_some() {
f |= CO_VARKEYWORDS;
}
if d.is_generator && !d.is_async {
f |= CO_GENERATOR;
}
if d.is_async && !d.is_generator {
f |= CO_COROUTINE;
}
if d.is_async && d.is_generator {
f |= CO_ASYNC_GENERATOR;
}
(
d.name.clone(),
q,
d.params.clone(),
d.posonly,
d.kwonly.clone(),
d.star.clone(),
d.kwargs.clone(),
d.locals.clone(),
f,
)
};
let varnames = || -> Vec<String> {
let mut names = params.clone();
names.extend(kwonly.iter().cloned());
if let Some(s) = &star {
names.push(s.clone());
}
if let Some(k) = &kwargs {
names.push(k.clone());
}
for l in &locals {
if !names.contains(l) {
names.push(l.clone());
}
}
names
};
match name {
"co_name" => Ok(self.new_str(co_name)),
"co_qualname" => Ok(self.new_str(co_qualname)),
"co_argcount" => Ok(Value::Int(params.len() as i64)),
"co_posonlyargcount" => Ok(Value::Int(posonly as i64)),
"co_kwonlyargcount" => Ok(Value::Int(kwonly.len() as i64)),
"co_flags" => Ok(Value::Int(flags)),
"co_varnames" => {
let vals: Vec<Value> = varnames().into_iter().map(|n| self.new_str(n)).collect();
Ok(self.new_tuple(vals))
}
"co_nlocals" => Ok(Value::Int(varnames().len() as i64)),
"co_filename" => {
let f = self.tb_filename.clone();
Ok(self.new_str(if f.is_empty() {
"<string>".to_string()
} else {
f
}))
}
"co_firstlineno" => Ok(Value::Int(1)),
"co_freevars" => {
let fv = self.funcs[def_id].freevars.clone();
let vals: Vec<Value> = fv.into_iter().map(|n| self.new_str(n)).collect();
Ok(self.new_tuple(vals))
}
"co_names" | "co_consts" | "co_cellvars" => Ok(self.new_tuple(vec![])),
"co_stacksize" => Ok(Value::Int(0)),
_ => Err(format!(
"AttributeError: 'code' object has no attribute '{name}'"
)),
}
}
fn func_dunder(
&mut self,
name: &str,
def_id: usize,
defaults: &[Value],
kwonly_defaults: &[Value],
) -> Result<Value, String> {
match name {
"__name__" => {
let n = self.funcs[def_id].name.clone();
Ok(self.new_str(n))
}
"__qualname__" => {
let d = &self.funcs[def_id];
let q = if d.qualname.is_empty() {
d.name.clone()
} else {
d.qualname.clone()
};
Ok(self.new_str(q))
}
"__module__" => Ok(self.new_str("__main__".to_string())),
"__doc__" => match self.funcs[def_id].doc.clone() {
Some(d) => Ok(self.new_str(d)),
None => Ok(Value::Undef),
},
"__code__" => Ok(self.alloc(PyObj::Code { def_id })),
"__kwdefaults__" => {
let d = &self.funcs[def_id];
let names: Vec<String> = d.kwonly.clone();
let required: Vec<bool> = d.kwonly_required.clone();
let mut map: IndexMap<PKey, (Value, Value)> = IndexMap::new();
let mut next = 0usize;
for (i, n) in names.iter().enumerate() {
if required.get(i).copied().unwrap_or(true) {
continue;
}
let Some(v) = kwonly_defaults.get(next).cloned() else {
break;
};
next += 1;
let kv = self.new_str(n.clone());
map.insert(PKey::Str(n.clone()), (kv, v));
}
if map.is_empty() {
return Ok(Value::Undef);
}
Ok(self.new_dict(map))
}
_ => {
if defaults.is_empty() {
Ok(Value::Undef)
} else {
Ok(self.new_tuple(defaults.to_vec()))
}
}
}
}
pub fn current_env_capture(&self) -> Env {
let f = self.frame();
if f.is_class_body {
if let Some(parent) = f.env.borrow().parent.clone() {
return parent;
}
}
f.env.clone()
}
pub fn exc_instance_args(&self, dict: &Value) -> Vec<Value> {
match self.inst_attr(dict, "args") {
Some(v) => match self.get(&v) {
Some(PyObj::Tuple(t)) => t.clone(),
_ => vec![v.clone()],
},
None => Vec::new(),
}
}
pub fn exc_message(&self, class: &str, args: &[Value]) -> String {
if args.is_empty() {
String::new()
} else if args.len() == 1 {
if self.is_keyerror_str_class(class) {
self.repr_of(&args[0])
} else {
self.str_of(&args[0])
}
} else {
let inner: Vec<String> = args.iter().map(|a| self.repr_of(a)).collect();
format!("({})", inner.join(", "))
}
}
fn is_keyerror_str_class(&self, class: &str) -> bool {
if class == "KeyError" {
return true;
}
self.classes.contains_key(class) && self.mro_of(class).iter().any(|c| c == "KeyError")
}
pub fn exc_line_of(&self, exc: &Value) -> Option<String> {
match self.get(exc) {
Some(PyObj::Exception { class, args }) => {
Some(join_exc(class, &self.exc_message(class, args)))
}
Some(PyObj::Instance(i)) if self.class_is_exception(&i.class) => {
let a = self.exc_instance_args(&i.dict);
Some(join_exc(&i.class, &self.exc_message(&i.class, &a)))
}
_ => None,
}
}
pub fn key_error(&mut self, key: &Value) -> String {
let repr = self.repr_of(key);
let context = self.exc.clone().unwrap_or(Value::Undef);
let e = self.alloc(PyObj::Exception {
class: "KeyError".to_string(),
args: vec![key.clone()],
});
let ctx = match &context {
Value::Obj(_) if e != context => context,
_ => Value::Undef,
};
self.set_exc_link(&e, Value::Undef, ctx);
self.exc = Some(e);
format!("KeyError: {repr}")
}
}
fn run_class_body(name: &str, body_func: &Value) -> Result<IndexMap<String, Value>, String> {
let fv = match with_host(|h| h.get(body_func).cloned()) {
Some(PyObj::Func(fv)) => fv,
_ => return Err(type_error("internal: class body is not a function")),
};
let def = with_host(|h| h.funcs[fv.def_id].clone());
let env = new_env(fv.env.clone());
let saved_mod = with_host(|h| {
let saved = h.swap_module(fv.module);
h.frames.push(Frame {
env: env.clone(),
globals_decl: HashSet::new(),
nonlocals_decl: HashSet::new(),
locals_set: Rc::new(HashSet::new()),
is_class_body: true,
self_obj: None,
owner: Some(name.to_string()),
name: Rc::from(name),
line: 0,
span: Span::NONE,
});
saved
});
let r = run_chunk_on(def.chunk.clone());
with_host(|h| {
if r.is_err() {
h.push_tb_frame();
}
h.frames.pop();
h.swap_module(saved_mod);
h.signal.take();
});
r?;
let mut vars = env.borrow().vars.clone();
vars.entry("__doc__".to_string()).or_insert_with(|| {
with_host(|h| match &def.doc {
Some(d) => h.new_str(d.clone()),
None => Value::Undef,
})
});
vars.entry("__module__".to_string()).or_insert_with(|| {
with_host(|h| {
h.module_globals[fv.module]
.get("__name__")
.cloned()
.unwrap_or_else(|| h.new_str("__main__".to_string()))
})
});
Ok(vars)
}
#[cfg(feature = "stdlib-ffi")]
pub fn build_class_foreign(
name: &str,
bases: Vec<Value>,
body_func: &Value,
) -> Result<Value, String> {
let ns = run_class_body(name, body_func)?;
let mut members: Vec<(String, Value)> = ns.into_iter().collect();
if !members.iter().any(|(k, _)| k == "__module__") {
let m = with_host(|h| h.new_str("__main__".to_string()));
members.push(("__module__".to_string(), m));
}
if !members.iter().any(|(k, _)| k == "__qualname__") {
let q = with_host(|h| h.new_str(name.to_string()));
members.push(("__qualname__".to_string(), q));
}
crate::ffi::build_foreign_class(name, &bases, &members)
}
pub fn fire_set_name(class_name: &str, ns: &IndexMap<String, Value>) -> Result<(), String> {
for (attr_name, val) in ns {
let fires = with_host(|h| match h.get(val) {
Some(PyObj::Instance(i)) => h.class_lookup(&i.class, "__set_name__").is_some(),
_ => false,
});
if fires {
let owner = with_host(|h| h.alloc(PyObj::Class(class_name.to_string())));
let nm = with_host(|h| h.new_str(attr_name.clone()));
call_method(val, "__set_name__", vec![owner, nm], vec![])?;
}
}
Ok(())
}
pub fn build_class(
name: &str,
bases: Vec<String>,
body_func: &Value,
meta_name: Option<String>,
class_kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
let def = match with_host(|h| h.get(body_func).cloned()) {
Some(PyObj::Func(fv)) => with_host(|h| h.funcs[fv.def_id].clone()),
_ => return Err(type_error("internal: class body is not a function")),
};
let ns: IndexMap<String, Value> = run_class_body(name, body_func)?;
let effective_meta = match meta_name {
Some(m) if with_host(|h| h.classes.contains_key(&m)) => Some(m),
_ => {
let dm = with_host(|h| default_metaclass(h, &bases));
(dm != "type").then_some(dm)
}
};
let cls = match &effective_meta {
Some(m) => metaclass_create(m, name, &bases, &ns, &class_kwargs)?,
None => with_host(|h| h.register_class(name, bases, ns.clone())),
};
if !def.qualname.is_empty() {
with_host(|h| {
if let Some(cd) = h.classes.get_mut(name) {
cd.qualname = def.qualname.clone();
}
});
}
if effective_meta.is_none() {
fire_set_name(name, &ns)?;
fire_init_subclass(name, class_kwargs)?;
}
Ok(cls)
}
pub fn fire_init_subclass(
class_name: &str,
class_kwargs: Vec<(String, Value)>,
) -> Result<(), String> {
let hook = with_host(|h| {
h.mro_of(class_name).into_iter().skip(1).find_map(|c| {
h.classes
.get(&c)
.and_then(|cd| cd.ns.get("__init_subclass__").cloned())
.map(|v| (v, c))
})
});
match hook {
Some((v, owner)) => {
let inner = match with_host(|h| h.get(&v).cloned()) {
Some(PyObj::ClassMethod(f)) => f,
_ => v,
};
if let Some(PyObj::Func(fv)) = with_host(|h| h.get(&inner).cloned()) {
let clsobj = with_host(|h| h.alloc(PyObj::Class(class_name.to_string())));
run_user_func(&fv, Some(clsobj), Some(owner), vec![], class_kwargs)?;
}
}
None if !class_kwargs.is_empty() => {
return Err(type_error(&format!(
"{class_name}.__init_subclass__() takes no keyword arguments"
)));
}
None => {}
}
Ok(())
}
fn metaclass_create(
meta: &str,
name: &str,
bases: &[String],
ns: &IndexMap<String, Value>,
class_kwargs: &[(String, Value)],
) -> Result<Value, String> {
let name_v = with_host(|h| h.new_str(name.to_string()));
let base_vals: Vec<Value> = with_host(|h| {
bases
.iter()
.map(|b| h.class_or_builtin_type(b.clone()))
.collect()
});
let bases_v = with_host(|h| h.new_tuple(base_vals));
let meta_v = with_host(|h| h.alloc(PyObj::Class(meta.to_string())));
let has_prepare = with_host(|h| h.class_lookup(meta, "__prepare__").is_some());
let ns_v = if has_prepare {
let prepared = call_method(
&meta_v,
"__prepare__",
vec![name_v.clone(), bases_v.clone()],
class_kwargs.to_vec(),
)?;
for (k, v) in ns {
let kv = with_host(|h| h.new_str(k.clone()));
call_method(&prepared, "__setitem__", vec![kv, v.clone()], vec![])?;
}
prepared
} else {
let ns_map: IndexMap<PKey, (Value, Value)> = with_host(|h| {
ns.iter()
.map(|(k, v)| {
let kv = h.new_str(k.clone());
(PKey::Str(k.clone()), (kv, v.clone()))
})
.collect()
});
with_host(|h| h.new_dict(ns_map))
};
invoke(&meta_v, vec![name_v, bases_v, ns_v], class_kwargs.to_vec())
}
pub fn default_metaclass(h: &PyHost, bases: &[String]) -> String {
let mut winner = "type".to_string();
for b in bases {
let mb = h
.classes
.get(b)
.map(|c| c.metaclass.clone())
.unwrap_or_else(|| "type".into());
if mb == winner {
continue;
}
if winner == "type" || class_is_subclass(h, &mb, &winner) {
winner = mb;
}
}
winner
}
fn class_is_subclass(h: &PyHost, sub: &str, sup: &str) -> bool {
if sub == sup {
return true;
}
match h.classes.get(sub) {
Some(cd) => cd.bases.iter().any(|b| class_is_subclass(h, b, sup)),
None => false,
}
}
pub fn class_inherits_type(h: &PyHost, class: &str) -> bool {
if class == "type" {
return true;
}
match h.classes.get(class) {
Some(cd) => cd
.bases
.iter()
.any(|b| b == "type" || class_inherits_type(h, b)),
None => false,
}
}
pub fn raise_value(exc: &Value) -> Result<String, String> {
with_host(|h| {
let obj = h.get(exc).cloned();
match obj {
Some(PyObj::Exception { class, args }) => {
let msg = h.exc_message(&class, &args);
h.exc = Some(exc.clone());
Ok(join_exc(&class, &msg))
}
Some(PyObj::Builtin(name)) if crate::builtins::is_exception_class(&name) => {
let e = h.alloc(PyObj::Exception {
class: name.clone(),
args: vec![],
});
h.exc = Some(e);
Ok(name)
}
Some(PyObj::Class(name)) => {
let mut attrs = IndexMap::new();
if h.class_is_exception(&name) {
let t = h.alloc(PyObj::Tuple(vec![]));
attrs.insert("args".to_string(), t);
}
let inst = h.new_instance(name.clone(), attrs);
h.exc = Some(inst);
Ok(name)
}
Some(PyObj::Instance(i)) => {
let class = i.class.clone();
let line = if h.class_is_exception(&class) {
let a = h.exc_instance_args(&i.dict);
join_exc(&class, &h.exc_message(&class, &a))
} else {
class
};
h.exc = Some(exc.clone());
Ok(line)
}
_ => Err(type_error("exceptions must derive from BaseException")),
}
})
}
fn join_exc(class: &str, msg: &str) -> String {
if msg.is_empty() {
class.to_string()
} else {
format!("{class}: {msg}")
}
}
pub enum TopExit {
SystemExit { code: i32, message: Option<String> },
Uncaught { traceback: String },
}
pub fn classify_top_error(err: &str) -> TopExit {
with_host(|h| {
if let Some(Value::Obj(_)) = &h.exc {
let exc = h.exc.clone().unwrap();
if let Some(PyObj::Exception { class, args }) = h.get(&exc) {
if class == "SystemExit" {
let args = args.clone();
return system_exit_outcome(h, &args);
}
}
}
TopExit::Uncaught {
traceback: h.render_traceback(err),
}
})
}
fn system_exit_outcome(h: &mut PyHost, args: &[Value]) -> TopExit {
let code = match args.len() {
0 => Value::Undef,
1 => args[0].clone(),
_ => h.new_tuple(args.to_vec()),
};
match &code {
Value::Undef => TopExit::SystemExit {
code: 0,
message: None,
},
Value::Bool(b) => TopExit::SystemExit {
code: *b as i32,
message: None,
},
Value::Int(n) => TopExit::SystemExit {
code: *n as i32,
message: None,
},
other => TopExit::SystemExit {
code: 1,
message: Some(format!("{}\n", h.str_of(other))),
},
}
}
impl PyHost {
pub fn render_traceback(&self, err: &str) -> String {
let mut final_frames: Vec<(String, u32, Span)> = Vec::new();
if let Some(f) = self.frames.first() {
final_frames.push((f.name.to_string(), f.line, f.span));
}
for f in self.traceback.iter().rev() {
final_frames.push(f.clone());
}
const CAUSE: &str =
"\nThe above exception was the direct cause of the following exception:\n\n";
const CONTEXT: &str =
"\nDuring handling of the above exception, another exception occurred:\n\n";
let mut ancestors: Vec<(Value, &'static str)> = Vec::new();
if let Some(final_exc) = &self.exc {
let mut cur = final_exc.clone();
let mut seen: HashSet<u32> = HashSet::new();
loop {
if let Value::Obj(id) = cur {
if !seen.insert(id) {
break;
}
}
let (cause, context) = self.exc_link(&cur);
let suppressed =
matches!(&cur, Value::Obj(id) if self.suppress_context.contains(id));
let (pred, connector) = if !matches!(cause, Value::Undef) {
(cause, CAUSE)
} else if !suppressed && !matches!(context, Value::Undef) {
(context, CONTEXT)
} else {
break;
};
ancestors.push((pred.clone(), connector));
cur = pred;
}
}
let mut out = String::new();
for (exc, connector) in ancestors.iter().rev() {
let frames = match exc {
Value::Obj(id) => self.exc_tb.get(id).cloned().unwrap_or_default(),
_ => Vec::new(),
};
out.push_str(&self.render_one_tb(&frames, &self.exc_final_line(exc)));
out.push_str(connector);
}
out.push_str(&self.render_one_tb(&final_frames, err));
out
}
fn render_one_tb(&self, frames: &[(String, u32, Span)], final_line: &str) -> String {
let mut out = String::from("Traceback (most recent call last):\n");
let src_lines: Vec<&str> = self.prog_source.lines().collect();
for (name, line, span) in frames {
out.push_str(&format!(
" File \"{}\", line {}, in {}\n",
self.tb_filename, line, name
));
if self.tb_show_source && *line > 0 {
if let Some(text) = src_lines.get((*line as usize).saturating_sub(1)) {
let stripped = text.trim();
if !stripped.is_empty() {
out.push_str(&format!(" {stripped}\n"));
if span.line == *line {
if let Some(carets) = caret_line(text, *span) {
out.push_str(&carets);
out.push('\n');
}
}
}
}
}
}
out.push_str(final_line);
out.push('\n');
out
}
fn exc_final_line(&self, exc: &Value) -> String {
match self.get(exc) {
Some(PyObj::Exception { class, args }) => {
join_exc(class, &self.exc_message(class, args))
}
Some(PyObj::Instance(i)) if self.class_is_exception(&i.class) => {
let a = self.exc_instance_args(&i.dict);
join_exc(&i.class, &self.exc_message(&i.class, &a))
}
_ => String::new(),
}
}
}
impl PyHost {
fn install_gen_ctx(&mut self, mut c: GenContext) -> GenContext {
std::mem::swap(&mut self.frames, &mut c.frames);
std::mem::swap(&mut self.error, &mut c.error);
std::mem::swap(&mut self.exc, &mut c.exc);
std::mem::swap(&mut self.signal, &mut c.signal);
std::mem::swap(&mut self.cur_module, &mut c.module);
c
}
}
fn caret_line(text: &str, span: Span) -> Option<String> {
if !span.is_some() || span.suppress {
return None;
}
let lead = text.chars().take_while(|c| c.is_whitespace()).count() as u32;
let stripped_len = text.trim().chars().count() as u32;
let start = span.start.saturating_sub(lead);
let end = span.end.saturating_sub(lead).min(stripped_len);
if end <= start {
return None;
}
let has_anchor = span.has_anchor();
let a0 = span.anchor_start.saturating_sub(lead);
let a1 = span.anchor_end.saturating_sub(lead);
let show = has_anchor || start > 0 || end < stripped_len;
if !show {
return None;
}
let primary = if has_anchor { '~' } else { '^' };
let mut carets = String::from(" ");
for col in 0..end {
let c = if col < start {
' '
} else if has_anchor && col >= a0 && col < a1 {
'^'
} else {
primary
};
carets.push(c);
}
Some(carets)
}
fn make_generator(
chunk: Chunk,
env: Env,
self_val: Option<Value>,
owner: Option<String>,
func_name: String,
locals: Vec<String>,
) -> Value {
make_gen_kind(
chunk,
env,
self_val,
owner,
GenKind::Generator,
func_name,
locals,
)
}
pub fn make_coroutine(
chunk: Chunk,
env: Env,
self_val: Option<Value>,
owner: Option<String>,
func_name: String,
locals: Vec<String>,
) -> Value {
make_gen_kind(
chunk,
env,
self_val,
owner,
GenKind::Coroutine,
func_name,
locals,
)
}
pub fn make_async_generator(
chunk: Chunk,
env: Env,
self_val: Option<Value>,
owner: Option<String>,
func_name: String,
locals: Vec<String>,
) -> Value {
make_gen_kind(
chunk,
env,
self_val,
owner,
GenKind::AsyncGen,
func_name,
locals,
)
}
pub fn is_async_generator(v: &Value) -> bool {
match with_host(|h| h.get(v).cloned()) {
Some(PyObj::Generator { id }) => {
with_host(|h| h.generators[id as usize].kind == GenKind::AsyncGen)
}
_ => false,
}
}
pub fn cur_gen_awaiting(gen: &Value) -> bool {
match with_host(|h| h.get(gen).cloned()) {
Some(PyObj::Generator { id }) => with_host(|h| h.generators[id as usize].awaiting),
_ => false,
}
}
pub fn set_agen_op(gen: &Value, op: AGenOp) {
if let Some(PyObj::Generator { id }) = with_host(|h| h.get(gen).cloned()) {
with_host(|h| h.generators[id as usize].agen_op = Some(op));
}
}
pub fn take_agen_op(gen: &Value) -> Option<AGenOp> {
match with_host(|h| h.get(gen).cloned()) {
Some(PyObj::Generator { id }) => with_host(|h| h.generators[id as usize].agen_op.take()),
_ => None,
}
}
impl PyHost {
pub fn subclasses_of(&self, cname: &str) -> Vec<String> {
self.classes
.values()
.filter(|cd| cd.bases.iter().any(|b| b == cname))
.map(|cd| cd.name.clone())
.collect()
}
pub fn current_frame_object(&mut self, depth: usize) -> Value {
let idx = self.frames.len().saturating_sub(1 + depth);
let (name, lineno) = self
.frames
.get(idx)
.map(|f| (f.name.to_string(), f.line))
.unwrap_or_else(|| ("<module>".to_string(), 0));
self.alloc(PyObj::PyFrame { name, lineno })
}
pub fn close_unstarted_gen(&mut self, gen: &Value) -> bool {
if let Some(PyObj::Generator { id }) = self.get(gen) {
let id = *id as usize;
if !self.generators[id].started {
self.generators[id].done = true;
return true;
}
}
false
}
}
pub fn warn_unawaited_coroutines() {
let names: Vec<String> = with_host(|h| {
h.generators
.iter()
.filter(|g| g.kind == GenKind::Coroutine && !g.started && !g.done)
.map(|g| g.func_name.clone())
.collect()
});
for name in names {
eprintln!("RuntimeWarning: coroutine '{name}' was never awaited");
}
}
pub fn run_atexit_callbacks() {
let callbacks: Vec<(Value, Vec<Value>, Vec<(String, Value)>)> =
with_host(|h| std::mem::take(&mut h.atexit_callbacks));
for (func, args, kwargs) in callbacks.into_iter().rev() {
if let Err(e) = invoke(&func, args, kwargs) {
with_host(|h| h.exc = None);
eprintln!("Error in atexit._run_exitfuncs:\n{e}");
}
}
}
fn make_gen_kind(
chunk: Chunk,
env: Env,
self_val: Option<Value>,
owner: Option<String>,
kind: GenKind,
func_name: String,
locals: Vec<String>,
) -> Value {
let frame = Frame {
env,
globals_decl: HashSet::new(),
nonlocals_decl: HashSet::new(),
locals_set: Rc::new(locals.into_iter().collect()),
is_class_body: false,
self_obj: self_val,
owner: owner.clone(),
name: owner.map_or_else(|| Rc::from("<genexpr>"), |o| Rc::from(o.as_str())),
line: 0,
span: Span::NONE,
};
let id = with_host(|h| {
let id = h.generators.len() as u32;
h.generators.push(GenCell {
kind,
coro: None,
yielder: std::ptr::null(),
ctx: GenContext {
frames: vec![frame],
module: h.cur_module,
..GenContext::default()
},
done: false,
started: false,
pending_throw: None,
ret_value: Value::Undef,
awaiting: false,
agen_op: None,
func_name,
});
id
});
const GENERATOR_STACK: usize = 64 * 1024 * 1024;
let stack = corosensei::stack::DefaultStack::new(GENERATOR_STACK)
.expect("allocate generator stack");
let coro = corosensei::Coroutine::with_stack(
stack,
move |yielder: &corosensei::Yielder<Value, Value>, _first: Value| {
with_host(|h| h.generators[id as usize].yielder = yielder as *const _ as *const ());
let r = run_chunk_on(chunk);
with_host(|h| {
if let Some(Signal::Return(v)) = h.signal.take() {
h.generators[id as usize].ret_value = v;
}
});
r.map(|_| Value::Undef)
},
);
with_host(|h| h.generators[id as usize].coro = Some(coro));
with_host(|h| h.alloc(PyObj::Generator { id }))
}
pub fn gen_yield(v: Value) -> Result<Value, String> {
gen_suspend(v, false)
}
pub fn gen_yield_awaiting(v: Value) -> Result<Value, String> {
gen_suspend(v, true)
}
fn gen_suspend(v: Value, awaiting: bool) -> Result<Value, String> {
match gen_suspend_raw(v, awaiting)? {
Resumed::Send(s) => Ok(s),
Resumed::Throw(exc) => Err(raise_value(&exc).unwrap_or_else(|e| e)),
}
}
enum Resumed {
Send(Value),
Throw(Value),
}
fn gen_suspend_raw(v: Value, awaiting: bool) -> Result<Resumed, String> {
let id = match CUR_GEN.with(|c| c.get()) {
Some(id) => id,
None => return Err(type_error("'yield' outside a generator")),
};
with_host(|h| h.generators[id as usize].awaiting = awaiting);
let yp = with_host(|h| h.generators[id as usize].yielder);
let yielder = unsafe { &*(yp as *const corosensei::Yielder<Value, Value>) };
let sent = yielder.suspend(v);
if let Some(exc) = with_host(|h| h.generators[id as usize].pending_throw.take()) {
return Ok(Resumed::Throw(exc));
}
Ok(Resumed::Send(sent))
}
enum SubStep {
Yield(Value),
Return(Value),
}
fn exc_class_name(v: &Value) -> Option<String> {
with_host(|h| match h.get(v) {
Some(PyObj::Exception { class, .. }) => Some(class.clone()),
Some(PyObj::Instance(i)) => Some(i.class.clone()),
Some(PyObj::Builtin(n)) => Some(n.clone()),
_ => None,
})
}
fn gen_ret_of(it: &Value) -> Value {
match with_host(|h| h.get(it).cloned()) {
Some(PyObj::Generator { id }) => with_host(|h| h.gen_return_value(id)),
_ => Value::Undef,
}
}
fn sub_send(it: &Value, is_gen: bool, s: Value) -> Result<SubStep, String> {
if is_gen {
match gen_resume(it, s)? {
Some(v) => Ok(SubStep::Yield(v)),
None => Ok(SubStep::Return(gen_ret_of(it))),
}
} else {
if !matches!(s, Value::Undef) {
let tn = with_host(|h| h.type_name(it));
return Err(format!(
"AttributeError: '{tn}' object has no attribute 'send'"
));
}
match iter_step(it)? {
Some(v) => Ok(SubStep::Yield(v)),
None => Ok(SubStep::Return(Value::Undef)),
}
}
}
fn sub_throw(it: &Value, is_gen: bool, exc: Value) -> Result<SubStep, String> {
if is_gen {
match gen_throw(it, exc)? {
Some(v) => Ok(SubStep::Yield(v)),
None => Ok(SubStep::Return(gen_ret_of(it))),
}
} else {
Err(raise_value(&exc).unwrap_or_else(|e| e))
}
}
fn sub_close(it: &Value, is_gen: bool) {
if is_gen {
let ge = with_host(|h| {
h.alloc(PyObj::Exception {
class: "GeneratorExit".into(),
args: vec![],
})
});
let _ = gen_throw(it, ge);
with_host(|h| {
h.error = None;
h.exc = None;
});
}
}
pub fn run_yield_from(it: Value) -> Result<Value, String> {
let is_gen = with_host(|h| matches!(h.get(&it), Some(PyObj::Generator { .. })));
let mut y = match sub_send(&it, is_gen, Value::Undef)? {
SubStep::Yield(v) => v,
SubStep::Return(r) => return Ok(r),
};
loop {
match gen_suspend_raw(y, false)? {
Resumed::Send(s) => match sub_send(&it, is_gen, s)? {
SubStep::Yield(v) => y = v,
SubStep::Return(r) => return Ok(r),
},
Resumed::Throw(exc) => {
if exc_class_name(&exc).as_deref() == Some("GeneratorExit") {
sub_close(&it, is_gen);
return Err(raise_value(&exc).unwrap_or_else(|e| e));
}
match sub_throw(&it, is_gen, exc)? {
SubStep::Yield(v) => y = v,
SubStep::Return(r) => return Ok(r),
}
}
}
}
}
pub fn gen_started(gen: &Value) -> bool {
match with_host(|h| h.get(gen).cloned()) {
Some(PyObj::Generator { id }) => with_host(|h| h.generators[id as usize].started),
_ => false,
}
}
pub fn coro_return_value(gen: &Value) -> Value {
match with_host(|h| h.get(gen).cloned()) {
Some(PyObj::Generator { id }) => with_host(|h| h.gen_return_value(id)),
_ => Value::Undef,
}
}
pub fn is_coroutine(v: &Value) -> bool {
match with_host(|h| h.get(v).cloned()) {
Some(PyObj::Generator { id }) => {
with_host(|h| h.generators[id as usize].kind == GenKind::Coroutine)
}
_ => false,
}
}
pub fn gen_stop_iteration(gen: &Value) -> Value {
let ret = match with_host(|h| h.get(gen).cloned()) {
Some(PyObj::Generator { id }) => with_host(|h| h.generators[id as usize].ret_value.clone()),
_ => Value::Undef,
};
let args = if matches!(ret, Value::Undef) {
vec![]
} else {
vec![ret]
};
with_host(|h| {
let e = h.alloc(PyObj::Exception {
class: "StopIteration".into(),
args,
});
h.exc = Some(e.clone());
e
});
with_host(|h| h.exc.clone().unwrap())
}
pub fn gen_throw(gen: &Value, exc: Value) -> Result<Option<Value>, String> {
let id = match with_host(|h| h.get(gen).cloned()) {
Some(PyObj::Generator { id }) => id,
_ => return Err(type_error("not a generator")),
};
let (started, done) = with_host(|h| {
let g = &h.generators[id as usize];
(g.started, g.done)
});
if !started || done {
return Err(raise_value(&exc).unwrap_or_else(|e| e));
}
with_host(|h| h.generators[id as usize].pending_throw = Some(exc));
gen_resume(gen, Value::Undef)
}
pub fn gen_resume(gen: &Value, send: Value) -> Result<Option<Value>, String> {
let id = match with_host(|h| h.get(gen).cloned()) {
Some(PyObj::Generator { id }) => id,
_ => return Err(type_error("not a generator")),
};
if with_host(|h| h.generators[id as usize].done) {
return Ok(None);
}
let mut coro = match with_host(|h| h.generators[id as usize].coro.take()) {
Some(c) => c,
None => return Err("ValueError: generator already executing".into()),
};
let gen_ctx = with_host(|h| std::mem::take(&mut h.generators[id as usize].ctx));
let caller_ctx = with_host(|h| h.install_gen_ctx(gen_ctx));
let prev = CUR_GEN.with(|c| c.replace(Some(id)));
with_host(|h| h.generators[id as usize].started = true);
let out = coro.resume(send);
CUR_GEN.with(|c| c.set(prev));
let gen_ctx = with_host(|h| h.install_gen_ctx(caller_ctx));
with_host(|h| {
h.generators[id as usize].ctx = gen_ctx;
h.generators[id as usize].coro = Some(coro);
});
match out {
corosensei::CoroutineResult::Yield(y) => Ok(Some(y)),
corosensei::CoroutineResult::Return(r) => {
with_host(|h| h.generators[id as usize].done = true);
match r {
Ok(_) => Ok(None),
Err(e) => Err(e),
}
}
}
}
pub fn iter_vec(v: &Value) -> Result<Vec<Value>, String> {
if with_host(|h| matches!(h.get(v), Some(PyObj::Generator { .. }))) {
let mut out = Vec::new();
while let Some(x) = gen_resume(v, Value::Undef)? {
out.push(x);
}
return Ok(out);
}
if with_host(|h| {
matches!(
h.get(v),
Some(PyObj::Zip { .. })
| Some(PyObj::MapObj { .. })
| Some(PyObj::FilterObj { .. })
| Some(PyObj::EnumerateObj { .. })
| Some(PyObj::ItertoolsIter { .. })
| Some(PyObj::CallIter { .. })
)
}) {
let mut out = Vec::new();
while let Some(x) = iter_step(v)? {
out.push(x);
}
return Ok(out);
}
#[cfg(feature = "stdlib-ffi")]
if let Some(id) = with_host(|h| h.foreign_id(v)) {
let it = crate::ffi::make_iter_cb(id)?;
let mut out = Vec::new();
while let Some(x) = iter_step(&it)? {
out.push(x);
}
return Ok(out);
}
if with_host(|h| matches!(h.get(v), Some(PyObj::Instance(_)))) {
return iter_instance_items(v);
}
if let Some(m) = with_host(|h| h.metaclass_method(v, "__iter__")) {
let it = invoke(&m, vec![v.clone()], vec![])?;
return iter_vec(&it);
}
with_host(|h| h.iter_items(v))
}
pub fn make_iterator(v: &Value) -> Result<Value, String> {
let protocol = 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 protocol {
Some((true, _)) => {
let it = call_method(v, "__iter__", vec![], vec![])?;
with_host(|h| h.make_iter(&it))
}
Some((false, true)) => {
let items = iter_instance_items(v)?;
Ok(with_host(|h| h.alloc(PyObj::Iter(IterState::Seq { items, idx: 0 }))))
}
_ => with_host(|h| h.make_iter(v)),
}
}
pub fn iter_instance_items(v: &Value) -> Result<Vec<Value>, String> {
if let Some(payload) = subclass_payload(v, "__iter__") {
return iter_vec(&payload);
}
let (has_iter, has_getitem) = with_host(|h| match h.get(v) {
Some(PyObj::Instance(i)) => (
h.class_lookup(&i.class, "__iter__").is_some(),
h.class_lookup(&i.class, "__getitem__").is_some(),
),
_ => (false, false),
});
if has_iter {
let it = call_method(v, "__iter__", vec![], vec![])?;
if with_host(|h| {
matches!(
h.get(&it),
Some(PyObj::Iter(_)) | Some(PyObj::Generator { .. })
)
}) {
return iter_vec(&it);
}
let mut items = Vec::new();
loop {
match call_method(&it, "__next__", vec![], vec![]) {
Ok(x) => items.push(x),
Err(e) if e.contains("StopIteration") => break,
Err(e) => return Err(e),
}
if items.len() > 10_000_000 {
break;
}
}
Ok(items)
} else if has_getitem {
let mut items = Vec::new();
let mut i: i64 = 0;
loop {
match call_method(v, "__getitem__", vec![Value::Int(i)], vec![]) {
Ok(x) => items.push(x),
Err(e) if e.contains("IndexError") || e.contains("StopIteration") => break,
Err(e) => return Err(e),
}
i += 1;
if items.len() > 10_000_000 {
break;
}
}
Ok(items)
} else {
Err(type_error(&format!(
"'{}' object is not iterable",
with_host(|h| h.type_name(v))
)))
}
}
fn itertools_step(it: &Value) -> Result<Option<Value>, String> {
let (kind, sources, func, mut nums, mut buf, mut flag, done) =
match with_host(|h| h.get(it).cloned()) {
Some(PyObj::ItertoolsIter {
kind,
sources,
func,
nums,
buf,
flag,
done,
}) => (kind, sources, func, nums, buf, flag, done),
_ => return Err(type_error("not an iterator")),
};
if done {
return Ok(None);
}
let mut finished = false;
let result: Option<Value> = match kind {
ItKind::Count => {
let cur = nums[0];
nums[0] = cur.wrapping_add(nums[1]);
Some(Value::Int(cur))
}
ItKind::Repeat => {
if nums[0] == 0 {
finished = true;
None
} else {
if nums[0] > 0 {
nums[0] -= 1;
}
Some(buf[0].clone())
}
}
ItKind::Cycle => {
if !flag {
match iter_step(&sources[0])? {
Some(v) => {
buf.push(v.clone());
Some(v)
}
None => {
flag = true; if buf.is_empty() {
finished = true;
None
} else {
let v = buf[0].clone();
nums = vec![1 % buf.len() as i64];
Some(v)
}
}
}
} else if buf.is_empty() {
finished = true;
None
} else {
let idx = nums[0] as usize;
let v = buf[idx].clone();
nums[0] = ((idx + 1) % buf.len()) as i64;
Some(v)
}
}
ItKind::Chain => {
let mut out = None;
while (nums[0] as usize) < sources.len() {
match iter_step(&sources[nums[0] as usize])? {
Some(v) => {
out = Some(v);
break;
}
None => nums[0] += 1,
}
}
if out.is_none() {
finished = true;
}
out
}
ItKind::Accumulate => match iter_step(&sources[0])? {
None => {
finished = true;
None
}
Some(v) => {
let acc = if buf.is_empty() {
v
} else {
let prev = buf[0].clone();
if matches!(func, Value::Undef) {
with_host(|h| h.arith(NumOp::Add, &prev, &v))?
} else {
invoke(&func, vec![prev, v], vec![])?
}
};
buf = vec![acc.clone()];
Some(acc)
}
},
ItKind::StarMap => match iter_step(&sources[0])? {
None => {
finished = true;
None
}
Some(tup) => {
let call_args = iter_vec(&tup)?;
Some(invoke(&func, call_args, vec![])?)
}
},
ItKind::Compress => {
let mut out = None;
loop {
let d = iter_step(&sources[0])?;
let s = iter_step(&sources[1])?;
match (d, s) {
(Some(dv), Some(sv)) => {
if with_host(|h| h.truthy(&sv)) {
out = Some(dv);
break;
}
}
_ => {
finished = true;
break;
}
}
}
out
}
ItKind::DropWhile => {
let mut out = None;
loop {
match iter_step(&sources[0])? {
None => {
finished = true;
break;
}
Some(v) => {
if flag {
let keep = invoke(&func, vec![v.clone()], vec![])?;
if with_host(|h| h.truthy(&keep)) {
continue; }
flag = false;
}
out = Some(v);
break;
}
}
}
out
}
ItKind::TakeWhile => match iter_step(&sources[0])? {
None => {
finished = true;
None
}
Some(v) => {
let keep = invoke(&func, vec![v.clone()], vec![])?;
if with_host(|h| h.truthy(&keep)) {
Some(v)
} else {
finished = true;
None
}
}
},
ItKind::FilterFalse => {
let mut out = None;
loop {
match iter_step(&sources[0])? {
None => {
finished = true;
break;
}
Some(v) => {
let truthy = if matches!(func, Value::Undef) {
with_host(|h| h.truthy(&v))
} else {
let r = invoke(&func, vec![v.clone()], vec![])?;
with_host(|h| h.truthy(&r))
};
if !truthy {
out = Some(v);
break;
}
}
}
}
out
}
ItKind::ISlice => {
let mut out = None;
loop {
if nums[1] >= 0 && nums[3] >= nums[1] {
finished = true;
break;
}
match iter_step(&sources[0])? {
None => {
finished = true;
break;
}
Some(v) => {
let cur = nums[3];
nums[3] += 1;
if cur == nums[0] {
nums[0] += nums[2];
out = Some(v);
break;
}
}
}
}
out
}
ItKind::ZipLongest => {
let mut row = Vec::with_capacity(sources.len());
let mut any = false;
for s in &sources {
match iter_step(s)? {
Some(v) => {
any = true;
row.push(v);
}
None => row.push(func.clone()),
}
}
if any {
Some(with_host(|h| h.new_tuple(row)))
} else {
finished = true;
None
}
}
ItKind::Pairwise => {
if buf.is_empty() {
match iter_step(&sources[0])? {
Some(v) => buf.push(v),
None => {
finished = true;
}
}
}
if finished {
None
} else {
match iter_step(&sources[0])? {
Some(v) => {
let prev = buf[0].clone();
buf = vec![v.clone()];
Some(with_host(|h| h.new_tuple(vec![prev, v])))
}
None => {
finished = true;
None
}
}
}
}
};
with_host(|h| {
if let Some(PyObj::ItertoolsIter {
nums: n,
buf: b,
flag: f,
done: d,
..
}) = h.get_mut(it)
{
*n = nums;
*b = buf;
*f = flag;
if finished {
*d = true;
}
}
});
Ok(result)
}
pub fn iter_step(it: &Value) -> Result<Option<Value>, String> {
enum Step {
Generator,
Zip,
Map,
Filter,
Enumerate,
CallIter,
Itertools,
#[cfg(feature = "stdlib-ffi")]
Foreign(u32),
Plain,
}
let step = with_host(|h| match h.get(it) {
Some(PyObj::Generator { .. }) => Step::Generator,
Some(PyObj::Zip { .. }) => Step::Zip,
Some(PyObj::MapObj { .. }) => Step::Map,
Some(PyObj::FilterObj { .. }) => Step::Filter,
Some(PyObj::EnumerateObj { .. }) => Step::Enumerate,
Some(PyObj::CallIter { .. }) => Step::CallIter,
Some(PyObj::CsvReader { .. }) => Step::Plain,
Some(PyObj::ItertoolsIter { .. }) => Step::Itertools,
#[cfg(feature = "stdlib-ffi")]
Some(PyObj::Foreign(id)) => Step::Foreign(*id),
_ => Step::Plain,
});
match step {
Step::Generator => gen_resume(it, Value::Undef),
Step::Zip => zip_step(it),
Step::Map => map_step(it),
Step::Filter => filter_step(it),
Step::Enumerate => enumerate_step(it),
Step::CallIter => calliter_step(it),
Step::Itertools => itertools_step(it),
#[cfg(feature = "stdlib-ffi")]
Step::Foreign(id) => crate::ffi::iter_next_cb(id),
Step::Plain => with_host(|h| h.iter_next(it)),
}
}
fn calliter_step(it: &Value) -> Result<Option<Value>, String> {
let (func, sentinel, done) = match with_host(|h| h.get(it).cloned()) {
Some(PyObj::CallIter {
func,
sentinel,
done,
}) => (func, sentinel, done),
_ => return Err(type_error("not an iterator")),
};
if done {
return Ok(None);
}
let v = invoke(&func, vec![], vec![])?;
if with_host(|h| h.equal(&v, &sentinel)) {
with_host(|h| {
if let Some(PyObj::CallIter { done, .. }) = h.get_mut(it) {
*done = true;
}
});
return Ok(None);
}
Ok(Some(v))
}
fn zip_step(it: &Value) -> Result<Option<Value>, String> {
let (sources, strict, done) = match with_host(|h| h.get(it).cloned()) {
Some(PyObj::Zip {
sources,
strict,
done,
}) => (sources, strict, done),
_ => return Err(type_error("not an iterator")),
};
if done {
return Ok(None);
}
if sources.is_empty() {
set_zip_done(it);
return Ok(None);
}
let mut out: Vec<Value> = Vec::with_capacity(sources.len());
for (i, s) in sources.iter().enumerate() {
match iter_step(s)? {
Some(v) => out.push(v),
None => {
set_zip_done(it);
if strict {
if let Some(e) = zip_strict_error(&sources, i) {
return Err(e);
}
}
return Ok(None);
}
}
}
Ok(Some(with_host(|h| h.new_tuple(out))))
}
fn set_zip_done(it: &Value) {
with_host(|h| {
if let Some(PyObj::Zip { done, .. }) = h.get_mut(it) {
*done = true;
}
});
}
fn zip_strict_error(sources: &[Value], i: usize) -> Option<String> {
if i > 0 {
let than = if i == 1 {
"argument 1".to_string()
} else {
format!("arguments 1-{i}")
};
return Some(format!(
"ValueError: zip() argument {} is shorter than {than}",
i + 1
));
}
for (j, s) in sources.iter().enumerate().skip(1) {
if let Ok(Some(_)) = iter_step(s) {
let than = if j == 1 {
"argument 1".to_string()
} else {
format!("arguments 1-{j}")
};
return Some(format!(
"ValueError: zip() argument {} is longer than {than}",
j + 1
));
}
}
None
}
fn map_step(it: &Value) -> Result<Option<Value>, String> {
let (func, sources, done) = match with_host(|h| h.get(it).cloned()) {
Some(PyObj::MapObj {
func,
sources,
done,
}) => (func, sources, done),
_ => return Err(type_error("not an iterator")),
};
if done {
return Ok(None);
}
let mut call_args: Vec<Value> = Vec::with_capacity(sources.len());
for s in &sources {
match iter_step(s)? {
Some(v) => call_args.push(v),
None => {
with_host(|h| {
if let Some(PyObj::MapObj { done, .. }) = h.get_mut(it) {
*done = true;
}
});
return Ok(None);
}
}
}
Ok(Some(invoke(&func, call_args, vec![])?))
}
fn filter_step(it: &Value) -> Result<Option<Value>, String> {
let (func, source, done) = match with_host(|h| h.get(it).cloned()) {
Some(PyObj::FilterObj { func, source, done }) => (func, source, done),
_ => return Err(type_error("not an iterator")),
};
if done {
return Ok(None);
}
loop {
match iter_step(&source)? {
Some(v) => {
let keep = if matches!(func, Value::Undef) {
with_host(|h| h.truthy(&v))
} else {
let r = invoke(&func, vec![v.clone()], vec![])?;
with_host(|h| h.truthy(&r))
};
if keep {
return Ok(Some(v));
}
}
None => {
with_host(|h| {
if let Some(PyObj::FilterObj { done, .. }) = h.get_mut(it) {
*done = true;
}
});
return Ok(None);
}
}
}
}
fn enumerate_step(it: &Value) -> Result<Option<Value>, String> {
let (source, idx, done) = match with_host(|h| h.get(it).cloned()) {
Some(PyObj::EnumerateObj { source, next, done }) => (source, next, done),
_ => return Err(type_error("not an iterator")),
};
if done {
return Ok(None);
}
match iter_step(&source)? {
Some(v) => {
with_host(|h| {
if let Some(PyObj::EnumerateObj { next, .. }) = h.get_mut(it) {
*next = idx + 1;
}
});
Ok(Some(with_host(|h| h.new_tuple(vec![Value::Int(idx), v]))))
}
None => {
with_host(|h| {
if let Some(PyObj::EnumerateObj { done, .. }) = h.get_mut(it) {
*done = true;
}
});
Ok(None)
}
}
}
thread_local! {
static IMPORTING: std::cell::RefCell<Vec<String>> = const { std::cell::RefCell::new(Vec::new()) };
}
fn import_in_progress(name: &str) -> bool {
IMPORTING.with(|s| s.borrow().iter().any(|n| n == name))
}
pub fn import_module(name: &str) -> Result<Value, String> {
if let Some(m) = with_host(|h| h.cached_module(name)) {
return Ok(m);
}
if let Some((parent, _)) = name.rsplit_once('.') {
if with_host(|h| h.cached_module(parent)).is_none() && !import_in_progress(parent) {
let _ = import_module(parent);
if let Some(m) = with_host(|h| h.cached_module(name)) {
return Ok(m);
}
}
}
IMPORTING.with(|s| s.borrow_mut().push(name.to_string()));
let module = import_module_inner(name);
IMPORTING.with(|s| {
let mut b = s.borrow_mut();
if let Some(p) = b.iter().rposition(|n| n == name) {
b.remove(p);
}
});
let module = module?;
with_host(|h| h.cache_module(name, module.clone()));
if let Some((parent, leaf)) = name.rsplit_once('.') {
if let Some(pmod) = with_host(|h| h.cached_module(parent)) {
with_host(|h| h.set_module_attr(&pmod, leaf, module.clone()));
}
}
Ok(module)
}
pub fn import_relative(level: usize, modpart: &str, name: &str) -> Result<Value, String> {
let pkg = with_host(|h| h.current_package());
let anchor = resolve_relative_anchor(&pkg, level)?;
let base = match (anchor.is_empty(), modpart.is_empty()) {
(_, true) => anchor.clone(),
(true, false) => modpart.to_string(),
(false, false) => format!("{anchor}.{modpart}"),
};
if name == "*" {
return import_module(&base);
}
if modpart.is_empty() {
let sub = if base.is_empty() {
name.to_string()
} else {
format!("{base}.{name}")
};
match import_module(&sub) {
Ok(m) => {
if let Some(base_mod) = with_host(|h| h.cached_module(&base)) {
with_host(|h| h.set_module_attr(&base_mod, name, m.clone()));
}
return Ok(m);
}
Err(e) => {
let missing_sub = e.contains("No module named")
&& (e.contains(&format!("'{sub}'")) || e.contains(&format!("'{name}'")));
if !missing_sub {
return Err(e);
}
let base_mod = import_module(&base)?;
return with_host(|h| h.get_attr(&base_mod, name));
}
}
}
let base_mod = import_module(&base)?;
with_host(|h| h.get_attr(&base_mod, name))
}
fn resolve_relative_anchor(pkg: &str, level: usize) -> Result<String, String> {
if level == 0 {
return Ok(pkg.to_string());
}
let mut bits: Vec<&str> = if pkg.is_empty() {
Vec::new()
} else {
pkg.split('.').collect()
};
let strip = level - 1;
if strip > bits.len() {
return Err(
"ImportError: attempted relative import beyond top-level package".to_string(),
);
}
bits.truncate(bits.len() - strip);
Ok(bits.join("."))
}
fn struct_seq(h: &mut PyHost, type_name: &str, fields: Vec<(&str, Value)>) -> Value {
let names: Vec<String> = fields.iter().map(|(k, _)| k.to_string()).collect();
let vals: Vec<Value> = fields.into_iter().map(|(_, v)| v).collect();
let t = h.new_tuple(vals);
if let Value::Obj(i) = t {
h.nt_meta.insert(
i,
NtMeta {
type_name: type_name.to_string(),
fields: names,
},
);
}
t
}
fn import_module_inner(name: &str) -> Result<Value, String> {
if name == "collections.abc" {
return import_module("_collections_abc");
}
#[cfg(not(feature = "stdlib-ffi"))]
if name == "_thread" {
let src = crate::stdlib::pythread::module_source();
return run_vendored_module("_thread", src, std::path::Path::new("<_thread>"));
}
#[cfg(not(feature = "stdlib-ffi"))]
if name == "_ast" {
let src = crate::stdlib::pyast::module_source();
return run_vendored_module("_ast", &src, std::path::Path::new("<_ast>"));
}
#[cfg(not(feature = "stdlib-ffi"))]
let stdlib_entries: Option<Vec<(String, Value)>> = match name {
"_struct" => Some(with_host(crate::stdlib::pystruct::entries)),
"binascii" => Some(with_host(crate::stdlib::binascii::entries)),
"_codecs" => Some(with_host(crate::stdlib::codecs::entries)),
"_io" => Some(with_host(crate::stdlib::pyio::entries)),
"_tokenize" => Some(with_host(crate::stdlib::pytokenize::entries)),
"_opcode" => Some(with_host(crate::stdlib::pyopcode::entries)),
"_imp" => Some(with_host(crate::stdlib::pyimp::entries)),
"_signal" => Some(with_host(crate::stdlib::pysignal::entries)),
"_csv" => Some(with_host(crate::stdlib::pycsv::entries)),
"_md5" | "_sha1" | "_sha2" | "_sha3" | "_blake2" => {
with_host(|h| crate::stdlib::pyhash::entries(h, name))
}
"marshal" => Some(with_host(crate::stdlib::pyimp::marshal_entries)),
_ => None,
};
#[cfg(feature = "stdlib-ffi")]
let stdlib_entries: Option<Vec<(String, Value)>> = None;
if let Some(entries) = stdlib_entries {
return Ok(with_host(|h| {
let mut ns = IndexMap::new();
for (k, v) in entries {
ns.insert(k, v);
}
let slot = h.new_module_slot(ns);
h.alloc(PyObj::Module {
name: name.to_string(),
slot,
})
}));
}
let entries: Vec<(&str, Value)> = match name {
#[cfg(not(feature = "stdlib-ffi"))]
"builtins" => with_host(|h| {
let mut v: Vec<(&str, Value)> = Vec::new();
for n in crate::builtins::builtin_names() {
v.push((n, h.alloc(PyObj::Builtin(n.to_string()))));
}
v.push(("None", Value::Undef));
v.push(("True", Value::Bool(true)));
v.push(("False", Value::Bool(false)));
v.push(("NotImplemented", h.alloc(PyObj::NotImplemented)));
v.push(("Ellipsis", h.alloc(PyObj::Ellipsis)));
v.push(("__debug__", Value::Bool(true)));
v
}),
"copy" => with_host(|h| {
vec![
("copy", h.alloc(PyObj::Builtin("copy.copy".into()))),
("deepcopy", h.alloc(PyObj::Builtin("copy.deepcopy".into()))),
]
}),
#[cfg(feature = "stdlib-ffi")]
"functools" => with_host(|h| {
vec![
(
"total_ordering",
h.alloc(PyObj::Builtin("functools.total_ordering".into())),
),
(
"cached_property",
h.alloc(PyObj::Builtin("functools.cached_property".into())),
),
]
}),
#[cfg(feature = "stdlib-ffi")]
"contextlib" => with_host(|h| {
vec![
(
"redirect_stdout",
h.alloc(PyObj::Builtin("contextlib.redirect_stdout".into())),
),
(
"redirect_stderr",
h.alloc(PyObj::Builtin("contextlib.redirect_stderr".into())),
),
]
}),
"_random" => with_host(|h| {
if !h.classes.contains_key("_random.Random") {
let mut ns: IndexMap<String, Value> = IndexMap::new();
for m in ["random", "seed", "getrandbits", "getstate", "setstate"] {
ns.insert(
m.to_string(),
h.alloc(PyObj::Builtin(format!("_random.Random.{m}"))),
);
}
h.register_class_meta("_random.Random", vec![], ns, "type");
}
let cls = h.alloc(PyObj::Class("_random.Random".to_string()));
vec![("Random", cls)]
}),
"posix" => with_host(|h| {
const FNS: &[&str] = &[
"getcwd", "getcwdb", "chdir", "listdir", "scandir", "stat", "lstat", "fstat",
"mkdir", "makedirs", "rmdir", "remove", "unlink", "rename", "replace", "getpid",
"getppid", "getuid", "geteuid", "getgid", "getegid", "urandom", "umask", "system",
"strerror", "getenv", "putenv", "unsetenv", "access", "fspath", "_exit", "abort",
"getpgrp", "cpu_count", "device_encoding", "get_terminal_size", "isatty", "pipe",
"dup", "close", "read", "write", "open", "lseek", "fsync", "kill", "waitpid",
"_create_environ", "readlink", "symlink", "link", "chmod", "utime", "truncate",
"sync", "get_inheritable", "set_inheritable", "ftruncate", "sched_yield",
];
let mut v: Vec<(&str, Value)> = FNS
.iter()
.map(|f| (*f, h.alloc(PyObj::Builtin(format!("posix.{f}")))))
.collect();
let consts: &[(&str, i64)] = &[
("F_OK", libc::F_OK as i64),
("R_OK", libc::R_OK as i64),
("W_OK", libc::W_OK as i64),
("X_OK", libc::X_OK as i64),
("SEEK_SET", libc::SEEK_SET as i64),
("SEEK_CUR", libc::SEEK_CUR as i64),
("SEEK_END", libc::SEEK_END as i64),
("O_RDONLY", libc::O_RDONLY as i64),
("O_WRONLY", libc::O_WRONLY as i64),
("O_RDWR", libc::O_RDWR as i64),
("O_APPEND", libc::O_APPEND as i64),
("O_CREAT", libc::O_CREAT as i64),
("O_EXCL", libc::O_EXCL as i64),
("O_TRUNC", libc::O_TRUNC as i64),
("O_NONBLOCK", libc::O_NONBLOCK as i64),
("O_CLOEXEC", libc::O_CLOEXEC as i64),
("WNOHANG", libc::WNOHANG as i64),
("EX_OK", 0),
];
for (k, val) in consts {
v.push((k, Value::Int(*val)));
}
let environ = {
let mut d: IndexMap<PKey, (Value, Value)> = IndexMap::new();
for (k, val) in std::env::vars() {
let kb = k.clone().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))
};
let stat_result = h.alloc(PyObj::Builtin("os.stat_result".into()));
v.push(("stat_result", stat_result));
v.push(("environ", environ));
let have = h.new_list(vec![]);
v.push(("_have_functions", have));
v
}),
"_thread_core" => with_host(|h| {
vec![
(
"allocate_lock",
h.alloc(PyObj::Builtin("_thread_core.allocate_lock".into())),
),
("RLock", h.alloc(PyObj::Builtin("_thread_core.RLock".into()))),
(
"get_ident",
h.alloc(PyObj::Builtin("_thread_core.get_ident".into())),
),
(
"get_native_id",
h.alloc(PyObj::Builtin("_thread_core.get_native_id".into())),
),
(
"start_new_thread",
h.alloc(PyObj::Builtin("_thread_core.start_new_thread".into())),
),
("error", h.alloc(PyObj::Builtin("RuntimeError".into()))),
("TIMEOUT_MAX", Value::Float(f64::MAX)),
]
}),
"itertools" => with_host(|h| {
const FNS: &[&str] = &[
"count",
"repeat",
"cycle",
"chain",
"accumulate",
"starmap",
"compress",
"dropwhile",
"takewhile",
"filterfalse",
"islice",
"zip_longest",
"pairwise",
"product",
"permutations",
"combinations",
"combinations_with_replacement",
"tee",
"groupby",
];
FNS.iter()
.map(|f| (*f, h.alloc(PyObj::Builtin(format!("itertools.{f}")))))
.collect()
}),
"_string" => with_host(|h| {
vec![
(
"formatter_parser",
h.alloc(PyObj::Builtin("_string.formatter_parser".into())),
),
(
"formatter_field_name_split",
h.alloc(PyObj::Builtin("_string.formatter_field_name_split".into())),
),
]
}),
"_typing" => with_host(|h| {
let no_default = h.alloc(PyObj::Builtin("_typing.NoDefault".into()));
vec![
("_idfunc", h.alloc(PyObj::Builtin("_typing._idfunc".into()))),
("TypeVar", h.alloc(PyObj::Builtin("_typing.TypeVar".into()))),
("ParamSpec", h.alloc(PyObj::Builtin("_typing.ParamSpec".into()))),
(
"TypeVarTuple",
h.alloc(PyObj::Builtin("_typing.TypeVarTuple".into())),
),
(
"ParamSpecArgs",
h.alloc(PyObj::Builtin("_typing.ParamSpecArgs".into())),
),
(
"ParamSpecKwargs",
h.alloc(PyObj::Builtin("_typing.ParamSpecKwargs".into())),
),
(
"TypeAliasType",
h.alloc(PyObj::Builtin("_typing.TypeAliasType".into())),
),
("Generic", h.alloc(PyObj::Builtin("Generic".into()))),
("Union", h.alloc(PyObj::Builtin("_typing.Union".into()))),
("NoDefault", no_default),
]
}),
"atexit" => with_host(|h| {
vec![
("register", h.alloc(PyObj::Builtin("atexit.register".into()))),
(
"unregister",
h.alloc(PyObj::Builtin("atexit.unregister".into())),
),
(
"_run_exitfuncs",
h.alloc(PyObj::Builtin("atexit._run_exitfuncs".into())),
),
("_clear", h.alloc(PyObj::Builtin("atexit._clear".into()))),
(
"_ncallbacks",
h.alloc(PyObj::Builtin("atexit._ncallbacks".into())),
),
]
}),
"time" => with_host(|h| {
const FNS: &[&str] = &[
"time", "time_ns", "monotonic", "monotonic_ns", "perf_counter",
"perf_counter_ns", "process_time", "process_time_ns", "sleep",
"gmtime", "localtime", "mktime", "strftime", "struct_time", "asctime",
"ctime",
];
let mut out: Vec<(&str, Value)> =
FNS.iter().map(|f| (*f, h.alloc(PyObj::Builtin(format!("time.{f}"))))).collect();
let (tz, alt, daylight, name_std, name_dst) = crate::builtins::tz_info();
out.push(("timezone", Value::Int(tz)));
out.push(("altzone", Value::Int(alt)));
out.push(("daylight", Value::Int(daylight)));
let n0 = h.new_str(name_std);
let n1 = h.new_str(name_dst);
let tzname = h.new_tuple(vec![n0, n1]);
out.push(("tzname", tzname));
out
}),
"re" => with_host(|h| {
const FNS: &[&str] = &[
"compile", "match", "search", "fullmatch", "findall", "finditer",
"sub", "subn", "split", "escape", "purge",
];
let mut out: Vec<(&str, Value)> =
FNS.iter().map(|f| (*f, h.alloc(PyObj::Builtin(format!("re.{f}"))))).collect();
for (name, bit) in [
("IGNORECASE", 2i64), ("I", 2),
("LOCALE", 4), ("L", 4),
("MULTILINE", 8), ("M", 8),
("DOTALL", 16), ("S", 16),
("UNICODE", 32), ("U", 32),
("VERBOSE", 64), ("X", 64),
("ASCII", 256), ("A", 256),
("NOFLAG", 0),
] {
out.push((name, Value::Int(bit)));
}
out.push(("error", h.alloc(PyObj::Builtin("re.error".into()))));
out.push(("Pattern", h.alloc(PyObj::Builtin("re.Pattern".into()))));
out.push(("Match", h.alloc(PyObj::Builtin("re.Match".into()))));
out
}),
"errno" => with_host(|h| {
let names: &[(&str, i32)] = &[
("EPERM", libc::EPERM),
("ENOENT", libc::ENOENT),
("ESRCH", libc::ESRCH),
("EINTR", libc::EINTR),
("EIO", libc::EIO),
("ENXIO", libc::ENXIO),
("E2BIG", libc::E2BIG),
("ENOEXEC", libc::ENOEXEC),
("EBADF", libc::EBADF),
("ECHILD", libc::ECHILD),
("EAGAIN", libc::EAGAIN),
("ENOMEM", libc::ENOMEM),
("EACCES", libc::EACCES),
("EFAULT", libc::EFAULT),
("EBUSY", libc::EBUSY),
("EEXIST", libc::EEXIST),
("EXDEV", libc::EXDEV),
("ENODEV", libc::ENODEV),
("ENOTDIR", libc::ENOTDIR),
("EISDIR", libc::EISDIR),
("EINVAL", libc::EINVAL),
("ENFILE", libc::ENFILE),
("EMFILE", libc::EMFILE),
("ENOTTY", libc::ENOTTY),
("EFBIG", libc::EFBIG),
("ENOSPC", libc::ENOSPC),
("ESPIPE", libc::ESPIPE),
("EROFS", libc::EROFS),
("EMLINK", libc::EMLINK),
("EPIPE", libc::EPIPE),
("EDOM", libc::EDOM),
("ERANGE", libc::ERANGE),
("EDEADLK", libc::EDEADLK),
("ENAMETOOLONG", libc::ENAMETOOLONG),
("ENOLCK", libc::ENOLCK),
("ENOSYS", libc::ENOSYS),
("ENOTEMPTY", libc::ENOTEMPTY),
("ELOOP", libc::ELOOP),
("EWOULDBLOCK", libc::EWOULDBLOCK),
("ENOMSG", libc::ENOMSG),
("EIDRM", libc::EIDRM),
("EOVERFLOW", libc::EOVERFLOW),
("EBADMSG", libc::EBADMSG),
("EILSEQ", libc::EILSEQ),
("ENOTSOCK", libc::ENOTSOCK),
("EDESTADDRREQ", libc::EDESTADDRREQ),
("EMSGSIZE", libc::EMSGSIZE),
("EPROTOTYPE", libc::EPROTOTYPE),
("ENOPROTOOPT", libc::ENOPROTOOPT),
("EPROTONOSUPPORT", libc::EPROTONOSUPPORT),
("EOPNOTSUPP", libc::EOPNOTSUPP),
("EAFNOSUPPORT", libc::EAFNOSUPPORT),
("EADDRINUSE", libc::EADDRINUSE),
("EADDRNOTAVAIL", libc::EADDRNOTAVAIL),
("ENETDOWN", libc::ENETDOWN),
("ENETUNREACH", libc::ENETUNREACH),
("ECONNABORTED", libc::ECONNABORTED),
("ECONNRESET", libc::ECONNRESET),
("ENOBUFS", libc::ENOBUFS),
("EISCONN", libc::EISCONN),
("ENOTCONN", libc::ENOTCONN),
("ETIMEDOUT", libc::ETIMEDOUT),
("ECONNREFUSED", libc::ECONNREFUSED),
("EHOSTUNREACH", libc::EHOSTUNREACH),
("EALREADY", libc::EALREADY),
("EINPROGRESS", libc::EINPROGRESS),
("ECANCELED", libc::ECANCELED),
("ENOTSUP", libc::ENOTSUP),
];
let mut v: Vec<(&str, Value)> = Vec::with_capacity(names.len() + 1);
let mut ec: IndexMap<PKey, (Value, Value)> = IndexMap::new();
for (n, val) in names {
v.push((n, Value::Int(*val as i64)));
let key = PKey::Int(*val as i64);
if !ec.contains_key(&key) {
let nv = h.new_str((*n).to_string());
ec.insert(key, (Value::Int(*val as i64), nv));
}
}
let errorcode = h.alloc(PyObj::Dict(ec));
v.push(("errorcode", errorcode));
v
}),
"math" => with_host(|h| {
const MATH_FNS: &[&str] = &[
"sqrt", "floor", "ceil", "fabs", "pow", "log", "log2", "log10", "log1p",
"exp", "exp2", "expm1", "cbrt", "sin", "cos", "tan", "asin", "acos", "atan",
"atan2", "sinh", "cosh", "tanh", "asinh", "acosh", "atanh", "degrees",
"radians", "hypot", "trunc", "copysign", "fmod", "ldexp", "isqrt", "isnan",
"isinf", "isfinite", "gcd", "factorial", "comb", "perm", "fsum", "sumprod", "prod",
"lgamma", "gamma", "erf", "erfc", "isclose", "remainder",
];
let mut out: Vec<(&str, Value)> = vec![
("pi", Value::Float(std::f64::consts::PI)),
("e", Value::Float(std::f64::consts::E)),
("tau", Value::Float(std::f64::consts::TAU)),
("inf", Value::Float(f64::INFINITY)),
("nan", Value::Float(f64::NAN)),
];
for &f in MATH_FNS {
out.push((f, h.alloc(PyObj::Builtin(format!("math.{f}")))));
}
out
}),
"sys" => with_host(|h| {
let argv_strs = h.argv.clone();
let argv_items: Vec<Value> = argv_strs.into_iter().map(|s| h.new_str(s)).collect();
let argv = h.new_list(argv_items);
let stdout = h.alloc(PyObj::File { id: 0 });
let stderr = h.alloc(PyObj::File { id: 1 });
let stdin = h.alloc(PyObj::File { id: 2 });
let orig_stdout = h.alloc(PyObj::File { id: 0 });
let orig_stderr = h.alloc(PyObj::File { id: 1 });
let orig_stdin = h.alloc(PyObj::File { id: 2 });
let vi_vals = vec![
Value::Int(PY_MAJOR),
Value::Int(PY_MINOR),
Value::Int(PY_MICRO),
h.new_str("final"),
Value::Int(0),
];
let version_info = h.new_tuple(vi_vals);
if let Value::Obj(i) = version_info {
h.nt_meta.insert(
i,
NtMeta {
type_name: "sys.version_info".to_string(),
fields: ["major", "minor", "micro", "releaselevel", "serial"]
.iter()
.map(|s| s.to_string())
.collect(),
},
);
}
let path0 = match &h.main_file {
Some(f) => std::path::Path::new(f)
.parent()
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_default(),
None => String::new(),
};
let path0 = h.new_str(path0);
let path = h.new_list(vec![path0]);
let exe_path = std::env::current_exe()
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_default();
let executable = h.new_str(exe_path.clone());
let exec_path = h.new_str(exe_path.clone());
let prefix = h.new_str(
std::path::Path::new(&exe_path)
.parent()
.and_then(|p| p.parent())
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_default(),
);
let flags = {
let mut a: IndexMap<String, Value> = IndexMap::new();
for (k, v) in [
("debug", 0),
("inspect", 0),
("interactive", 0),
("optimize", 0),
("dont_write_bytecode", 0),
("no_user_site", 0),
("no_site", 0),
("ignore_environment", 0),
("verbose", 0),
("bytes_warning", 0),
("quiet", 0),
("hash_randomization", 0),
("isolated", 0),
("dev_mode", 0),
("utf8_mode", 0),
("safe_path", 0),
("int_max_str_digits", -1),
("warn_default_encoding", 0),
("gil", 1),
("thread_inherit_context", 1),
("context_aware_warnings", 1),
] {
a.insert(k.to_string(), Value::Int(v));
}
h.alloc(PyObj::Namespace { attrs: a })
};
let modules = h.new_dict(IndexMap::new());
h.sys_modules = Some(modules.clone());
let cached: Vec<(String, Value)> =
h.modules.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
for (k, v) in cached {
let kv = h.new_str(k.clone());
if let Some(PyObj::Dict(d)) = h.get_mut(&modules) {
d.insert(PKey::Str(k), (kv, v));
}
}
let version = h.new_str(format!("{PY_MAJOR}.{PY_MINOR}.{PY_MICRO} (pythonrs)"));
let platform = h.new_str(py_platform());
let implementation = {
let mut a: IndexMap<String, Value> = IndexMap::new();
let name = h.new_str("pythonrs");
let cache_tag = h.new_str(format!("pythonrs-{PY_MAJOR}{PY_MINOR}"));
let hexversion = ((PY_MAJOR) << 24) | ((PY_MINOR) << 16) | ((PY_MICRO) << 8) | 0xf0;
a.insert("name".to_string(), name);
a.insert("cache_tag".to_string(), cache_tag);
a.insert("version".to_string(), version_info.clone());
a.insert("hexversion".to_string(), Value::Int(hexversion));
h.alloc(PyObj::Namespace { attrs: a })
};
let hash_info = {
let algorithm = h.new_str("siphash13".to_string());
struct_seq(
h,
"sys.hash_info",
vec![
("width", Value::Int(64)),
("modulus", Value::Int(2_305_843_009_213_693_951)),
("inf", Value::Int(314_159)),
("nan", Value::Int(0)),
("imag", Value::Int(1_000_003)),
("algorithm", algorithm),
("hash_bits", Value::Int(64)),
("seed_bits", Value::Int(128)),
("cutoff", Value::Int(0)),
],
)
};
let float_info = struct_seq(
h,
"sys.float_info",
vec![
("max", Value::Float(f64::MAX)),
("max_exp", Value::Int(f64::MAX_EXP as i64)),
("max_10_exp", Value::Int(f64::MAX_10_EXP as i64)),
("min", Value::Float(f64::MIN_POSITIVE)),
("min_exp", Value::Int(f64::MIN_EXP as i64)),
("min_10_exp", Value::Int(f64::MIN_10_EXP as i64)),
("dig", Value::Int(f64::DIGITS as i64)),
("mant_dig", Value::Int(f64::MANTISSA_DIGITS as i64)),
("epsilon", Value::Float(f64::EPSILON)),
("radix", Value::Int(f64::RADIX as i64)),
("rounds", Value::Int(1)),
],
);
let int_info = struct_seq(
h,
"sys.int_info",
vec![
("bits_per_digit", Value::Int(30)),
("sizeof_digit", Value::Int(4)),
("default_max_str_digits", Value::Int(4300)),
("str_digits_check_threshold", Value::Int(640)),
],
);
let builtin_module_names = {
let names = crate::stdlib::pyimp::BUILTIN_MODULES;
let vals: Vec<Value> = names.iter().map(|n| h.new_str(*n)).collect();
h.new_tuple(vals)
};
vec![
("argv", argv),
("maxsize", Value::Int(i64::MAX)),
("version", version),
("version_info", version_info),
("implementation", implementation),
("builtin_module_names", builtin_module_names),
("platform", platform),
("path", path),
("modules", modules),
("executable", executable),
("stdout", stdout),
("stderr", stderr),
("stdin", stdin),
("__stdout__", orig_stdout),
("__stderr__", orig_stderr),
("__stdin__", orig_stdin),
("exit", h.alloc(PyObj::Builtin("sys.exit".into()))),
(
"getrecursionlimit",
h.alloc(PyObj::Builtin("sys.getrecursionlimit".into())),
),
(
"setrecursionlimit",
h.alloc(PyObj::Builtin("sys.setrecursionlimit".into())),
),
("_getframe", h.alloc(PyObj::Builtin("sys._getframe".into()))),
(
"getfilesystemencoding",
h.alloc(PyObj::Builtin("sys.getfilesystemencoding".into())),
),
(
"getfilesystemencodeerrors",
h.alloc(PyObj::Builtin("sys.getfilesystemencodeerrors".into())),
),
(
"getdefaultencoding",
h.alloc(PyObj::Builtin("sys.getdefaultencoding".into())),
),
("intern", h.alloc(PyObj::Builtin("sys.intern".into()))),
("excepthook", h.alloc(PyObj::Builtin("sys.excepthook".into()))),
(
"__excepthook__",
h.alloc(PyObj::Builtin("sys.excepthook".into())),
),
(
"unraisablehook",
h.alloc(PyObj::Builtin("sys.unraisablehook".into())),
),
("exc_info", h.alloc(PyObj::Builtin("sys.exc_info".into()))),
("exception", h.alloc(PyObj::Builtin("sys.exception".into()))),
("audit", h.alloc(PyObj::Builtin("sys.audit".into()))),
("is_finalizing", h.alloc(PyObj::Builtin("sys.is_finalizing".into()))),
("prefix", prefix.clone()),
("base_prefix", prefix.clone()),
("exec_prefix", prefix.clone()),
("base_exec_prefix", prefix),
("dont_write_bytecode", Value::Bool(false)),
(
"byteorder",
h.new_str(
if cfg!(target_endian = "big") { "big" } else { "little" }.to_string(),
),
),
("flags", flags),
("warnoptions", h.new_list(Vec::new())),
("hexversion", Value::Int(0x030E_00F0)),
("hash_info", hash_info),
("float_info", float_info),
("int_info", int_info),
("api_version", Value::Int(1013)),
("_base_executable", exec_path),
]
}),
"asyncio" => with_host(|h| {
vec![
("run", h.alloc(PyObj::Builtin("asyncio.run".into()))),
("sleep", h.alloc(PyObj::Builtin("asyncio.sleep".into()))),
("gather", h.alloc(PyObj::Builtin("asyncio.gather".into()))),
(
"create_task",
h.alloc(PyObj::Builtin("asyncio.create_task".into())),
),
(
"ensure_future",
h.alloc(PyObj::Builtin("asyncio.ensure_future".into())),
),
(
"wait_for",
h.alloc(PyObj::Builtin("asyncio.wait_for".into())),
),
("wait", h.alloc(PyObj::Builtin("asyncio.wait".into()))),
(
"as_completed",
h.alloc(PyObj::Builtin("asyncio.as_completed".into())),
),
("Event", h.alloc(PyObj::Builtin("asyncio.Event".into()))),
("Lock", h.alloc(PyObj::Builtin("asyncio.Lock".into()))),
("Queue", h.alloc(PyObj::Builtin("asyncio.Queue".into()))),
(
"get_event_loop",
h.alloc(PyObj::Builtin("asyncio.get_event_loop".into())),
),
(
"get_running_loop",
h.alloc(PyObj::Builtin("asyncio.get_running_loop".into())),
),
(
"new_event_loop",
h.alloc(PyObj::Builtin("asyncio.new_event_loop".into())),
),
("Future", h.alloc(PyObj::Builtin("asyncio.Future".into()))),
(
"TimeoutError",
h.alloc(PyObj::Builtin("TimeoutError".into())),
),
(
"CancelledError",
h.alloc(PyObj::Builtin("CancelledError".into())),
),
(
"InvalidStateError",
h.alloc(PyObj::Builtin("InvalidStateError".into())),
),
("QueueEmpty", h.alloc(PyObj::Builtin("QueueEmpty".into()))),
("QueueFull", h.alloc(PyObj::Builtin("QueueFull".into()))),
("FIRST_COMPLETED", h.new_str("FIRST_COMPLETED".to_string())),
("FIRST_EXCEPTION", h.new_str("FIRST_EXCEPTION".to_string())),
("ALL_COMPLETED", h.new_str("ALL_COMPLETED".to_string())),
]
}),
"_contextvars" => with_host(|h| {
vec![
("ContextVar", h.alloc(PyObj::Builtin("_contextvars.ContextVar".into()))),
("Token", h.alloc(PyObj::Builtin("_contextvars.Token".into()))),
("Context", h.alloc(PyObj::Builtin("_contextvars.Context".into()))),
(
"copy_context",
h.alloc(PyObj::Builtin("_contextvars.copy_context".into())),
),
]
}),
"_collections" => with_host(|h| {
vec![
("deque", h.alloc(PyObj::Builtin("collections.deque".into()))),
(
"defaultdict",
h.alloc(PyObj::Builtin("collections.defaultdict".into())),
),
(
"OrderedDict",
h.alloc(PyObj::Builtin("collections.OrderedDict".into())),
),
]
}),
_ => {
#[cfg(not(feature = "stdlib-ffi"))]
{
return match try_import_vendored(name) {
Some(res) => res,
None => Err(format!("ModuleNotFoundError: No module named '{name}'")),
};
}
#[cfg(feature = "stdlib-ffi")]
{
let id = crate::ffi::import(name)?;
return Ok(with_host(|h| h.alloc(PyObj::Foreign(id))));
}
}
};
Ok(with_host(|h| {
let mut ns = IndexMap::new();
for (k, v) in entries {
ns.insert(k.to_string(), v);
}
let slot = h.new_module_slot(ns);
h.alloc(PyObj::Module {
name: name.to_string(),
slot,
})
}))
}
#[cfg(not(feature = "stdlib-ffi"))]
fn pylib_dir() -> Option<std::path::PathBuf> {
if let Some(p) = std::env::var_os("PYTHONRS_LIB") {
let p = std::path::PathBuf::from(p);
if p.is_dir() {
return Some(p);
}
}
if let Ok(exe) = std::env::current_exe() {
if let Some(dir) = exe.parent() {
for cand in [
dir.join("../lib/pythonrs/pylib"),
dir.join("../pylib"),
dir.join("pylib"),
] {
if cand.is_dir() {
return Some(cand);
}
}
}
}
let dev = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("pylib");
dev.is_dir().then_some(dev)
}
#[cfg(not(feature = "stdlib-ffi"))]
fn pip_dir() -> Option<std::path::PathBuf> {
let d = dirs::home_dir()?.join(".pythonrs").join("pip");
d.is_dir().then_some(d)
}
#[cfg(not(feature = "stdlib-ffi"))]
fn resolve_vendored_path(name: &str) -> Option<std::path::PathBuf> {
let rel = name.replace('.', "/");
for root in [pylib_dir(), pip_dir()].into_iter().flatten() {
let module_file = root.join(format!("{rel}.py"));
if module_file.is_file() {
return Some(module_file);
}
let package_init = root.join(&rel).join("__init__.py");
if package_init.is_file() {
return Some(package_init);
}
}
None
}
#[cfg(not(feature = "stdlib-ffi"))]
fn try_import_vendored(name: &str) -> Option<Result<Value, String>> {
let path = resolve_vendored_path(name)?;
let src = match std::fs::read_to_string(&path) {
Ok(s) => s,
Err(e) => return Some(Err(format!("ImportError: cannot read {}: {e}", path.display()))),
};
Some(run_vendored_module(name, &src, &path))
}
#[cfg(not(feature = "stdlib-ffi"))]
fn run_vendored_module(name: &str, src: &str, path: &std::path::Path) -> Result<Value, String> {
let parked = with_host(|h| h.enter_module_scope());
let (mid, saved_mod) = with_host(|h| {
let name_v = h.new_str(name);
let file_v = h.new_str(path.to_string_lossy());
let is_package = path.file_name().is_some_and(|f| f == "__init__.py");
let package = if is_package {
name.to_string()
} else {
match name.rsplit_once('.') {
Some((parent, _)) => parent.to_string(),
None => String::new(),
}
};
let package_v = h.new_str(&package);
let mut ns: IndexMap<String, Value> = IndexMap::new();
ns.insert("__name__".to_string(), name_v);
ns.insert("__file__".to_string(), file_v);
ns.insert("__package__".to_string(), package_v);
ns.insert("__doc__".to_string(), Value::Undef);
let mid = h.new_module_slot(ns);
let saved = h.swap_module(mid);
(mid, saved)
});
let module = with_host(|h| {
let m = h.alloc(PyObj::Module {
name: name.to_string(),
slot: mid,
});
h.cache_module(name, m.clone());
m
});
let run = (|| -> Result<(), String> {
let prog = crate::compile_or_load_labeled(src, &path.to_string_lossy())?;
let chunk = crate::load_merged(prog);
run_chunk_on(chunk)?;
Ok(())
})();
with_host(|h| {
h.swap_module(saved_mod);
h.restore_scope(parked);
if run.is_ok() {
} else {
h.uncache_module(name);
}
});
run?;
Ok(with_host(|h| h.sys_module_entry(name)).unwrap_or(module))
}
fn io_err(e: std::io::Error) -> String {
format!("OSError: {e}")
}
fn closed_err() -> String {
"ValueError: I/O operation on closed file.".into()
}
fn unsupported_read() -> String {
"io.UnsupportedOperation: not readable".into()
}
fn unsupported_write() -> String {
"io.UnsupportedOperation: not writable".into()
}
impl PyHost {
pub fn io_alloc_file(
&mut self,
file: std::fs::File,
path: String,
readable: bool,
writable: bool,
) -> Value {
let id = self.io_handles.len() as u32;
self.io_handles.push(IoCell::File {
file: Some(file),
path,
readable,
writable,
});
self.alloc(PyObj::File { id })
}
pub fn io_closed(&self, id: u32) -> bool {
matches!(
self.io_handles.get(id as usize),
Some(IoCell::File { file: None, .. })
)
}
fn file_repr(&self, id: u32) -> String {
match self.io_handles.get(id as usize) {
Some(IoCell::Stdout) => {
"<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>".into()
}
Some(IoCell::Stderr) => {
"<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>".into()
}
Some(IoCell::Stdin) => {
"<_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>".into()
}
Some(IoCell::File {
file,
path,
readable,
writable,
}) => {
let mode = match (readable, writable) {
(true, true) => "r+",
(false, true) => "w",
_ => "r",
};
let closed = if file.is_none() { " (closed)" } else { "" };
format!("<_io.TextIOWrapper name='{path}' mode='{mode}' encoding='utf-8'{closed}>")
}
None => "<_io.TextIOWrapper>".into(),
}
}
pub fn file_id(&self, v: &Value) -> Option<u32> {
match self.get(v) {
Some(PyObj::File { id }) => Some(*id),
_ => None,
}
}
pub fn io_write(&mut self, id: u32, s: &str) -> Result<Value, String> {
self.io_write_bytes(id, s.as_bytes())?;
Ok(Value::Int(s.chars().count() as i64))
}
pub fn io_write_bytes(&mut self, id: u32, bytes: &[u8]) -> Result<Value, String> {
use std::io::Write;
match self.io_handles.get_mut(id as usize) {
Some(IoCell::Stdout) => {
let mut o = std::io::stdout();
o.write_all(bytes).and_then(|_| o.flush()).map_err(io_err)?;
}
Some(IoCell::Stderr) => {
let mut o = std::io::stderr();
o.write_all(bytes).and_then(|_| o.flush()).map_err(io_err)?;
}
Some(IoCell::Stdin) => return Err(unsupported_write()),
Some(IoCell::File {
file: Some(f),
writable: true,
..
}) => {
f.write_all(bytes).and_then(|_| f.flush()).map_err(io_err)?;
}
Some(IoCell::File { file: Some(_), .. }) => return Err(unsupported_write()),
Some(IoCell::File { file: None, .. }) => return Err(closed_err()),
None => return Err(closed_err()),
}
Ok(Value::Int(bytes.len() as i64))
}
pub fn io_read_all(&mut self, id: u32) -> Result<String, String> {
use std::io::Read;
let mut s = String::new();
match self.io_handles.get_mut(id as usize) {
Some(IoCell::File {
file: Some(f),
readable: true,
..
}) => {
f.read_to_string(&mut s).map_err(io_err)?;
Ok(s)
}
Some(IoCell::File { file: Some(_), .. }) => Err(unsupported_read()),
Some(IoCell::File { file: None, .. }) => Err(closed_err()),
Some(IoCell::Stdin) => {
std::io::stdin().read_to_string(&mut s).map_err(io_err)?;
Ok(s)
}
_ => Err(unsupported_read()),
}
}
pub fn io_readline(&mut self, id: u32) -> Result<String, String> {
use std::io::Read;
let mut buf: Vec<u8> = Vec::new();
loop {
let mut one = [0u8; 1];
let n = match self.io_handles.get_mut(id as usize) {
Some(IoCell::File {
file: Some(f),
readable: true,
..
}) => f.read(&mut one),
Some(IoCell::File { file: Some(_), .. }) => return Err(unsupported_read()),
Some(IoCell::File { file: None, .. }) => return Err(closed_err()),
Some(IoCell::Stdin) => std::io::stdin().read(&mut one),
_ => return Err(unsupported_read()),
};
match n {
Ok(0) => break,
Ok(_) => {
buf.push(one[0]);
if one[0] == b'\n' {
break;
}
}
Err(e) => return Err(io_err(e)),
}
}
Ok(String::from_utf8_lossy(&buf).into_owned())
}
pub fn io_read_lines(&mut self, id: u32) -> Result<Vec<String>, String> {
let all = self.io_read_all(id)?;
Ok(all.split_inclusive('\n').map(|l| l.to_string()).collect())
}
pub fn io_close(&mut self, id: u32) {
if let Some(IoCell::File { file, .. }) = self.io_handles.get_mut(id as usize) {
*file = None;
}
}
pub fn io_flush(&mut self, id: u32) -> Result<(), String> {
use std::io::Write;
match self.io_handles.get_mut(id as usize) {
Some(IoCell::Stdout) => std::io::stdout().flush().map_err(io_err),
Some(IoCell::Stderr) => std::io::stderr().flush().map_err(io_err),
Some(IoCell::File { file: Some(f), .. }) => f.flush().map_err(io_err),
_ => Ok(()),
}
}
fn lru_new(&mut self, maxsize: Option<usize>) -> u32 {
let id = self.lru_caches.len() as u32;
self.lru_caches.push(LruData {
map: IndexMap::new(),
order: VecDeque::new(),
maxsize,
hits: 0,
misses: 0,
});
id
}
fn lru_lookup(&mut self, cache_id: u32, key: &PKey) -> Option<Value> {
let c = self.lru_caches.get_mut(cache_id as usize)?;
if let Some(v) = c.map.get(key).cloned() {
c.hits += 1;
if let Some(pos) = c.order.iter().position(|k| k == key) {
if let Some(k) = c.order.remove(pos) {
c.order.push_back(k);
}
}
Some(v)
} else {
c.misses += 1;
None
}
}
fn lru_store(&mut self, cache_id: u32, key: PKey, val: Value) {
if let Some(c) = self.lru_caches.get_mut(cache_id as usize) {
if c.map.insert(key.clone(), val).is_none() {
c.order.push_back(key);
}
if let Some(max) = c.maxsize {
while c.map.len() > max {
match c.order.pop_front() {
Some(old) => {
c.map.shift_remove(&old);
}
None => break,
}
}
}
}
}
fn lru_info(&self, cache_id: u32) -> (u64, u64, Option<usize>, usize) {
match self.lru_caches.get(cache_id as usize) {
Some(c) => (c.hits, c.misses, c.maxsize, c.map.len()),
None => (0, 0, None, 0),
}
}
fn lru_clear(&mut self, cache_id: u32) {
if let Some(c) = self.lru_caches.get_mut(cache_id as usize) {
c.map.clear();
c.order.clear();
c.hits = 0;
c.misses = 0;
}
}
}
pub fn open_file(path: &str, mode: &str) -> Result<Value, String> {
use std::fs::OpenOptions;
let m: String = mode.chars().filter(|c| *c != 'b' && *c != 't').collect();
let base = m.chars().next().unwrap_or('r');
let plus = m.contains('+');
let mut opts = OpenOptions::new();
let (readable, writable) = match base {
'r' => {
opts.read(true);
if plus {
opts.write(true);
}
(true, plus)
}
'w' => {
opts.write(true).create(true).truncate(true);
if plus {
opts.read(true);
}
(plus, true)
}
'a' => {
opts.append(true).create(true);
if plus {
opts.read(true);
}
(plus, true)
}
'x' => {
opts.write(true).create_new(true);
if plus {
opts.read(true);
}
(plus, true)
}
_ => return Err(format!("ValueError: invalid mode: '{mode}'")),
};
let f = opts.open(path).map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => {
format!("FileNotFoundError: [Errno 2] No such file or directory: '{path}'")
}
std::io::ErrorKind::AlreadyExists => {
format!("FileExistsError: [Errno 17] File exists: '{path}'")
}
std::io::ErrorKind::PermissionDenied => {
format!("PermissionError: [Errno 13] Permission denied: '{path}'")
}
_ => format!("OSError: {e}: '{path}'"),
})?;
Ok(with_host(|h| {
h.io_alloc_file(f, path.to_string(), readable, writable)
}))
}
pub fn write_to_stream(target: &Value, s: &str) -> Result<(), String> {
if let Some(id) = with_host(|h| h.file_id(target)) {
with_host(|h| h.io_write(id, s))?;
return Ok(());
}
let sv = with_host(|h| h.new_str(s.to_string()));
call_method(target, "write", vec![sv], vec![])?;
Ok(())
}
pub fn write_stdout(s: &str) -> Result<(), String> {
match with_host(|h| h.stdout_target.clone()) {
Some(t) => write_to_stream(&t, s),
None => {
use std::io::Write;
let mut o = std::io::stdout();
let _ = o.write_all(s.as_bytes());
let _ = o.flush();
Ok(())
}
}
}
pub fn write_stderr(s: &str) -> Result<(), String> {
match with_host(|h| h.stderr_target.clone()) {
Some(t) => write_to_stream(&t, s),
None => {
use std::io::Write;
let mut o = std::io::stderr();
let _ = o.write_all(s.as_bytes());
let _ = o.flush();
Ok(())
}
}
}
pub fn alloc_deque(items: VecDeque<Value>, maxlen: Option<usize>) -> Value {
with_host(|h| h.alloc(PyObj::Deque { items, maxlen }))
}
pub fn alloc_dict_subtype(
pairs: IndexMap<PKey, (Value, Value)>,
kind: DictKind,
factory: Option<Value>,
) -> Value {
with_host(|h| {
let d = h.alloc(PyObj::Dict(pairs));
if let Value::Obj(i) = d {
h.dict_meta.insert(i, DictMeta { kind, factory });
}
d
})
}
pub fn dict_meta_of(v: &Value) -> Option<DictMeta> {
with_host(|h| match v {
Value::Obj(i) => h.dict_meta.get(i).cloned(),
_ => None,
})
}
pub fn make_namedtuple_type(name: &str, fields: Vec<String>) -> Value {
with_host(|h| {
h.alloc(PyObj::NamedTupleType {
type_name: name.to_string(),
fields,
})
})
}
pub fn namedtuple_construct(
type_name: &str,
fields: &[String],
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
if args.len() > fields.len() {
return Err(type_error(&format!(
"{type_name}() takes {} positional arguments but {} were given",
fields.len(),
args.len()
)));
}
let mut values: Vec<Option<Value>> = vec![None; fields.len()];
for (i, a) in args.into_iter().enumerate() {
values[i] = Some(a);
}
for (k, v) in kwargs {
match fields.iter().position(|f| *f == k) {
Some(i) => {
if values[i].is_some() {
return Err(type_error(&format!(
"{type_name}() got multiple values for argument '{k}'"
)));
}
values[i] = Some(v);
}
None => {
return Err(type_error(&format!(
"{type_name}() got an unexpected keyword argument '{k}'"
)))
}
}
}
let mut items = Vec::with_capacity(fields.len());
for (i, slot) in values.into_iter().enumerate() {
match slot {
Some(v) => items.push(v),
None => {
return Err(type_error(&format!(
"{type_name}() missing required argument: '{}'",
fields[i]
)))
}
}
}
Ok(with_host(|h| {
let tup = h.alloc(PyObj::Tuple(items));
if let Value::Obj(idx) = tup {
h.nt_meta.insert(
idx,
NtMeta {
type_name: type_name.to_string(),
fields: fields.to_vec(),
},
);
}
tup
}))
}
impl PyHost {
pub fn mark_total_ordering(&mut self, class: &str) {
self.total_ordering.insert(class.to_string());
}
pub fn is_total_ordering(&self, class: &str) -> bool {
self.total_ordering.contains(class)
}
}
pub fn make_partial(func: Value, args: Vec<Value>, kwargs: Vec<(String, Value)>) -> Value {
with_host(|h| h.alloc(PyObj::Partial { func, args, kwargs }))
}
pub fn make_lru_cache(func: Value, maxsize: Option<usize>) -> Value {
with_host(|h| {
let cache_id = h.lru_new(maxsize);
h.alloc(PyObj::LruCache { func, cache_id })
})
}
pub fn lru_cache_info(v: &Value) -> Option<(u64, u64, Option<usize>, usize)> {
let id = match with_host(|h| h.get(v).cloned()) {
Some(PyObj::LruCache { cache_id, .. }) => cache_id,
_ => return None,
};
Some(with_host(|h| h.lru_info(id)))
}
pub fn lru_cache_clear(v: &Value) -> bool {
match with_host(|h| h.get(v).cloned()) {
Some(PyObj::LruCache { cache_id, .. }) => {
with_host(|h| h.lru_clear(cache_id));
true
}
_ => false,
}
}
fn lru_invoke(
func: &Value,
cache_id: u32,
args: Vec<Value>,
kwargs: Vec<(String, Value)>,
) -> Result<Value, String> {
let key = with_host(|h| {
args.iter()
.map(|a| h.to_key(a))
.collect::<Result<Vec<PKey>, String>>()
.map(PKey::Tuple)
});
let key = match (key, kwargs.is_empty()) {
(Ok(k), true) => k,
_ => return invoke(func, args, kwargs),
};
if let Some(v) = with_host(|h| h.lru_lookup(cache_id, &key)) {
return Ok(v);
}
let result = invoke(func, args, kwargs)?;
with_host(|h| h.lru_store(cache_id, key, result.clone()));
Ok(result)
}