Skip to content

Commit 520f71f

Browse files
Add NotImplemented built-in constant
1 parent d66ca54 commit 520f71f

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

tests/snippets/builtin_complex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
assert not complex(1, 0).__eq__(1.5)
1515
assert complex(1, 0).__eq__(True)
1616
assert not complex(1, 2).__eq__(complex(1, 1))
17-
#assert complex(1, 2).__eq__('foo') == NotImplemented
17+
assert complex(1, 2).__eq__('foo') == NotImplemented
1818

1919
# __neg__
2020

vm/src/builtins.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
685685
ctx.set_attr(&py_mod, "type", ctx.type_type());
686686
ctx.set_attr(&py_mod, "zip", ctx.zip_type());
687687

688+
// Constants
689+
ctx.set_attr(&py_mod, "NotImplemented", ctx.not_implemented.clone());
690+
688691
// Exceptions:
689692
ctx.set_attr(
690693
&py_mod,

vm/src/obj/objcomplex.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ fn complex_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
112112
);
113113

114114
let z = get_value(zelf);
115+
115116
let result = if objtype::isinstance(other, &vm.ctx.complex_type()) {
116117
z == get_value(other)
117118
} else if objtype::isinstance(other, &vm.ctx.int_type()) {
@@ -122,8 +123,9 @@ fn complex_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
122123
} else if objtype::isinstance(other, &vm.ctx.float_type()) {
123124
z.im == 0.0 && z.re == objfloat::get_value(other)
124125
} else {
125-
false
126+
return Ok(vm.ctx.not_implemented());
126127
};
128+
127129
Ok(vm.ctx.new_bool(result))
128130
}
129131

vm/src/pyobject.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ pub struct PyContext {
130130
pub map_type: PyObjectRef,
131131
pub memoryview_type: PyObjectRef,
132132
pub none: PyObjectRef,
133+
pub not_implemented: PyObjectRef,
133134
pub tuple_type: PyObjectRef,
134135
pub set_type: PyObjectRef,
135136
pub staticmethod_type: PyObjectRef,
@@ -223,6 +224,11 @@ impl PyContext {
223224
create_type("NoneType", &type_type, &object_type, &dict_type),
224225
);
225226

227+
let not_implemented = PyObject::new(
228+
PyObjectPayload::NotImplemented,
229+
create_type("NotImplementedType", &type_type, &object_type, &dict_type),
230+
);
231+
226232
let true_value = PyObject::new(
227233
PyObjectPayload::Integer { value: One::one() },
228234
bool_type.clone(),
@@ -258,6 +264,7 @@ impl PyContext {
258264
zip_type,
259265
dict_type,
260266
none,
267+
not_implemented,
261268
str_type,
262269
range_type,
263270
object: object_type,
@@ -423,6 +430,9 @@ impl PyContext {
423430
pub fn none(&self) -> PyObjectRef {
424431
self.none.clone()
425432
}
433+
pub fn not_implemented(&self) -> PyObjectRef {
434+
self.not_implemented.clone()
435+
}
426436
pub fn object(&self) -> PyObjectRef {
427437
self.object.clone()
428438
}
@@ -956,6 +966,7 @@ pub enum PyObjectPayload {
956966
dict: PyObjectRef,
957967
},
958968
None,
969+
NotImplemented,
959970
Class {
960971
name: String,
961972
dict: RefCell<PyAttributes>,
@@ -1002,6 +1013,7 @@ impl fmt::Debug for PyObjectPayload {
10021013
PyObjectPayload::Module { .. } => write!(f, "module"),
10031014
PyObjectPayload::Scope { .. } => write!(f, "scope"),
10041015
PyObjectPayload::None => write!(f, "None"),
1016+
PyObjectPayload::NotImplemented => write!(f, "NotImplemented"),
10051017
PyObjectPayload::Class { ref name, .. } => write!(f, "class {:?}", name),
10061018
PyObjectPayload::Instance { .. } => write!(f, "instance"),
10071019
PyObjectPayload::RustFunction { .. } => write!(f, "rust function"),
@@ -1058,6 +1070,7 @@ impl PyObject {
10581070
),
10591071
PyObjectPayload::WeakRef { .. } => String::from("weakref"),
10601072
PyObjectPayload::None => String::from("None"),
1073+
PyObjectPayload::NotImplemented => String::from("NotImplemented"),
10611074
PyObjectPayload::Class {
10621075
ref name,
10631076
dict: ref _dict,

0 commit comments

Comments
 (0)