Add NotImplemented built-in constant

This commit is contained in:
Joey Hain
2019-02-08 19:24:08 -08:00
parent d66ca54a2d
commit 520f71f354
4 changed files with 20 additions and 2 deletions

View File

@@ -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__

View File

@@ -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,

View File

@@ -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))
}

View File

@@ -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<PyAttributes>,
@@ -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,