diff --git a/vm/src/builtins/namespace.rs b/vm/src/builtins/namespace.rs index ec0ffcea9..3d4838803 100644 --- a/vm/src/builtins/namespace.rs +++ b/vm/src/builtins/namespace.rs @@ -4,8 +4,8 @@ use crate::{ class::PyClassImpl, function::{FuncArgs, PyComparisonValue}, recursion::ReprGuard, - types::{Comparable, Constructor, PyComparisonOp}, - AsObject, Context, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, + types::{Comparable, Constructor, Initializer, PyComparisonOp}, + AsObject, Context, PyObject, PyPayload, PyRef, PyResult, VirtualMachine, }; /// A simple attribute-based namespace. @@ -39,21 +39,8 @@ impl PyNamespace { } } -#[pyimpl(flags(BASETYPE, HAS_DICT), with(Constructor, Comparable))] +#[pyimpl(flags(BASETYPE, HAS_DICT), with(Constructor, Initializer, Comparable))] impl PyNamespace { - #[pyslot] - #[pymethod(magic)] - fn init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> { - let zelf: PyRef = zelf.try_into_value(vm)?; - if !args.args.is_empty() { - return Err(vm.new_type_error("no positional arguments expected".to_owned())); - } - for (name, value) in args.kwargs.into_iter() { - zelf.as_object().set_attr(name, value, vm)?; - } - Ok(()) - } - #[pymethod(magic)] fn repr(zelf: PyRef, vm: &VirtualMachine) -> PyResult { let o = zelf.as_object(); @@ -80,6 +67,20 @@ impl PyNamespace { } } +impl Initializer for PyNamespace { + type Args = FuncArgs; + + fn init(zelf: PyRef, args: Self::Args, vm: &VirtualMachine) -> PyResult<()> { + if !args.args.is_empty() { + return Err(vm.new_type_error("no positional arguments expected".to_owned())); + } + for (name, value) in args.kwargs.into_iter() { + zelf.as_object().set_attr(name, value, vm)?; + } + Ok(()) + } +} + impl Comparable for PyNamespace { fn cmp( zelf: &crate::Py,