@@ -98,7 +98,7 @@ impl PyClassRef {
9898
9999 #[ pymethod( magic) ]
100100 fn dir ( self , vm : & VirtualMachine ) -> PyList {
101- let attributes = get_attributes ( self ) ;
101+ let attributes = self . get_attributes ( ) ;
102102 let attributes: Vec < PyObjectRef > = attributes
103103 . keys ( )
104104 . map ( |k| vm. ctx . new_str ( k. to_string ( ) ) )
@@ -156,10 +156,10 @@ impl PyClassRef {
156156 vm_trace ! ( "type.__getattribute__({:?}, {:?})" , self , name) ;
157157 let mcl = self . class ( ) ;
158158
159- if let Some ( attr) = class_get_attr ( & mcl, & name) {
159+ if let Some ( attr) = mcl. get_attr ( & name) {
160160 let attr_class = attr. class ( ) ;
161- if class_has_attr ( & attr_class, "__set__" ) {
162- if let Some ( ref descriptor) = class_get_attr ( & attr_class, "__get__" ) {
161+ if attr_class. has_attr ( "__set__" ) {
162+ if let Some ( ref descriptor) = attr_class. get_attr ( "__get__" ) {
163163 return vm. invoke (
164164 descriptor,
165165 vec ! [ attr, self . into_object( ) , mcl. into_object( ) ] ,
@@ -168,18 +168,18 @@ impl PyClassRef {
168168 }
169169 }
170170
171- if let Some ( attr) = class_get_attr ( & self , & name) {
171+ if let Some ( attr) = self . get_attr ( & name) {
172172 let attr_class = attr. class ( ) ;
173- if let Some ( ref descriptor) = class_get_attr ( & attr_class, "__get__" ) {
173+ if let Some ( ref descriptor) = attr_class. get_attr ( "__get__" ) {
174174 return vm. invoke ( descriptor, vec ! [ attr, vm. get_none( ) , self . into_object( ) ] ) ;
175175 }
176176 }
177177
178- if let Some ( cls_attr) = class_get_attr ( & self , & name) {
178+ if let Some ( cls_attr) = self . get_attr ( & name) {
179179 Ok ( cls_attr)
180- } else if let Some ( attr) = class_get_attr ( & mcl, & name) {
180+ } else if let Some ( attr) = mcl. get_attr ( & name) {
181181 vm. call_get_descriptor ( attr, self . into_object ( ) )
182- } else if let Some ( ref getter) = class_get_attr ( & self , "__getattr__" ) {
182+ } else if let Some ( ref getter) = self . get_attr ( "__getattr__" ) {
183183 vm. invoke ( getter, vec ! [ mcl. into_object( ) , name_ref. into_object( ) ] )
184184 } else {
185185 Err ( vm. new_attribute_error ( format ! ( "{} has no attribute '{}'" , self , name) ) )
@@ -193,8 +193,8 @@ impl PyClassRef {
193193 value : PyObjectRef ,
194194 vm : & VirtualMachine ,
195195 ) -> PyResult < ( ) > {
196- if let Some ( attr) = class_get_attr ( & self . class ( ) , attr_name. as_str ( ) ) {
197- if let Some ( ref descriptor) = class_get_attr ( & attr. class ( ) , "__set__" ) {
196+ if let Some ( attr) = self . class ( ) . get_attr ( attr_name. as_str ( ) ) {
197+ if let Some ( ref descriptor) = attr. class ( ) . get_attr ( "__set__" ) {
198198 vm. invoke ( descriptor, vec ! [ attr, self . into_object( ) , value] ) ?;
199199 return Ok ( ( ) ) ;
200200 }
@@ -208,15 +208,15 @@ impl PyClassRef {
208208
209209 #[ pymethod( magic) ]
210210 fn delattr ( self , attr_name : PyStringRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
211- if let Some ( attr) = class_get_attr ( & self . class ( ) , attr_name. as_str ( ) ) {
212- if let Some ( ref descriptor) = class_get_attr ( & attr. class ( ) , "__delete__" ) {
211+ if let Some ( attr) = self . class ( ) . get_attr ( attr_name. as_str ( ) ) {
212+ if let Some ( ref descriptor) = attr. class ( ) . get_attr ( "__delete__" ) {
213213 return vm
214214 . invoke ( descriptor, vec ! [ attr, self . into_object( ) ] )
215215 . map ( |_| ( ) ) ;
216216 }
217217 }
218218
219- if class_get_attr ( & self , attr_name. as_str ( ) ) . is_some ( ) {
219+ if self . get_attr ( attr_name. as_str ( ) ) . is_some ( ) {
220220 self . attributes . borrow_mut ( ) . remove ( attr_name. as_str ( ) ) ;
221221 Ok ( ( ) )
222222 } else {
@@ -397,48 +397,48 @@ fn type_dict_setter(
397397 ) )
398398}
399399
400- /// This is the internal get_attr implementation for fast lookup on a class.
401- pub fn class_get_attr ( class : & PyClassRef , attr_name : & str ) -> Option < PyObjectRef > {
402- flame_guard ! ( format!( "class_get_attr({:?})" , attr_name) ) ;
403-
404- class
405- . attributes
406- . borrow ( )
407- . get ( attr_name)
408- . cloned ( )
409- . or_else ( || class_get_super_attr ( class, attr_name) )
410- }
400+ impl PyClassRef {
401+ /// This is the internal get_attr implementation for fast lookup on a class.
402+ pub fn get_attr ( & self , attr_name : & str ) -> Option < PyObjectRef > {
403+ flame_guard ! ( format!( "class_get_attr({:?})" , attr_name) ) ;
411404
412- pub fn class_get_super_attr ( class : & PyClassRef , attr_name : & str ) -> Option < PyObjectRef > {
413- class
414- . mro
415- . iter ( )
416- . find_map ( |class| class . attributes . borrow ( ) . get ( attr_name) . cloned ( ) )
417- }
405+ self . attributes
406+ . borrow ( )
407+ . get ( attr_name )
408+ . cloned ( )
409+ . or_else ( || self . get_super_attr ( attr_name) )
410+ }
418411
419- // This is the internal has_attr implementation for fast lookup on a class.
420- pub fn class_has_attr ( class : & PyClassRef , attr_name : & str ) -> bool {
421- class. attributes . borrow ( ) . contains_key ( attr_name)
422- || class
423- . mro
412+ pub fn get_super_attr ( & self , attr_name : & str ) -> Option < PyObjectRef > {
413+ self . mro
424414 . iter ( )
425- . any ( |c| c. attributes . borrow ( ) . contains_key ( attr_name) )
426- }
415+ . find_map ( |class| class. attributes . borrow ( ) . get ( attr_name) . cloned ( ) )
416+ }
417+
418+ // This is the internal has_attr implementation for fast lookup on a class.
419+ pub fn has_attr ( & self , attr_name : & str ) -> bool {
420+ self . attributes . borrow ( ) . contains_key ( attr_name)
421+ || self
422+ . mro
423+ . iter ( )
424+ . any ( |c| c. attributes . borrow ( ) . contains_key ( attr_name) )
425+ }
427426
428- pub fn get_attributes ( cls : PyClassRef ) -> PyAttributes {
429- // Gather all members here:
430- let mut attributes = PyAttributes :: new ( ) ;
427+ pub fn get_attributes ( self ) -> PyAttributes {
428+ // Gather all members here:
429+ let mut attributes = PyAttributes :: new ( ) ;
431430
432- let mut base_classes: Vec < & PyClassRef > = cls . iter_mro ( ) . collect ( ) ;
433- base_classes. reverse ( ) ;
431+ let mut base_classes: Vec < & PyClassRef > = self . iter_mro ( ) . collect ( ) ;
432+ base_classes. reverse ( ) ;
434433
435- for bc in base_classes {
436- for ( name, value) in bc. attributes . borrow ( ) . iter ( ) {
437- attributes. insert ( name. to_string ( ) , value. clone ( ) ) ;
434+ for bc in base_classes {
435+ for ( name, value) in bc. attributes . borrow ( ) . iter ( ) {
436+ attributes. insert ( name. to_string ( ) , value. clone ( ) ) ;
437+ }
438438 }
439- }
440439
441- attributes
440+ attributes
441+ }
442442}
443443
444444fn take_next_base ( mut bases : Vec < Vec < PyClassRef > > ) -> Option < ( PyClassRef , Vec < Vec < PyClassRef > > ) > {
0 commit comments