mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
33 lines
839 B
Rust
33 lines
839 B
Rust
use crate::function::KwArgs;
|
|
use crate::obj::objtype::PyClassRef;
|
|
use crate::pyobject::{PyClassImpl, PyContext, PyRef, PyResult, PyValue};
|
|
use crate::vm::VirtualMachine;
|
|
|
|
/// A simple attribute-based namespace.
|
|
///
|
|
/// SimpleNamespace(**kwargs)
|
|
#[pyclass(name = "SimpleNamespace")]
|
|
#[derive(Debug)]
|
|
pub struct PyNamespace;
|
|
|
|
impl PyValue for PyNamespace {
|
|
fn class(vm: &VirtualMachine) -> PyClassRef {
|
|
vm.ctx.namespace_type()
|
|
}
|
|
}
|
|
|
|
#[pyimpl]
|
|
impl PyNamespace {
|
|
#[pymethod(name = "__init__")]
|
|
fn init(zelf: PyRef<Self>, kwargs: KwArgs, vm: &VirtualMachine) -> PyResult<()> {
|
|
for (name, value) in kwargs.into_iter() {
|
|
vm.set_attr(zelf.as_object(), name, value)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
pub fn init(context: &PyContext) {
|
|
PyNamespace::extend_class(context, &context.namespace_type);
|
|
}
|