Skip to content

Commit 647cb08

Browse files
committed
Remove special case for None payload from boolval.
1 parent d278fb8 commit 647cb08

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

tests/snippets/bools.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
assert bool(1) == True
1717
assert bool({}) == False
1818

19+
assert bool(NotImplemented) == True
20+
assert bool(...) == True
21+
1922
if not 1:
2023
raise BaseException
2124

vm/src/obj/objbool.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl IntoPyObject for bool {
1313
}
1414
}
1515

16-
pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result<bool, PyObjectRef> {
16+
pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> PyResult<bool> {
1717
if let Some(s) = obj.payload::<PyString>() {
1818
return Ok(!s.value.is_empty());
1919
}
@@ -24,7 +24,6 @@ pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result<bool, PyObje
2424
PyObjectPayload::Integer { ref value } => !value.is_zero(),
2525
PyObjectPayload::Sequence { ref elements } => !elements.borrow().is_empty(),
2626
PyObjectPayload::Dict { ref elements } => !elements.borrow().is_empty(),
27-
PyObjectPayload::None { .. } => false,
2827
_ => {
2928
if let Ok(f) = vm.get_method(obj.clone(), "__bool__") {
3029
let bool_res = vm.invoke(f, PyFuncArgs::default())?;

vm/src/obj/objnone.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub fn init(context: &PyContext) {
55
let none_type = &context.none.typ();
66
context.set_attr(&none_type, "__new__", context.new_rustfunc(none_new));
77
context.set_attr(&none_type, "__repr__", context.new_rustfunc(none_repr));
8+
context.set_attr(&none_type, "__bool__", context.new_rustfunc(none_bool));
89
}
910

1011
fn none_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -20,3 +21,8 @@ fn none_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2021
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.none().typ()))]);
2122
Ok(vm.ctx.new_str("None".to_string()))
2223
}
24+
25+
fn none_bool(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
26+
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.none().typ()))]);
27+
Ok(vm.ctx.new_bool(false))
28+
}

0 commit comments

Comments
 (0)