diff --git a/tests/snippets/bools.py b/tests/snippets/bools.py index 2aa817ca4..0c277143b 100644 --- a/tests/snippets/bools.py +++ b/tests/snippets/bools.py @@ -16,6 +16,9 @@ assert bool() == False assert bool(1) == True assert bool({}) == False +assert bool(NotImplemented) == True +assert bool(...) == True + if not 1: raise BaseException diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 977759607..f5dc38ff0 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -286,10 +286,14 @@ impl Frame { let mut out: Vec> = elements .into_iter() - .map(|x| match x.payload { - PyObjectPayload::Integer { ref value } => Some(value.clone()), - PyObjectPayload::None => None, - _ => panic!("Expect Int or None as BUILD_SLICE arguments, got {:?}", x), + .map(|x| { + if x.is(&vm.get_none()) { + None + } else if let PyObjectPayload::Integer { ref value } = x.payload { + Some(value.clone()) + } else { + panic!("Expect Int or None as BUILD_SLICE arguments, got {:?}", x); + } }) .collect(); @@ -575,18 +579,15 @@ impl Frame { } bytecode::Instruction::PrintExpr => { let expr = self.pop_value(); - match expr.payload { - PyObjectPayload::None => (), - _ => { - let repr = vm.to_repr(&expr)?; - builtins::builtin_print( - vm, - PyFuncArgs { - args: vec![repr], - kwargs: vec![], - }, - )?; - } + if !expr.is(&vm.get_none()) { + let repr = vm.to_repr(&expr)?; + builtins::builtin_print( + vm, + PyFuncArgs { + args: vec![repr], + kwargs: vec![], + }, + )?; } Ok(None) } diff --git a/vm/src/obj/objbool.rs b/vm/src/obj/objbool.rs index e45018ea6..7a96effd2 100644 --- a/vm/src/obj/objbool.rs +++ b/vm/src/obj/objbool.rs @@ -14,7 +14,7 @@ impl IntoPyObject for bool { } } -pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result { +pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> PyResult { if let Some(s) = obj.payload::() { return Ok(!s.value.is_empty()); } @@ -27,7 +27,6 @@ pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result !value.is_zero(), PyObjectPayload::Sequence { ref elements } => !elements.borrow().is_empty(), - PyObjectPayload::None { .. } => false, _ => { if let Ok(f) = vm.get_method(obj.clone(), "__bool__") { let bool_res = vm.invoke(f, PyFuncArgs::default())?; diff --git a/vm/src/obj/objnone.rs b/vm/src/obj/objnone.rs index 2aceea915..bc94f9cb9 100644 --- a/vm/src/obj/objnone.rs +++ b/vm/src/obj/objnone.rs @@ -5,6 +5,7 @@ pub fn init(context: &PyContext) { let none_type = &context.none.typ(); context.set_attr(&none_type, "__new__", context.new_rustfunc(none_new)); context.set_attr(&none_type, "__repr__", context.new_rustfunc(none_repr)); + context.set_attr(&none_type, "__bool__", context.new_rustfunc(none_bool)); } fn none_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { @@ -20,3 +21,8 @@ fn none_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.none().typ()))]); Ok(vm.ctx.new_str("None".to_string())) } + +fn none_bool(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.none().typ()))]); + Ok(vm.ctx.new_bool(false)) +} diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 6a618cd7b..75f52b37d 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -156,7 +156,9 @@ pub struct PyContext { fn _nothing() -> PyObjectRef { PyObject { - payload: PyObjectPayload::None, + payload: PyObjectPayload::AnyRustValue { + value: Box::new(()), + }, typ: None, } .into_ref() @@ -224,14 +226,23 @@ impl PyContext { let exceptions = exceptions::ExceptionZoo::new(&type_type, &object_type, &dict_type); let none = PyObject::new( - PyObjectPayload::None, + PyObjectPayload::AnyRustValue { + value: Box::new(()), + }, create_type("NoneType", &type_type, &object_type, &dict_type), ); - let ellipsis = PyObject::new(PyObjectPayload::None, ellipsis_type.clone()); + let ellipsis = PyObject::new( + PyObjectPayload::AnyRustValue { + value: Box::new(()), + }, + ellipsis_type.clone(), + ); let not_implemented = PyObject::new( - PyObjectPayload::NotImplemented, + PyObjectPayload::AnyRustValue { + value: Box::new(()), + }, create_type("NotImplementedType", &type_type, &object_type, &dict_type), ); @@ -1500,8 +1511,6 @@ pub enum PyObjectPayload { name: String, scope: ScopeRef, }, - None, - NotImplemented, Class { name: String, dict: RefCell, @@ -1542,8 +1551,6 @@ impl fmt::Debug for PyObjectPayload { ref object, } => write!(f, "bound-method: {:?} of {:?}", function, object), PyObjectPayload::Module { .. } => write!(f, "module"), - PyObjectPayload::None => write!(f, "None"), - PyObjectPayload::NotImplemented => write!(f, "NotImplemented"), PyObjectPayload::Class { ref name, .. } => write!(f, "class {:?}", name), PyObjectPayload::Instance { .. } => write!(f, "instance"), PyObjectPayload::RustFunction { .. } => write!(f, "rust function"), diff --git a/vm/src/stdlib/json.rs b/vm/src/stdlib/json.rs index 7403c8d8c..0d0bc23dc 100644 --- a/vm/src/stdlib/json.rs +++ b/vm/src/stdlib/json.rs @@ -11,7 +11,7 @@ use crate::obj::{ objtype, }; use crate::pyobject::{ - create_type, DictProtocol, PyContext, PyFuncArgs, PyObjectPayload, PyObjectRef, PyResult, + create_type, DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol, }; use crate::VirtualMachine; @@ -69,7 +69,7 @@ impl<'s> serde::Serialize for PyObjectSerializer<'s> { map.serialize_entry(&key, &self.clone_with_object(&e.1))?; } map.end() - } else if let PyObjectPayload::None = self.pyobject.payload { + } else if self.pyobject.is(&self.vm.get_none()) { serializer.serialize_none() } else { Err(serde::ser::Error::custom(format!( diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 709a9f46c..e3d06d5fa 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -444,10 +444,12 @@ impl VirtualMachine { // Add missing positional arguments, if we have fewer positional arguments than the // function definition calls for if nargs < nexpected_args { - let available_defaults = match defaults.payload { - PyObjectPayload::Sequence { ref elements } => elements.borrow().clone(), - PyObjectPayload::None => vec![], - _ => panic!("function defaults not tuple or None"), + let available_defaults = if defaults.is(&self.get_none()) { + vec![] + } else if let PyObjectPayload::Sequence { ref elements } = defaults.payload { + elements.borrow().clone() + } else { + panic!("function defaults not tuple or None"); }; // Given the number of defaults available, check all the arguments for which we