Mini optimizations

This commit is contained in:
coolreader18
2019-11-24 20:44:45 -06:00
parent bca6214c4f
commit 765529abab
2 changed files with 13 additions and 3 deletions

View File

@@ -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<bool> {
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

View File

@@ -449,8 +449,12 @@ impl VirtualMachine {
// Container of the virtual machine state:
pub fn to_str(&self, obj: &PyObjectRef) -> PyResult<PyStringRef> {
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<String> {