Test __bool__ and fix it.

This commit is contained in:
Adam Kelly
2018-08-24 18:48:09 +01:00
parent d5668c32ee
commit d60dfd2ad8
2 changed files with 8 additions and 2 deletions

View File

@@ -22,3 +22,9 @@ if not {} and not [1]:
if not object():
raise BaseException
class Falsey:
def __bool__(self):
return False
assert not Falsey()

View File

@@ -1,6 +1,6 @@
use super::objtype;
use super::pyobject::{
AttributeProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
AttributeProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult,
};
use super::vm::VirtualMachine;
@@ -14,7 +14,7 @@ pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result<bool, PyObje
PyObjectKind::Dict { ref elements } => !elements.is_empty(),
PyObjectKind::String { ref value } => !value.is_empty(),
_ => {
let f = objtype::get_attribute(vm, obj.typ(), &String::from("__bool__"))?;
let f = objtype::get_attribute(vm, obj.clone(), &String::from("__bool__"))?;
match vm.invoke(f, PyFuncArgs::new()) {
Ok(result) => match result.borrow().kind {
PyObjectKind::Boolean { value } => value,