1- use super :: pystr:: IntoPyStrRef ;
21use super :: { PyDictRef , PyStr , PyStrRef , PyType , PyTypeRef } ;
32use crate :: {
4- builtins:: PyStrInterned ,
3+ builtins:: { pystr :: AsPyStr , PyStrInterned } ,
54 class:: PyClassImpl ,
65 convert:: ToPyObject ,
76 function:: FuncArgs ,
@@ -26,60 +25,36 @@ pub struct ModuleInitArgs {
2625 doc : Option < PyStrRef > ,
2726}
2827
29- #[ pyclass( with( GetAttr , Initializer , Representable ) , flags( BASETYPE , HAS_DICT ) ) ]
3028impl PyModule {
3129 // pub(crate) fn new(d: PyDictRef) -> Self {
3230 // PyModule { dict: d.into() }
3331 // }
32+ }
3433
35- // #[inline]
36- // pub fn dict(&self) -> PyDictRef {
37- // self.dict.get()
38- // }
39-
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-
45- 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- {
34+ impl Py < PyModule > {
35+ fn getattr_inner ( & self , name : & Py < PyStr > , vm : & VirtualMachine ) -> PyResult {
36+ if let Some ( attr) = self . as_object ( ) . generic_getattr_opt ( name, None , vm) ? {
5037 return Ok ( attr) ;
5138 }
52- if let Ok ( getattr) = zelf . dict ( ) . get_item ( identifier ! ( vm, __getattr__) , vm) {
53- return getattr. call ( ( name, ) , vm) ;
39+ if let Ok ( getattr) = self . dict ( ) . get_item ( identifier ! ( vm, __getattr__) , vm) {
40+ return getattr. call ( ( name. to_owned ( ) , ) , vm) ;
5441 }
55- let module_name = if let Some ( name) = Self :: name ( zelf . to_owned ( ) , vm) {
42+ let module_name = if let Some ( name) = self . name ( vm) {
5643 format ! ( " '{name}'" )
5744 } else {
5845 "" . to_owned ( )
5946 } ;
6047 Err ( vm. new_attribute_error ( format ! ( "module{module_name} has no attribute '{name}'" ) ) )
6148 }
6249
63- fn name ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> Option < PyStrRef > {
64- let name = zelf
50+ fn name ( & self , vm : & VirtualMachine ) -> Option < PyStrRef > {
51+ let name = self
6552 . as_object ( )
66- . generic_getattr_opt ( identifier ! ( vm, __name__) . to_owned ( ) , None , vm)
53+ . generic_getattr_opt ( identifier ! ( vm, __name__) , None , vm)
6754 . unwrap_or_default ( ) ?;
6855 name. downcast :: < PyStr > ( ) . ok ( )
6956 }
7057
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 > {
8358 // TODO: to be replaced by the commented-out dict method above once dictoffsets land
8459 pub fn dict ( & self ) -> PyDictRef {
8560 self . as_object ( ) . dict ( ) . unwrap ( )
@@ -104,19 +79,38 @@ impl Py<PyModule> {
10479 . expect ( "Failed to set __spec__ on module" ) ;
10580 }
10681
107- pub fn get_attr ( & self , attr_name : impl IntoPyStrRef , vm : & VirtualMachine ) -> PyResult {
108- PyModule :: getattr_inner ( self , attr_name. into_pystr_ref ( vm ) , vm)
82+ pub fn get_attr < ' a > ( & self , attr_name : impl AsPyStr < ' a > , vm : & VirtualMachine ) -> PyResult {
83+ self . getattr_inner ( attr_name. as_pystr ( & vm . ctx ) , vm)
10984 }
110- pub fn set_attr (
85+
86+ pub fn set_attr < ' a > (
11187 & self ,
112- attr_name : impl IntoPyStrRef ,
88+ attr_name : impl AsPyStr < ' a > ,
11389 attr_value : impl Into < PyObjectRef > ,
11490 vm : & VirtualMachine ,
11591 ) -> PyResult < ( ) > {
11692 self . as_object ( ) . set_attr ( attr_name, attr_value, vm)
11793 }
11894}
11995
96+ #[ pyclass( with( GetAttr , Initializer , Representable ) , flags( BASETYPE , HAS_DICT ) ) ]
97+ impl PyModule {
98+ #[ pyslot]
99+ fn slot_new ( cls : PyTypeRef , _args : FuncArgs , vm : & VirtualMachine ) -> PyResult {
100+ PyModule { } . into_ref_with_type ( vm, cls) . map ( Into :: into)
101+ }
102+
103+ #[ pymethod( magic) ]
104+ fn dir ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < Vec < PyObjectRef > > {
105+ let dict = zelf
106+ . as_object ( )
107+ . dict ( )
108+ . ok_or_else ( || vm. new_value_error ( "module has no dict" . to_owned ( ) ) ) ?;
109+ let attrs = dict. into_iter ( ) . map ( |( k, _v) | k) . collect ( ) ;
110+ Ok ( attrs)
111+ }
112+ }
113+
120114impl Initializer for PyModule {
121115 type Args = ModuleInitArgs ;
122116
@@ -136,8 +130,8 @@ impl Initializer for PyModule {
136130}
137131
138132impl GetAttr for PyModule {
139- fn getattro ( zelf : & Py < Self > , name : PyStrRef , vm : & VirtualMachine ) -> PyResult {
140- Self :: getattr_inner ( zelf , name, vm)
133+ fn getattro ( zelf : & Py < Self > , name : & Py < PyStr > , vm : & VirtualMachine ) -> PyResult {
134+ zelf . getattr_inner ( name, vm)
141135 }
142136}
143137
0 commit comments