diff --git a/vm/src/obj/objbool.rs b/vm/src/obj/objbool.rs index 3a97312cd..fc5b57658 100644 --- a/vm/src/obj/objbool.rs +++ b/vm/src/obj/objbool.rs @@ -3,7 +3,7 @@ use num_traits::Zero; use crate::function::PyFuncArgs; use crate::pyobject::{ - IntoPyObject, PyContext, PyObjectRef, PyResult, TryFromObject, TypeProtocol, + IdProtocol, IntoPyObject, PyContext, PyObjectRef, PyResult, TryFromObject, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -29,6 +29,12 @@ impl TryFromObject for bool { /// Convert Python bool into Rust bool. pub fn boolval(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult { + if obj.is(&vm.ctx.true_value) { + return Ok(true); + } + if obj.is(&vm.ctx.false_value) { + return Ok(false); + } let rs_bool = match vm.get_method(obj.clone(), "__bool__") { Some(method_or_err) => { // If descriptor returns Error, propagate it further diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 635418a28..c79323094 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -449,8 +449,12 @@ impl VirtualMachine { // Container of the virtual machine state: pub fn to_str(&self, obj: &PyObjectRef) -> PyResult { - let str = self.call_method(&obj, "__str__", vec![])?; - TryFromObject::try_from_object(self, str) + if obj.class().is(&self.ctx.types.str_type) { + Ok(obj.clone().downcast().unwrap()) + } else { + let s = self.call_method(&obj, "__str__", vec![])?; + PyStringRef::try_from_object(self, s) + } } pub fn to_pystr<'a, T: Into<&'a PyObjectRef>>(&'a self, obj: T) -> PyResult {