From d60dfd2ad8fadb43822eae86c9698a8f13de7320 Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Fri, 24 Aug 2018 18:48:09 +0100 Subject: [PATCH] Test __bool__ and fix it. --- tests/snippets/bools.py | 6 ++++++ vm/src/objbool.rs | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/snippets/bools.py b/tests/snippets/bools.py index 97be6c084..e4ff5c52c 100644 --- a/tests/snippets/bools.py +++ b/tests/snippets/bools.py @@ -22,3 +22,9 @@ if not {} and not [1]: if not object(): raise BaseException + +class Falsey: + def __bool__(self): + return False + +assert not Falsey() diff --git a/vm/src/objbool.rs b/vm/src/objbool.rs index ad0344a60..4e18fd61d 100644 --- a/vm/src/objbool.rs +++ b/vm/src/objbool.rs @@ -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 !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,