Files
RustPython/vm/src/obj/objmodule.rs
Adam Kelly bbb7162472 Various dictionary changes.
* vm.ctx.new_dict returns a PyDictRef
* Special case for module goes away.
* Instances get a real dictionary.
2019-03-25 16:37:20 +00:00

37 lines
957 B
Rust

use crate::obj::objtype::PyClassRef;
use crate::pyobject::{DictProtocol, PyContext, PyRef, PyResult, PyValue};
use crate::vm::VirtualMachine;
#[derive(Debug)]
pub struct PyModule {
pub name: String,
}
pub type PyModuleRef = PyRef<PyModule>;
impl PyValue for PyModule {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.ctx.module_type()
}
}
impl PyModuleRef {
fn dir(self: PyModuleRef, vm: &VirtualMachine) -> PyResult {
if let Some(dict) = &self.into_object().dict {
let keys = dict
.get_key_value_pairs()
.iter()
.map(|(k, _v)| k.clone())
.collect();
Ok(vm.ctx.new_list(keys))
} else {
panic!("Modules should definitely have a dict.");
}
}
}
pub fn init(context: &PyContext) {
extend_class!(&context, &context.module_type, {
"__dir__" => context.new_rustfunc(PyModuleRef::dir),
});
}