mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-17 01:51:39 +09:00
Add objmodule.
This commit is contained in:
@@ -18,6 +18,7 @@ pub mod objiter;
|
||||
pub mod objlist;
|
||||
pub mod objmap;
|
||||
pub mod objmemory;
|
||||
pub mod objmodule;
|
||||
pub mod objnone;
|
||||
pub mod objobject;
|
||||
pub mod objproperty;
|
||||
|
||||
30
vm/src/obj/objmodule.rs
Normal file
30
vm/src/obj/objmodule.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use crate::frame::ScopeRef;
|
||||
use crate::pyobject::{
|
||||
DictProtocol, PyContext, PyFuncArgs, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
|
||||
};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
fn module_dir(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
arg_check!(vm, args, required = [(obj, Some(vm.ctx.module_type()))]);
|
||||
let scope = get_scope(obj);
|
||||
let keys = scope
|
||||
.locals
|
||||
.get_key_value_pairs()
|
||||
.iter()
|
||||
.map(|(k, _v)| k.clone())
|
||||
.collect();
|
||||
Ok(vm.ctx.new_list(keys))
|
||||
}
|
||||
|
||||
pub fn init(context: &PyContext) {
|
||||
let module_type = &context.module_type;
|
||||
context.set_attr(&module_type, "__dir__", context.new_rustfunc(module_dir));
|
||||
}
|
||||
|
||||
fn get_scope(obj: &PyObjectRef) -> &ScopeRef {
|
||||
if let PyObjectPayload::Module { ref scope, .. } = &obj.payload {
|
||||
&scope
|
||||
} else {
|
||||
panic!("Can't get scope from non-module.")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user