Initializer for PyNamespace

This commit is contained in:
Jeong Yunwon
2022-04-27 01:46:54 +09:00
parent 11cc0165a4
commit 0ee3e7ee2b

View File

@@ -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<Self> = 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<Self>, vm: &VirtualMachine) -> PyResult<String> {
let o = zelf.as_object();
@@ -80,6 +67,20 @@ impl PyNamespace {
}
}
impl Initializer for PyNamespace {
type Args = FuncArgs;
fn init(zelf: PyRef<Self>, 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<Self>,