From e1afef106af542e7cb0abc0ade9f57bc62bb7dd2 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sat, 28 Aug 2021 14:18:55 +0900 Subject: [PATCH] PyValue::_into_ref --- vm/src/pyobject.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index cdd89e66c..ea66d3db2 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -991,8 +991,7 @@ pub trait PyValue: fmt::Debug + PyThreadingConstraint + Sized + 'static { None } - fn into_ref(self, vm: &VirtualMachine) -> PyRef { - let cls = Self::class(vm).clone(); + fn _into_ref(self, cls: PyTypeRef, vm: &VirtualMachine) -> PyRef { let dict = if cls.slots.flags.has_feature(PyTpFlags::HAS_DICT) { Some(vm.ctx.new_dict()) } else { @@ -1001,19 +1000,20 @@ pub trait PyValue: fmt::Debug + PyThreadingConstraint + Sized + 'static { PyRef::new_ref(self, cls, dict) } + fn into_ref(self, vm: &VirtualMachine) -> PyRef { + let cls = Self::class(vm); + self._into_ref(cls.clone(), vm) + } + fn into_ref_with_type(self, vm: &VirtualMachine, cls: PyTypeRef) -> PyResult> { let exact_class = Self::class(vm); if cls.issubclass(exact_class) { - let dict = if cls.slots.flags.has_feature(PyTpFlags::HAS_DICT) { - Some(vm.ctx.new_dict()) - } else { - None - }; - Ok(PyRef::new_ref(self, cls, dict)) + Ok(self._into_ref(cls, vm)) } else { - let subtype = vm.to_str(cls.as_object())?; - let basetype = vm.to_str(exact_class.as_object())?; - Err(vm.new_type_error(format!("{} is not a subtype of {}", subtype, basetype))) + Err(vm.new_type_error(format!( + "'{}' is not a subtype of '{}'", + &cls.name, exact_class.name + ))) } } }