mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
Initializer for PyNamespace
This commit is contained in:
@@ -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>,
|
||||
|
||||
Reference in New Issue
Block a user