From 152cbc6b184eac2e8b55d1b797ec63f37f721363 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 5 Oct 2021 02:48:33 +0900 Subject: [PATCH 1/5] Use as_str to take &str form PyStr --- vm/src/builtins/dict.rs | 2 +- vm/src/frame.rs | 2 +- vm/src/stdlib/imp.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index c04332d69..c1e289a31 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -495,7 +495,7 @@ impl PyDictRef { let mut attrs = PyAttributes::default(); for (key, value) in self { let key: PyStrRef = key.downcast().expect("dict has non-string keys"); - attrs.insert(key.as_ref().to_owned(), value); + attrs.insert(key.as_str().to_owned(), value); } attrs } diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 3dbbe28c3..dd58f4962 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -1110,7 +1110,7 @@ impl ExecutingFrame<'_> { let all: Vec = vm.extract_elements(&all)?; let all: Vec = all .into_iter() - .map(|name| name.as_ref().to_owned()) + .map(|name| name.as_str().to_owned()) .collect(); Box::new(move |name| all.contains(&name.to_owned())) } else { diff --git a/vm/src/stdlib/imp.rs b/vm/src/stdlib/imp.rs index c147fcb84..337863d6c 100644 --- a/vm/src/stdlib/imp.rs +++ b/vm/src/stdlib/imp.rs @@ -60,7 +60,7 @@ fn _imp_create_builtin(spec: PyObjectRef, vm: &VirtualMachine) -> PyResult { if let Ok(module) = sys_modules.get_item(name.clone(), vm) { Ok(module) - } else if let Some(make_module_func) = vm.state.module_inits.get(name.as_ref()) { + } else if let Some(make_module_func) = vm.state.module_inits.get(name.as_str()) { Ok(make_module_func(vm)) } else { Ok(vm.ctx.none()) From 055bad342b6cf33119e20177328d477a31090f78 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 5 Oct 2021 02:48:56 +0900 Subject: [PATCH 2/5] impl AsRef for PyRef --- vm/src/pyobjectrc.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/vm/src/pyobjectrc.rs b/vm/src/pyobjectrc.rs index 1d85bf407..571561f38 100644 --- a/vm/src/pyobjectrc.rs +++ b/vm/src/pyobjectrc.rs @@ -418,6 +418,15 @@ impl PyRef { } } +impl AsRef for PyRef +where + T: PyObjectPayload, +{ + fn as_ref(&self) -> &PyObjectRef { + &self.obj + } +} + impl Deref for PyRef where T: PyObjectPayload, From 4fb1cdb31651b08c9348a365fec5617c83180068 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 5 Oct 2021 05:56:20 +0900 Subject: [PATCH 3/5] PyObjectWrap --- derive/src/pystructseq.rs | 2 +- src/lib.rs | 3 +- stdlib/src/array.rs | 2 +- stdlib/src/dis.rs | 12 +- stdlib/src/hashlib.rs | 4 +- stdlib/src/json.rs | 20 +-- stdlib/src/pyexpat.rs | 2 +- stdlib/src/re.rs | 34 ++--- stdlib/src/ssl.rs | 4 +- stdlib/src/unicodedata.rs | 2 +- vm/src/builtins/asyncgenerator.rs | 2 +- vm/src/builtins/builtinfunc.rs | 4 +- vm/src/builtins/bytearray.rs | 4 +- vm/src/builtins/bytes.rs | 4 +- vm/src/builtins/classmethod.rs | 2 +- vm/src/builtins/code.rs | 12 +- vm/src/builtins/complex.rs | 2 +- vm/src/builtins/dict.rs | 6 +- vm/src/builtins/float.rs | 2 +- vm/src/builtins/function.rs | 13 +- vm/src/builtins/function/jitfunc.rs | 2 +- vm/src/builtins/list.rs | 6 +- vm/src/builtins/mappingproxy.rs | 2 +- vm/src/builtins/memory.rs | 4 +- vm/src/builtins/module.rs | 2 +- vm/src/builtins/object.rs | 6 +- vm/src/builtins/property.rs | 2 +- vm/src/builtins/pystr.rs | 16 +-- vm/src/builtins/pysuper.rs | 8 +- vm/src/builtins/pytype.rs | 33 ++--- vm/src/builtins/range.rs | 14 +- vm/src/builtins/set.rs | 8 +- vm/src/builtins/singletons.rs | 4 +- vm/src/builtins/slice.rs | 2 +- vm/src/builtins/tuple.rs | 8 +- vm/src/builtins/zip.rs | 2 +- vm/src/codecs.rs | 14 +- vm/src/coroutine.rs | 2 +- vm/src/exceptions.rs | 6 +- vm/src/format.rs | 4 +- vm/src/frame.rs | 22 ++-- vm/src/function.rs | 2 +- vm/src/function/argument.rs | 13 +- vm/src/macros.rs | 16 +-- vm/src/protocol/iter.rs | 21 +-- vm/src/protocol/mapping.rs | 25 ++-- vm/src/py_io.rs | 4 +- vm/src/py_serde.rs | 2 +- vm/src/pyobject.rs | 43 +++---- vm/src/pyobjectrc.rs | 47 ++++++- vm/src/stdlib/ast.rs | 4 +- vm/src/stdlib/ast/gen.rs | 192 ++++++++++++++-------------- vm/src/stdlib/builtins.rs | 26 ++-- vm/src/stdlib/codecs.rs | 2 +- vm/src/stdlib/collections.rs | 24 +--- vm/src/stdlib/io.rs | 2 +- vm/src/stdlib/itertools.rs | 12 +- vm/src/stdlib/nt.rs | 2 +- vm/src/stdlib/operator.rs | 19 +-- vm/src/stdlib/os.rs | 14 +- vm/src/stdlib/posix.rs | 4 +- vm/src/stdlib/pystruct.rs | 2 +- vm/src/stdlib/sre.rs | 8 +- vm/src/stdlib/symtable.rs | 4 +- vm/src/stdlib/sys.rs | 2 +- vm/src/stdlib/thread.rs | 13 +- vm/src/stdlib/winapi.rs | 2 +- vm/src/utils.rs | 14 +- vm/src/vm.rs | 13 +- wasm/lib/src/browser_module.rs | 2 +- wasm/lib/src/convert.rs | 6 +- wasm/lib/src/js_module.rs | 27 ++-- 72 files changed, 426 insertions(+), 444 deletions(-) diff --git a/derive/src/pystructseq.rs b/derive/src/pystructseq.rs index 2d7917c7b..e7263e723 100644 --- a/derive/src/pystructseq.rs +++ b/derive/src/pystructseq.rs @@ -38,7 +38,7 @@ pub(crate) fn impl_pystruct_sequence(input: DeriveInput) -> Result } impl ::rustpython_vm::function::IntoPyObject for #ty { fn into_pyobject(self, vm: &::rustpython_vm::VirtualMachine) -> ::rustpython_vm::PyObjectRef { - ::rustpython_vm::PyStructSequence::into_struct_sequence(self, vm).into_object() + ::rustpython_vm::PyStructSequence::into_struct_sequence(self, vm).into() } } }; diff --git a/src/lib.rs b/src/lib.rs index 35c5d3795..5e8607945 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -640,7 +640,8 @@ fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult let path = vm.ctx.new_utf8_str(path); let path_hooks = vm.get_attribute(vm.sys_module.clone(), "path_hooks")?; let mut importer = None; - for path_hook in vm.extract_elements(&path_hooks)? { + let path_hooks: Vec = vm.extract_elements(&path_hooks)?; + for path_hook in path_hooks { match vm.invoke(&path_hook, (path.clone(),)) { Ok(imp) => { importer = Some(imp); diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index d127b9478..994fae736 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -665,7 +665,7 @@ mod array { } let zelf = Self::from(array).into_ref_with_type(vm, cls)?; - Ok(zelf.into_object()) + Ok(zelf.into()) } } diff --git a/stdlib/src/dis.rs b/stdlib/src/dis.rs index 8ca7eabab..9a97a72ca 100644 --- a/stdlib/src/dis.rs +++ b/stdlib/src/dis.rs @@ -12,22 +12,20 @@ mod decl { fn dis(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { let co = if let Ok(co) = vm.get_attribute(obj.clone(), "__code__") { // Method or function: - co + PyRef::try_from_object(vm, co)? } else if let Ok(co_str) = PyStrRef::try_from_object(vm, obj.clone()) { // String: vm.compile(co_str.as_str(), compile::Mode::Exec, "".to_owned()) .map_err(|err| vm.new_syntax_error(&err))? - .into_object() } else { - obj + PyRef::try_from_object(vm, obj)? }; - disassemble(co, vm) + disassemble(co) } #[pyfunction] - fn disassemble(co: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { - let code = PyRef::::try_from_object(vm, co)?; - print!("{}", &code.code); + fn disassemble(co: PyRef) -> PyResult<()> { + print!("{}", &co.code); Ok(()) } diff --git a/stdlib/src/hashlib.rs b/stdlib/src/hashlib.rs index 9ae9cab30..4f2f6f71e 100644 --- a/stdlib/src/hashlib.rs +++ b/stdlib/src/hashlib.rs @@ -48,9 +48,7 @@ mod hashlib { #[pyslot] fn slot_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult { - Ok(PyHasher::new("md5", HashWrapper::md5()) - .into_ref(vm) - .into_object()) + Ok(PyHasher::new("md5", HashWrapper::md5()).into_ref(vm).into()) } #[pyproperty] diff --git a/stdlib/src/json.rs b/stdlib/src/json.rs index 6bf2025a2..cafcb4033 100644 --- a/stdlib/src/json.rs +++ b/stdlib/src/json.rs @@ -90,8 +90,7 @@ mod _json { vm.invoke( &parse_obj, ( - vm.ctx - .new_tuple(vec![pystr.into_object(), vm.ctx.new_int(next_idx)]), + (pystr, next_idx), self.strict, scan_once, self.object_hook.clone(), @@ -105,14 +104,7 @@ mod _json { // TODO: parse the array in rust let parse_array = vm.get_attribute(self.ctx.clone(), "parse_array")?; return PyIterReturn::from_pyresult( - vm.invoke( - &parse_array, - vec![ - vm.ctx - .new_tuple(vec![pystr.into_object(), vm.ctx.new_int(next_idx)]), - scan_once, - ], - ), + vm.invoke(&parse_array, ((pystr, next_idx), scan_once)), vm, ); } @@ -207,13 +199,7 @@ mod _json { if idx > 0 && chars.nth(idx - 1).is_none() { return Ok(PyIterReturn::StopIteration(Some(vm.ctx.new_int(idx)))); } - zelf.parse( - chars.as_str(), - pystr.clone(), - idx, - zelf.clone().into_object(), - vm, - ) + zelf.parse(chars.as_str(), pystr.clone(), idx, zelf.clone().into(), vm) } } diff --git a/stdlib/src/pyexpat.rs b/stdlib/src/pyexpat.rs index 75672d3d1..eb56caf23 100644 --- a/stdlib/src/pyexpat.rs +++ b/stdlib/src/pyexpat.rs @@ -138,7 +138,7 @@ mod _pyexpat { } let name_str = PyStr::from(name.local_name).into_ref(vm); - invoke_handler(vm, &self.start_element, (name_str, dict.into_object())); + invoke_handler(vm, &self.start_element, (name_str, dict)); } Ok(XmlEvent::EndElement { name, .. }) => { let name_str = PyStr::from(name.local_name).into_ref(vm); diff --git a/stdlib/src/re.rs b/stdlib/src/re.rs index 3e5bcd694..e0765e036 100644 --- a/stdlib/src/re.rs +++ b/stdlib/src/re.rs @@ -3,15 +3,15 @@ pub(crate) use re::make_module; #[pymodule] mod re { /* - * Regular expressions. - * - * This module fits the python re interface onto the rust regular expression - * system. - */ - use crate::{ - builtins::{PyInt, PyIntRef, PyStr, PyStrRef, PyTypeRef}, + * Regular expressions. + * + * This module fits the python re interface onto the rust regular expression + * system. + */ + use crate::vm::{ + builtins::{PyInt, PyIntRef, PyStr, PyStrRef}, function::{IntoPyObject, OptionalArg, PosArgs}, - PyClassImpl, PyObjectRef, PyResult, PyValue, StaticType, TryFromObject, VirtualMachine, + match_class, PyObjectRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use num_traits::Signed; use regex::bytes::{Captures, Regex, RegexBuilder}; @@ -91,8 +91,8 @@ mod re { // type PyPatternRef = PyRef; // type PyMatchRef = PyRef; - #[pyfunction] - fn match( + #[pyfunction(name = "match")] + fn match_( pattern: PyStrRef, string: PyStrRef, flags: OptionalArg, @@ -222,12 +222,12 @@ mod re { ) -> PyResult { if maxsplit .as_ref() - .map_or(false, |i| i.as_str().is_negative()) + .map_or(false, |i| i.as_bigint().is_negative()) { - return Ok(vm.ctx.new_list(vec![search_text.into_object()])); + return Ok(vm.ctx.new_list(vec![search_text.into()])); } let maxsplit = maxsplit - .map(|i| usize::try_from_object(vm, i.into_object())) + .map(|i| i.try_to_primitive::(vm)) .transpose()? .unwrap_or(0); let text = search_text.as_str().as_bytes(); @@ -388,10 +388,14 @@ mod re { self.haystack.as_str()[bounds].to_owned() } - fn get_bounds(&self, id: PyObjectRef, vm: &VirtualMachine) -> PyResult>> { + fn get_bounds( + &self, + id: PyObjectRef, + vm: &VirtualMachine, + ) -> PyResult>> { match_class!(match id { i @ PyInt => { - let i = usize::try_from_object(vm, i.into_object())?; + let i = usize::try_from_object(vm, i.into())?; let capture = self .captures .get(i) diff --git a/stdlib/src/ssl.rs b/stdlib/src/ssl.rs index bf7a8e63a..fe1c10d87 100644 --- a/stdlib/src/ssl.rs +++ b/stdlib/src/ssl.rs @@ -197,7 +197,7 @@ fn _ssl_enum_certificates(store_name: PyStrRef, vm: &VirtualMachine) -> PyResult PyFrozenSet::from_iter(vm, oids.into_iter().map(|oid| vm.ctx.new_utf8_str(oid))) .unwrap() .into_ref(vm) - .into_object() + .into() } }; Ok(vm.ctx.new_tuple(vec![cert, enc_type, usage])) @@ -1133,7 +1133,7 @@ fn cert_to_py(vm: &VirtualMachine, cert: &X509Ref, binary: bool) -> PyResult { dict.set_item("subjectAltName", vm.ctx.new_tuple(san), vm)?; }; - dict.into_object() + dict.into() }; Ok(r) } diff --git a/stdlib/src/unicodedata.rs b/stdlib/src/unicodedata.rs index c5417e9c7..e68f739db 100644 --- a/stdlib/src/unicodedata.rs +++ b/stdlib/src/unicodedata.rs @@ -33,7 +33,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { ); let module = py_module!(vm, "unicodedata", { - "UCD" => ucd_class.into_object(), + "UCD" => ucd_class, "ucd_3_2_0" => ucd_3_2_0, // we do unidata_version here because the getter tries to do PyUCD::class() before // the module is in the VM diff --git a/vm/src/builtins/asyncgenerator.rs b/vm/src/builtins/asyncgenerator.rs index 24fa94256..b3231bad7 100644 --- a/vm/src/builtins/asyncgenerator.rs +++ b/vm/src/builtins/asyncgenerator.rs @@ -100,7 +100,7 @@ impl PyAsyncGen { aclose: true, state: AtomicCell::new(AwaitableState::Init), value: ( - vm.ctx.exceptions.generator_exit.clone().into_object(), + vm.ctx.exceptions.generator_exit.clone().into(), vm.ctx.none(), vm.ctx.none(), ), diff --git a/vm/src/builtins/builtinfunc.rs b/vm/src/builtins/builtinfunc.rs index 0e8cd15c8..bed33c19b 100644 --- a/vm/src/builtins/builtinfunc.rs +++ b/vm/src/builtins/builtinfunc.rs @@ -184,9 +184,9 @@ impl SlotDescriptor for PyBuiltinMethod { Err(result) => return result, }; if vm.is_none(&obj) && !Self::_cls_is(&cls, &obj.class()) { - Ok(zelf.into_object()) + Ok(zelf.into()) } else { - Ok(vm.ctx.new_bound_method(zelf.into_object(), obj)) + Ok(vm.ctx.new_bound_method(zelf.into(), obj)) } } } diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index 184ffcfb7..91aeaac96 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -634,7 +634,7 @@ impl PyByteArray { #[pymethod] fn decode(zelf: PyRef, args: DecodeArgs, vm: &VirtualMachine) -> PyResult { - bytes_decode(zelf.into_object(), args, vm) + bytes_decode(zelf.into(), args, vm) } #[pymethod(magic)] @@ -793,7 +793,7 @@ impl PyByteArrayIterator { fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef { self.internal .lock() - .builtins_iter_reduce(|x| x.clone().into_object(), vm) + .builtins_iter_reduce(|x| x.clone().into(), vm) } #[pymethod(magic)] diff --git a/vm/src/builtins/bytes.rs b/vm/src/builtins/bytes.rs index 29206a316..03da51b87 100644 --- a/vm/src/builtins/bytes.rs +++ b/vm/src/builtins/bytes.rs @@ -480,7 +480,7 @@ impl PyBytes { /// currently, only 'utf-8' and 'ascii' emplemented #[pymethod] fn decode(zelf: PyRef, args: DecodeArgs, vm: &VirtualMachine) -> PyResult { - bytes_decode(zelf.into_object(), args, vm) + bytes_decode(zelf.into(), args, vm) } #[pymethod(magic)] @@ -637,7 +637,7 @@ impl PyBytesIterator { fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef { self.internal .lock() - .builtins_iter_reduce(|x| x.clone().into_object(), vm) + .builtins_iter_reduce(|x| x.clone().into(), vm) } #[pymethod(magic)] diff --git a/vm/src/builtins/classmethod.rs b/vm/src/builtins/classmethod.rs index b018385c2..058d9e704 100644 --- a/vm/src/builtins/classmethod.rs +++ b/vm/src/builtins/classmethod.rs @@ -50,7 +50,7 @@ impl SlotDescriptor for PyClassMethod { vm: &VirtualMachine, ) -> PyResult { let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?; - let cls = cls.unwrap_or_else(|| obj.clone_class().into_object()); + let cls = cls.unwrap_or_else(|| obj.clone_class().into()); Ok(vm.ctx.new_bound_method(zelf.callable.clone(), cls)) } } diff --git a/vm/src/builtins/code.rs b/vm/src/builtins/code.rs index e4e2cca30..dff8c02c1 100644 --- a/vm/src/builtins/code.rs +++ b/vm/src/builtins/code.rs @@ -84,14 +84,12 @@ impl ConstantBag for PyObjBag<'_> { bytecode::ConstantData::Float { value } => ctx.new_float(value), bytecode::ConstantData::Complex { value } => ctx.new_complex(value), bytecode::ConstantData::Str { value } if value.len() <= 20 => { - vm.intern_string(value).into_object() + vm.intern_string(value).into() } bytecode::ConstantData::Str { value } => vm.ctx.new_utf8_str(value), bytecode::ConstantData::Bytes { value } => ctx.new_bytes(value.to_vec()), bytecode::ConstantData::Boolean { value } => ctx.new_bool(value), - bytecode::ConstantData::Code { code } => { - ctx.new_code_object(code.map_bag(self)).into_object() - } + bytecode::ConstantData::Code { code } => ctx.new_code_object(code.map_bag(self)).into(), bytecode::ConstantData::Tuple { elements } => { let elements = elements .into_iter() @@ -112,13 +110,13 @@ impl ConstantBag for PyObjBag<'_> { bytecode::BorrowedConstant::Float { value } => ctx.new_float(value), bytecode::BorrowedConstant::Complex { value } => ctx.new_complex(value), bytecode::BorrowedConstant::Str { value } if value.len() <= 20 => { - vm.intern_string(value).into_object() + vm.intern_string(value).into() } bytecode::BorrowedConstant::Str { value } => vm.ctx.new_utf8_str(value), bytecode::BorrowedConstant::Bytes { value } => ctx.new_bytes(value.to_vec()), bytecode::BorrowedConstant::Boolean { value } => ctx.new_bool(value), bytecode::BorrowedConstant::Code { code } => { - ctx.new_code_object(code.map_clone_bag(self)).into_object() + ctx.new_code_object(code.map_clone_bag(self)).into() } bytecode::BorrowedConstant::Tuple { elements } => { let elements = elements @@ -256,7 +254,7 @@ impl PyRef { .code .varnames .iter() - .map(|s| s.clone().into_object()) + .map(|s| s.clone().into()) .collect(); vm.ctx.new_tuple(varnames) } diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index db5d6b0c3..333ce0bf5 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -121,7 +121,7 @@ impl SlotConstructor for PyComplex { let val = if cls.is(&vm.ctx.types.complex_type) && imag_missing { match val.downcast_exact::(vm) { Ok(c) => { - return Ok(c.into_object()); + return Ok(c.into()); } Err(val) => val, } diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index c1e289a31..b57679e2d 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -331,9 +331,9 @@ impl PyDict { } #[pymethod(magic)] - fn ior(zelf: PyRef, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn ior(zelf: PyRef, other: PyObjectRef, vm: &VirtualMachine) -> PyResult> { PyDict::merge_object(&zelf.entries, other, vm)?; - Ok(zelf.into_object()) + Ok(zelf) } #[pymethod(magic)] @@ -482,7 +482,7 @@ impl PyDictRef { } if !exact { - if let Some(method_or_err) = vm.get_method(self.clone().into_object(), "__missing__") { + if let Some(method_or_err) = vm.get_method(self.clone().into(), "__missing__") { let method = method_or_err?; return vm.invoke(&method, (key,)).map(Some); } diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index fd7959970..e91b88cf0 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -165,7 +165,7 @@ impl SlotConstructor for PyFloat { OptionalArg::Present(val) => { let val = if cls.is(&vm.ctx.types.float_type) { match val.downcast_exact::(vm) { - Ok(f) => return Ok(f.into_object()), + Ok(f) => return Ok(f.into()), Err(val) => val, } } else { diff --git a/vm/src/builtins/function.rs b/vm/src/builtins/function.rs index 4617f2a02..0d1c95959 100644 --- a/vm/src/builtins/function.rs +++ b/vm/src/builtins/function.rs @@ -107,7 +107,7 @@ impl PyFunction { // Do we support `**kwargs` ? let kwargs = if code.flags.contains(bytecode::CodeFlags::HAS_VARKEYWORDS) { let d = vm.ctx.new_dict(); - fastlocals[vararg_offset] = Some(d.clone().into_object()); + fastlocals[vararg_offset] = Some(d.clone().into()); Some(d) } else { None @@ -405,11 +405,12 @@ impl SlotDescriptor for PyFunction { vm: &VirtualMachine, ) -> PyResult { let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?; - if vm.is_none(&obj) && !Self::_cls_is(&cls, &obj.class()) { - Ok(zelf.into_object()) + let obj = if vm.is_none(&obj) && !Self::_cls_is(&cls, &obj.class()) { + zelf.into() } else { - Ok(vm.ctx.new_bound_method(zelf.into_object(), obj)) - } + vm.ctx.new_bound_method(zelf.into(), obj) + }; + Ok(obj) } } @@ -453,7 +454,7 @@ impl Comparable for PyBoundMethod { impl SlotGetattro for PyBoundMethod { fn getattro(zelf: PyRef, name: PyStrRef, vm: &VirtualMachine) -> PyResult { if let Some(obj) = zelf.get_class_attr(name.as_str()) { - return vm.call_if_get_descriptor(obj, zelf.into_object()); + return vm.call_if_get_descriptor(obj, zelf.into()); } vm.get_attribute(zelf.function.clone(), name) } diff --git a/vm/src/builtins/function/jitfunc.rs b/vm/src/builtins/function/jitfunc.rs index f3f29ed3a..1b0c2d3da 100644 --- a/vm/src/builtins/function/jitfunc.rs +++ b/vm/src/builtins/function/jitfunc.rs @@ -81,7 +81,7 @@ pub fn get_jit_arg_types(func: &PyRef, vm: &VirtualMachine) -> PyRes return Ok(Vec::new()); } - let annotations = vm.get_attribute(func.clone().into_object(), "__annotations__")?; + let annotations = vm.get_attribute(func.clone().into(), "__annotations__")?; if vm.is_none(&annotations) { Err(new_jit_error( "Jitting function requires arguments to have annotations".to_owned(), diff --git a/vm/src/builtins/list.rs b/vm/src/builtins/list.rs index e173299cf..803ee7e2f 100644 --- a/vm/src/builtins/list.rs +++ b/vm/src/builtins/list.rs @@ -120,7 +120,7 @@ impl PyList { if let Ok(new_elements) = vm.extract_elements(&other) { let mut e = new_elements; zelf.borrow_vec_mut().append(&mut e); - zelf.into_object() + zelf.into() } else { vm.ctx.not_implemented() } @@ -524,7 +524,7 @@ impl PyListIterator { fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef { self.internal .lock() - .builtins_iter_reduce(|x| x.clone().into_object(), vm) + .builtins_iter_reduce(|x| x.clone().into(), vm) } } @@ -571,7 +571,7 @@ impl PyListReverseIterator { fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef { self.internal .lock() - .builtins_reversed_reduce(|x| x.clone().into_object(), vm) + .builtins_reversed_reduce(|x| x.clone().into(), vm) } } diff --git a/vm/src/builtins/mappingproxy.rs b/vm/src/builtins/mappingproxy.rs index 0e672029c..d2b210faa 100644 --- a/vm/src/builtins/mappingproxy.rs +++ b/vm/src/builtins/mappingproxy.rs @@ -189,7 +189,7 @@ impl Iterable for PyMappingProxy { } }; let iter = obj.get_iter(vm)?; - Ok(iter.into_object()) + Ok(iter.into()) } } diff --git a/vm/src/builtins/memory.rs b/vm/src/builtins/memory.rs index f578c05c1..d7cdb7b24 100644 --- a/vm/src/builtins/memory.rs +++ b/vm/src/builtins/memory.rs @@ -245,7 +245,7 @@ impl PyMemoryView { if x.len() == 1 { x.fast_getitem(0) } else { - x.into_object() + x.into() } }) } @@ -838,7 +838,7 @@ fn format_unpack( if x.len() == 1 { x.fast_getitem(0) } else { - x.into_object() + x.into() } }) } diff --git a/vm/src/builtins/module.rs b/vm/src/builtins/module.rs index a189b90bc..5d90bc3fe 100644 --- a/vm/src/builtins/module.rs +++ b/vm/src/builtins/module.rs @@ -61,7 +61,7 @@ impl PyModule { init_module_dict( vm, &zelf.as_object().dict().unwrap(), - args.name.into_object(), + args.name.into(), args.doc.into_pyobject(vm), ); } diff --git a/vm/src/builtins/object.rs b/vm/src/builtins/object.rs index 7cfa6242c..38f63c640 100644 --- a/vm/src/builtins/object.rs +++ b/vm/src/builtins/object.rs @@ -240,8 +240,8 @@ impl PyBaseObject { fn init(_args: FuncArgs) {} #[pyproperty(name = "__class__")] - fn get_class(obj: PyObjectRef) -> PyObjectRef { - obj.clone_class().into_object() + fn get_class(obj: PyObjectRef) -> PyTypeRef { + obj.clone_class() } #[pyproperty(name = "__class__", setter)] @@ -285,7 +285,7 @@ impl PyBaseObject { fn reduce_ex(obj: PyObjectRef, proto: usize, vm: &VirtualMachine) -> PyResult { if let Some(reduce) = vm.get_attribute_opt(obj.clone(), "__reduce__")? { let object_reduce = vm.ctx.types.object_type.get_attr("__reduce__").unwrap(); - let class_reduce = vm.get_attribute(obj.clone_class().into_object(), "__reduce__")?; + let class_reduce = vm.get_attribute(obj.clone_class().into(), "__reduce__")?; if !class_reduce.is(&object_reduce) { return vm.invoke(&reduce, ()); } diff --git a/vm/src/builtins/property.rs b/vm/src/builtins/property.rs index 0c167bab4..e534d5f8d 100644 --- a/vm/src/builtins/property.rs +++ b/vm/src/builtins/property.rs @@ -76,7 +76,7 @@ impl SlotDescriptor for PyProperty { ) -> PyResult { let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?; if vm.is_none(&obj) { - Ok(zelf.into_object()) + Ok(zelf.into()) } else if let Some(getter) = zelf.getter.read().as_ref() { vm.invoke(getter, (obj,)) } else { diff --git a/vm/src/builtins/pystr.rs b/vm/src/builtins/pystr.rs index 6cad6a4ed..09fdb711e 100644 --- a/vm/src/builtins/pystr.rs +++ b/vm/src/builtins/pystr.rs @@ -200,7 +200,7 @@ impl PyStrIterator { self.internal .lock() .0 - .builtins_iter_reduce(|x| x.clone().into_object(), vm) + .builtins_iter_reduce(|x| x.clone().into(), vm) } } @@ -263,7 +263,7 @@ impl SlotConstructor for PyStr { } }; if string.class().is(&cls) { - Ok(string.into_object()) + Ok(string.into()) } else { PyStr::from(string.as_str()).into_pyresult_with_type(vm, cls) } @@ -949,16 +949,16 @@ impl PyStr { || self.as_str().splitn(2, sep.as_str()), vm, )?; - Ok(( + let partition = ( self.new_substr(front), if has_mid { - sep.into_object() + sep.into() } else { vm.ctx.new_ascii_literal(ascii!("")) }, self.new_substr(back), - ) - .into_pyobject(vm)) + ); + Ok(partition.into_pyobject(vm)) } #[pymethod] @@ -971,7 +971,7 @@ impl PyStr { Ok(( self.new_substr(front), if has_mid { - sep.into_object() + sep.into() } else { vm.ctx.new_ascii_literal(ascii!("")) }, @@ -1520,7 +1520,7 @@ mod tests { .set_item("c", vm.ctx.new_ascii_literal(ascii!("xda")), &vm) .unwrap(); let translated = PyStr::maketrans( - table.into_object(), + table.into(), OptionalArg::Missing, OptionalArg::Missing, &vm, diff --git a/vm/src/builtins/pysuper.rs b/vm/src/builtins/pysuper.rs index 8ae8856aa..cf813646c 100644 --- a/vm/src/builtins/pysuper.rs +++ b/vm/src/builtins/pysuper.rs @@ -122,7 +122,7 @@ impl PySuper { impl SlotGetattro for PySuper { fn getattro(zelf: PyRef, name: PyStrRef, vm: &VirtualMachine) -> PyResult { - let skip = |zelf: PyRef, name| vm.generic_getattribute(zelf.into_object(), name); + let skip = |zelf: PyRef, name| vm.generic_getattribute(zelf.into(), name); let (obj, start_type): (PyObjectRef, PyTypeRef) = match zelf.obj.clone() { Some(o) => o, None => return skip(zelf, name), @@ -165,13 +165,11 @@ impl SlotDescriptor for PySuper { ) -> PyResult { let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?; if vm.is_none(&obj) || zelf.obj.is_some() { - return Ok(zelf.into_object()); + return Ok(zelf.into()); } let zelf_class = zelf.as_object().class(); if zelf_class.is(&vm.ctx.types.super_type) { - Ok(PySuper::new(zelf.typ.clone(), obj, vm)? - .into_ref(vm) - .into_object()) + Ok(PySuper::new(zelf.typ.clone(), obj, vm)?.into_ref(vm).into()) } else { let obj = vm.unwrap_or_none(zelf.obj.clone().map(|(o, _)| o)); vm.invoke( diff --git a/vm/src/builtins/pytype.rs b/vm/src/builtins/pytype.rs index 2f922199e..d6b014a5f 100644 --- a/vm/src/builtins/pytype.rs +++ b/vm/src/builtins/pytype.rs @@ -187,7 +187,7 @@ impl PyType { let new = vm .get_attribute_opt(cls.as_object().clone(), "__new__")? .unwrap(); - args.prepend_arg(cls.into_object()); + args.prepend_arg(cls.into()); vm.invoke(&new, args) }; update_slot!(new, func); @@ -493,11 +493,8 @@ impl PyType { #[pymethod] fn mro(zelf: PyRef, vm: &VirtualMachine) -> PyObjectRef { - vm.ctx.new_list( - zelf.iter_mro() - .map(|cls| cls.clone().into_object()) - .collect(), - ) + vm.ctx + .new_list(zelf.iter_mro().map(|cls| cls.clone().into()).collect()) } #[pyslot] fn slot_new(metatype: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult { @@ -505,7 +502,7 @@ impl PyType { let is_type_type = metatype.is(&vm.ctx.types.type_type); if is_type_type && args.args.len() == 1 && args.kwargs.is_empty() { - return Ok(args.args[0].clone_class().into_object()); + return Ok(args.args[0].clone_class().into()); } if args.args.len() != 3 { @@ -632,16 +629,12 @@ impl PyType { if let Some(initter) = typ.get_super_attr("__init_subclass__") { let initter = vm - .call_get_descriptor_specific( - initter.clone(), - None, - Some(typ.clone().into_object()), - ) + .call_get_descriptor_specific(initter.clone(), None, Some(typ.clone().into())) .unwrap_or(Ok(initter))?; vm.invoke(&initter, kwargs)?; }; - Ok(typ.into_object()) + Ok(typ.into()) } #[pyproperty(magic)] @@ -703,8 +696,8 @@ impl SlotGetattro for PyType { .is_some() { if let Some(descr_get) = attr_class.mro_find_map(|cls| cls.slots.descr_get.load()) { - let mcl = PyLease::into_pyref(mcl).into_object(); - return descr_get(attr.clone(), Some(zelf.into_object()), Some(mcl), vm); + let mcl = PyLease::into_pyref(mcl).into(); + return descr_get(attr.clone(), Some(zelf.into()), Some(mcl), vm); } } } @@ -714,7 +707,7 @@ impl SlotGetattro for PyType { if let Some(ref attr) = zelf_attr { if let Some(descr_get) = attr.class().mro_find_map(|cls| cls.slots.descr_get.load()) { drop(mcl); - return descr_get(attr.clone(), None, Some(zelf.into_object()), vm); + return descr_get(attr.clone(), None, Some(zelf.into()), vm); } } @@ -722,7 +715,7 @@ impl SlotGetattro for PyType { Ok(cls_attr) } else if let Some(attr) = mcl_attr { drop(mcl); - vm.call_if_get_descriptor(attr, zelf.into_object()) + vm.call_if_get_descriptor(attr, zelf.into()) } else { Err(vm.new_attribute_error(format!( "type object '{}' has no attribute '{}'", @@ -743,7 +736,7 @@ impl SlotSetattro for PyType { if let Some(attr) = zelf.get_class_attr(attr_name.as_str()) { let descr_set = attr.class().mro_find_map(|cls| cls.slots.descr_set.load()); if let Some(descriptor) = descr_set { - return descriptor(attr, zelf.clone().into_object(), value, vm); + return descriptor(attr, zelf.clone().into(), value, vm); } } let assign = value.is_some(); @@ -756,7 +749,7 @@ impl SlotSetattro for PyType { if prev_value.is_none() { return Err(vm.new_exception( vm.ctx.exceptions.attribute_error.clone(), - vec![attr_name.into_object()], + vec![attr_name.into()], )); } } @@ -810,7 +803,7 @@ fn subtype_get_dict(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { cls.name() ))) })?, - None => object::object_get_dict(obj, vm)?.into_object(), + None => object::object_get_dict(obj, vm)?.into(), }; Ok(ret) } diff --git a/vm/src/builtins/range.rs b/vm/src/builtins/range.rs index 205d541be..9bfef77e2 100644 --- a/vm/src/builtins/range.rs +++ b/vm/src/builtins/range.rs @@ -352,7 +352,7 @@ impl PyRange { step: substep.into_pyref(vm), } .into_ref(vm) - .into_object()) + .into()) } RangeIndex::Int(index) => match self.get(index.as_bigint()) { Some(value) => Ok(vm.ctx.new_int(value)), @@ -371,7 +371,7 @@ impl PyRange { PyRange::new_from(cls, start, stop, step, vm) }?; - Ok(range.into_object()) + Ok(range.into()) } } @@ -411,16 +411,12 @@ impl Hashable for PyRange { let elements = if length.is_zero() { [vm.ctx.new_int(length), vm.ctx.none(), vm.ctx.none()] } else if length.is_one() { - [ - vm.ctx.new_int(length), - zelf.start().into_object(), - vm.ctx.none(), - ] + [vm.ctx.new_int(length), zelf.start().into(), vm.ctx.none()] } else { [ vm.ctx.new_int(length), - zelf.start().into_object(), - zelf.step().into_object(), + zelf.start().into(), + zelf.step().into(), ] }; crate::utils::hash_iter(elements.iter(), vm) diff --git a/vm/src/builtins/set.rs b/vm/src/builtins/set.rs index 868c72aae..069e89699 100644 --- a/vm/src/builtins/set.rs +++ b/vm/src/builtins/set.rs @@ -609,7 +609,7 @@ impl PySet { zelf: PyRef, vm: &VirtualMachine, ) -> PyResult<(PyTypeRef, PyObjectRef, Option)> { - reduce_set(&zelf.into_object(), vm) + reduce_set(&zelf.into(), vm) } } @@ -651,7 +651,7 @@ impl SlotConstructor for PyFrozenSet { let elements = if let OptionalArg::Present(iterable) = iterable { let iterable = if cls.is(&vm.ctx.types.frozenset_type) { match iterable.downcast_exact::(vm) { - Ok(fs) => return Ok(fs.into_object()), + Ok(fs) => return Ok(fs.into()), Err(iterable) => iterable, } } else { @@ -664,7 +664,7 @@ impl SlotConstructor for PyFrozenSet { // Return empty fs if iterable passed is empty and only for exact fs types. if elements.is_empty() && cls.is(&vm.ctx.types.frozenset_type) { - Ok(vm.ctx.empty_frozenset.clone().into_object()) + Ok(vm.ctx.empty_frozenset.clone().into()) } else { Self::from_iter(vm, elements).and_then(|o| o.into_pyresult_with_type(vm, cls)) } @@ -798,7 +798,7 @@ impl PyFrozenSet { zelf: PyRef, vm: &VirtualMachine, ) -> PyResult<(PyTypeRef, PyObjectRef, Option)> { - reduce_set(&zelf.into_object(), vm) + reduce_set(&zelf.into(), vm) } } diff --git a/vm/src/builtins/singletons.rs b/vm/src/builtins/singletons.rs index a32c47988..4bcbae09f 100644 --- a/vm/src/builtins/singletons.rs +++ b/vm/src/builtins/singletons.rs @@ -35,7 +35,7 @@ impl SlotConstructor for PyNone { type Args = (); fn py_new(_: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult { - Ok(vm.ctx.none.clone().into_object()) + Ok(vm.ctx.none.clone().into()) } } @@ -66,7 +66,7 @@ impl SlotConstructor for PyNotImplemented { type Args = (); fn py_new(_: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult { - Ok(vm.ctx.not_implemented.clone().into_object()) + Ok(vm.ctx.not_implemented.clone().into()) } } diff --git a/vm/src/builtins/slice.rs b/vm/src/builtins/slice.rs index a610a4f24..97af68555 100644 --- a/vm/src/builtins/slice.rs +++ b/vm/src/builtins/slice.rs @@ -295,7 +295,7 @@ impl SlotConstructor for PyEllipsis { type Args = (); fn py_new(_cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult { - Ok(vm.ctx.ellipsis.clone().into_object()) + Ok(vm.ctx.ellipsis.clone().into()) } } diff --git a/vm/src/builtins/tuple.rs b/vm/src/builtins/tuple.rs index f718122c2..26c24faaa 100644 --- a/vm/src/builtins/tuple.rs +++ b/vm/src/builtins/tuple.rs @@ -85,7 +85,7 @@ impl SlotConstructor for PyTuple { let elements = if let OptionalArg::Present(iterable) = iterable { let iterable = if cls.is(&vm.ctx.types.tuple_type) { match iterable.downcast_exact::(vm) { - Ok(tuple) => return Ok(tuple.into_object()), + Ok(tuple) => return Ok(tuple.into()), Err(iterable) => iterable, } } else { @@ -97,7 +97,7 @@ impl SlotConstructor for PyTuple { }; // Return empty tuple only for exact tuple types if the iterable is empty. if elements.is_empty() && cls.is(&vm.ctx.types.tuple_type) { - Ok(vm.ctx.empty_tuple.clone().into_object()) + Ok(vm.ctx.empty_tuple.clone().into()) } else { Self { elements: elements.into_boxed_slice(), @@ -375,7 +375,7 @@ impl PyTupleIterator { fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef { self.internal .lock() - .builtins_iter_reduce(|x| x.clone().into_object(), vm) + .builtins_iter_reduce(|x| x.clone().into(), vm) } } @@ -458,6 +458,6 @@ impl From> for PyTupleRef { impl IntoPyObject for PyTupleTyped { #[inline] fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { - self.tuple.into_object() + self.tuple.into() } } diff --git a/vm/src/builtins/zip.rs b/vm/src/builtins/zip.rs index ea5e3ef11..0786b1d9d 100644 --- a/vm/src/builtins/zip.rs +++ b/vm/src/builtins/zip.rs @@ -45,7 +45,7 @@ impl PyZip { let iterators = zelf .iterators .iter() - .map(|obj| obj.clone().into_object()) + .map(|obj| obj.clone().into()) .collect::>(); let tuple_iter = vm.ctx.new_tuple(iterators); Ok(if zelf.strict.load(atomic::Ordering::Acquire) { diff --git a/vm/src/codecs.rs b/vm/src/codecs.rs index 9384f7af7..15c30afc4 100644 --- a/vm/src/codecs.rs +++ b/vm/src/codecs.rs @@ -51,7 +51,7 @@ impl PyCodec { } pub fn is_text_codec(&self, vm: &VirtualMachine) -> PyResult { - let is_text = vm.get_attribute_opt(self.0.clone().into_object(), "_is_text_encoding")?; + let is_text = vm.get_attribute_opt(self.0.clone().into(), "_is_text_encoding")?; is_text.map_or(Ok(true), |is_text| is_text.try_to_bool(vm)) } @@ -62,7 +62,7 @@ impl PyCodec { vm: &VirtualMachine, ) -> PyResult { let args = match errors { - Some(errors) => vec![obj, errors.into_object()], + Some(errors) => vec![obj, errors.into()], None => vec![obj], }; let res = vm.invoke(self.get_encode_func(), args)?; @@ -84,7 +84,7 @@ impl PyCodec { vm: &VirtualMachine, ) -> PyResult { let args = match errors { - Some(errors) => vec![obj, errors.into_object()], + Some(errors) => vec![obj, errors.into()], None => vec![obj], }; let res = vm.invoke(self.get_decode_func(), args)?; @@ -105,7 +105,7 @@ impl PyCodec { vm: &VirtualMachine, ) -> PyResult { let args = match errors { - Some(e) => vec![e.into_object()], + Some(e) => vec![e.into()], None => vec![], }; vm.call_method(self.0.as_object(), "incrementalencoder", args) @@ -117,7 +117,7 @@ impl PyCodec { vm: &VirtualMachine, ) -> PyResult { let args = match errors { - Some(e) => vec![e.into_object()], + Some(e) => vec![e.into()], None => vec![], }; vm.call_method(self.0.as_object(), "incrementaldecoder", args) @@ -138,7 +138,7 @@ impl TryFromObject for PyCodec { impl IntoPyObject for PyCodec { #[inline] fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { - self.0.into_object() + self.0.into() } } @@ -271,7 +271,7 @@ impl CodecsRegistry { ) -> PyResult { let codec = self._lookup_text_encoding(encoding, "codecs.encode()", vm)?; codec - .encode(obj.into_object(), errors, vm)? + .encode(obj.into(), errors, vm)? .downcast() .map_err(|obj| { vm.new_type_error(format!( diff --git a/vm/src/coroutine.rs b/vm/src/coroutine.rs index 440ebff27..58c69e98e 100644 --- a/vm/src/coroutine.rs +++ b/vm/src/coroutine.rs @@ -154,7 +154,7 @@ impl Coro { let result = self.run_with_context(gen, vm, |f| { f.gen_throw( vm, - vm.ctx.exceptions.generator_exit.clone().into_object(), + vm.ctx.exceptions.generator_exit.clone().into(), vm.ctx.none(), vm.ctx.none(), ) diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 55b0caf40..a37dc4d3c 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -229,7 +229,7 @@ impl TryFromObject for ExceptionCtor { if cls.issubclass(&vm.ctx.exceptions.base_exception_type) { Ok(Self::Class(cls)) } else { - Err(cls.into_object()) + Err(cls.into()) } }) .or_else(|obj| obj.downcast::().map(Self::Instance)) @@ -295,7 +295,7 @@ pub fn split( vm: &VirtualMachine, ) -> (PyObjectRef, PyObjectRef, PyObjectRef) { let tb = exc.traceback().into_pyobject(vm); - (exc.clone_class().into_object(), exc.into_object(), tb) + (exc.clone_class().into(), exc.into(), tb) } /// Similar to PyErr_NormalizeException in CPython @@ -1081,7 +1081,7 @@ pub(super) mod types { args: FuncArgs, vm: &VirtualMachine, ) -> PyResult<()> { - let exc_self = zelf.into_object(); + let exc_self = zelf.into(); vm.set_attr( &exc_self, "name", diff --git a/vm/src/format.rs b/vm/src/format.rs index 60ba6ccef..04de92352 100644 --- a/vm/src/format.rs +++ b/vm/src/format.rs @@ -892,8 +892,8 @@ fn call_object_format( format_spec: &str, ) -> PyResult { let argument = match preconversion_spec.and_then(FormatPreconversor::from_char) { - Some(FormatPreconversor::Str) => vm.to_str(&argument)?.into_object(), - Some(FormatPreconversor::Repr) => vm.to_repr(&argument)?.into_object(), + Some(FormatPreconversor::Str) => vm.to_str(&argument)?.into(), + Some(FormatPreconversor::Repr) => vm.to_repr(&argument)?.into(), Some(FormatPreconversor::Ascii) => vm.ctx.new_utf8_str(builtins::ascii(argument, vm)?), Some(FormatPreconversor::Bytes) => vm.call_method(&argument, "decode", ())?, None => argument, diff --git a/vm/src/frame.rs b/vm/src/frame.rs index dd58f4962..5eb5a9b9c 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -588,7 +588,7 @@ impl ExecutingFrame<'_> { } bytecode::Instruction::LoadClosure(i) => { let value = self.cells_frees[*i as usize].clone(); - self.push_value(value.into_object()); + self.push_value(value.into()); Ok(None) } bytecode::Instruction::Subscript => self.execute_subscript(vm), @@ -638,7 +638,7 @@ impl ExecutingFrame<'_> { } } } - self.push_value(set.into_object()); + self.push_value(set.into()); Ok(None) } bytecode::Instruction::BuildTuple { size, unpack } => { @@ -706,7 +706,7 @@ impl ExecutingFrame<'_> { bytecode::Instruction::SetupAnnotation => { if !self.locals.contains_key("__annotations__", vm) { self.locals - .set_item("__annotations__", vm.ctx.new_dict().into_object(), vm)?; + .set_item("__annotations__", vm.ctx.new_dict().into(), vm)?; } Ok(None) } @@ -823,7 +823,7 @@ impl ExecutingFrame<'_> { bytecode::Instruction::GetIter => { let iterated_obj = self.pop_value(); let iter_obj = iterated_obj.get_iter(vm)?; - self.push_value(iter_obj.into_object()); + self.push_value(iter_obj.into()); Ok(None) } bytecode::Instruction::GetAwaitable => { @@ -1014,8 +1014,8 @@ impl ExecutingFrame<'_> { use bytecode::ConversionFlag; let value = self.pop_value(); let value = match conversion { - ConversionFlag::Str => vm.to_str(&value)?.into_object(), - ConversionFlag::Repr => vm.to_repr(&value)?.into_object(), + ConversionFlag::Str => vm.to_str(&value)?.into(), + ConversionFlag::Repr => vm.to_repr(&value)?.into(), ConversionFlag::Ascii => vm.ctx.new_utf8_str(builtins::ascii(value, vm)?), ConversionFlag::None => value, }; @@ -1170,7 +1170,7 @@ impl ExecutingFrame<'_> { }); vm.contextualize_exception(&exception); vm.set_exception(Some(exception.clone())); - self.push_value(exception.into_object()); + self.push_value(exception.into()); self.jump(handler); return Ok(None); } @@ -1275,7 +1275,7 @@ impl ExecutingFrame<'_> { } } - self.push_value(map_obj.into_object()); + self.push_value(map_obj.into()); Ok(None) } @@ -1290,7 +1290,7 @@ impl ExecutingFrame<'_> { step, } .into_ref(vm); - self.push_value(obj.into_object()); + self.push_value(obj.into()); Ok(None) } @@ -1528,7 +1528,7 @@ impl ExecutingFrame<'_> { let annotations = if flags.contains(bytecode::MakeFunctionFlags::ANNOTATIONS) { self.pop_value() } else { - vm.ctx.new_dict().into_object() + vm.ctx.new_dict().into() }; let kw_only_defaults = if flags.contains(bytecode::MakeFunctionFlags::KW_ONLY_DEFAULTS) { @@ -1718,7 +1718,7 @@ impl ExecutingFrame<'_> { } fn delete_attr(&mut self, vm: &VirtualMachine, attr: bytecode::NameIdx) -> FrameResult { - let attr_name = self.code.names[attr as usize].clone().into_object(); + let attr_name = self.code.names[attr as usize].clone(); let parent = self.pop_value(); vm.del_attr(&parent, attr_name)?; Ok(None) diff --git a/vm/src/function.rs b/vm/src/function.rs index 280cc4f04..e0deca96f 100644 --- a/vm/src/function.rs +++ b/vm/src/function.rs @@ -435,7 +435,7 @@ impl AsRef<[T]> for PosArgs { impl PosArgs> { pub fn into_tuple(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx - .new_tuple(self.0.into_iter().map(PyRef::into_object).collect()) + .new_tuple(self.0.into_iter().map(Into::into).collect()) } } diff --git a/vm/src/function/argument.rs b/vm/src/function/argument.rs index e6b6f24d8..2b8ae01f7 100644 --- a/vm/src/function/argument.rs +++ b/vm/src/function/argument.rs @@ -1,7 +1,7 @@ use super::IntoFuncArgs; use crate::{ builtins::iter::PySequenceIterator, protocol::PyIter, protocol::PyIterIter, PyObjectRef, - PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, + PyObjectWrap, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, }; use std::marker::PhantomData; @@ -15,9 +15,16 @@ impl ArgCallable { pub fn invoke(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult { vm.invoke(&self.obj, args) } +} - #[inline] - pub fn into_object(self) -> PyObjectRef { +impl AsRef for ArgCallable { + fn as_ref(&self) -> &PyObjectRef { + &self.obj + } +} + +impl PyObjectWrap for ArgCallable { + fn into_object(self) -> PyObjectRef { self.obj } } diff --git a/vm/src/macros.rs b/vm/src/macros.rs index 7a57a99e0..365a8f070 100644 --- a/vm/src/macros.rs +++ b/vm/src/macros.rs @@ -77,15 +77,14 @@ macro_rules! py_namespace { /// use num_traits::Zero; /// /// use rustpython_vm::match_class; -/// use rustpython_vm::builtins::PyFloat; -/// use rustpython_vm::builtins::PyInt; -/// use rustpython_vm::PyValue; +/// use rustpython_vm::builtins::{PyFloat, PyInt}; +/// use rustpython_vm::{PyObjectRef, PyValue}; /// /// # rustpython_vm::Interpreter::default().enter(|vm| { -/// let obj = PyInt::from(0).into_ref(&vm).into_object(); +/// let obj: PyObjectRef = PyInt::from(0).into_ref(&vm).into(); /// assert_eq!( /// "int", -/// match_class!(match obj.clone() { +/// match_class!(match obj { /// PyInt => "int", /// PyFloat => "float", /// _ => "neither", @@ -102,12 +101,11 @@ macro_rules! py_namespace { /// use num_traits::Zero; /// /// use rustpython_vm::match_class; -/// use rustpython_vm::builtins::PyFloat; -/// use rustpython_vm::builtins::PyInt; -/// use rustpython_vm::PyValue; +/// use rustpython_vm::builtins::{PyFloat, PyInt}; +/// use rustpython_vm::{PyObjectRef, PyValue}; /// /// # rustpython_vm::Interpreter::default().enter(|vm| { -/// let obj = PyInt::from(0).into_ref(vm).into_object(); +/// let obj: PyObjectRef = PyInt::from(0).into_ref(vm).into(); /// /// let int_value = match_class!(match obj { /// i @ PyInt => i.as_bigint().clone(), diff --git a/vm/src/protocol/iter.rs b/vm/src/protocol/iter.rs index 6eb64f5fc..178c5473b 100644 --- a/vm/src/protocol/iter.rs +++ b/vm/src/protocol/iter.rs @@ -1,7 +1,7 @@ use crate::{ builtins::iter::PySequenceIterator, function::{IntoPyObject, IntoPyResult}, - PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, + PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, }; use std::borrow::Borrow; use std::ops::Deref; @@ -15,9 +15,6 @@ where O: Borrow; impl PyIter { - pub fn into_object(self) -> PyObjectRef { - self.0 - } pub fn check(obj: &PyObjectRef) -> bool { obj.class() .mro_find_map(|x| x.slots.iternext.load()) @@ -75,11 +72,17 @@ impl PyIter { } } -impl Borrow for PyIter +impl PyObjectWrap for PyIter { + fn into_object(self) -> PyObjectRef { + self.0 + } +} + +impl AsRef for PyIter where O: Borrow, { - fn borrow(&self) -> &PyObjectRef { + fn as_ref(&self) -> &PyObjectRef { self.0.borrow() } } @@ -96,7 +99,7 @@ where impl IntoPyObject for PyIter { fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { - self.into_object() + self.into() } } @@ -125,9 +128,7 @@ impl TryFromObject for PyIter { format!("'{}' object is not iterable", iter_target.class().name()) })?; Ok(Self( - PySequenceIterator::new(iter_target) - .into_ref(vm) - .into_object(), + PySequenceIterator::new(iter_target).into_ref(vm).into(), )) } } diff --git a/vm/src/protocol/mapping.rs b/vm/src/protocol/mapping.rs index aeaff22b8..174652c45 100644 --- a/vm/src/protocol/mapping.rs +++ b/vm/src/protocol/mapping.rs @@ -4,10 +4,9 @@ use crate::{ PyDictRef, PyList, }, function::IntoPyObject, - IdProtocol, PyObjectRef, PyResult, TryFromObject, TypeProtocol, VirtualMachine, + IdProtocol, PyObjectRef, PyObjectWrap, PyResult, TryFromObject, TypeProtocol, VirtualMachine, }; use std::borrow::Borrow; -use std::ops::Deref; // Mapping protocol // https://docs.python.org/3/c-api/mapping.html @@ -27,10 +26,6 @@ where T: Borrow; impl PyMapping { - pub fn into_object(self) -> PyObjectRef { - self.0 - } - pub fn check(obj: &PyObjectRef) -> bool { obj.class() .mro_find_map(|x| x.slots.as_mapping.load()) @@ -101,28 +96,24 @@ where } } -impl Borrow for PyMapping -where - T: Borrow, -{ - fn borrow(&self) -> &PyObjectRef { - self.0.borrow() +impl PyObjectWrap for PyMapping { + fn into_object(self) -> PyObjectRef { + self.0 } } -impl Deref for PyMapping +impl AsRef for PyMapping where - T: Borrow, + O: Borrow, { - type Target = PyObjectRef; - fn deref(&self) -> &Self::Target { + fn as_ref(&self) -> &PyObjectRef { self.0.borrow() } } impl IntoPyObject for PyMapping { fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { - self.into_object() + self.into() } } diff --git a/vm/src/py_io.rs b/vm/src/py_io.rs index dd267539f..d7855f0f6 100644 --- a/vm/src/py_io.rs +++ b/vm/src/py_io.rs @@ -77,7 +77,7 @@ pub fn file_readline(obj: &PyObjectRef, size: Option, vm: &VirtualMachine if let Some(nonl) = sval.strip_suffix('\n') { vm.ctx.new_utf8_str(nonl) } else { - s.into_object() + s.into() } } b @ PyBytes => { @@ -88,7 +88,7 @@ pub fn file_readline(obj: &PyObjectRef, size: Option, vm: &VirtualMachine if buf.last() == Some(&b'\n') { vm.ctx.new_bytes(buf[..buf.len() - 1].to_owned()) } else { - b.into_object() + b.into() } } _ => return Err(vm.new_type_error("object.readline() returned non-string".to_owned())), diff --git a/vm/src/py_serde.rs b/vm/src/py_serde.rs index d92b016d2..c4c66588d 100644 --- a/vm/src/py_serde.rs +++ b/vm/src/py_serde.rs @@ -208,6 +208,6 @@ impl<'de> Visitor<'de> for PyObjectDeserializer<'de> { while let Some((key_obj, value)) = access.next_entry_seed(self.clone(), self.clone())? { dict.set_item(key_obj, value, self.vm).unwrap(); } - Ok(dict.into_object()) + Ok(dict.into()) } } diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index e1d7aee94..f96b02c8d 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -3,7 +3,7 @@ use crate::common::{ rc::PyRc, static_cell, }; -pub use crate::pyobjectrc::{PyObject, PyObjectRef, PyObjectWeak, PyRef, PyWeakRef}; +pub use crate::pyobjectrc::{PyObject, PyObjectRef, PyObjectWeak, PyObjectWrap, PyRef, PyWeakRef}; use crate::{ builtins::{ builtinfunc::PyNativeFuncDef, @@ -120,7 +120,7 @@ impl PyContext { PyNativeFuncDef::new(PyType::__new__.into_func(), new_str).into_function(), &types.builtin_function_or_method_type, ) - .into_object(); + .into(); let context = PyContext { true_value, @@ -149,15 +149,15 @@ impl PyContext { } pub fn none(&self) -> PyObjectRef { - self.none.clone().into_object() + self.none.clone().into() } pub fn ellipsis(&self) -> PyObjectRef { - self.ellipsis.clone().into_object() + self.ellipsis.clone().into() } pub fn not_implemented(&self) -> PyObjectRef { - self.not_implemented.clone().into_object() + self.not_implemented.clone().into() } #[inline] @@ -235,11 +235,11 @@ impl PyContext { } else { &self.false_value }; - value.clone().into_object() + value.clone().into() } pub fn new_tuple(&self, elements: Vec) -> PyObjectRef { - PyTupleRef::with_elements(elements, self).into_object() + PyTupleRef::with_elements(elements, self).into() } pub fn new_list(&self, elements: Vec) -> PyObjectRef { @@ -449,18 +449,6 @@ pub(crate) fn pyref_type_error( )) } -impl<'a, T: PyValue> From<&'a PyRef> for &'a PyObjectRef { - fn from(obj: &'a PyRef) -> Self { - obj.as_object() - } -} - -impl From> for PyObjectRef { - fn from(obj: PyRef) -> Self { - obj.into_object() - } -} - impl fmt::Display for PyRef where T: PyObjectPayload + fmt::Display, @@ -507,7 +495,7 @@ impl Deref for PyRefExact { impl IntoPyObject for PyRefExact { #[inline] fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { - self.obj.into_object() + self.obj.into() } } impl TryIntoRef for PyRefExact { @@ -816,7 +804,7 @@ where impl IntoPyObject for PyRef { #[inline] fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { - self.into_object() + self.into() } } @@ -872,7 +860,7 @@ pub trait PyValue: fmt::Debug + PyThreadingConstraint + Sized + 'static { #[inline] fn into_object(self, vm: &VirtualMachine) -> PyObjectRef { - self.into_ref(vm).into_object() + self.into_ref(vm).into() } #[inline(always)] @@ -1005,8 +993,7 @@ pub trait PyClassImpl: PyClassDef { class.set_str_attr("__module__", ctx.new_utf8_str(module_name)); } if class.slots.new.load().is_some() { - let bound = - ctx.new_bound_method(ctx.slot_new_wrapper.clone(), class.clone().into_object()); + let bound = ctx.new_bound_method(ctx.slot_new_wrapper.clone(), class.clone().into()); class.set_str_attr("__new__", bound); } } @@ -1080,7 +1067,7 @@ pub trait PyStructSequence: StaticType + PyClassImpl + Sized + 'static { #[pymethod(magic)] fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.new_tuple(vec![ - zelf.clone_class().into_object(), + zelf.clone_class().into(), vm.ctx .new_tuple(vec![vm.ctx.new_tuple(zelf.as_slice().to_vec())]), ]) @@ -1187,7 +1174,7 @@ impl PyMethod { .is_some() { drop(descr_cls); - let cls = PyLease::into_pyref(cls).into_object(); + let cls = PyLease::into_pyref(cls).into(); return descr_get(descr, Some(obj), Some(cls), vm).map(Self::Attribute); } } @@ -1215,7 +1202,7 @@ impl PyMethod { }) } Some(descr_get) => { - let cls = PyLease::into_pyref(cls).into_object(); + let cls = PyLease::into_pyref(cls).into(); descr_get(attr, Some(obj), Some(cls), vm).map(Self::Attribute) } None => Ok(Self::Attribute(attr)), @@ -1254,7 +1241,7 @@ impl PyMethod { drop(obj_cls); Self::Function { target: obj, func } } else { - let obj_cls = PyLease::into_pyref(obj_cls).into_object(); + let obj_cls = PyLease::into_pyref(obj_cls).into(); let attr = vm .call_get_descriptor_specific(func, Some(obj), Some(obj_cls)) .unwrap_or_else(Ok)?; diff --git a/vm/src/pyobjectrc.rs b/vm/src/pyobjectrc.rs index 571561f38..2d7aa6b03 100644 --- a/vm/src/pyobjectrc.rs +++ b/vm/src/pyobjectrc.rs @@ -139,6 +139,18 @@ pub struct PyObjectWeak { weak: PyWeak>, } +pub trait PyObjectWrap +where + Self: AsRef, +{ + #[inline(always)] + fn as_object(&self) -> &PyObjectRef { + self.as_ref() + } + + fn into_object(self) -> PyObjectRef; +} + /// A marker type that just references a raw python object. Don't use directly, pass as a pointer /// back to [`PyObjectRef::from_raw`] pub enum RawPyObject {} @@ -283,12 +295,33 @@ impl PyObjectRef { } } +impl AsRef for PyObjectRef { + fn as_ref(&self) -> &Self { + self + } +} + impl IdProtocol for PyObjectRef { fn get_id(&self) -> usize { self.rc.get_id() } } +impl<'a, T: PyObjectPayload> From<&'a PyRef> for &'a PyObjectRef { + fn from(py_ref: &'a PyRef) -> Self { + &py_ref.obj + } +} + +impl From for PyObjectRef +where + T: PyObjectWrap, +{ + fn from(py_ref: T) -> Self { + py_ref.into_object() + } +} + impl PyObjectWeak { pub fn upgrade(&self) -> Option { self.weak.upgrade().map(|rc| PyObjectRef { rc }) @@ -394,11 +427,6 @@ impl PyRef { &self.obj } - #[inline(always)] - pub fn into_object(self) -> PyObjectRef { - self.obj - } - pub fn downgrade(this: &Self) -> PyWeakRef { PyWeakRef { weak: PyObjectRef::downgrade(&this.obj), @@ -418,6 +446,15 @@ impl PyRef { } } +impl PyObjectWrap for PyRef +where + T: PyObjectPayload, +{ + fn into_object(self) -> PyObjectRef { + self.obj + } +} + impl AsRef for PyRef where T: PyObjectPayload, diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs index 76dadbfe7..69cc7c6b3 100644 --- a/vm/src/stdlib/ast.rs +++ b/vm/src/stdlib/ast.rs @@ -46,7 +46,7 @@ pub(crate) struct AstNode; impl AstNode { #[pymethod(magic)] fn init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> { - let fields = vm.get_attribute(zelf.clone_class().into_object(), "_fields")?; + let fields = vm.get_attribute(zelf.clone_class().into(), "_fields")?; let fields = vm.extract_elements::(&fields)?; let numargs = args.args.len(); if numargs > fields.len() { @@ -269,7 +269,7 @@ pub(crate) fn compile( let code = rustpython_compiler_core::compile::compile_top(&ast, filename.to_owned(), opts) // TODO: use vm.new_syntax_error() .map_err(|err| vm.new_value_error(err.to_string()))?; - Ok(vm.new_code_object(code).into_object()) + Ok(vm.new_code_object(code).into()) } pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { diff --git a/vm/src/stdlib/ast/gen.rs b/vm/src/stdlib/ast/gen.rs index 544ced4c8..cea14de20 100644 --- a/vm/src/stdlib/ast/gen.rs +++ b/vm/src/stdlib/ast/gen.rs @@ -975,26 +975,26 @@ impl Node for ast::Mod { let _dict = _node.as_object().dict().unwrap(); _dict.set_item("body", body.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("type_ignores", type_ignores.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::Mod::Interactive { body } => { let _node = AstNode.into_ref_with_type(_vm, NodeInteractive::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("body", body.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::Mod::Expression { body } => { let _node = AstNode.into_ref_with_type(_vm, NodeExpression::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("body", body.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::Mod::FunctionType { argtypes,returns } => { let _node = AstNode.into_ref_with_type(_vm, NodeFunctionType::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("argtypes", argtypes.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("returns", returns.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } } } @@ -1043,7 +1043,7 @@ impl Node for ast::StmtKind { _dict.set_item("decorator_list", decorator_list.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("returns", returns.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("type_comment", type_comment.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::AsyncFunctionDef { name,args,body,decorator_list,returns,type_comment } => { let _node = AstNode.into_ref_with_type(_vm, NodeAsyncFunctionDef::static_type().clone()).unwrap(); @@ -1054,7 +1054,7 @@ impl Node for ast::StmtKind { _dict.set_item("decorator_list", decorator_list.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("returns", returns.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("type_comment", type_comment.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::ClassDef { name,bases,keywords,body,decorator_list } => { let _node = AstNode.into_ref_with_type(_vm, NodeClassDef::static_type().clone()).unwrap(); @@ -1064,19 +1064,19 @@ impl Node for ast::StmtKind { _dict.set_item("keywords", keywords.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("body", body.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("decorator_list", decorator_list.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::Return { value } => { let _node = AstNode.into_ref_with_type(_vm, NodeReturn::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::Delete { targets } => { let _node = AstNode.into_ref_with_type(_vm, NodeDelete::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("targets", targets.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::Assign { targets,value,type_comment } => { let _node = AstNode.into_ref_with_type(_vm, NodeAssign::static_type().clone()).unwrap(); @@ -1084,7 +1084,7 @@ impl Node for ast::StmtKind { _dict.set_item("targets", targets.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("type_comment", type_comment.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::AugAssign { target,op,value } => { let _node = AstNode.into_ref_with_type(_vm, NodeAugAssign::static_type().clone()).unwrap(); @@ -1092,7 +1092,7 @@ impl Node for ast::StmtKind { _dict.set_item("target", target.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("op", op.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::AnnAssign { target,annotation,value,simple } => { let _node = AstNode.into_ref_with_type(_vm, NodeAnnAssign::static_type().clone()).unwrap(); @@ -1101,7 +1101,7 @@ impl Node for ast::StmtKind { _dict.set_item("annotation", annotation.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("simple", simple.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::For { target,iter,body,orelse,type_comment } => { let _node = AstNode.into_ref_with_type(_vm, NodeFor::static_type().clone()).unwrap(); @@ -1111,7 +1111,7 @@ impl Node for ast::StmtKind { _dict.set_item("body", body.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("orelse", orelse.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("type_comment", type_comment.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::AsyncFor { target,iter,body,orelse,type_comment } => { let _node = AstNode.into_ref_with_type(_vm, NodeAsyncFor::static_type().clone()).unwrap(); @@ -1121,7 +1121,7 @@ impl Node for ast::StmtKind { _dict.set_item("body", body.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("orelse", orelse.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("type_comment", type_comment.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::While { test,body,orelse } => { let _node = AstNode.into_ref_with_type(_vm, NodeWhile::static_type().clone()).unwrap(); @@ -1129,7 +1129,7 @@ impl Node for ast::StmtKind { _dict.set_item("test", test.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("body", body.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("orelse", orelse.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::If { test,body,orelse } => { let _node = AstNode.into_ref_with_type(_vm, NodeIf::static_type().clone()).unwrap(); @@ -1137,7 +1137,7 @@ impl Node for ast::StmtKind { _dict.set_item("test", test.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("body", body.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("orelse", orelse.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::With { items,body,type_comment } => { let _node = AstNode.into_ref_with_type(_vm, NodeWith::static_type().clone()).unwrap(); @@ -1145,7 +1145,7 @@ impl Node for ast::StmtKind { _dict.set_item("items", items.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("body", body.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("type_comment", type_comment.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::AsyncWith { items,body,type_comment } => { let _node = AstNode.into_ref_with_type(_vm, NodeAsyncWith::static_type().clone()).unwrap(); @@ -1153,14 +1153,14 @@ impl Node for ast::StmtKind { _dict.set_item("items", items.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("body", body.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("type_comment", type_comment.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::Raise { exc,cause } => { let _node = AstNode.into_ref_with_type(_vm, NodeRaise::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("exc", exc.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("cause", cause.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::Try { body,handlers,orelse,finalbody } => { let _node = AstNode.into_ref_with_type(_vm, NodeTry::static_type().clone()).unwrap(); @@ -1169,20 +1169,20 @@ impl Node for ast::StmtKind { _dict.set_item("handlers", handlers.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("orelse", orelse.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("finalbody", finalbody.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::Assert { test,msg } => { let _node = AstNode.into_ref_with_type(_vm, NodeAssert::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("test", test.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("msg", msg.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::Import { names } => { let _node = AstNode.into_ref_with_type(_vm, NodeImport::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("names", names.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::ImportFrom { module,names,level } => { let _node = AstNode.into_ref_with_type(_vm, NodeImportFrom::static_type().clone()).unwrap(); @@ -1190,37 +1190,37 @@ impl Node for ast::StmtKind { _dict.set_item("module", module.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("names", names.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("level", level.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::Global { names } => { let _node = AstNode.into_ref_with_type(_vm, NodeGlobal::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("names", names.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::Nonlocal { names } => { let _node = AstNode.into_ref_with_type(_vm, NodeNonlocal::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("names", names.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::Expr { value } => { let _node = AstNode.into_ref_with_type(_vm, NodeExpr::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::Pass { } => { let _node = AstNode.into_ref_with_type(_vm, NodePass::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::Break { } => { let _node = AstNode.into_ref_with_type(_vm, NodeBreak::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::StmtKind::Continue { } => { let _node = AstNode.into_ref_with_type(_vm, NodeContinue::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } } } @@ -1410,14 +1410,14 @@ impl Node for ast::ExprKind { let _dict = _node.as_object().dict().unwrap(); _dict.set_item("op", op.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("values", values.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::NamedExpr { target,value } => { let _node = AstNode.into_ref_with_type(_vm, NodeNamedExpr::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("target", target.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::BinOp { left,op,right } => { let _node = AstNode.into_ref_with_type(_vm, NodeBinOp::static_type().clone()).unwrap(); @@ -1425,21 +1425,21 @@ impl Node for ast::ExprKind { _dict.set_item("left", left.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("op", op.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("right", right.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::UnaryOp { op,operand } => { let _node = AstNode.into_ref_with_type(_vm, NodeUnaryOp::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("op", op.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("operand", operand.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Lambda { args,body } => { let _node = AstNode.into_ref_with_type(_vm, NodeLambda::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("args", args.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("body", body.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::IfExp { test,body,orelse } => { let _node = AstNode.into_ref_with_type(_vm, NodeIfExp::static_type().clone()).unwrap(); @@ -1447,34 +1447,34 @@ impl Node for ast::ExprKind { _dict.set_item("test", test.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("body", body.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("orelse", orelse.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Dict { keys,values } => { let _node = AstNode.into_ref_with_type(_vm, NodeDict::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("keys", keys.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("values", values.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Set { elts } => { let _node = AstNode.into_ref_with_type(_vm, NodeSet::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("elts", elts.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::ListComp { elt,generators } => { let _node = AstNode.into_ref_with_type(_vm, NodeListComp::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("elt", elt.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("generators", generators.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::SetComp { elt,generators } => { let _node = AstNode.into_ref_with_type(_vm, NodeSetComp::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("elt", elt.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("generators", generators.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::DictComp { key,value,generators } => { let _node = AstNode.into_ref_with_type(_vm, NodeDictComp::static_type().clone()).unwrap(); @@ -1482,32 +1482,32 @@ impl Node for ast::ExprKind { _dict.set_item("key", key.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("generators", generators.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::GeneratorExp { elt,generators } => { let _node = AstNode.into_ref_with_type(_vm, NodeGeneratorExp::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("elt", elt.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("generators", generators.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Await { value } => { let _node = AstNode.into_ref_with_type(_vm, NodeAwait::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Yield { value } => { let _node = AstNode.into_ref_with_type(_vm, NodeYield::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::YieldFrom { value } => { let _node = AstNode.into_ref_with_type(_vm, NodeYieldFrom::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Compare { left,ops,comparators } => { let _node = AstNode.into_ref_with_type(_vm, NodeCompare::static_type().clone()).unwrap(); @@ -1515,7 +1515,7 @@ impl Node for ast::ExprKind { _dict.set_item("left", left.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("ops", ops.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("comparators", comparators.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Call { func,args,keywords } => { let _node = AstNode.into_ref_with_type(_vm, NodeCall::static_type().clone()).unwrap(); @@ -1523,7 +1523,7 @@ impl Node for ast::ExprKind { _dict.set_item("func", func.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("args", args.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("keywords", keywords.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::FormattedValue { value,conversion,format_spec } => { let _node = AstNode.into_ref_with_type(_vm, NodeFormattedValue::static_type().clone()).unwrap(); @@ -1531,20 +1531,20 @@ impl Node for ast::ExprKind { _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("conversion", conversion.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("format_spec", format_spec.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::JoinedStr { values } => { let _node = AstNode.into_ref_with_type(_vm, NodeJoinedStr::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("values", values.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Constant { value,kind } => { let _node = AstNode.into_ref_with_type(_vm, NodeConstant::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("kind", kind.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Attribute { value,attr,ctx } => { let _node = AstNode.into_ref_with_type(_vm, NodeAttribute::static_type().clone()).unwrap(); @@ -1552,7 +1552,7 @@ impl Node for ast::ExprKind { _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("attr", attr.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("ctx", ctx.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Subscript { value,slice,ctx } => { let _node = AstNode.into_ref_with_type(_vm, NodeSubscript::static_type().clone()).unwrap(); @@ -1560,35 +1560,35 @@ impl Node for ast::ExprKind { _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("slice", slice.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("ctx", ctx.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Starred { value,ctx } => { let _node = AstNode.into_ref_with_type(_vm, NodeStarred::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("ctx", ctx.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Name { id,ctx } => { let _node = AstNode.into_ref_with_type(_vm, NodeName::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("id", id.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("ctx", ctx.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::List { elts,ctx } => { let _node = AstNode.into_ref_with_type(_vm, NodeList::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("elts", elts.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("ctx", ctx.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Tuple { elts,ctx } => { let _node = AstNode.into_ref_with_type(_vm, NodeTuple::static_type().clone()).unwrap(); let _dict = _node.as_object().dict().unwrap(); _dict.set_item("elts", elts.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("ctx", ctx.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } ast::ExprKind::Slice { lower,upper,step } => { let _node = AstNode.into_ref_with_type(_vm, NodeSlice::static_type().clone()).unwrap(); @@ -1596,7 +1596,7 @@ impl Node for ast::ExprKind { _dict.set_item("lower", lower.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("upper", upper.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("step", step.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } } } @@ -1783,15 +1783,15 @@ impl Node for ast::ExprContext { match self { ast::ExprContext::Load { } => { let _node = AstNode.into_ref_with_type(_vm, NodeLoad::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::ExprContext::Store { } => { let _node = AstNode.into_ref_with_type(_vm, NodeStore::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::ExprContext::Del { } => { let _node = AstNode.into_ref_with_type(_vm, NodeDel::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } } } @@ -1823,11 +1823,11 @@ impl Node for ast::Boolop { match self { ast::Boolop::And { } => { let _node = AstNode.into_ref_with_type(_vm, NodeAnd::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Boolop::Or { } => { let _node = AstNode.into_ref_with_type(_vm, NodeOr::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } } } @@ -1855,55 +1855,55 @@ impl Node for ast::Operator { match self { ast::Operator::Add { } => { let _node = AstNode.into_ref_with_type(_vm, NodeAdd::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Operator::Sub { } => { let _node = AstNode.into_ref_with_type(_vm, NodeSub::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Operator::Mult { } => { let _node = AstNode.into_ref_with_type(_vm, NodeMult::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Operator::MatMult { } => { let _node = AstNode.into_ref_with_type(_vm, NodeMatMult::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Operator::Div { } => { let _node = AstNode.into_ref_with_type(_vm, NodeDiv::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Operator::Mod { } => { let _node = AstNode.into_ref_with_type(_vm, NodeMod::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Operator::Pow { } => { let _node = AstNode.into_ref_with_type(_vm, NodePow::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Operator::LShift { } => { let _node = AstNode.into_ref_with_type(_vm, NodeLShift::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Operator::RShift { } => { let _node = AstNode.into_ref_with_type(_vm, NodeRShift::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Operator::BitOr { } => { let _node = AstNode.into_ref_with_type(_vm, NodeBitOr::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Operator::BitXor { } => { let _node = AstNode.into_ref_with_type(_vm, NodeBitXor::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Operator::BitAnd { } => { let _node = AstNode.into_ref_with_type(_vm, NodeBitAnd::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Operator::FloorDiv { } => { let _node = AstNode.into_ref_with_type(_vm, NodeFloorDiv::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } } } @@ -1975,19 +1975,19 @@ impl Node for ast::Unaryop { match self { ast::Unaryop::Invert { } => { let _node = AstNode.into_ref_with_type(_vm, NodeInvert::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Unaryop::Not { } => { let _node = AstNode.into_ref_with_type(_vm, NodeNot::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Unaryop::UAdd { } => { let _node = AstNode.into_ref_with_type(_vm, NodeUAdd::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Unaryop::USub { } => { let _node = AstNode.into_ref_with_type(_vm, NodeUSub::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } } } @@ -2023,43 +2023,43 @@ impl Node for ast::Cmpop { match self { ast::Cmpop::Eq { } => { let _node = AstNode.into_ref_with_type(_vm, NodeEq::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Cmpop::NotEq { } => { let _node = AstNode.into_ref_with_type(_vm, NodeNotEq::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Cmpop::Lt { } => { let _node = AstNode.into_ref_with_type(_vm, NodeLt::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Cmpop::LtE { } => { let _node = AstNode.into_ref_with_type(_vm, NodeLtE::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Cmpop::Gt { } => { let _node = AstNode.into_ref_with_type(_vm, NodeGt::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Cmpop::GtE { } => { let _node = AstNode.into_ref_with_type(_vm, NodeGtE::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Cmpop::Is { } => { let _node = AstNode.into_ref_with_type(_vm, NodeIs::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Cmpop::IsNot { } => { let _node = AstNode.into_ref_with_type(_vm, NodeIsNot::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Cmpop::In { } => { let _node = AstNode.into_ref_with_type(_vm, NodeIn::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } ast::Cmpop::NotIn { } => { let _node = AstNode.into_ref_with_type(_vm, NodeNotIn::static_type().clone()).unwrap(); - _node.into_object() + _node.into() } } } @@ -2123,7 +2123,7 @@ impl Node for ast::Comprehension { _dict.set_item("iter", iter.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("ifs", ifs.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("is_async", is_async.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } fn ast_from_object(_vm: &VirtualMachine, _object: PyObjectRef) -> PyResult { Ok( @@ -2148,7 +2148,7 @@ impl Node for ast::ExcepthandlerKind { _dict.set_item("type", type_.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("name", name.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("body", body.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } } } @@ -2183,7 +2183,7 @@ impl Node for ast::Arguments { _dict.set_item("kw_defaults", kw_defaults.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("kwarg", kwarg.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("defaults", defaults.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } fn ast_from_object(_vm: &VirtualMachine, _object: PyObjectRef) -> PyResult { Ok( @@ -2210,7 +2210,7 @@ impl Node for ast::ArgData { _dict.set_item("arg", arg.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("annotation", annotation.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("type_comment", type_comment.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } fn ast_from_object(_vm: &VirtualMachine, _object: PyObjectRef) -> PyResult { let _location = ast::Location::new(Node::ast_from_object(_vm, get_node_field(_vm, &_object, "lineno", "arg")?)?, Node::ast_from_object(_vm, get_node_field(_vm, &_object, "col_offset", "arg")?)?); @@ -2233,7 +2233,7 @@ impl Node for ast::KeywordData { let _dict = _node.as_object().dict().unwrap(); _dict.set_item("arg", arg.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("value", value.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } fn ast_from_object(_vm: &VirtualMachine, _object: PyObjectRef) -> PyResult { let _location = ast::Location::new(Node::ast_from_object(_vm, get_node_field(_vm, &_object, "lineno", "keyword")?)?, Node::ast_from_object(_vm, get_node_field(_vm, &_object, "col_offset", "keyword")?)?); @@ -2255,7 +2255,7 @@ impl Node for ast::Alias { let _dict = _node.as_object().dict().unwrap(); _dict.set_item("name", name.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("asname", asname.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } fn ast_from_object(_vm: &VirtualMachine, _object: PyObjectRef) -> PyResult { Ok( @@ -2276,7 +2276,7 @@ impl Node for ast::Withitem { let _dict = _node.as_object().dict().unwrap(); _dict.set_item("context_expr", context_expr.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("optional_vars", optional_vars.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } fn ast_from_object(_vm: &VirtualMachine, _object: PyObjectRef) -> PyResult { Ok( @@ -2298,7 +2298,7 @@ impl Node for ast::TypeIgnore { let _dict = _node.as_object().dict().unwrap(); _dict.set_item("lineno", lineno.ast_to_object(_vm), _vm).unwrap(); _dict.set_item("tag", tag.ast_to_object(_vm), _vm).unwrap(); - _node.into_object() + _node.into() } } } diff --git a/vm/src/stdlib/builtins.rs b/vm/src/stdlib/builtins.rs index 8accf77ea..77ff20b89 100644 --- a/vm/src/stdlib/builtins.rs +++ b/vm/src/stdlib/builtins.rs @@ -169,10 +169,10 @@ mod builtins { let mode = mode_str .parse::() .map_err(|err| vm.new_value_error(err.to_string()))?; - - vm.compile(source, mode, args.filename.as_str().to_owned()) - .map(|o| o.into_object()) - .map_err(|err| vm.new_syntax_error(&err)) + let code = vm + .compile(source, mode, args.filename.as_str().to_owned()) + .map_err(|err| vm.new_syntax_error(&err))?; + Ok(code.into()) } } else { let mode = mode_str @@ -186,7 +186,7 @@ mod builtins { #[pyfunction] fn delattr(obj: PyObjectRef, attr: PyStrRef, vm: &VirtualMachine) -> PyResult<()> { - vm.del_attr(&obj, attr.into_object()) + vm.del_attr(&obj, attr) } #[pyfunction] @@ -223,7 +223,7 @@ mod builtins { let (globals, locals) = match self.globals { Some(globals) => { if !globals.contains_key("__builtins__", vm) { - let builtins_dict = vm.builtins.dict().unwrap().into_object(); + let builtins_dict = vm.builtins.dict().unwrap().into(); globals.set_item("__builtins__", builtins_dict, vm)?; } let locals = self.locals.unwrap_or_else(|| globals.clone()); @@ -421,7 +421,7 @@ mod builtins { let callable = ArgCallable::try_from_object(vm, iter_target)?; let iterator = PyCallableIterator::new(callable, sentinel) .into_ref(vm) - .into_object(); + .into(); Ok(PyIter::new(iterator)) } else { iter_target.get_iter(vm) @@ -738,7 +738,7 @@ mod builtins { value: PyObjectRef, vm: &VirtualMachine, ) -> PyResult<()> { - vm.set_attr(&obj, attr.into_object(), value)?; + vm.set_attr(&obj, attr, value)?; Ok(()) } @@ -799,7 +799,7 @@ mod builtins { vm.new_type_error("vars() argument must have __dict__ attribute".to_owned()) }) } else { - Ok(vm.current_locals()?.into_object()) + Ok(vm.current_locals()?.into()) } } @@ -867,10 +867,10 @@ mod builtins { } } - let bases = bases.into_object(); + let bases: PyObjectRef = bases.into(); // Prepare uses full __getattribute__ resolution chain. - let prepare = vm.get_attribute(metaclass.clone().into_object(), "__prepare__")?; + let prepare = vm.get_attribute(metaclass.clone().into(), "__prepare__")?; let namespace = vm.invoke( &prepare, FuncArgs::new(vec![name_obj.clone(), bases.clone()], kwargs.clone()), @@ -882,12 +882,12 @@ mod builtins { let classcell = >::try_from_object(vm, classcell)?; if let Some(orig_bases) = orig_bases { - namespace.set_item("__orig_bases__", orig_bases.into_object(), vm)?; + namespace.set_item("__orig_bases__", orig_bases.into(), vm)?; } let class = vm.invoke( metaclass.as_object(), - FuncArgs::new(vec![name_obj, bases, namespace.into_object()], kwargs), + FuncArgs::new(vec![name_obj, bases, namespace.into()], kwargs), )?; if let Some(ref classcell) = classcell { diff --git a/vm/src/stdlib/codecs.rs b/vm/src/stdlib/codecs.rs index 8a8d34386..4c458fff9 100644 --- a/vm/src/stdlib/codecs.rs +++ b/vm/src/stdlib/codecs.rs @@ -21,7 +21,7 @@ mod _codecs { vm.state .codec_registry .lookup(encoding.as_str(), vm) - .map(|codec| codec.into_tuple().into_object()) + .map(|codec| codec.into_tuple().into()) } #[derive(FromArgs)] diff --git a/vm/src/stdlib/collections.rs b/vm/src/stdlib/collections.rs index f162f21ca..0031c756f 100644 --- a/vm/src/stdlib/collections.rs +++ b/vm/src/stdlib/collections.rs @@ -521,24 +521,12 @@ mod _collections { #[pymethod(magic)] fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyResult { - let cls = zelf.clone_class().into_object(); - Ok(match zelf.maxlen { - Some(v) => vm.ctx.new_tuple(vec![ - cls, - vm.ctx.new_tuple(vec![ - vm.ctx.empty_tuple.clone().into_object(), - vm.ctx.new_int(v), - ]), - vm.ctx.none(), - PyDequeIterator::new(zelf).into_object(vm), - ]), - None => vm.ctx.new_tuple(vec![ - cls, - vm.ctx.empty_tuple.clone().into_object(), - vm.ctx.none(), - PyDequeIterator::new(zelf).into_object(vm), - ]), - }) + let cls = zelf.clone_class(); + let value = match zelf.maxlen { + Some(v) => vm.new_pyobj((vm.ctx.empty_tuple.clone(), v)), + None => vm.ctx.empty_tuple.clone().into(), + }; + Ok(vm.new_pyobj((cls, value, vm.ctx.none(), PyDequeIterator::new(zelf)))) } } diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 5dd3b2d49..b1b81de47 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -2418,7 +2418,7 @@ mod _io { let start_of_stream = cookie.start_pos == 0 && cookie.dec_flags == 0; reset_encoder(encoder, start_of_stream)?; } - Ok(cookie_obj.into_object()) + Ok(cookie_obj.into()) } #[pymethod] diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index fe2f146cf..e93e3317c 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -492,7 +492,7 @@ mod decl { } }; let pred = predicate.clone(); - let pred_value = vm.invoke(&pred.into_object(), (obj.clone(),))?; + let pred_value = vm.invoke(&pred, (obj.clone(),))?; if !pred_value.try_to_bool(vm)? { zelf.start_flag.store(true); return Ok(PyIterReturn::Return(obj)); @@ -1017,7 +1017,7 @@ mod decl { tee_vec.push(vm.call_method(©able, "__copy__", ())?); } - Ok(PyTupleRef::with_elements(tee_vec, &vm.ctx).into_object()) + Ok(PyTupleRef::with_elements(tee_vec, &vm.ctx).into()) } } @@ -1033,17 +1033,15 @@ mod decl { index: AtomicCell::new(0), } .into_ref_with_type(vm, class.clone())? - .into_object()) + .into()) } #[pymethod(magic)] - fn copy(&self, vm: &VirtualMachine) -> PyResult { - Ok(PyItertoolsTee { + fn copy(&self) -> Self { + Self { tee_data: PyRc::clone(&self.tee_data), index: AtomicCell::new(self.index.load()), } - .into_ref_with_type(vm, Self::class(vm).clone())? - .into_object()) } } impl IteratorIterable for PyItertoolsTee {} diff --git a/vm/src/stdlib/nt.rs b/vm/src/stdlib/nt.rs index 3fa8227ae..58572c6f6 100644 --- a/vm/src/stdlib/nt.rs +++ b/vm/src/stdlib/nt.rs @@ -239,7 +239,7 @@ pub(crate) mod module { let path = make_widestring(path.as_str())?; - let argv = vm.extract_elements_func(argv.as_object(), |obj| { + let argv = vm.extract_elements_func(argv.as_ref(), |obj| { let arg = PyStrRef::try_from_object(vm, obj)?; make_widestring(arg.as_str()) })?; diff --git a/vm/src/stdlib/operator.rs b/vm/src/stdlib/operator.rs index 1dc5a18db..87c728b60 100644 --- a/vm/src/stdlib/operator.rs +++ b/vm/src/stdlib/operator.rs @@ -473,9 +473,7 @@ mod _operator { let attrs = vm .ctx .new_tuple(zelf.attrs.iter().map(|v| v.as_object()).cloned().collect()); - Ok(vm - .ctx - .new_tuple(vec![zelf.clone_class().into_object(), attrs])) + Ok(vm.new_pyobj((zelf.clone_class(), attrs))) } // Go through dotted parts of string and call getattr on whatever is returned. @@ -559,8 +557,7 @@ mod _operator { #[pymethod(magic)] fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyObjectRef { let items = vm.ctx.new_tuple(zelf.items.to_vec()); - vm.ctx - .new_tuple(vec![zelf.clone_class().into_object(), items]) + vm.new_pyobj((zelf.clone_class(), items)) } fn call(zelf: &PyRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { @@ -647,10 +644,9 @@ mod _operator { if zelf.args.kwargs.is_empty() { let mut pargs = vec![zelf.name.as_object().to_owned()]; pargs.append(&mut zelf.args.args.clone()); - Ok(vm.ctx.new_tuple(vec![ - zelf.clone_class().into_object(), - vm.ctx.new_tuple(pargs), - ])) + Ok(vm + .ctx + .new_tuple(vec![zelf.clone_class().into(), vm.ctx.new_tuple(pargs)])) } else { // If we have kwargs, create a partial function that contains them and pass back that // along with the args. @@ -658,10 +654,7 @@ mod _operator { let callable = vm.invoke( &partial, FuncArgs::new( - vec![ - zelf.clone_class().into_object(), - zelf.name.as_object().to_owned(), - ], + vec![zelf.clone_class().into(), zelf.name.as_object().to_owned()], KwArgs::new(zelf.args.kwargs.clone()), ), )?; diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index a0104460c..b19e411e7 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -182,8 +182,8 @@ impl FsPath { impl IntoPyObject for FsPath { fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { match self { - Self::Str(s) => s.into_object(), - Self::Bytes(b) => b.into_object(), + Self::Str(s) => s.into(), + Self::Bytes(b) => b.into(), } } } @@ -914,7 +914,7 @@ pub(super) mod _os { ino: AtomicCell::new(None), } .into_ref(vm) - .into_object(), + .into(), )), Err(err) => Err(err.into_pyexception(vm)), }, @@ -940,7 +940,7 @@ pub(super) mod _os { mode: path.mode, } .into_ref(vm) - .into_object()) + .into()) } #[pyattr] @@ -1744,9 +1744,9 @@ pub fn extend_module(vm: &VirtualMachine, module: &PyObjectRef) { } extend_module!(vm, module, { - "supports_fd" => supports_fd.into_object(), - "supports_dir_fd" => supports_dir_fd.into_object(), - "supports_follow_symlinks" => supports_follow_symlinks.into_object(), + "supports_fd" => supports_fd, + "supports_dir_fd" => supports_dir_fd, + "supports_follow_symlinks" => supports_follow_symlinks, "error" => vm.ctx.exceptions.os_error.clone(), }); } diff --git a/vm/src/stdlib/posix.rs b/vm/src/stdlib/posix.rs index 69e4249a0..1179f36c7 100644 --- a/vm/src/stdlib/posix.rs +++ b/vm/src/stdlib/posix.rs @@ -774,7 +774,7 @@ pub mod module { ) -> PyResult<()> { let path = path.into_cstring(vm)?; - let argv = vm.extract_elements_func(argv.as_object(), |obj| { + let argv = vm.extract_elements_func(argv.as_ref(), |obj| { PyStrRef::try_from_object(vm, obj)?.to_cstring(vm) })?; let argv: Vec<&CStr> = argv.iter().map(|entry| entry.as_c_str()).collect(); @@ -802,7 +802,7 @@ pub mod module { ) -> PyResult<()> { let path = path.into_cstring(vm)?; - let argv = vm.extract_elements_func(argv.as_object(), |obj| { + let argv = vm.extract_elements_func(argv.as_ref(), |obj| { PyStrRef::try_from_object(vm, obj)?.to_cstring(vm) })?; let argv: Vec<&CStr> = argv.iter().map(|entry| entry.as_c_str()).collect(); diff --git a/vm/src/stdlib/pystruct.rs b/vm/src/stdlib/pystruct.rs index 6258b23f5..0e7348b82 100644 --- a/vm/src/stdlib/pystruct.rs +++ b/vm/src/stdlib/pystruct.rs @@ -839,7 +839,7 @@ pub(crate) mod _struct { if let Some(buf) = buf.get(offset..offset + size) { zelf.format_spec .unpack(buf, vm) - .map(|x| PyIterReturn::Return(x.into_object())) + .map(|x| PyIterReturn::Return(x.into())) } else { Ok(PyIterReturn::StopIteration(None)) } diff --git a/vm/src/stdlib/sre.rs b/vm/src/stdlib/sre.rs index a28368ace..4dbba5d05 100644 --- a/vm/src/stdlib/sre.rs +++ b/vm/src/stdlib/sre.rs @@ -268,7 +268,7 @@ mod _sre { OptionalArg::Present(vm.ctx.new_ascii_literal(ascii!(""))), vm, )? - .into_object() + .into() }; matchlist.push(item); @@ -296,7 +296,7 @@ mod _sre { must_advance: AtomicCell::new(false), } .into_ref(vm); - let search = vm.get_method(scanner.into_object(), "search").unwrap()?; + let search = vm.get_method(scanner.into(), "search").unwrap()?; let search = ArgCallable::try_from_object(vm, search)?; let iterator = PyCallableIterator::new(search, vm.ctx.none()); Ok(iterator) @@ -613,8 +613,8 @@ mod _sre { .and_then(|i| self.pattern.indexgroup.get(i).cloned().flatten()) } #[pyproperty] - fn re(&self) -> PyObjectRef { - self.pattern.clone().into_object() + fn re(&self) -> PyRef { + self.pattern.clone() } #[pyproperty] fn string(&self) -> PyObjectRef { diff --git a/vm/src/stdlib/symtable.rs b/vm/src/stdlib/symtable.rs index 53667311a..c5326cce1 100644 --- a/vm/src/stdlib/symtable.rs +++ b/vm/src/stdlib/symtable.rs @@ -126,7 +126,7 @@ mod symtable { .collect(), }) .into_ref(vm) - .into_object() + .into() }) .collect(); Ok(vm.ctx.new_list(symbols)) @@ -243,7 +243,7 @@ mod symtable { } else { Ok(to_py_symbol_table(self.namespaces.first().unwrap().clone()) .into_ref(vm) - .into_object()) + .into()) } } } diff --git a/vm/src/stdlib/sys.rs b/vm/src/stdlib/sys.rs index e7c189877..828e1b2c0 100644 --- a/vm/src/stdlib/sys.rs +++ b/vm/src/stdlib/sys.rs @@ -265,7 +265,7 @@ fn sys_displayhook(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { // set to none to avoid recursion while printing vm.set_attr(&vm.builtins, "_", vm.ctx.none())?; // TODO: catch encoding errors - let repr = vm.to_repr(&obj)?.into_object(); + let repr = vm.to_repr(&obj)?.into(); builtins::print(PosArgs::new(vec![repr]), Default::default(), vm)?; vm.set_attr(&vm.builtins, "_", obj)?; Ok(()) diff --git a/vm/src/stdlib/thread.rs b/vm/src/stdlib/thread.rs index 3a903a3a1..29745e200 100644 --- a/vm/src/stdlib/thread.rs +++ b/vm/src/stdlib/thread.rs @@ -253,7 +253,7 @@ fn run_thread(func: ArgCallable, args: FuncArgs, vm: &VirtualMachine) { // TODO: sys.unraisablehook let stderr = std::io::stderr(); let mut stderr = py_io::IoWriter(stderr.lock()); - let repr = vm.to_repr(&func.into_object()).ok(); + let repr = vm.to_repr(func.as_ref()).ok(); let repr = repr .as_ref() .map_or("", |s| s.as_str()); @@ -319,12 +319,15 @@ impl SlotGetattro for PyLocal { fn getattro(zelf: PyRef, attr: PyStrRef, vm: &VirtualMachine) -> PyResult { let ldict = zelf.ldict(vm); if attr.as_str() == "__dict__" { - Ok(ldict.into_object()) + Ok(ldict.into()) } else { - let zelf = zelf.into_object(); - vm.generic_getattribute_opt(zelf.clone(), attr.clone(), Some(ldict))? + vm.generic_getattribute_opt(zelf.clone().into(), attr.clone(), Some(ldict))? .ok_or_else(|| { - vm.new_attribute_error(format!("{} has no attribute '{}'", zelf, attr)) + vm.new_attribute_error(format!( + "{} has no attribute '{}'", + zelf.as_object(), + attr + )) }) } } diff --git a/vm/src/stdlib/winapi.rs b/vm/src/stdlib/winapi.rs index b2d9c9b92..7668d1064 100644 --- a/vm/src/stdlib/winapi.rs +++ b/vm/src/stdlib/winapi.rs @@ -266,7 +266,7 @@ fn getattributelist(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult>::try_from_object(vm, obj)? .map(|mapping| { let handlelist = mapping - .into_object() + .as_ref() .get_item("handle_list", vm) .ok() .and_then(|obj| { diff --git a/vm/src/utils.rs b/vm/src/utils.rs index 981f94b0e..081211ec2 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -1,7 +1,7 @@ use crate::{ builtins::{PyFloat, PyStr}, function::{IntoPyException, IntoPyObject}, - PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, + PyObjectRef, PyObjectWrap, PyResult, TryFromObject, TypeProtocol, VirtualMachine, }; use num_traits::ToPrimitive; @@ -10,15 +10,17 @@ pub enum Either { B(B), } -impl Either, PyRef> { - pub fn as_object(&self) -> &PyObjectRef { +impl, B: AsRef> AsRef for Either { + fn as_ref(&self) -> &PyObjectRef { match self { - Either::A(a) => a.as_object(), - Either::B(b) => b.as_object(), + Either::A(a) => a.as_ref(), + Either::B(b) => b.as_ref(), } } +} - pub fn into_object(self) -> PyObjectRef { +impl PyObjectWrap for Either { + fn into_object(self) -> PyObjectRef { match self { Either::A(a) => a.into_object(), Either::B(b) => b.into_object(), diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 6da776c7f..bb98f9eb7 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -1151,7 +1151,7 @@ impl VirtualMachine { descr: PyObjectRef, obj: PyObjectRef, ) -> Result { - let cls = obj.clone_class().into_object(); + let cls = obj.clone_class().into(); self.call_get_descriptor_specific(descr, Some(obj), Some(cls)) } @@ -1210,11 +1210,12 @@ impl VirtualMachine { } #[inline(always)] - pub fn invoke(&self, func_ref: &PyObjectRef, args: T) -> PyResult + pub fn invoke(&self, func: &O, args: A) -> PyResult where - T: IntoFuncArgs, + O: AsRef, + A: IntoFuncArgs, { - self._invoke(func_ref, args.into_args(self)) + self._invoke(func.as_ref(), args.into_args(self)) } /// Call registered trace function. @@ -1528,7 +1529,7 @@ impl VirtualMachine { .is_some() { drop(descr_cls); - let cls = PyLease::into_pyref(obj_cls).into_object(); + let cls = PyLease::into_pyref(obj_cls).into(); return descr_get(descr, Some(obj), Some(cls), self).map(Some); } } @@ -1551,7 +1552,7 @@ impl VirtualMachine { } else if let Some((attr, descr_get)) = cls_attr { match descr_get { Some(descr_get) => { - let cls = PyLease::into_pyref(obj_cls).into_object(); + let cls = PyLease::into_pyref(obj_cls).into(); descr_get(attr, Some(obj), Some(cls), self).map(Some) } None => Ok(Some(attr)), diff --git a/wasm/lib/src/browser_module.rs b/wasm/lib/src/browser_module.rs index 6f0577c55..f7b5b24d7 100644 --- a/wasm/lib/src/browser_module.rs +++ b/wasm/lib/src/browser_module.rs @@ -124,7 +124,7 @@ fn browser_request_animation_frame(func: ArgCallable, vm: &VirtualMachine) -> Py stored_vm.interp.enter(|vm| { let func = func.clone(); let args = vec![vm.ctx.new_float(time)]; - let _ = vm.invoke(&func.into_object(), args); + let _ = vm.invoke(&func, args); let closure = f.borrow_mut().take(); drop(closure); diff --git a/wasm/lib/src/convert.rs b/wasm/lib/src/convert.rs index 75521b57f..450f46542 100644 --- a/wasm/lib/src/convert.rs +++ b/wasm/lib/src/convert.rs @@ -176,7 +176,7 @@ pub fn js_to_py(vm: &VirtualMachine, js_val: JsValue) -> PyObjectRef { if vm.try_class("browser", "Promise").is_ok() { return js_module::PyPromise::new(promise.clone()) .into_ref(vm) - .into_object(); + .into(); } } if Array::is_array(&js_val) { @@ -211,7 +211,7 @@ pub fn js_to_py(vm: &VirtualMachine, js_val: JsValue) -> PyObjectRef { ) .unwrap(); } - dict.into_object() + dict.into() } } else if js_val.is_function() { let func = js_sys::Function::from(js_val); @@ -234,7 +234,7 @@ pub fn js_to_py(vm: &VirtualMachine, js_val: JsValue) -> PyObjectRef { }, ) } else if let Some(err) = js_val.dyn_ref::() { - js_err_to_py_err(vm, err).into_object() + js_err_to_py_err(vm, err).into() } else if js_val.is_undefined() { // Because `JSON.stringify(undefined)` returns undefined vm.ctx.none() diff --git a/wasm/lib/src/js_module.rs b/wasm/lib/src/js_module.rs index 5f69018cc..b9e6edf87 100644 --- a/wasm/lib/src/js_module.rs +++ b/wasm/lib/src/js_module.rs @@ -1,19 +1,22 @@ -use crate::convert; -use crate::vm_class::{stored_vm_from_wasm, WASMVirtualMachine}; -use crate::weak_vm; +use crate::{ + convert, + vm_class::{stored_vm_from_wasm, WASMVirtualMachine}, + weak_vm, +}; use js_sys::{Array, Object, Promise, Reflect}; -use std::{cell, fmt, future}; -use wasm_bindgen::{closure::Closure, prelude::*, JsCast}; -use wasm_bindgen_futures::{future_to_promise, JsFuture}; - use rustpython_vm::{ builtins::{PyBaseExceptionRef, PyFloatRef, PyStrRef, PyTypeRef}, function::{ArgCallable, IntoPyObject, OptionalArg, OptionalOption, PosArgs}, protocol::PyIterReturn, slots::{IteratorIterable, SlotIterator}, types::create_simple_type, - PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, + PyClassImpl, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject, + VirtualMachine, }; +use std::ops::Deref; +use std::{cell, fmt, future}; +use wasm_bindgen::{closure::Closure, prelude::*, JsCast}; +use wasm_bindgen_futures::{future_to_promise, JsFuture}; #[wasm_bindgen(inline_js = " export function has_prop(target, prop) { return prop in Object(target); } @@ -172,7 +175,7 @@ impl PyJsValue { .value .dyn_ref::() .ok_or_else(|| vm.new_type_error("JS value is not callable".to_owned()))?; - let js_args = args.iter().map(|x| x.as_ref()).collect::(); + let js_args = args.iter().map(|x| x.deref()).collect::(); let res = match opts.this { Some(this) => Reflect::apply(func, &this.value, &js_args), None => call_func(func, &js_args), @@ -187,7 +190,7 @@ impl PyJsValue { args: PosArgs, vm: &VirtualMachine, ) -> PyResult { - let js_args = args.iter().map(|x| x.as_ref()).collect::(); + let js_args = args.iter().map(|x| x.deref()).collect::(); call_method(&self.value, &name.into_jsvalue(), &js_args) .map(PyJsValue::new) .map_err(|err| new_js_error(vm, err)) @@ -208,7 +211,7 @@ impl PyJsValue { .prototype .as_ref() .and_then(|proto| proto.value.dyn_ref::()); - let js_args = args.iter().map(|x| x.as_ref()).collect::(); + let js_args = args.iter().map(|x| x.deref()).collect::(); let constructed_result = if let Some(proto) = proto { Reflect::construct_with_new_target(ctor, &js_args, proto) } else { @@ -530,7 +533,7 @@ impl PyPromise { #[pymethod(name = "__await__")] fn r#await(zelf: PyRef) -> AwaitPromise { AwaitPromise { - obj: Some(zelf.into_object()).into(), + obj: Some(zelf.into()).into(), } } } From 86430f70d8f6929330e20fa56380455870c47f9c Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 5 Oct 2021 06:07:54 +0900 Subject: [PATCH 4/5] Remove PyIter::as_object --- vm/src/builtins/map.rs | 2 +- vm/src/protocol/iter.rs | 5 +---- vm/src/vm.rs | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/vm/src/builtins/map.rs b/vm/src/builtins/map.rs index f2102f6e9..f9a3677a3 100644 --- a/vm/src/builtins/map.rs +++ b/vm/src/builtins/map.rs @@ -37,7 +37,7 @@ impl PyMap { #[pymethod(magic)] fn length_hint(&self, vm: &VirtualMachine) -> PyResult { self.iterators.iter().try_fold(0, |prev, cur| { - let cur = vm.length_hint(cur.as_object().clone())?.unwrap_or(0); + let cur = vm.length_hint(cur.as_ref().clone())?.unwrap_or(0); let max = std::cmp::max(prev, cur); Ok(max) }) diff --git a/vm/src/protocol/iter.rs b/vm/src/protocol/iter.rs index 178c5473b..30dc177bd 100644 --- a/vm/src/protocol/iter.rs +++ b/vm/src/protocol/iter.rs @@ -29,9 +29,6 @@ where pub fn new(obj: O) -> Self { Self(obj) } - pub fn as_object(&self) -> &PyObjectRef { - self.0.borrow() - } pub fn next(&self, vm: &VirtualMachine) -> PyResult { let iternext = { self.0 @@ -52,7 +49,7 @@ where &'b self, vm: &'a VirtualMachine, ) -> PyResult> { - let length_hint = vm.length_hint(self.as_object().clone())?; + let length_hint = vm.length_hint(self.as_ref().clone())?; Ok(PyIterIter::new(vm, self.0.borrow(), length_hint)) } diff --git a/vm/src/vm.rs b/vm/src/vm.rs index bb98f9eb7..1c446d502 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -1349,7 +1349,7 @@ impl VirtualMachine { } } - let mut results = PyIterIter::new(self, iter.as_object(), cap) + let mut results = PyIterIter::new(self, iter.as_ref(), cap) .map(|element| f(element?)) .collect::>>()?; results.shrink_to_fit(); From e19609ced3c1533a14da83c62915bff5e370a31f Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 5 Oct 2021 17:26:01 +0900 Subject: [PATCH 5/5] Use PyRef::into_object(vm) instead of into_ref(vm).into() combo --- stdlib/src/hashlib.rs | 2 +- vm/src/builtins/pysuper.rs | 2 +- vm/src/macros.rs | 8 ++++---- vm/src/protocol/iter.rs | 4 +--- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/stdlib/src/hashlib.rs b/stdlib/src/hashlib.rs index 4f2f6f71e..5dd51bd2f 100644 --- a/stdlib/src/hashlib.rs +++ b/stdlib/src/hashlib.rs @@ -48,7 +48,7 @@ mod hashlib { #[pyslot] fn slot_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult { - Ok(PyHasher::new("md5", HashWrapper::md5()).into_ref(vm).into()) + Ok(PyHasher::new("md5", HashWrapper::md5()).into_object(vm)) } #[pyproperty] diff --git a/vm/src/builtins/pysuper.rs b/vm/src/builtins/pysuper.rs index cf813646c..5cc9d6c1b 100644 --- a/vm/src/builtins/pysuper.rs +++ b/vm/src/builtins/pysuper.rs @@ -169,7 +169,7 @@ impl SlotDescriptor for PySuper { } let zelf_class = zelf.as_object().class(); if zelf_class.is(&vm.ctx.types.super_type) { - Ok(PySuper::new(zelf.typ.clone(), obj, vm)?.into_ref(vm).into()) + Ok(PySuper::new(zelf.typ.clone(), obj, vm)?.into_object(vm)) } else { let obj = vm.unwrap_or_none(zelf.obj.clone().map(|(o, _)| o)); vm.invoke( diff --git a/vm/src/macros.rs b/vm/src/macros.rs index 365a8f070..60b3324a4 100644 --- a/vm/src/macros.rs +++ b/vm/src/macros.rs @@ -78,10 +78,10 @@ macro_rules! py_namespace { /// /// use rustpython_vm::match_class; /// use rustpython_vm::builtins::{PyFloat, PyInt}; -/// use rustpython_vm::{PyObjectRef, PyValue}; +/// use rustpython_vm::{PyValue}; /// /// # rustpython_vm::Interpreter::default().enter(|vm| { -/// let obj: PyObjectRef = PyInt::from(0).into_ref(&vm).into(); +/// let obj = PyInt::from(0).into_object(vm); /// assert_eq!( /// "int", /// match_class!(match obj { @@ -102,10 +102,10 @@ macro_rules! py_namespace { /// /// use rustpython_vm::match_class; /// use rustpython_vm::builtins::{PyFloat, PyInt}; -/// use rustpython_vm::{PyObjectRef, PyValue}; +/// use rustpython_vm::{ PyValue}; /// /// # rustpython_vm::Interpreter::default().enter(|vm| { -/// let obj: PyObjectRef = PyInt::from(0).into_ref(vm).into(); +/// let obj = PyInt::from(0).into_object(vm); /// /// let int_value = match_class!(match obj { /// i @ PyInt => i.as_bigint().clone(), diff --git a/vm/src/protocol/iter.rs b/vm/src/protocol/iter.rs index 30dc177bd..24f34f6ff 100644 --- a/vm/src/protocol/iter.rs +++ b/vm/src/protocol/iter.rs @@ -124,9 +124,7 @@ impl TryFromObject for PyIter { vm.get_method_or_type_error(iter_target.clone(), "__getitem__", || { format!("'{}' object is not iterable", iter_target.class().name()) })?; - Ok(Self( - PySequenceIterator::new(iter_target).into_ref(vm).into(), - )) + Ok(Self(PySequenceIterator::new(iter_target).into_object(vm))) } } }