forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjmodule.rs
More file actions
39 lines (34 loc) · 1.05 KB
/
objmodule.rs
File metadata and controls
39 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::obj::objstr::PyStringRef;
use crate::pyobject::{DictProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
use crate::vm::VirtualMachine;
#[derive(Clone, Debug)]
pub struct PyModule {
pub name: String,
pub dict: PyObjectRef,
}
pub type PyModuleRef = PyRef<PyModule>;
impl PyValue for PyModule {
fn class(vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.module_type()
}
}
impl PyModuleRef {
fn dir(self: PyModuleRef, vm: &VirtualMachine) -> PyResult {
let keys = self
.dict
.get_key_value_pairs()
.iter()
.map(|(k, _v)| k.clone())
.collect();
Ok(vm.ctx.new_list(keys))
}
fn set_attr(self, attr: PyStringRef, value: PyObjectRef, vm: &VirtualMachine) {
self.dict.set_item(&vm.ctx, &attr.value, value)
}
}
pub fn init(context: &PyContext) {
extend_class!(&context, &context.module_type, {
"__dir__" => context.new_rustfunc(PyModuleRef::dir),
"__setattr__" => context.new_rustfunc(PyModuleRef::set_attr)
});
}