diff --git a/vm/src/obj/objnone.rs b/vm/src/obj/objnone.rs index 3ccb6581f..fab016b63 100644 --- a/vm/src/obj/objnone.rs +++ b/vm/src/obj/objnone.rs @@ -2,10 +2,12 @@ use crate::obj::objproperty::PyPropertyRef; use crate::obj::objstr::PyStringRef; use crate::obj::objtype::{class_get_attr, class_has_attr, PyClassRef}; use crate::pyobject::{ - IntoPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, + IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, + TypeProtocol, }; use crate::vm::VirtualMachine; +#[pyclass(name = "none")] #[derive(Debug)] pub struct PyNone; pub type PyNoneRef = PyRef; @@ -33,15 +35,24 @@ impl IntoPyObject for Option { } } +#[pyimpl] impl PyNoneRef { + #[pymethod(name = "__new__")] + fn none_new(_: PyClassRef, vm: &VirtualMachine) -> PyNoneRef { + vm.ctx.none.clone() + } + + #[pymethod(name = "__repr__")] fn repr(self, _vm: &VirtualMachine) -> PyResult { Ok("None".to_string()) } + #[pymethod(name = "__bool__")] fn bool(self, _vm: &VirtualMachine) -> PyResult { Ok(false) } + #[pymethod(name = "__getattribute__")] fn get_attribute(self, name: PyStringRef, vm: &VirtualMachine) -> PyResult { vm_trace!("None.__getattribute__({:?}, {:?})", self, name); let cls = self.class(); @@ -102,15 +113,6 @@ impl PyNoneRef { } } -fn none_new(_: PyClassRef, vm: &VirtualMachine) -> PyNoneRef { - vm.ctx.none.clone() -} - pub fn init(context: &PyContext) { - extend_class!(context, &context.none.class(), { - "__new__" => context.new_rustfunc(none_new), - "__repr__" => context.new_rustfunc(PyNoneRef::repr), - "__bool__" => context.new_rustfunc(PyNoneRef::bool), - "__getattribute__" => context.new_rustfunc(PyNoneRef::get_attribute) - }); + PyNoneRef::extend_class(context, &context.none.class()); }