forked from Rust-related/RustPython
Mini optimizations
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user