From dca59e983258639dc812e3a605fb7e88cf39f63c Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Sun, 17 Apr 2022 09:34:59 +0900 Subject: [PATCH 1/9] Add borrow for wrapping types --- vm/src/function/argument.rs | 14 +++++++++++++- vm/src/protocol/iter.rs | 9 +++++++++ vm/src/pyobject.rs | 10 ++++++++-- vm/src/pyobjectrc.rs | 15 +++++++++++++++ vm/src/utils.rs | 10 ++++++++++ 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/vm/src/function/argument.rs b/vm/src/function/argument.rs index 672271a185..18a9a81b72 100644 --- a/vm/src/function/argument.rs +++ b/vm/src/function/argument.rs @@ -6,7 +6,7 @@ use crate::{ PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, }; -use std::marker::PhantomData; +use std::{borrow::Borrow, marker::PhantomData}; #[derive(Clone, Debug)] pub struct ArgCallable { @@ -20,6 +20,12 @@ impl ArgCallable { } } +impl Borrow for ArgCallable { + fn borrow(&self) -> &PyObject { + &self.obj + } +} + impl AsRef for ArgCallable { fn as_ref(&self) -> &PyObject { &self.obj @@ -109,6 +115,12 @@ impl ArgMapping { } } +impl Borrow for ArgMapping { + fn borrow(&self) -> &PyObject { + &self.obj + } +} + impl AsRef for ArgMapping { fn as_ref(&self) -> &PyObject { &self.obj diff --git a/vm/src/protocol/iter.rs b/vm/src/protocol/iter.rs index 55a752b3c8..a79534694f 100644 --- a/vm/src/protocol/iter.rs +++ b/vm/src/protocol/iter.rs @@ -76,6 +76,15 @@ impl PyObjectWrap for PyIter { } } +impl Borrow for PyIter +where + O: Borrow, +{ + fn borrow(&self) -> &PyObject { + self.0.borrow() + } +} + impl AsRef for PyIter where O: Borrow, diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index edc2a2dcf2..2171fd7ae8 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -23,7 +23,7 @@ use crate::{ }; use num_bigint::BigInt; use num_traits::ToPrimitive; -use std::{any::Any, fmt, ops::Deref}; +use std::{any::Any, borrow::Borrow, fmt, ops::Deref}; /* Python objects and references. @@ -443,8 +443,14 @@ impl<'a, T: PyObjectPayload + PyValue> PyLease<'a, T> { } } +impl<'a, T: PyObjectPayload + PyValue> Borrow for PyLease<'a, T> { + fn borrow(&self) -> &PyObject { + self.inner.as_object() + } +} + impl<'a, T: PyObjectPayload + PyValue> Deref for PyLease<'a, T> { - type Target = T; + type Target = PyRef; fn deref(&self) -> &Self::Target { &self.inner diff --git a/vm/src/pyobjectrc.rs b/vm/src/pyobjectrc.rs index 56847ca00b..28c2892a55 100644 --- a/vm/src/pyobjectrc.rs +++ b/vm/src/pyobjectrc.rs @@ -891,6 +891,12 @@ impl Deref for PyObjectView { } } +impl Borrow for PyObjectView { + fn borrow(&self) -> &PyObject { + self.as_object() + } +} + impl AsRef for PyObjectView where T: PyObjectPayload, @@ -975,6 +981,15 @@ impl PyRef { } } +impl Borrow for PyRef +where + T: PyObjectPayload, +{ + fn borrow(&self) -> &PyObject { + self.as_object() + } +} + impl PyObjectWrap for PyRef where T: PyObjectPayload, diff --git a/vm/src/utils.rs b/vm/src/utils.rs index 567e517fce..dc990afba6 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -4,12 +4,22 @@ use crate::{ PyObject, PyObjectRef, PyObjectWrap, PyResult, TryFromObject, TypeProtocol, VirtualMachine, }; use num_traits::ToPrimitive; +use std::borrow::Borrow; pub enum Either { A(A), B(B), } +impl, B: Borrow> Borrow for Either { + fn borrow(&self) -> &PyObject { + match self { + Either::A(a) => a.borrow(), + Either::B(b) => b.borrow(), + } + } +} + impl, B: AsRef> AsRef for Either { fn as_ref(&self) -> &PyObject { match self { From dd09a9489bd1fcfb40711d046bd182511656b8af Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Sun, 17 Apr 2022 09:46:15 +0900 Subject: [PATCH 2/9] Remove DerefToPyType for &DerefToPyType --- vm/src/builtins/pytype.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/vm/src/builtins/pytype.rs b/vm/src/builtins/pytype.rs index 85a79951fa..6d2cf9cc7c 100644 --- a/vm/src/builtins/pytype.rs +++ b/vm/src/builtins/pytype.rs @@ -12,11 +12,12 @@ use crate::{ pyclass::{PyClassImpl, StaticType}, pyobject::PyLease, types::{Callable, GetAttr, PyTypeFlags, PyTypeSlots, SetAttr}, - IdProtocol, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, TypeProtocol, - VirtualMachine, + IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, + TypeProtocol, VirtualMachine, }; use itertools::Itertools; use std::{ + borrow::Borrow, collections::{HashMap, HashSet}, fmt, ops::Deref, @@ -793,7 +794,7 @@ impl PyLease<'_, PyType> { } } -pub trait DerefToPyType { +pub trait DerefToPyType: Borrow { fn deref_to_type(&self) -> &PyType; /// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__, @@ -819,12 +820,6 @@ impl<'a> DerefToPyType for PyLease<'a, PyType> { } } -impl DerefToPyType for &'_ T { - fn deref_to_type(&self) -> &PyType { - (&**self).deref_to_type() - } -} - pub(crate) fn call_slot_new( typ: PyTypeRef, subtype: PyTypeRef, From ba1a9b5b7e887daaad7c79c99b5a45f4f67b3691 Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Sun, 17 Apr 2022 09:50:54 +0900 Subject: [PATCH 3/9] issubclass takes &impl Borrow --- vm/src/builtins/pytype.rs | 12 +++++------- vm/src/protocol/object.rs | 6 +++--- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/vm/src/builtins/pytype.rs b/vm/src/builtins/pytype.rs index 6d2cf9cc7c..a784f14e4c 100644 --- a/vm/src/builtins/pytype.rs +++ b/vm/src/builtins/pytype.rs @@ -209,7 +209,7 @@ impl PyType { } impl PyTypeRef { - pub fn issubclass(&self, cls: R) -> bool { + pub fn issubclass(&self, cls: &impl Borrow) -> bool { self._issubclass(cls) } @@ -789,7 +789,7 @@ pub(crate) fn init(ctx: &PyContext) { } impl PyLease<'_, PyType> { - pub fn issubclass(&self, cls: R) -> bool { + pub fn issubclass(&self, cls: &impl Borrow) -> bool { self._issubclass(cls) } } @@ -800,11 +800,9 @@ pub trait DerefToPyType: Borrow { /// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__, /// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic /// method. - fn _issubclass(&self, cls: R) -> bool - where - Self: IdProtocol, - { - self.is(&cls) || self.deref_to_type().mro.iter().any(|c| c.is(&cls)) + fn _issubclass(&self, cls: &impl Borrow) -> bool { + self.borrow().is(cls.borrow()) + || self.deref_to_type().mro.iter().any(|c| c.is(cls.borrow())) } } diff --git a/vm/src/protocol/object.rs b/vm/src/protocol/object.rs index c2daa2c758..1fd8565c1b 100644 --- a/vm/src/protocol/object.rs +++ b/vm/src/protocol/object.rs @@ -278,7 +278,7 @@ impl PyObject { PyTypeRef::try_from_object(vm, self.to_owned()), PyTypeRef::try_from_object(vm, cls.to_owned()), ) { - Ok(obj.issubclass(cls)) + Ok(obj.issubclass(&cls)) } else { self.check_cls(self, vm, || { format!("issubclass() arg 1 must be a class, not {}", self.class()) @@ -324,7 +324,7 @@ impl PyObject { fn abstract_isinstance(&self, cls: &PyObject, vm: &VirtualMachine) -> PyResult { if let Ok(typ) = PyTypeRef::try_from_object(vm, cls.to_owned()) { - if self.class().issubclass(typ.clone()) { + if self.class().issubclass(&typ) { Ok(true) } else if let Ok(icls) = PyTypeRef::try_from_object(vm, self.to_owned().get_attr("__class__", vm)?) @@ -332,7 +332,7 @@ impl PyObject { if icls.is(&self.class()) { Ok(false) } else { - Ok(icls.issubclass(typ)) + Ok(icls.issubclass(&typ)) } } else { Ok(false) From 2e6875c8d5b226fa3b8ad10e4f989ad2651098ac Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Sun, 17 Apr 2022 09:56:42 +0900 Subject: [PATCH 4/9] checkpoint --- vm/src/pyobject.rs | 10 +++++----- vm/src/pyobjectrc.rs | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 2171fd7ae8..c850d20c5f 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -399,11 +399,11 @@ pub trait IdProtocol { } } -impl IdProtocol for PyRc { - fn get_id(&self) -> usize { - &**self as *const T as *const () as usize - } -} +// impl IdProtocol for PyRc { +// fn get_id(&self) -> usize { +// &**self as *const T as *const () as usize +// } +// } impl IdProtocol for PyRef { fn get_id(&self) -> usize { diff --git a/vm/src/pyobjectrc.rs b/vm/src/pyobjectrc.rs index 28c2892a55..c45034b1d6 100644 --- a/vm/src/pyobjectrc.rs +++ b/vm/src/pyobjectrc.rs @@ -466,7 +466,7 @@ impl ToOwned for PyObject { pub trait PyObjectWrap where - Self: AsRef, + Self: Borrow + AsRef, { #[inline(always)] fn as_object(&self) -> &PyObject { From 7edebbf28cf3e27ec91d75d5d2e59ae2e6616c60 Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Sun, 17 Apr 2022 10:03:48 +0900 Subject: [PATCH 5/9] Remove DerefToPyType --- vm/src/builtins/pytype.rs | 39 ++++++--------------------------------- vm/src/pyobject.rs | 4 ++-- vm/src/pyobjectrc.rs | 26 +++++++++++++------------- 3 files changed, 21 insertions(+), 48 deletions(-) diff --git a/vm/src/builtins/pytype.rs b/vm/src/builtins/pytype.rs index a784f14e4c..3ea2561d76 100644 --- a/vm/src/builtins/pytype.rs +++ b/vm/src/builtins/pytype.rs @@ -12,8 +12,8 @@ use crate::{ pyclass::{PyClassImpl, StaticType}, pyobject::PyLease, types::{Callable, GetAttr, PyTypeFlags, PyTypeSlots, SetAttr}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, - TypeProtocol, VirtualMachine, + IdProtocol, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, TypeProtocol, + VirtualMachine, }; use itertools::Itertools; use std::{ @@ -209,8 +209,11 @@ impl PyType { } impl PyTypeRef { + /// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__, + /// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic + /// method. pub fn issubclass(&self, cls: &impl Borrow) -> bool { - self._issubclass(cls) + self.as_object().is(cls.borrow()) || self.mro.iter().any(|c| c.is(cls.borrow())) } pub fn iter_mro(&self) -> impl Iterator + DoubleEndedIterator { @@ -788,36 +791,6 @@ pub(crate) fn init(ctx: &PyContext) { PyType::extend_class(ctx, &ctx.types.type_type); } -impl PyLease<'_, PyType> { - pub fn issubclass(&self, cls: &impl Borrow) -> bool { - self._issubclass(cls) - } -} - -pub trait DerefToPyType: Borrow { - fn deref_to_type(&self) -> &PyType; - - /// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__, - /// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic - /// method. - fn _issubclass(&self, cls: &impl Borrow) -> bool { - self.borrow().is(cls.borrow()) - || self.deref_to_type().mro.iter().any(|c| c.is(cls.borrow())) - } -} - -impl DerefToPyType for PyTypeRef { - fn deref_to_type(&self) -> &PyType { - self.deref() - } -} - -impl<'a> DerefToPyType for PyLease<'a, PyType> { - fn deref_to_type(&self) -> &PyType { - self.deref() - } -} - pub(crate) fn call_slot_new( typ: PyTypeRef, subtype: PyTypeRef, diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index c850d20c5f..df5f3cadb3 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -1,7 +1,7 @@ pub use crate::_pyobjectrc::{ PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyObjectWrap, PyRef, PyWeakRef, }; -use crate::common::{lock::PyRwLockReadGuard, rc::PyRc}; +use crate::common::lock::PyRwLockReadGuard; use crate::{ builtins::{ builtinfunc::{PyBuiltinFunction, PyBuiltinMethod, PyNativeFuncDef}, @@ -445,7 +445,7 @@ impl<'a, T: PyObjectPayload + PyValue> PyLease<'a, T> { impl<'a, T: PyObjectPayload + PyValue> Borrow for PyLease<'a, T> { fn borrow(&self) -> &PyObject { - self.inner.as_object() + self.inner.as_ref() } } diff --git a/vm/src/pyobjectrc.rs b/vm/src/pyobjectrc.rs index c45034b1d6..a3f2fc25be 100644 --- a/vm/src/pyobjectrc.rs +++ b/vm/src/pyobjectrc.rs @@ -466,11 +466,11 @@ impl ToOwned for PyObject { pub trait PyObjectWrap where - Self: Borrow + AsRef, + Self: Borrow, { #[inline(always)] fn as_object(&self) -> &PyObject { - self.as_ref() + self.borrow() } fn into_object(self) -> PyObjectRef; @@ -986,7 +986,17 @@ where T: PyObjectPayload, { fn borrow(&self) -> &PyObject { - self.as_object() + (**self).as_object() + } +} + +impl AsRef for PyRef +where + T: PyObjectPayload, +{ + #[inline(always)] + fn as_ref(&self) -> &PyObject { + self.borrow() } } @@ -1001,16 +1011,6 @@ where } } -impl AsRef for PyRef -where - T: PyObjectPayload, -{ - #[inline(always)] - fn as_ref(&self) -> &PyObject { - (**self).as_object() - } -} - impl Borrow> for PyRef where T: PyObjectPayload, From 9be1dc1f8bfd95ab4ebbe26dd1fc1b6966cd12d5 Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Sun, 17 Apr 2022 10:25:02 +0900 Subject: [PATCH 6/9] no more IdProtocol-depending api --- vm/src/builtins/bytearray.rs | 2 +- vm/src/pyobjectrc.rs | 2 +- vm/src/types/slot.rs | 20 ++++++++++++-------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index fb80e302fb..08b7e7af29 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -719,7 +719,7 @@ impl Comparable for PyByteArray { op: PyComparisonOp, vm: &VirtualMachine, ) -> PyResult { - if let Some(res) = op.identical_optimization(&zelf, &other) { + if let Some(res) = op.identical_optimization(zelf, other) { return Ok(res.into()); } Ok(zelf.inner().cmp(other, op, vm)) diff --git a/vm/src/pyobjectrc.rs b/vm/src/pyobjectrc.rs index a3f2fc25be..6c2ac805e3 100644 --- a/vm/src/pyobjectrc.rs +++ b/vm/src/pyobjectrc.rs @@ -772,7 +772,7 @@ impl AsRef for PyObject { impl IdProtocol for PyObjectRef { fn get_id(&self) -> usize { - self.ptr.as_ptr() as usize + PyObject::get_id(self) } } diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index 6d98a974fc..c002e84d39 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -11,7 +11,10 @@ use crate::{ }; use crossbeam_utils::atomic::AtomicCell; use num_traits::{Signed, ToPrimitive}; -use std::{borrow::Cow, cmp::Ordering}; +use std::{ + borrow::{Borrow, Cow}, + cmp::Ordering, +}; // The corresponding field in CPython is `tp_` prefixed. // e.g. name -> tp_name @@ -517,11 +520,8 @@ pub trait GetDescriptor: PyValue { } #[inline] - fn _cls_is(cls: &Option, other: &T) -> bool - where - T: IdProtocol, - { - cls.as_ref().map_or(false, |cls| other.is(cls)) + fn _cls_is(cls: &Option, other: &impl Borrow) -> bool { + cls.as_ref().map_or(false, |cls| other.borrow().is(cls)) } } @@ -711,8 +711,12 @@ impl PyComparisonOp { /// Returns an appropriate return value for the comparison when a and b are the same object, if an /// appropriate return value exists. #[inline] - pub fn identical_optimization(self, a: &impl IdProtocol, b: &impl IdProtocol) -> Option { - self.map_eq(|| a.is(b)) + pub fn identical_optimization( + self, + a: &impl Borrow, + b: &impl Borrow, + ) -> Option { + self.map_eq(|| a.borrow().is(b.borrow())) } /// Returns `Some(true)` when self is `Eq` and `f()` returns true. Returns `Some(false)` when self From 625f8675aefdc9a2de059cae242546bc418367a7 Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Sun, 17 Apr 2022 08:20:28 +0900 Subject: [PATCH 7/9] Introduce AsPyObject to replace IdProtocol --- src/lib.rs | 12 ++--- src/shell/helper.rs | 5 +- stdlib/src/array.rs | 7 ++- stdlib/src/json.rs | 2 +- vm/src/builtins/asyncgenerator.rs | 2 +- vm/src/builtins/bytearray.rs | 2 +- vm/src/builtins/bytes.rs | 2 +- vm/src/builtins/code.rs | 2 +- vm/src/builtins/complex.rs | 2 +- vm/src/builtins/coroutine.rs | 2 +- vm/src/builtins/dict.rs | 2 +- vm/src/builtins/float.rs | 2 +- vm/src/builtins/frame.rs | 2 +- vm/src/builtins/function.rs | 2 +- vm/src/builtins/function/jitfunc.rs | 2 +- vm/src/builtins/generator.rs | 2 +- vm/src/builtins/genericalias.rs | 2 +- vm/src/builtins/int.rs | 2 +- vm/src/builtins/list.rs | 4 +- vm/src/builtins/memory.rs | 2 +- vm/src/builtins/module.rs | 2 +- vm/src/builtins/namespace.rs | 2 +- vm/src/builtins/object.rs | 2 +- vm/src/builtins/pybool.rs | 2 +- vm/src/builtins/pystr.rs | 2 +- vm/src/builtins/pysuper.rs | 2 +- vm/src/builtins/pytype.rs | 2 +- vm/src/builtins/pyunion.rs | 2 +- vm/src/builtins/range.rs | 2 +- vm/src/builtins/set.rs | 2 +- vm/src/builtins/slice.rs | 3 +- vm/src/builtins/tuple.rs | 2 +- vm/src/builtins/weakref.rs | 4 +- vm/src/bytesinner.rs | 2 +- vm/src/codecs.rs | 2 +- vm/src/coroutine.rs | 2 +- vm/src/dictdatatype.rs | 2 +- vm/src/exceptions.rs | 2 +- vm/src/frame.rs | 2 +- vm/src/function/arithmetic.rs | 2 +- vm/src/import.rs | 2 +- vm/src/lib.rs | 4 +- vm/src/macros.rs | 2 +- vm/src/protocol/iter.rs | 4 +- vm/src/protocol/mapping.rs | 2 +- vm/src/protocol/object.rs | 2 +- vm/src/protocol/sequence.rs | 2 +- vm/src/pyobject.rs | 76 ++++++++++++++++------------- vm/src/pyobjectrc.rs | 58 ++++++---------------- vm/src/sequence.rs | 2 +- vm/src/stdlib/ast.rs | 2 +- vm/src/stdlib/builtins.rs | 2 +- vm/src/stdlib/codecs.rs | 2 +- vm/src/stdlib/collections.rs | 2 +- vm/src/stdlib/io.rs | 5 +- vm/src/stdlib/itertools.rs | 2 +- vm/src/stdlib/marshal.rs | 2 +- vm/src/stdlib/operator.rs | 2 +- vm/src/stdlib/os.rs | 4 +- vm/src/stdlib/pwd.rs | 2 +- vm/src/stdlib/thread.rs | 2 +- vm/src/stdlib/weakref.rs | 2 +- vm/src/suggestion.rs | 2 +- vm/src/types/slot.rs | 2 +- vm/src/vm.rs | 6 +-- vm/src/vm_new.rs | 2 +- vm/src/vm_object.rs | 2 +- vm/src/vm_ops.rs | 2 +- 68 files changed, 141 insertions(+), 161 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 142f79615c..bf0ad6be3c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,8 +51,8 @@ use rustpython_vm::{ compile, match_class, scope::Scope, stdlib::{atexit, sys}, - InitParameter, Interpreter, PyObjectRef, PyResult, PySettings, TryFromObject, TypeProtocol, - VirtualMachine, + AsPyObject, InitParameter, Interpreter, PyObjectRef, PyResult, PySettings, TryFromObject, + TypeProtocol, VirtualMachine, }; use std::{env, path::Path, process, str::FromStr}; @@ -536,12 +536,8 @@ fn setup_main_module(vm: &VirtualMachine) -> PyResult { main_module .dict() .and_then(|d| { - d.set_item( - "__annotations__", - vm.ctx.new_dict().as_object().to_owned(), - vm, - ) - .ok() + d.set_item("__annotations__", vm.ctx.new_dict().into(), vm) + .ok() }) .expect("Failed to initialize __main__.__annotations__"); diff --git a/src/shell/helper.rs b/src/shell/helper.rs index 171b7c274b..33b584a0df 100644 --- a/src/shell/helper.rs +++ b/src/shell/helper.rs @@ -85,9 +85,8 @@ impl<'vm> ShellHelper<'vm> { } else { // we need to get a variable based off of globals/builtins - let globals = str_iter_method(self.globals.as_object().to_owned(), "keys").ok()?; - let builtins = - str_iter_method(self.vm.builtins.as_object().to_owned(), "__dir__").ok()?; + let globals = str_iter_method(self.globals.clone().into(), "keys").ok()?; + let builtins = str_iter_method(self.vm.builtins.clone().into(), "__dir__").ok()?; (first, globals, Some(builtins)) }; Some((word_start, iter1.chain(iter2.into_iter().flatten()))) diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index 850ea73e61..137a1cd780 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -33,14 +33,13 @@ mod array { AsBuffer, AsMapping, Comparable, Constructor, IterNext, IterNextIterable, Iterable, PyComparisonOp, }, - IdProtocol, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, - PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine, + AsPyObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, + TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine, }, }; use itertools::Itertools; use num_traits::ToPrimitive; - use std::cmp::Ordering; - use std::{fmt, os::raw}; + use std::{cmp::Ordering, fmt, os::raw}; macro_rules! def_array_enum { ($(($n:ident, $t:ty, $c:literal, $scode:literal)),*$(,)?) => { diff --git a/stdlib/src/json.rs b/stdlib/src/json.rs index 2da596efcf..ad28c6e4ca 100644 --- a/stdlib/src/json.rs +++ b/stdlib/src/json.rs @@ -9,7 +9,7 @@ mod _json { function::{IntoPyObject, IntoPyResult, OptionalArg}, protocol::PyIterReturn, types::{Callable, Constructor}, - IdProtocol, PyObjectRef, PyObjectView, PyResult, PyValue, VirtualMachine, + AsPyObject, PyObjectRef, PyObjectView, PyResult, PyValue, VirtualMachine, }; use num_bigint::BigInt; use std::str::FromStr; diff --git a/vm/src/builtins/asyncgenerator.rs b/vm/src/builtins/asyncgenerator.rs index 39bb767f90..c6304cca5a 100644 --- a/vm/src/builtins/asyncgenerator.rs +++ b/vm/src/builtins/asyncgenerator.rs @@ -7,7 +7,7 @@ use crate::{ protocol::PyIterReturn, pyclass::PyClassImpl, types::{Constructor, IterNext, IterNextIterable, Unconstructible}, - IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, + AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index 08b7e7af29..2cd9358704 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -33,7 +33,7 @@ use crate::{ IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable, }, utils::Either, - IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, + AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine, }; use bstr::ByteSlice; diff --git a/vm/src/builtins/bytes.rs b/vm/src/builtins/bytes.rs index 9106461561..fbd459a83d 100644 --- a/vm/src/builtins/bytes.rs +++ b/vm/src/builtins/bytes.rs @@ -22,7 +22,7 @@ use crate::{ IterNextIterable, Iterable, PyComparisonOp, Unconstructible, }, utils::Either, - IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, + AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine, }; use bstr::ByteSlice; diff --git a/vm/src/builtins/code.rs b/vm/src/builtins/code.rs index 7b325c1868..3e10632a61 100644 --- a/vm/src/builtins/code.rs +++ b/vm/src/builtins/code.rs @@ -7,7 +7,7 @@ use crate::{ bytecode::{self, BorrowedConstant, Constant, ConstantBag}, function::FuncArgs, pyclass::{PyClassImpl, StaticType}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, + AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; use num_traits::Zero; diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 21ad3d38fa..1aa41e0352 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -7,7 +7,7 @@ use crate::{ }, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, + AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; use num_complex::Complex64; diff --git a/vm/src/builtins/coroutine.rs b/vm/src/builtins/coroutine.rs index 0eafde4fa0..c6859ba7d5 100644 --- a/vm/src/builtins/coroutine.rs +++ b/vm/src/builtins/coroutine.rs @@ -6,7 +6,7 @@ use crate::{ protocol::PyIterReturn, pyclass::PyClassImpl, types::{Constructor, IterNext, IterNextIterable, Unconstructible}, - IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; #[pyclass(module = false, name = "coroutine")] diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index 0f75607d55..299037110e 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -21,7 +21,7 @@ use crate::{ IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable, }, vm::{ReprGuard, VirtualMachine}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, + AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, }; use rustpython_common::lock::PyMutex; diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index 087643b5e8..de888f5946 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -9,7 +9,7 @@ use crate::{ }, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromBorrowedObject, + AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine, }; use num_bigint::{BigInt, ToBigInt}; diff --git a/vm/src/builtins/frame.rs b/vm/src/builtins/frame.rs index 547c97f82c..c38b4c9b38 100644 --- a/vm/src/builtins/frame.rs +++ b/vm/src/builtins/frame.rs @@ -7,7 +7,7 @@ use crate::{ frame::{Frame, FrameRef}, pyclass::PyClassImpl, types::{Constructor, Unconstructible}, - IdProtocol, PyContext, PyObjectRef, PyObjectWrap, PyRef, PyResult, VirtualMachine, + AsPyObject, PyContext, PyObjectRef, PyObjectWrap, PyRef, PyResult, VirtualMachine, }; pub fn init(context: &PyContext) { diff --git a/vm/src/builtins/function.rs b/vm/src/builtins/function.rs index 2b2a6cac69..1e3f9c777c 100644 --- a/vm/src/builtins/function.rs +++ b/vm/src/builtins/function.rs @@ -14,7 +14,7 @@ use crate::{ pyclass::PyClassImpl, scope::Scope, types::{Callable, Comparable, Constructor, GetAttr, GetDescriptor, PyComparisonOp}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, + AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; #[cfg(feature = "jit")] diff --git a/vm/src/builtins/function/jitfunc.rs b/vm/src/builtins/function/jitfunc.rs index cbc761847c..9ced316fdd 100644 --- a/vm/src/builtins/function/jitfunc.rs +++ b/vm/src/builtins/function/jitfunc.rs @@ -2,7 +2,7 @@ use crate::{ builtins::{float, int, pybool, PyBaseExceptionRef, PyDictRef, PyFunction, PyStrRef}, bytecode::CodeFlags, function::{FuncArgs, IntoPyObject}, - IdProtocol, PyObject, PyObjectRef, PyResult, TryFromObject, TypeProtocol, VirtualMachine, + AsPyObject, PyObject, PyObjectRef, PyResult, TryFromObject, TypeProtocol, VirtualMachine, }; use num_traits::ToPrimitive; use rustpython_jit::{AbiValue, Args, CompiledCode, JitArgumentError, JitType}; diff --git a/vm/src/builtins/generator.rs b/vm/src/builtins/generator.rs index 5cd5deffd1..7528981b79 100644 --- a/vm/src/builtins/generator.rs +++ b/vm/src/builtins/generator.rs @@ -10,7 +10,7 @@ use crate::{ protocol::PyIterReturn, pyclass::PyClassImpl, types::{Constructor, IterNext, IterNextIterable, Unconstructible}, - IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; #[pyclass(module = false, name = "generator")] diff --git a/vm/src/builtins/genericalias.rs b/vm/src/builtins/genericalias.rs index 2de0d451e2..d6d793360f 100644 --- a/vm/src/builtins/genericalias.rs +++ b/vm/src/builtins/genericalias.rs @@ -5,7 +5,7 @@ use crate::{ protocol::PyMappingMethods, pyclass::PyClassImpl, types::{AsMapping, Callable, Comparable, Constructor, GetAttr, Hashable, PyComparisonOp}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, + AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, }; use std::fmt; diff --git a/vm/src/builtins/int.rs b/vm/src/builtins/int.rs index 2fb3b0d587..da837a099b 100644 --- a/vm/src/builtins/int.rs +++ b/vm/src/builtins/int.rs @@ -9,7 +9,7 @@ use crate::{ }, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromBorrowedObject, + AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromBorrowedObject, TypeProtocol, VirtualMachine, }; use bstr::ByteSlice; diff --git a/vm/src/builtins/list.rs b/vm/src/builtins/list.rs index 346504d586..622f56153e 100644 --- a/vm/src/builtins/list.rs +++ b/vm/src/builtins/list.rs @@ -15,8 +15,8 @@ use crate::{ }, utils::collection_repr, vm::{ReprGuard, VirtualMachine}, - PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, - TypeProtocol, + AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, + PyValue, TypeProtocol, }; use std::{borrow::Cow, fmt, ops::DerefMut}; diff --git a/vm/src/builtins/memory.rs b/vm/src/builtins/memory.rs index 5f8527bb82..6a12551c8c 100644 --- a/vm/src/builtins/memory.rs +++ b/vm/src/builtins/memory.rs @@ -18,7 +18,7 @@ use crate::{ sliceable::wrap_index, types::{AsBuffer, AsMapping, AsSequence, Comparable, Constructor, Hashable, PyComparisonOp}, utils::Either, - IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, + AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; diff --git a/vm/src/builtins/module.rs b/vm/src/builtins/module.rs index 90a43c45dd..1586c93716 100644 --- a/vm/src/builtins/module.rs +++ b/vm/src/builtins/module.rs @@ -4,7 +4,7 @@ use crate::{ function::{FuncArgs, IntoPyObject}, pyclass::PyClassImpl, types::GetAttr, - PyContext, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine, + AsPyObject, PyContext, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine, }; #[pyclass(module = false, name = "module")] diff --git a/vm/src/builtins/namespace.rs b/vm/src/builtins/namespace.rs index 6668e367b3..aaebfdb3e1 100644 --- a/vm/src/builtins/namespace.rs +++ b/vm/src/builtins/namespace.rs @@ -5,7 +5,7 @@ use crate::{ pyclass::PyClassImpl, types::{Comparable, Constructor, PyComparisonOp}, vm::ReprGuard, - IdProtocol, PyContext, PyObject, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, + AsPyObject, PyContext, PyObject, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; /// A simple attribute-based namespace. diff --git a/vm/src/builtins/object.rs b/vm/src/builtins/object.rs index 4498afa09e..a079cb3abe 100644 --- a/vm/src/builtins/object.rs +++ b/vm/src/builtins/object.rs @@ -5,7 +5,7 @@ use crate::{ pyclass::PyClassImpl, types::PyComparisonOp, utils::Either, - IdProtocol, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, VirtualMachine, + AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; /// object() diff --git a/vm/src/builtins/pybool.rs b/vm/src/builtins/pybool.rs index a44a4396b3..98315b9546 100644 --- a/vm/src/builtins/pybool.rs +++ b/vm/src/builtins/pybool.rs @@ -3,7 +3,7 @@ use crate::{ function::{IntoPyObject, OptionalArg}, pyclass::PyClassImpl, types::Constructor, - IdProtocol, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, + AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TypeProtocol, VirtualMachine, }; use num_bigint::Sign; diff --git a/vm/src/builtins/pystr.rs b/vm/src/builtins/pystr.rs index 55077213ca..9ee232a0bd 100644 --- a/vm/src/builtins/pystr.rs +++ b/vm/src/builtins/pystr.rs @@ -18,7 +18,7 @@ use crate::{ AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, IterNextIterable, Iterable, PyComparisonOp, Unconstructible, }, - IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, + AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TryFromBorrowedObject, TypeProtocol, VirtualMachine, }; use ascii::{AsciiStr, AsciiString}; diff --git a/vm/src/builtins/pysuper.rs b/vm/src/builtins/pysuper.rs index b86c0cc418..ea01de8bfb 100644 --- a/vm/src/builtins/pysuper.rs +++ b/vm/src/builtins/pysuper.rs @@ -8,7 +8,7 @@ use crate::{ function::OptionalArg, pyclass::PyClassImpl, types::{Constructor, GetAttr, GetDescriptor}, - IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, + AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; #[pyclass(module = false, name = "super")] diff --git a/vm/src/builtins/pytype.rs b/vm/src/builtins/pytype.rs index 3ea2561d76..8798bca74c 100644 --- a/vm/src/builtins/pytype.rs +++ b/vm/src/builtins/pytype.rs @@ -12,7 +12,7 @@ use crate::{ pyclass::{PyClassImpl, StaticType}, pyobject::PyLease, types::{Callable, GetAttr, PyTypeFlags, PyTypeSlots, SetAttr}, - IdProtocol, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, TypeProtocol, + AsPyObject, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; use itertools::Itertools; diff --git a/vm/src/builtins/pyunion.rs b/vm/src/builtins/pyunion.rs index 7ea922cc91..42a1846665 100644 --- a/vm/src/builtins/pyunion.rs +++ b/vm/src/builtins/pyunion.rs @@ -6,7 +6,7 @@ use crate::{ protocol::PyMappingMethods, pyclass::PyClassImpl, types::{AsMapping, Comparable, GetAttr, Hashable, Iterable, PyComparisonOp}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, + AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, }; use std::fmt; diff --git a/vm/src/builtins/range.rs b/vm/src/builtins/range.rs index b48dd31d7c..dd22a4541a 100644 --- a/vm/src/builtins/range.rs +++ b/vm/src/builtins/range.rs @@ -9,7 +9,7 @@ use crate::{ AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, IterNextIterable, Iterable, PyComparisonOp, Unconstructible, }, - IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, + AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; diff --git a/vm/src/builtins/set.rs b/vm/src/builtins/set.rs index d04ff1315b..b3339e64a9 100644 --- a/vm/src/builtins/set.rs +++ b/vm/src/builtins/set.rs @@ -17,7 +17,7 @@ use crate::{ }, utils::collection_repr, vm::{ReprGuard, VirtualMachine}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, + AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, }; use std::borrow::Cow; diff --git a/vm/src/builtins/slice.rs b/vm/src/builtins/slice.rs index 60effa4cb7..3a3f998dda 100644 --- a/vm/src/builtins/slice.rs +++ b/vm/src/builtins/slice.rs @@ -4,7 +4,8 @@ use crate::{ function::{FuncArgs, IntoPyObject, OptionalArg, PyComparisonValue}, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp, Unhashable}, - PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, + AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, + VirtualMachine, }; use num_bigint::{BigInt, ToBigInt}; use num_traits::{One, Signed, ToPrimitive, Zero}; diff --git a/vm/src/builtins/tuple.rs b/vm/src/builtins/tuple.rs index 7256c36e7e..7473e2a56a 100644 --- a/vm/src/builtins/tuple.rs +++ b/vm/src/builtins/tuple.rs @@ -14,7 +14,7 @@ use crate::{ }, utils::collection_repr, vm::{ReprGuard, VirtualMachine}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, + AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, }; use std::{borrow::Cow, fmt, marker::PhantomData}; diff --git a/vm/src/builtins/weakref.rs b/vm/src/builtins/weakref.rs index 192818eccc..51e0e5dcc1 100644 --- a/vm/src/builtins/weakref.rs +++ b/vm/src/builtins/weakref.rs @@ -7,8 +7,8 @@ use crate::{ function::OptionalArg, pyclass::PyClassImpl, types::{Callable, Comparable, Constructor, Hashable, PyComparisonOp}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, - VirtualMachine, + AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, + TypeProtocol, VirtualMachine, }; pub use crate::pyobject::PyWeak; diff --git a/vm/src/bytesinner.rs b/vm/src/bytesinner.rs index 83dc19df7c..ed50c56f4d 100644 --- a/vm/src/bytesinner.rs +++ b/vm/src/bytesinner.rs @@ -9,7 +9,7 @@ use crate::{ sequence::{SequenceMutOp, SequenceOp}, types::PyComparisonOp, utils::Either, - IdProtocol, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, VirtualMachine, + AsPyObject, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, VirtualMachine, }; use bstr::ByteSlice; use itertools::Itertools; diff --git a/vm/src/codecs.rs b/vm/src/codecs.rs index af47a927d4..978374c54f 100644 --- a/vm/src/codecs.rs +++ b/vm/src/codecs.rs @@ -2,7 +2,7 @@ use crate::{ builtins::{PyBaseExceptionRef, PyBytesRef, PyStr, PyStrRef, PyTuple, PyTupleRef}, common::{ascii, lock::PyRwLock}, function::IntoPyObject, - IdProtocol, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol, + AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, }; use std::{borrow::Cow, collections::HashMap, fmt::Write, ops::Range}; diff --git a/vm/src/coroutine.rs b/vm/src/coroutine.rs index 051614b74e..e3bbe45d08 100644 --- a/vm/src/coroutine.rs +++ b/vm/src/coroutine.rs @@ -3,7 +3,7 @@ use crate::{ common::lock::PyMutex, frame::{ExecutionResult, FrameRef}, protocol::PyIterReturn, - IdProtocol, PyObject, PyObjectRef, PyResult, TypeProtocol, VirtualMachine, + AsPyObject, PyObject, PyObjectRef, PyResult, TypeProtocol, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; diff --git a/vm/src/dictdatatype.rs b/vm/src/dictdatatype.rs index 5503b13e1d..ddbcca800e 100644 --- a/vm/src/dictdatatype.rs +++ b/vm/src/dictdatatype.rs @@ -10,7 +10,7 @@ use crate::common::{ use crate::{ builtins::{PyInt, PyStr, PyStrRef}, function::IntoPyObject, - IdProtocol, PyObject, PyObjectRef, PyObjectWrap, PyRefExact, PyResult, TypeProtocol, + AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRefExact, PyResult, TypeProtocol, VirtualMachine, }; use num_traits::ToPrimitive; diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 24b2b342b7..3674d8542b 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -9,7 +9,7 @@ use crate::{ pyclass::{PyClassImpl, StaticType}, stdlib::sys, suggestion::offer_suggestions, - IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, + AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; diff --git a/vm/src/frame.rs b/vm/src/frame.rs index c6ab7b2c0e..fa0ba1386e 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -15,7 +15,7 @@ use crate::{ scope::Scope, stdlib::builtins, types::PyComparisonOp, - IdProtocol, PyMethod, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, + AsPyObject, PyMethod, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, }; use indexmap::IndexMap; diff --git a/vm/src/function/arithmetic.rs b/vm/src/function/arithmetic.rs index d13397d04e..a4871e5964 100644 --- a/vm/src/function/arithmetic.rs +++ b/vm/src/function/arithmetic.rs @@ -1,7 +1,7 @@ use crate::{ convert::TryFromObject, function::IntoPyObject, - pyobject::{IdProtocol, PyObjectRef, PyResult}, + pyobject::{AsPyObject, PyObjectRef, PyResult}, VirtualMachine, }; diff --git a/vm/src/import.rs b/vm/src/import.rs index 215ab65cd7..2dc632bc17 100644 --- a/vm/src/import.rs +++ b/vm/src/import.rs @@ -127,7 +127,7 @@ pub fn import_codeobj( let attrs = vm.ctx.new_dict(); attrs.set_item("__name__", vm.ctx.new_str(module_name).into(), vm)?; if set_file_attr { - attrs.set_item("__file__", code_obj.source_path.as_object().to_owned(), vm)?; + attrs.set_item("__file__", code_obj.source_path.clone().into(), vm)?; } let module = vm.new_module(module_name, attrs.clone(), None); diff --git a/vm/src/lib.rs b/vm/src/lib.rs index 4fee1ee8b3..5a88bdd0a8 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -89,12 +89,12 @@ mod pyobject { pub use self::convert::{TryFromBorrowedObject, TryFromObject}; // pyobject items pub use self::pyobject::{ - IdProtocol, PyContext, PyMethod, PyObjectPayload, PyRefExact, PyResult, PyStructSequence, + PyContext, PyMethod, PyObjectPayload, PyObjectWrap, PyRefExact, PyResult, PyStructSequence, PyValue, TypeProtocol, }; // pyobjectrc items pub use self::pyobject::{ - PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyObjectWrap, PyRef, PyWeakRef, + AsPyObject, PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyRef, PyWeakRef, }; pub use self::vm::{InitParameter, Interpreter, PySettings, VirtualMachine}; pub use rustpython_bytecode as bytecode; diff --git a/vm/src/macros.rs b/vm/src/macros.rs index 6cd3ec5c6d..5945682730 100644 --- a/vm/src/macros.rs +++ b/vm/src/macros.rs @@ -49,7 +49,7 @@ macro_rules! py_namespace { { let namespace = $crate::builtins::PyNamespace::new_ref(&$vm.ctx); $( - $vm.__module_set_attr(namespace.as_object(), $name, $value).unwrap(); + $vm.__module_set_attr($crate::pyobject::AsPyObject::as_object(&namespace), $name, $value).unwrap(); )* namespace } diff --git a/vm/src/protocol/iter.rs b/vm/src/protocol/iter.rs index a79534694f..1ccfce00ff 100644 --- a/vm/src/protocol/iter.rs +++ b/vm/src/protocol/iter.rs @@ -1,8 +1,8 @@ use crate::{ builtins::iter::PySequenceIterator, function::{IntoPyObject, IntoPyResult}, - PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject, TypeProtocol, - VirtualMachine, + AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject, + TypeProtocol, VirtualMachine, }; use std::borrow::Borrow; use std::ops::Deref; diff --git a/vm/src/protocol/mapping.rs b/vm/src/protocol/mapping.rs index e844d42026..bd88ae1161 100644 --- a/vm/src/protocol/mapping.rs +++ b/vm/src/protocol/mapping.rs @@ -5,7 +5,7 @@ use crate::{ }, common::lock::OnceCell, function::IntoPyResult, - IdProtocol, PyObject, PyObjectRef, PyResult, TypeProtocol, VirtualMachine, + AsPyObject, PyObject, PyObjectRef, PyResult, TypeProtocol, VirtualMachine, }; // Mapping protocol diff --git a/vm/src/protocol/object.rs b/vm/src/protocol/object.rs index 1fd8565c1b..edc1ddde9e 100644 --- a/vm/src/protocol/object.rs +++ b/vm/src/protocol/object.rs @@ -13,7 +13,7 @@ use crate::{ protocol::{PyIter, PyMapping, PySequence}, types::{Constructor, PyComparisonOp}, utils::Either, - IdProtocol, PyObject, PyObjectRef, PyResult, TryFromObject, TypeProtocol, VirtualMachine, + AsPyObject, PyObject, PyObjectRef, PyResult, TryFromObject, TypeProtocol, VirtualMachine, }; // RustPython doesn't need these items diff --git a/vm/src/protocol/sequence.rs b/vm/src/protocol/sequence.rs index ffe3137aa7..684e6cd6e7 100644 --- a/vm/src/protocol/sequence.rs +++ b/vm/src/protocol/sequence.rs @@ -3,7 +3,7 @@ use crate::{ common::lock::OnceCell, function::{IntoPyObject, PyArithmeticValue}, protocol::PyMapping, - IdProtocol, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, VirtualMachine, + AsPyObject, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; use itertools::Itertools; use std::{ diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index df5f3cadb3..0054c190a5 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -1,6 +1,3 @@ -pub use crate::_pyobjectrc::{ - PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyObjectWrap, PyRef, PyWeakRef, -}; use crate::common::lock::PyRwLockReadGuard; use crate::{ builtins::{ @@ -20,6 +17,7 @@ use crate::{ pyclass::{PyClassImpl, StaticType}, types::{PyTypeFlags, PyTypeSlots, TypeZoo}, VirtualMachine, + _pyobjectrc::{PyObject, PyObjectRef, PyObjectView, PyRef}, }; use num_bigint::BigInt; use num_traits::ToPrimitive; @@ -389,46 +387,42 @@ impl IntoPyObject for PyRefExact { } } -pub trait IdProtocol { - fn get_id(&self) -> usize; +pub trait AsPyObject +where + Self: Borrow, +{ + #[inline(always)] + fn as_object(&self) -> &PyObject { + self.borrow() + } + #[inline(always)] + fn get_id(&self) -> usize { + self.as_object()._get_id() + } + #[inline(always)] fn is(&self, other: &T) -> bool where - T: IdProtocol, + T: AsPyObject, { self.get_id() == other.get_id() } } -// impl IdProtocol for PyRc { -// fn get_id(&self) -> usize { -// &**self as *const T as *const () as usize +impl AsPyObject for T where T: Borrow {} + +impl PyObject { + #[inline] + fn _get_id(&self) -> usize { + self as *const PyObject as usize + } +} + +// impl Borrow for PyRc { +// fn borrow(&self) -> &PyObject { +// unsafe { &*(&**self as *const T as *const PyObject) } // } // } -impl IdProtocol for PyRef { - fn get_id(&self) -> usize { - self.as_object().get_id() - } -} - -impl IdProtocol for PyObjectView { - fn get_id(&self) -> usize { - self.as_object().get_id() - } -} - -impl<'a, T: PyObjectPayload> IdProtocol for PyLease<'a, T> { - fn get_id(&self) -> usize { - self.inner.get_id() - } -} - -impl IdProtocol for &'_ T { - fn get_id(&self) -> usize { - (&**self).get_id() - } -} - /// A borrow of a reference to a Python object. This avoids having clone the `PyRef`/ /// `PyObjectRef`, which isn't that cheap as that increments the atomic reference counter. pub struct PyLease<'a, T: PyObjectPayload> { @@ -705,6 +699,22 @@ pub trait PyStructSequence: StaticType + PyClassImpl + Sized + 'static { } } +pub trait PyObjectWrap +where + Self: AsPyObject, +{ + fn into_object(self) -> PyObjectRef; +} + +impl From for PyObjectRef +where + T: PyObjectWrap, +{ + fn from(py_ref: T) -> Self { + py_ref.into_object() + } +} + #[derive(Debug)] pub enum PyMethod { Function { diff --git a/vm/src/pyobjectrc.rs b/vm/src/pyobjectrc.rs index 6c2ac805e3..0e1630c688 100644 --- a/vm/src/pyobjectrc.rs +++ b/vm/src/pyobjectrc.rs @@ -3,7 +3,7 @@ use crate::common::linked_list::{Link, LinkedList, Pointers}; use crate::common::lock::{PyMutex, PyMutexGuard, PyRwLock}; use crate::common::refcount::RefCount; use crate::{ - _pyobject::{IdProtocol, PyObjectPayload, PyResult, TypeProtocol}, + _pyobject::{AsPyObject, PyObjectPayload, PyObjectWrap, PyResult, TypeProtocol}, builtins::{PyBaseExceptionRef, PyDictRef, PyTypeRef}, vm::VirtualMachine, }; @@ -464,18 +464,6 @@ impl ToOwned for PyObject { } } -pub trait PyObjectWrap -where - Self: Borrow, -{ - #[inline(always)] - fn as_object(&self) -> &PyObject { - self.borrow() - } - - fn into_object(self) -> PyObjectRef; -} - impl PyObjectRef { pub fn into_raw(self) -> *const PyObject { let ptr = self.as_raw(); @@ -770,30 +758,22 @@ impl AsRef for PyObject { } } -impl IdProtocol for PyObjectRef { - fn get_id(&self) -> usize { - PyObject::get_id(self) - } -} - -impl IdProtocol for PyObject { - fn get_id(&self) -> usize { - self as *const PyObject as usize - } -} - impl<'a, T: PyObjectPayload> From<&'a PyObjectView> for &'a PyObject { fn from(py_ref: &'a PyObjectView) -> Self { py_ref.as_object() } } -impl From for PyObjectRef -where - T: PyObjectWrap, -{ - fn from(py_ref: T) -> Self { - py_ref.into_object() +impl Borrow for PyObjectWeak { + #[inline] + fn borrow(&self) -> &PyObject { + self.weak.as_object() + } +} + +impl PyObjectWrap for PyObjectWeak { + fn into_object(self) -> PyObjectRef { + self.weak.into_object() } } @@ -802,10 +782,6 @@ impl PyObjectWeak { pub fn upgrade(&self) -> Option { self.weak.upgrade() } - - pub fn into_object(self) -> PyObjectRef { - self.weak.into_object() - } } impl Drop for PyObjectRef { @@ -854,11 +830,6 @@ impl fmt::Debug for PyObjectWeak { pub struct PyObjectView(PyInner); impl PyObjectView { - #[inline(always)] - pub fn as_object(&self) -> &PyObject { - unsafe { &*(&self.0 as *const PyInner as *const PyObject) } - } - pub fn downgrade( &self, callback: Option, @@ -886,14 +857,16 @@ impl ToOwned for PyObjectView { impl Deref for PyObjectView { type Target = T; + #[inline(always)] fn deref(&self) -> &Self::Target { &self.0.payload } } impl Borrow for PyObjectView { + #[inline(always)] fn borrow(&self) -> &PyObject { - self.as_object() + unsafe { &*(&self.0 as *const PyInner as *const PyObject) } } } @@ -901,8 +874,9 @@ impl AsRef for PyObjectView where T: PyObjectPayload, { + #[inline(always)] fn as_ref(&self) -> &PyObject { - self.as_object() + self.borrow() } } diff --git a/vm/src/sequence.rs b/vm/src/sequence.rs index 14bf025787..2088156b22 100644 --- a/vm/src/sequence.rs +++ b/vm/src/sequence.rs @@ -3,7 +3,7 @@ use crate::{ types::{richcompare_wrapper, PyComparisonOp, RichCompareFunc}, utils::Either, vm::VirtualMachine, - IdProtocol, PyObject, PyObjectRef, PyResult, TypeProtocol, + AsPyObject, PyObject, PyObjectRef, PyResult, TypeProtocol, }; use itertools::Itertools; use optional::Optioned; diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs index b01e54e7c0..f58fcc5fb3 100644 --- a/vm/src/stdlib/ast.rs +++ b/vm/src/stdlib/ast.rs @@ -8,7 +8,7 @@ mod gen; use crate::{ builtins::{self, PyStrRef, PyTypeRef}, pyclass::{PyClassImpl, StaticType}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol, + AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, }; use num_complex::Complex64; diff --git a/vm/src/stdlib/builtins.rs b/vm/src/stdlib/builtins.rs index bf53b02251..102d98bda1 100644 --- a/vm/src/stdlib/builtins.rs +++ b/vm/src/stdlib/builtins.rs @@ -33,7 +33,7 @@ mod builtins { stdlib::sys, types::PyComparisonOp, utils::Either, - IdProtocol, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject, + AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, }; use num_traits::{Signed, ToPrimitive, Zero}; diff --git a/vm/src/stdlib/codecs.rs b/vm/src/stdlib/codecs.rs index 15ab8e60ea..484e1dda52 100644 --- a/vm/src/stdlib/codecs.rs +++ b/vm/src/stdlib/codecs.rs @@ -7,7 +7,7 @@ mod _codecs { builtins::{PyBaseExceptionRef, PyBytes, PyBytesRef, PyStr, PyStrRef, PyTuple}, codecs, function::{ArgBytesLike, FuncArgs}, - IdProtocol, PyObject, PyObjectRef, PyResult, TryFromBorrowedObject, VirtualMachine, + AsPyObject, PyObject, PyObjectRef, PyResult, TryFromBorrowedObject, VirtualMachine, }; use std::ops::Range; diff --git a/vm/src/stdlib/collections.rs b/vm/src/stdlib/collections.rs index a59909ae0b..d99a279c7a 100644 --- a/vm/src/stdlib/collections.rs +++ b/vm/src/stdlib/collections.rs @@ -19,7 +19,7 @@ mod _collections { }, utils::collection_repr, vm::ReprGuard, - PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, + AsPyObject, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; use std::cmp::max; diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 3e286b2942..6f9992ba98 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -88,7 +88,7 @@ mod _io { types::{Constructor, Destructor, IterNext, Iterable}, utils::Either, vm::{ReprGuard, VirtualMachine}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, + AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, }; use bstr::ByteSlice; @@ -3661,7 +3661,8 @@ mod fileio { ArgBytesLike, ArgMemoryBuffer, FuncArgs, IntoPyException, OptionalArg, OptionalOption, }, stdlib::os, - PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, + AsPyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, + VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; use std::io::{Read, Write}; diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 04f539027b..bd8264f3a6 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -12,7 +12,7 @@ mod decl { protocol::{PyIter, PyIterReturn}, stdlib::sys, types::{Constructor, IterNext, IterNextIterable}, - IdProtocol, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, PyWeakRef, TypeProtocol, + AsPyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, PyWeakRef, TypeProtocol, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; diff --git a/vm/src/stdlib/marshal.rs b/vm/src/stdlib/marshal.rs index d8791d0f0c..03cfc0cd24 100644 --- a/vm/src/stdlib/marshal.rs +++ b/vm/src/stdlib/marshal.rs @@ -10,7 +10,7 @@ mod decl { bytecode, function::{ArgBytesLike, IntoPyObject}, protocol::PyBuffer, - pyobject::{IdProtocol, TypeProtocol}, + pyobject::{AsPyObject, TypeProtocol}, PyObjectRef, PyResult, TryFromObject, VirtualMachine, }; /// TODO diff --git a/vm/src/stdlib/operator.rs b/vm/src/stdlib/operator.rs index 4830ba87f4..5d46321a75 100644 --- a/vm/src/stdlib/operator.rs +++ b/vm/src/stdlib/operator.rs @@ -20,7 +20,7 @@ mod _operator { }, utils::Either, vm::ReprGuard, - IdProtocol, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TypeProtocol, + AsPyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 701ff8f396..d892d9d3f6 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -3,8 +3,8 @@ use crate::{ crt_fd::Fd, function::{ArgumentError, FromArgs, FuncArgs, IntoPyException, IntoPyObject}, protocol::PyBuffer, - PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, - VirtualMachine, + AsPyObject, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, + TypeProtocol, VirtualMachine, }; use std::{ ffi, fs, diff --git a/vm/src/stdlib/pwd.rs b/vm/src/stdlib/pwd.rs index 0ac569ef0d..be32213fda 100644 --- a/vm/src/stdlib/pwd.rs +++ b/vm/src/stdlib/pwd.rs @@ -5,7 +5,7 @@ mod pwd { use crate::{ builtins::{PyIntRef, PyStrRef}, function::{IntoPyException, IntoPyObject}, - PyObjectRef, PyResult, PyStructSequence, VirtualMachine, + AsPyObject, PyObjectRef, PyResult, PyStructSequence, VirtualMachine, }; use nix::unistd::{self, User}; use std::ptr::NonNull; diff --git a/vm/src/stdlib/thread.rs b/vm/src/stdlib/thread.rs index abb67fe22a..deab60bd4d 100644 --- a/vm/src/stdlib/thread.rs +++ b/vm/src/stdlib/thread.rs @@ -10,7 +10,7 @@ pub(crate) mod _thread { py_io, types::{Constructor, GetAttr, SetAttr}, utils::Either, - IdProtocol, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, + AsPyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; use parking_lot::{ lock_api::{RawMutex as RawMutexT, RawMutexTimed, RawReentrantMutex}, diff --git a/vm/src/stdlib/weakref.rs b/vm/src/stdlib/weakref.rs index ffeb036280..e5eefddef2 100644 --- a/vm/src/stdlib/weakref.rs +++ b/vm/src/stdlib/weakref.rs @@ -9,7 +9,7 @@ pub(crate) use _weakref::make_module; #[pymodule] mod _weakref { use crate::builtins::{PyDictRef, PyTypeRef, PyWeak}; - use crate::{PyObjectRef, PyResult, VirtualMachine}; + use crate::{PyObjectRef, PyObjectWrap, PyResult, VirtualMachine}; #[pyattr(name = "ref")] fn ref_(vm: &VirtualMachine) -> PyTypeRef { diff --git a/vm/src/suggestion.rs b/vm/src/suggestion.rs index 5ac4b95d65..e38713a321 100644 --- a/vm/src/suggestion.rs +++ b/vm/src/suggestion.rs @@ -2,7 +2,7 @@ use crate::{ builtins::{PyStr, PyStrRef}, exceptions::types::PyBaseExceptionRef, sliceable::SliceableSequenceOp, - IdProtocol, PyObjectRef, PyObjectView, TypeProtocol, VirtualMachine, + AsPyObject, PyObjectRef, PyObjectView, TypeProtocol, VirtualMachine, }; use rustpython_common::str::levenshtein::{levenshtein_distance, MOVE_COST}; use std::iter::ExactSizeIterator; diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index c002e84d39..713fd9e7ac 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -6,7 +6,7 @@ use crate::{ protocol::{PyBuffer, PyIterReturn, PyMapping, PyMappingMethods}, protocol::{PySequence, PySequenceMethods}, utils::Either, - IdProtocol, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TypeProtocol, + AsPyObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 59ab75d139..d61ec88ea1 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -24,8 +24,8 @@ use crate::{ protocol::PyIterIter, pyobject::PyLease, scope::Scope, - signal, stdlib, IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyRefExact, - PyResult, PyValue, TypeProtocol, + signal, stdlib, AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyRefExact, PyResult, + PyValue, TypeProtocol, }; use crossbeam_utils::atomic::AtomicCell; use std::{ @@ -872,7 +872,7 @@ impl VirtualMachine { if !context_exc.is(exception) { let mut o = context_exc.clone(); while let Some(context) = o.context() { - if context.is(&exception) { + if context.is(exception) { o.set_context(None); break; } diff --git a/vm/src/vm_new.rs b/vm/src/vm_new.rs index 492ed91a86..e219535849 100644 --- a/vm/src/vm_new.rs +++ b/vm/src/vm_new.rs @@ -10,7 +10,7 @@ use crate::{ function::IntoPyObject, scope::Scope, vm::VirtualMachine, - PyObject, PyObjectRef, PyObjectWrap, PyRef, TypeProtocol, + AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, TypeProtocol, }; /// Collection of object creation helpers diff --git a/vm/src/vm_object.rs b/vm/src/vm_object.rs index 3d2c3200c2..dc7cecf558 100644 --- a/vm/src/vm_object.rs +++ b/vm/src/vm_object.rs @@ -2,7 +2,7 @@ use crate::{ builtins::{PyBaseExceptionRef, PyList, PyStr}, function::{FuncArgs, IntoFuncArgs}, vm::VirtualMachine, - IdProtocol, PyMethod, PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TypeProtocol, + AsPyObject, PyMethod, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; /// Trace events for sys.settrace and sys.setprofile. diff --git a/vm/src/vm_ops.rs b/vm/src/vm_ops.rs index 7766730afa..c783611ec7 100644 --- a/vm/src/vm_ops.rs +++ b/vm/src/vm_ops.rs @@ -4,7 +4,7 @@ use crate::{ protocol::PyIterReturn, types::PyComparisonOp, vm::VirtualMachine, - IdProtocol, PyMethod, PyObject, PyObjectRef, PyResult, TypeProtocol, + AsPyObject, PyMethod, PyObject, PyObjectRef, PyResult, TypeProtocol, }; /// Collection of operators From 935bfcda861846a7042609f599fa2582d71f2ec7 Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Sun, 17 Apr 2022 08:39:50 +0900 Subject: [PATCH 8/9] inline for object pointer operations --- vm/src/function/argument.rs | 7 ++++- vm/src/protocol/iter.rs | 4 +++ vm/src/pyobject.rs | 54 +++++++++++++++++++++++++++-------- vm/src/pyobjectrc.rs | 56 +++++++++++++++++++++++++++---------- vm/src/utils.rs | 4 +++ 5 files changed, 97 insertions(+), 28 deletions(-) diff --git a/vm/src/function/argument.rs b/vm/src/function/argument.rs index 18a9a81b72..aa01450fef 100644 --- a/vm/src/function/argument.rs +++ b/vm/src/function/argument.rs @@ -14,19 +14,20 @@ pub struct ArgCallable { } impl ArgCallable { - #[inline] pub fn invoke(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult { vm.invoke(&self.obj, args) } } impl Borrow for ArgCallable { + #[inline(always)] fn borrow(&self) -> &PyObject { &self.obj } } impl AsRef for ArgCallable { + #[inline(always)] fn as_ref(&self) -> &PyObject { &self.obj } @@ -116,24 +117,28 @@ impl ArgMapping { } impl Borrow for ArgMapping { + #[inline(always)] fn borrow(&self) -> &PyObject { &self.obj } } impl AsRef for ArgMapping { + #[inline(always)] fn as_ref(&self) -> &PyObject { &self.obj } } impl PyObjectWrap for ArgMapping { + #[inline(always)] fn into_object(self) -> PyObjectRef { self.obj } } impl IntoPyObject for ArgMapping { + #[inline(always)] fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self.obj } diff --git a/vm/src/protocol/iter.rs b/vm/src/protocol/iter.rs index 1ccfce00ff..c651ddf678 100644 --- a/vm/src/protocol/iter.rs +++ b/vm/src/protocol/iter.rs @@ -80,6 +80,7 @@ impl Borrow for PyIter where O: Borrow, { + #[inline(always)] fn borrow(&self) -> &PyObject { self.0.borrow() } @@ -89,6 +90,7 @@ impl AsRef for PyIter where O: Borrow, { + #[inline(always)] fn as_ref(&self) -> &PyObject { self.0.borrow() } @@ -99,12 +101,14 @@ where O: Borrow, { type Target = PyObject; + #[inline(always)] fn deref(&self) -> &Self::Target { self.0.borrow() } } impl IntoPyObject for PyIter { + #[inline(always)] fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self.into() } diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 0054c190a5..f3f40ddbd3 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -134,14 +134,17 @@ impl PyContext { context } + #[inline(always)] pub fn none(&self) -> PyObjectRef { self.none.clone().into() } + #[inline(always)] pub fn ellipsis(&self) -> PyObjectRef { self.ellipsis.clone().into() } + #[inline(always)] pub fn not_implemented(&self) -> PyObjectRef { self.not_implemented.clone().into() } @@ -170,19 +173,22 @@ impl PyContext { PyRef::new_ref(PyInt::from(i.clone()), self.types.int_type.clone(), None) } + #[inline] pub fn new_float(&self, value: f64) -> PyRef { PyRef::new_ref(PyFloat::from(value), self.types.float_type.clone(), None) } + #[inline] pub fn new_str(&self, s: impl Into) -> PyRef { pystr::PyStr::new_ref(s, self) } + #[inline] pub fn new_bytes(&self, data: Vec) -> PyRef { bytes::PyBytes::new_ref(data, self) } - #[inline] + #[inline(always)] pub fn new_bool(&self, b: bool) -> PyIntRef { let value = if b { &self.true_value @@ -192,14 +198,17 @@ impl PyContext { value.clone() } + #[inline(always)] pub fn new_tuple(&self, elements: Vec) -> PyTupleRef { PyTuple::new_ref(elements, self) } + #[inline(always)] pub fn new_list(&self, elements: Vec) -> PyListRef { PyList::new_ref(elements, self) } + #[inline(always)] pub fn new_dict(&self) -> PyDictRef { PyDict::new_ref(self) } @@ -376,12 +385,13 @@ impl TryFromObject for PyRefExact { } impl Deref for PyRefExact { type Target = PyRef; + #[inline(always)] fn deref(&self) -> &PyRef { &self.obj } } impl IntoPyObject for PyRefExact { - #[inline] + #[inline(always)] fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self.obj.into() } @@ -418,6 +428,7 @@ impl PyObject { } // impl Borrow for PyRc { +// #[inline(always)] // fn borrow(&self) -> &PyObject { // unsafe { &*(&**self as *const T as *const PyObject) } // } @@ -432,12 +443,14 @@ pub struct PyLease<'a, T: PyObjectPayload> { impl<'a, T: PyObjectPayload + PyValue> PyLease<'a, T> { // Associated function on purpose, because of deref #[allow(clippy::wrong_self_convention)] + #[inline(always)] pub fn into_pyref(zelf: Self) -> PyRef { zelf.inner.clone() } } impl<'a, T: PyObjectPayload + PyValue> Borrow for PyLease<'a, T> { + #[inline(always)] fn borrow(&self) -> &PyObject { self.inner.as_ref() } @@ -445,7 +458,7 @@ impl<'a, T: PyObjectPayload + PyValue> Borrow for PyLease<'a, T> { impl<'a, T: PyObjectPayload + PyValue> Deref for PyLease<'a, T> { type Target = PyRef; - + #[inline(always)] fn deref(&self) -> &Self::Target { &self.inner } @@ -521,27 +534,28 @@ impl IntoPyRef

