@@ -26,7 +26,6 @@ pub struct ModuleInitArgs {
2626 doc : Option < PyStrRef > ,
2727}
2828
29- #[ pyclass( with( GetAttr , Initializer , Representable ) , flags( BASETYPE , HAS_DICT ) ) ]
3029impl PyModule {
3130 // pub(crate) fn new(d: PyDictRef) -> Self {
3231 // PyModule { dict: d.into() }
@@ -37,49 +36,31 @@ impl PyModule {
3736 // self.dict.get()
3837 // }
3938
40- #[ pyslot]
41- fn slot_new ( cls : PyTypeRef , _args : FuncArgs , vm : & VirtualMachine ) -> PyResult {
42- PyModule { } . into_ref_with_type ( vm, cls) . map ( Into :: into)
43- }
44-
4539 fn getattr_inner ( zelf : & Py < Self > , name : PyStrRef , vm : & VirtualMachine ) -> PyResult {
46- if let Some ( attr) = zelf
47- . as_object ( )
48- . generic_getattr_opt ( name. clone ( ) , None , vm) ?
49- {
40+ if let Some ( attr) = zelf. as_object ( ) . generic_getattr_opt ( & name, None , vm) ? {
5041 return Ok ( attr) ;
5142 }
5243 if let Ok ( getattr) = zelf. dict ( ) . get_item ( identifier ! ( vm, __getattr__) , vm) {
5344 return getattr. call ( ( name, ) , vm) ;
5445 }
55- let module_name = if let Some ( name) = Self :: name ( zelf. to_owned ( ) , vm) {
46+ let module_name = if let Some ( name) = zelf. name ( vm) {
5647 format ! ( " '{name}'" )
5748 } else {
5849 "" . to_owned ( )
5950 } ;
6051 Err ( vm. new_attribute_error ( format ! ( "module{module_name} has no attribute '{name}'" ) ) )
6152 }
53+ }
6254
63- fn name ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> Option < PyStrRef > {
64- let name = zelf
55+ impl Py < PyModule > {
56+ fn name ( & self , vm : & VirtualMachine ) -> Option < PyStrRef > {
57+ let name = self
6558 . as_object ( )
66- . generic_getattr_opt ( identifier ! ( vm, __name__) . to_owned ( ) , None , vm)
59+ . generic_getattr_opt ( identifier ! ( vm, __name__) , None , vm)
6760 . unwrap_or_default ( ) ?;
6861 name. downcast :: < PyStr > ( ) . ok ( )
6962 }
7063
71- #[ pymethod( magic) ]
72- fn dir ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyResult < Vec < PyObjectRef > > {
73- let dict = zelf
74- . as_object ( )
75- . dict ( )
76- . ok_or_else ( || vm. new_value_error ( "module has no dict" . to_owned ( ) ) ) ?;
77- let attrs = dict. into_iter ( ) . map ( |( k, _v) | k) . collect ( ) ;
78- Ok ( attrs)
79- }
80- }
81-
82- impl Py < PyModule > {
8364 // TODO: to be replaced by the commented-out dict method above once dictoffsets land
8465 pub fn dict ( & self ) -> PyDictRef {
8566 self . as_object ( ) . dict ( ) . unwrap ( )
@@ -117,6 +98,24 @@ impl Py<PyModule> {
11798 }
11899}
119100
101+ #[ pyclass( with( GetAttr , Initializer , Representable ) , flags( BASETYPE , HAS_DICT ) ) ]
102+ impl PyModule {
103+ #[ pyslot]
104+ fn slot_new ( cls : PyTypeRef , _args : FuncArgs , vm : & VirtualMachine ) -> PyResult {
105+ PyModule { } . into_ref_with_type ( vm, cls) . map ( Into :: into)
106+ }
107+
108+ #[ pymethod( magic) ]
109+ fn dir ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < Vec < PyObjectRef > > {
110+ let dict = zelf
111+ . as_object ( )
112+ . dict ( )
113+ . ok_or_else ( || vm. new_value_error ( "module has no dict" . to_owned ( ) ) ) ?;
114+ let attrs = dict. into_iter ( ) . map ( |( k, _v) | k) . collect ( ) ;
115+ Ok ( attrs)
116+ }
117+ }
118+
120119impl Initializer for PyModule {
121120 type Args = ModuleInitArgs ;
122121
0 commit comments