diff --git a/tests/snippets/none.py b/tests/snippets/none.py index 0edbfb05aa..a77079162b 100644 --- a/tests/snippets/none.py +++ b/tests/snippets/none.py @@ -14,3 +14,7 @@ def none2(): assert none() is x assert none() is none2() + +assert str(None) == 'None' +assert repr(None) == 'None' +assert type(None)() is None diff --git a/vm/src/obj/mod.rs b/vm/src/obj/mod.rs index f08998fcbb..f5dff7563c 100644 --- a/vm/src/obj/mod.rs +++ b/vm/src/obj/mod.rs @@ -17,6 +17,7 @@ pub mod objiter; pub mod objlist; pub mod objmap; pub mod objmemory; +pub mod objnone; pub mod objobject; pub mod objproperty; pub mod objrange; diff --git a/vm/src/obj/objnone.rs b/vm/src/obj/objnone.rs new file mode 100644 index 0000000000..429abaff3e --- /dev/null +++ b/vm/src/obj/objnone.rs @@ -0,0 +1,23 @@ +use super::super::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol}; +use super::super::vm::VirtualMachine; +use super::objtype; + +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)); +} + +fn none_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!( + vm, + args, + required = [(_zelf, Some(vm.ctx.type_type.clone()))] + ); + Ok(vm.get_none()) +} + +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())) +} diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 163821bd09..5cd8c9b9e2 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -18,6 +18,7 @@ use super::obj::objiter; use super::obj::objlist; use super::obj::objmap; use super::obj::objmemory; +use super::obj::objnone; use super::obj::objobject; use super::obj::objproperty; use super::obj::objrange; @@ -318,6 +319,7 @@ impl PyContext { objbool::init(&context); objcode::init(&context); objframe::init(&context); + objnone::init(&context); exceptions::init(&context); context }