for T where P: PyValue + IntoPyObject + From, { + #[inline(always)] fn into_pyref(self, vm: &VirtualMachine) -> PyRef

{ P::from(self).into_ref(vm) } } impl IntoPyObject for PyRef { - #[inline] + #[inline(always)] fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self.into() } } impl IntoPyObject for PyObjectRef { - #[inline] + #[inline(always)] fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self } } impl IntoPyObject for &PyObject { - #[inline] + #[inline(always)] fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self.to_owned() } @@ -553,7 +567,7 @@ impl IntoPyObject for T where T: PyValue + Sized, { - #[inline] + #[inline(always)] fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { PyValue::into_object(self, vm) } @@ -563,6 +577,7 @@ impl IntoPyResult for T where T: IntoPyObject, { + #[inline(always)] fn into_pyresult(self, vm: &VirtualMachine) -> PyResult { Ok(self.into_pyobject(vm)) } @@ -572,6 +587,7 @@ impl IntoPyResult for PyResult where T: IntoPyObject, { + #[inline(always)] fn into_pyresult(self, vm: &VirtualMachine) -> PyResult { self.map(|res| T::into_pyobject(res, vm)) } @@ -600,6 +616,7 @@ pub trait PyValue: fmt::Debug + PyThreadingConstraint + Sized + 'static { None } + #[inline] fn _into_ref(self, cls: PyTypeRef, vm: &VirtualMachine) -> PyRef { let dict = if cls.slots.flags.has_feature(PyTypeFlags::HAS_DICT) { Some(vm.ctx.new_dict()) @@ -609,24 +626,36 @@ pub trait PyValue: fmt::Debug + PyThreadingConstraint + Sized + 'static { PyRef::new_ref(self, cls, dict) } + #[inline] fn into_ref(self, vm: &VirtualMachine) -> PyRef { let cls = Self::class(vm); self._into_ref(cls.clone(), vm) } + #[cold] + fn _into_ref_with_type_error( + vm: &VirtualMachine, + cls: &PyTypeRef, + exact_class: &PyTypeRef, + ) -> PyBaseExceptionRef { + vm.new_type_error(format!( + "'{}' is not a subtype of '{}'", + &cls.name(), + exact_class.name() + )) + } + + #[inline] fn into_ref_with_type(self, vm: &VirtualMachine, cls: PyTypeRef) -> PyResult> { let exact_class = Self::class(vm); if cls.issubclass(exact_class) { Ok(self._into_ref(cls, vm)) } else { - Err(vm.new_type_error(format!( - "'{}' is not a subtype of '{}'", - &cls.name(), - exact_class.name() - ))) + Err(Self::_into_ref_with_type_error(vm, &cls, exact_class)) } } + #[inline] fn into_pyresult_with_type(self, vm: &VirtualMachine, cls: PyTypeRef) -> PyResult { self.into_ref_with_type(vm, cls).into_pyresult(vm) } @@ -710,6 +739,7 @@ impl From for PyObjectRef where T: PyObjectWrap, { + #[inline(always)] fn from(py_ref: T) -> Self { py_ref.into_object() } diff --git a/vm/src/pyobjectrc.rs b/vm/src/pyobjectrc.rs index 0e1630c688..ec1841d1a5 100644 --- a/vm/src/pyobjectrc.rs +++ b/vm/src/pyobjectrc.rs @@ -283,14 +283,17 @@ unsafe impl Link for WeakLink { type Target = PyObjectView; + #[inline(always)] fn as_raw(handle: &PyRef) -> NonNull { NonNull::from(&**handle) } + #[inline(always)] unsafe fn from_raw(ptr: NonNull) -> Self::Handle { PyRef::from_raw(ptr.as_ptr()) } + #[inline(always)] unsafe fn pointers(target: NonNull) -> NonNull> { NonNull::new_unchecked(ptr::addr_of_mut!((*target.as_ptr()).0.payload.pointers)) } @@ -331,7 +334,6 @@ impl PyWeak { guard.obj.is_none() } - #[inline(always)] fn drop_inner(&self) { let dealloc = { let mut guard = unsafe { self.parent.as_ref().lock() }; @@ -354,6 +356,7 @@ impl PyWeak { } impl Drop for PyWeak { + #[inline(always)] fn drop(&mut self) { // we do NOT have actual exclusive access! // no clue if doing this actually reduces chance of UB @@ -368,7 +371,7 @@ struct InstanceDict { } impl From for InstanceDict { - #[inline] + #[inline(always)] fn from(d: PyDictRef) -> Self { Self::new(d) } @@ -447,6 +450,7 @@ pub struct PyObject(PyInner); impl Deref for PyObjectRef { type Target = PyObject; + #[inline(always)] fn deref(&self) -> &PyObject { unsafe { self.ptr.as_ref() } } @@ -465,6 +469,7 @@ impl ToOwned for PyObject { } impl PyObjectRef { + #[inline(always)] pub fn into_raw(self) -> *const PyObject { let ptr = self.as_raw(); std::mem::forget(self); @@ -476,6 +481,7 @@ impl PyObjectRef { /// [`PyObjectRef::into_raw`]. The user is responsible for ensuring that the inner data is not /// dropped more than once due to mishandling the reference count by calling this function /// too many times. + #[inline(always)] pub unsafe fn from_raw(ptr: *const PyObject) -> Self { Self { ptr: NonNull::new_unchecked(ptr as *mut PyObject), @@ -486,6 +492,7 @@ impl PyObjectRef { /// /// If the downcast fails, the original ref is returned in as `Err` so /// another downcast can be attempted without unnecessary cloning. + #[inline(always)] pub fn downcast(self) -> Result, Self> { if self.payload_is::() { Ok(unsafe { PyRef::from_obj_unchecked(self) }) @@ -494,6 +501,7 @@ impl PyObjectRef { } } + #[inline(always)] pub fn downcast_ref(&self) -> Option<&PyObjectView> { if self.payload_is::() { // SAFETY: just checked that the payload is T, and PyRef is repr(transparent) over @@ -506,12 +514,14 @@ impl PyObjectRef { /// # Safety /// T must be the exact payload type + #[inline(always)] pub unsafe fn downcast_unchecked(self) -> PyRef { PyRef::from_obj_unchecked(self) } /// # Safety /// T must be the exact payload type + #[inline(always)] pub unsafe fn downcast_unchecked_ref(&self) -> &crate::PyObjectView { debug_assert!(self.payload_is::()); &*(self as *const PyObjectRef as *const PyRef) @@ -523,6 +533,7 @@ impl PyObjectRef { /// /// If the downcast fails, the original ref is returned in as `Err` so /// another downcast can be attempted without unnecessary cloning. + #[inline] pub fn downcast_exact( self, vm: &VirtualMachine, @@ -542,7 +553,7 @@ impl PyObjectRef { } impl PyObject { - #[inline] + #[inline(always)] fn weak_ref_list(&self) -> Option<&WeakRefList> { Some(&self.0.weak_list) } @@ -595,11 +606,12 @@ impl PyObject { self.weak_ref_list().map(|wrl| wrl.get_weak_references()) } - #[inline] + #[inline(always)] pub fn payload_is(&self) -> bool { self.0.typeid == TypeId::of::() } + #[inline(always)] pub fn payload(&self) -> Option<&T> { if self.payload_is::() { // we cast to a PyInner first because we don't know T's exact offset because of @@ -611,11 +623,12 @@ impl PyObject { } } + #[inline(always)] pub(crate) fn class_lock(&self) -> &PyRwLock { &self.0.typ } - #[inline] + #[inline(always)] pub fn payload_if_exact( &self, vm: &VirtualMachine, @@ -627,14 +640,16 @@ impl PyObject { } } - #[inline] + #[inline(always)] fn instance_dict(&self) -> Option<&InstanceDict> { self.0.dict.as_ref() } + #[inline(always)] pub fn dict(&self) -> Option { self.instance_dict().map(|d| d.get()) } + /// Set the dict field. Returns `Err(dict)` if this object does not have a dict field /// in the first place. pub fn set_dict(&self, dict: PyDictRef) -> Result<(), PyDictRef> { @@ -647,7 +662,7 @@ impl PyObject { } } - #[inline] + #[inline(always)] pub fn payload_if_subclass(&self, vm: &VirtualMachine) -> Option<&T> { if self.class().issubclass(T::class(vm)) { self.payload() @@ -656,6 +671,7 @@ impl PyObject { } } + #[inline(always)] pub fn downcast_ref(&self) -> Option<&PyObjectView> { if self.payload_is::() { // SAFETY: just checked that the payload is T, and PyRef is repr(transparent) over @@ -666,6 +682,7 @@ impl PyObject { } } + #[inline(always)] pub fn downcast_ref_if_exact( &self, vm: &VirtualMachine, @@ -677,13 +694,13 @@ impl PyObject { /// # Safety /// T must be the exact payload type - #[inline] + #[inline(always)] pub unsafe fn downcast_unchecked_ref(&self) -> &PyObjectView { debug_assert!(self.payload_is::()); &*(self as *const PyObject as *const PyObjectView) } - #[inline] + #[inline(always)] pub fn strong_count(&self) -> usize { self.0.ref_count.get() } @@ -693,12 +710,12 @@ impl PyObject { self.weak_ref_list().map(|wrl| wrl.count()) } - #[inline] + #[inline(always)] pub fn as_raw(&self) -> *const PyObject { self } - #[inline] + #[inline(always)] // the outer function is never inlined fn drop_slow_inner(&self) -> Result<(), ()> { // CPython-compatible drop implementation if let Some(slot_del) = self.class().mro_find_map(|cls| cls.slots.del.load()) { @@ -741,50 +758,56 @@ impl PyObject { } impl Borrow for PyObjectRef { + #[inline(always)] fn borrow(&self) -> &PyObject { self } } impl AsRef for PyObjectRef { + #[inline(always)] fn as_ref(&self) -> &PyObject { self } } impl AsRef for PyObject { + #[inline(always)] fn as_ref(&self) -> &PyObject { self } } impl<'a, T: PyObjectPayload> From<&'a PyObjectView> for &'a PyObject { + #[inline(always)] fn from(py_ref: &'a PyObjectView) -> Self { py_ref.as_object() } } impl Borrow for PyObjectWeak { - #[inline] + #[inline(always)] fn borrow(&self) -> &PyObject { self.weak.as_object() } } impl PyObjectWrap for PyObjectWeak { + #[inline(always)] fn into_object(self) -> PyObjectRef { self.weak.into_object() } } impl PyObjectWeak { - #[inline] + #[inline(always)] pub fn upgrade(&self) -> Option { self.weak.upgrade() } } impl Drop for PyObjectRef { + #[inline] fn drop(&mut self) { if self.0.ref_count.dec() { unsafe { PyObject::drop_slow(self.ptr) } @@ -930,6 +953,7 @@ impl Clone for PyRef { } impl PyRef { + #[inline(always)] unsafe fn from_raw(raw: *const PyObjectView) -> Self { Self { ptr: NonNull::new_unchecked(raw as *mut _), @@ -937,7 +961,7 @@ impl PyRef { } /// Safety: payload type of `obj` must be `T` - #[inline] + #[inline(always)] unsafe fn from_obj_unchecked(obj: PyObjectRef) -> Self { debug_assert!(obj.payload_is::()); let obj = ManuallyDrop::new(obj); @@ -946,7 +970,7 @@ impl PyRef { } } - #[inline] + #[inline(always)] pub fn new_ref(payload: T, typ: crate::builtins::PyTypeRef, dict: Option) -> Self { let inner = Box::into_raw(PyInner::new(payload, typ, dict)); Self { @@ -959,6 +983,7 @@ impl Borrow for PyRef where T: PyObjectPayload, { + #[inline(always)] fn borrow(&self) -> &PyObject { (**self).as_object() } @@ -989,6 +1014,7 @@ impl Borrow> for PyRef where T: PyObjectPayload, { + #[inline(always)] fn borrow(&self) -> &PyObjectView { self } diff --git a/vm/src/utils.rs b/vm/src/utils.rs index dc990afba6..5d1ec49ce5 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -12,6 +12,7 @@ pub enum Either { } impl, B: Borrow> Borrow for Either { + #[inline(always)] fn borrow(&self) -> &PyObject { match self { Either::A(a) => a.borrow(), @@ -21,6 +22,7 @@ impl, B: Borrow> Borrow for Either } impl, B: AsRef> AsRef for Either { + #[inline(always)] fn as_ref(&self) -> &PyObject { match self { Either::A(a) => a.as_ref(), @@ -30,6 +32,7 @@ impl, B: AsRef> AsRef for Either { } impl PyObjectWrap for Either { + #[inline(always)] fn into_object(self) -> PyObjectRef { match self { Either::A(a) => a.into_object(), @@ -39,6 +42,7 @@ impl PyObjectWrap for Either { } impl IntoPyObject for Either { + #[inline(always)] fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { match self { Self::A(a) => a.into_pyobject(vm), From f01e28e4cadc4b0dd482752e811445a6b4f52f0a Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Sun, 17 Apr 2022 18:39:11 +0900 Subject: [PATCH 9/9] Fix unused import --- vm/src/stdlib/posix.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vm/src/stdlib/posix.rs b/vm/src/stdlib/posix.rs index 2b2c153f58..5b1a9eff69 100644 --- a/vm/src/stdlib/posix.rs +++ b/vm/src/stdlib/posix.rs @@ -30,7 +30,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef { pub mod module { use crate::{ builtins::{PyDictRef, PyInt, PyIntRef, PyListRef, PyStrRef, PyTupleRef, PyTypeRef}, - function::{ArgBytesLike, IntoPyException, IntoPyObject, OptionalArg}, + function::{IntoPyException, IntoPyObject, OptionalArg}, stdlib::os::{ errno_err, DirFd, FollowSymlinks, PathOrFd, PyPathLike, SupportFunc, TargetIsDirectory, _os, fs_metadata, IOErrorBuilder, @@ -1929,10 +1929,10 @@ pub mod module { fn _extract_vec_bytes( x: OptionalArg, vm: &VirtualMachine, - ) -> PyResult>> { + ) -> PyResult>> { x.into_option() .map(|x| { - let v: Vec = x.try_to_value(vm)?; + let v: Vec = x.try_to_value(vm)?; Ok(if v.is_empty() { None } else { Some(v) }) }) .transpose()