mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
module init
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use super::pystr::IntoPyStrRef;
|
||||
use super::{PyDictRef, PyStr, PyStrRef, PyTypeRef};
|
||||
use crate::{
|
||||
builtins::PyStrInterned,
|
||||
class::PyClassImpl,
|
||||
convert::ToPyObject,
|
||||
function::FuncArgs,
|
||||
@@ -48,7 +49,7 @@ impl PyModule {
|
||||
{
|
||||
return Ok(attr);
|
||||
}
|
||||
if let Ok(getattr) = zelf.dict().get_item("__getattr__", vm) {
|
||||
if let Ok(getattr) = zelf.dict().get_item(identifier!(vm, __getattr__), vm) {
|
||||
return vm.invoke(&getattr, (name,));
|
||||
}
|
||||
let module_name = if let Some(name) = Self::name(zelf.to_owned(), vm) {
|
||||
@@ -61,7 +62,7 @@ impl PyModule {
|
||||
|
||||
fn name(zelf: PyRef<Self>, vm: &VirtualMachine) -> Option<PyStrRef> {
|
||||
zelf.as_object()
|
||||
.generic_getattr_opt(PyStr::from("__name__").into_ref(vm), None, vm)
|
||||
.generic_getattr_opt(identifier!(vm, __name__).to_owned(), None, vm)
|
||||
.unwrap_or(None)
|
||||
.and_then(|obj| obj.downcast::<PyStr>().ok())
|
||||
}
|
||||
@@ -92,14 +93,14 @@ impl Py<PyModule> {
|
||||
// TODO: should be on PyModule, not Py<PyModule>
|
||||
pub(crate) fn init_module_dict(
|
||||
&self,
|
||||
name: PyObjectRef,
|
||||
name: &'static PyStrInterned,
|
||||
doc: PyObjectRef,
|
||||
vm: &VirtualMachine,
|
||||
) {
|
||||
let dict = self.dict();
|
||||
dict.set_item("__name__", name, vm)
|
||||
dict.set_item(identifier!(vm, __name__), name.to_object(), vm)
|
||||
.expect("Failed to set __name__ on module");
|
||||
dict.set_item("__doc__", doc, vm)
|
||||
dict.set_item(identifier!(vm, __doc__), doc, vm)
|
||||
.expect("Failed to set __doc__ on module");
|
||||
dict.set_item("__package__", vm.ctx.none(), vm)
|
||||
.expect("Failed to set __package__ on module");
|
||||
@@ -131,7 +132,11 @@ impl Initializer for PyModule {
|
||||
.slots
|
||||
.flags
|
||||
.has_feature(crate::types::PyTypeFlags::HAS_DICT));
|
||||
zelf.init_module_dict(args.name.into(), args.doc.to_pyobject(vm), vm);
|
||||
zelf.init_module_dict(
|
||||
vm.ctx.intern_str(args.name.as_str()),
|
||||
args.doc.to_pyobject(vm),
|
||||
vm,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,15 +309,11 @@ impl PyBaseObject {
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn reduce_ex(obj: PyObjectRef, proto: usize, vm: &VirtualMachine) -> PyResult {
|
||||
if let Some(reduce) = vm.get_attribute_opt(obj.clone(), identifier!(vm, __reduce__))? {
|
||||
let object_reduce = vm
|
||||
.ctx
|
||||
.types
|
||||
.object_type
|
||||
.get_attr(identifier!(vm, __reduce__))
|
||||
.unwrap();
|
||||
let __reduce__ = identifier!(vm, __reduce__);
|
||||
if let Some(reduce) = vm.get_attribute_opt(obj.clone(), __reduce__)? {
|
||||
let object_reduce = vm.ctx.types.object_type.get_attr(__reduce__).unwrap();
|
||||
let typ_obj: PyObjectRef = obj.class().clone().into();
|
||||
let class_reduce = typ_obj.get_attr(identifier!(vm, __reduce__), vm)?;
|
||||
let class_reduce = typ_obj.get_attr(__reduce__, vm)?;
|
||||
if !class_reduce.is(&object_reduce) {
|
||||
return vm.invoke(&reduce, ());
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ pub(crate) fn init_importlib_base(vm: &mut VirtualMachine) -> PyResult<PyObjectR
|
||||
vm.invoke(&install, (vm.sys_module.clone(), impmod))?;
|
||||
Ok(importlib)
|
||||
})?;
|
||||
vm.import_func = importlib.get_attr("__import__", vm)?;
|
||||
vm.import_func = importlib.get_attr(identifier!(vm, __import__).to_owned(), vm)?;
|
||||
Ok(importlib)
|
||||
}
|
||||
|
||||
@@ -134,9 +134,17 @@ pub fn import_codeobj(
|
||||
set_file_attr: bool,
|
||||
) -> PyResult {
|
||||
let attrs = vm.ctx.new_dict();
|
||||
attrs.set_item("__name__", vm.ctx.new_str(module_name).into(), vm)?;
|
||||
attrs.set_item(
|
||||
identifier!(vm, __name__),
|
||||
vm.ctx.new_str(module_name).into(),
|
||||
vm,
|
||||
)?;
|
||||
if set_file_attr {
|
||||
attrs.set_item("__file__", code_obj.source_path.clone().into(), vm)?;
|
||||
attrs.set_item(
|
||||
identifier!(vm, __file__),
|
||||
code_obj.source_path.clone().into(),
|
||||
vm,
|
||||
)?;
|
||||
}
|
||||
let module = vm.new_module(module_name, attrs.clone(), None);
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ use crate::{
|
||||
},
|
||||
bytecode,
|
||||
codecs::CodecsRegistry,
|
||||
common::{ascii, hash::HashSecret, lock::PyMutex, rc::PyRc},
|
||||
common::{hash::HashSecret, lock::PyMutex, rc::PyRc},
|
||||
convert::{ToPyObject, TryFromObject},
|
||||
frame::{ExecutionResult, Frame, FrameRef},
|
||||
frozen,
|
||||
@@ -160,13 +160,10 @@ impl VirtualMachine {
|
||||
let frozen = frozen::get_module_inits().collect();
|
||||
PyRc::get_mut(&mut vm.state).unwrap().frozen = frozen;
|
||||
|
||||
vm.builtins.init_module_dict(
|
||||
vm.ctx.new_str(ascii!("builtins")).into(),
|
||||
vm.ctx.none(),
|
||||
&vm,
|
||||
);
|
||||
vm.builtins
|
||||
.init_module_dict(vm.ctx.intern_str("builtins"), vm.ctx.none(), &vm);
|
||||
vm.sys_module
|
||||
.init_module_dict(vm.ctx.new_str(ascii!("sys")).into(), vm.ctx.none(), &vm);
|
||||
.init_module_dict(vm.ctx.intern_str("sys"), vm.ctx.none(), &vm);
|
||||
|
||||
vm
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ impl VirtualMachine {
|
||||
pub fn new_module(&self, name: &str, dict: PyDictRef, doc: Option<&str>) -> PyObjectRef {
|
||||
let module = PyRef::new_ref(PyModule {}, self.ctx.types.module_type.clone(), Some(dict));
|
||||
module.init_module_dict(
|
||||
self.new_pyobj(name.to_owned()),
|
||||
self.ctx.intern_str(name),
|
||||
doc.map(|doc| self.new_pyobj(doc.to_owned()))
|
||||
.unwrap_or_else(|| self.ctx.none()),
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user