mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Expose __name__ in __dict__
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
use crate::obj::objdict::PyDictRef;
|
||||
use crate::obj::objproperty::PropertyBuilder;
|
||||
use crate::obj::objstr::PyStringRef;
|
||||
use crate::obj::objtype::PyClassRef;
|
||||
use crate::pyobject::{PyContext, PyRef, PyResult, PyValue};
|
||||
use crate::pyobject::{ItemProtocol, PyContext, PyRef, PyResult, PyValue};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -25,6 +26,7 @@ impl PyModuleRef {
|
||||
}
|
||||
.into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
fn dir(self: PyModuleRef, vm: &VirtualMachine) -> PyResult {
|
||||
if let Some(dict) = &self.into_object().dict {
|
||||
let keys = dict.into_iter().map(|(k, _v)| k.clone()).collect();
|
||||
@@ -34,6 +36,17 @@ impl PyModuleRef {
|
||||
}
|
||||
}
|
||||
|
||||
fn dict(self, vm: &VirtualMachine) -> PyResult<PyDictRef> {
|
||||
let name_obj = vm.new_str(self.name.clone());
|
||||
if let Some(ref dict) = &self.into_object().dict {
|
||||
let mod_dict = dict.clone();
|
||||
mod_dict.set_item("__name__", name_obj, vm)?;
|
||||
Ok(mod_dict)
|
||||
} else {
|
||||
panic!("Modules should definitely have a dict.");
|
||||
}
|
||||
}
|
||||
|
||||
fn name(self, _vm: &VirtualMachine) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
@@ -45,6 +58,10 @@ pub fn init(context: &PyContext) {
|
||||
"__name__" => PropertyBuilder::new(context)
|
||||
.add_getter(PyModuleRef::name)
|
||||
.create(),
|
||||
"__dict__" =>
|
||||
PropertyBuilder::new(context)
|
||||
.add_getter(PyModuleRef::dict)
|
||||
.create(),
|
||||
"__new__" => context.new_rustfunc(PyModuleRef::new),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user