From 520f71f354ff5ec1af554cc4b8fdc603f48b84c9 Mon Sep 17 00:00:00 2001 From: Joey Hain Date: Fri, 8 Feb 2019 19:24:08 -0800 Subject: [PATCH] Add NotImplemented built-in constant --- tests/snippets/builtin_complex.py | 2 +- vm/src/builtins.rs | 3 +++ vm/src/obj/objcomplex.rs | 4 +++- vm/src/pyobject.rs | 13 +++++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/snippets/builtin_complex.py b/tests/snippets/builtin_complex.py index 8c897fd85..ae88777b4 100644 --- a/tests/snippets/builtin_complex.py +++ b/tests/snippets/builtin_complex.py @@ -14,7 +14,7 @@ assert not complex(1, 1).__eq__(1.0) assert not complex(1, 0).__eq__(1.5) assert complex(1, 0).__eq__(True) assert not complex(1, 2).__eq__(complex(1, 1)) -#assert complex(1, 2).__eq__('foo') == NotImplemented +assert complex(1, 2).__eq__('foo') == NotImplemented # __neg__ diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs index f8df09a62..ff903c116 100644 --- a/vm/src/builtins.rs +++ b/vm/src/builtins.rs @@ -685,6 +685,9 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef { ctx.set_attr(&py_mod, "type", ctx.type_type()); ctx.set_attr(&py_mod, "zip", ctx.zip_type()); + // Constants + ctx.set_attr(&py_mod, "NotImplemented", ctx.not_implemented.clone()); + // Exceptions: ctx.set_attr( &py_mod, diff --git a/vm/src/obj/objcomplex.rs b/vm/src/obj/objcomplex.rs index e8bb66ea7..77f200f7e 100644 --- a/vm/src/obj/objcomplex.rs +++ b/vm/src/obj/objcomplex.rs @@ -112,6 +112,7 @@ fn complex_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { ); let z = get_value(zelf); + let result = if objtype::isinstance(other, &vm.ctx.complex_type()) { z == get_value(other) } else if objtype::isinstance(other, &vm.ctx.int_type()) { @@ -122,8 +123,9 @@ fn complex_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { } else if objtype::isinstance(other, &vm.ctx.float_type()) { z.im == 0.0 && z.re == objfloat::get_value(other) } else { - false + return Ok(vm.ctx.not_implemented()); }; + Ok(vm.ctx.new_bool(result)) } diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index d486bfaef..cf9d7b231 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -130,6 +130,7 @@ pub struct PyContext { pub map_type: PyObjectRef, pub memoryview_type: PyObjectRef, pub none: PyObjectRef, + pub not_implemented: PyObjectRef, pub tuple_type: PyObjectRef, pub set_type: PyObjectRef, pub staticmethod_type: PyObjectRef, @@ -223,6 +224,11 @@ impl PyContext { create_type("NoneType", &type_type, &object_type, &dict_type), ); + let not_implemented = PyObject::new( + PyObjectPayload::NotImplemented, + create_type("NotImplementedType", &type_type, &object_type, &dict_type), + ); + let true_value = PyObject::new( PyObjectPayload::Integer { value: One::one() }, bool_type.clone(), @@ -258,6 +264,7 @@ impl PyContext { zip_type, dict_type, none, + not_implemented, str_type, range_type, object: object_type, @@ -423,6 +430,9 @@ impl PyContext { pub fn none(&self) -> PyObjectRef { self.none.clone() } + pub fn not_implemented(&self) -> PyObjectRef { + self.not_implemented.clone() + } pub fn object(&self) -> PyObjectRef { self.object.clone() } @@ -956,6 +966,7 @@ pub enum PyObjectPayload { dict: PyObjectRef, }, None, + NotImplemented, Class { name: String, dict: RefCell, @@ -1002,6 +1013,7 @@ impl fmt::Debug for PyObjectPayload { PyObjectPayload::Module { .. } => write!(f, "module"), PyObjectPayload::Scope { .. } => write!(f, "scope"), 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"), @@ -1058,6 +1070,7 @@ impl PyObject { ), PyObjectPayload::WeakRef { .. } => String::from("weakref"), PyObjectPayload::None => String::from("None"), + PyObjectPayload::NotImplemented => String::from("NotImplemented"), PyObjectPayload::Class { ref name, dict: ref _dict,