@@ -598,55 +598,60 @@ def _transform_attrs(
598
598
return _Attributes ((AttrsClass (attrs ), base_attrs , base_attr_map ))
599
599
600
600
601
- def _make_cached_property_getattr (cached_properties , original_getattr , cls ):
601
+ def _make_cached_property_getattribute (
602
+ cached_properties , original_getattribute , cls
603
+ ):
602
604
lines = [
603
605
# Wrapped to get `__class__` into closure cell for super()
604
606
# (It will be replaced with the newly constructed class after construction).
605
607
"def wrapper(_cls):" ,
606
608
" __class__ = _cls" ,
607
- " def __getattr__(self, item, cached_properties=cached_properties, original_getattr=original_getattr, _cached_setattr_get=_cached_setattr_get):" ,
608
- " func = cached_properties.get(item)" ,
609
- " if func is not None:" ,
610
- " result = func(self)" ,
611
- " _setter = _cached_setattr_get(self)" ,
612
- " _setter(item, result)" ,
613
- " return result" ,
609
+ " def __getattribute__(self, item, cached_properties=cached_properties, original_getattr=original_getattr, _cached_setattr_get=_cached_setattr_get):" ,
610
+ " try:" ,
611
+ " return object.__getattribute__(self, item)" ,
612
+ " except AttributeError:" ,
613
+ " func = cached_properties.get(item)" ,
614
+ " if func is not None:" ,
615
+ " result = func(self)" ,
616
+ " _setter = _cached_setattr_get(self)" ,
617
+ " _setter(item, result)" ,
618
+ " return result" ,
614
619
]
615
- if original_getattr is not None :
620
+ if original_getattribute is not None :
616
621
lines .append (
617
- " return original_getattr (self, item)" ,
622
+ " return original_getattribute (self, item)" ,
618
623
)
619
624
else :
620
625
lines .extend (
621
626
[
622
- " try:" ,
623
- " return super().__getattribute__(item)" ,
624
- " except AttributeError:" ,
625
- " if not hasattr(super(), '__getattr__ '):" ,
626
- " raise" ,
627
- " return super().__getattr__ (item)" ,
628
- " original_error = f\" '{self.__class__.__name__}' object has no attribute '{item}'\" " ,
629
- " raise AttributeError(original_error)" ,
627
+ " try:" ,
628
+ " return super().__getattribute__(item)" ,
629
+ " except AttributeError:" ,
630
+ " if not hasattr(super(), '__getattribute__ '):" ,
631
+ " raise" ,
632
+ " return super().__getattribute__ (item)" ,
633
+ " original_error = f\" '{self.__class__.__name__}' object has no attribute '{item}'\" " ,
634
+ " raise AttributeError(original_error)" ,
630
635
]
631
636
)
632
637
633
638
lines .extend (
634
639
[
635
- " return __getattr__ " ,
636
- "__getattr__ = wrapper(_cls)" ,
640
+ " return __getattribute__ " ,
641
+ "__getattribute__ = wrapper(_cls)" ,
637
642
]
638
643
)
639
644
640
- unique_filename = _generate_unique_filename (cls , "getattr " )
645
+ unique_filename = _generate_unique_filename (cls , "getattribute " )
641
646
642
647
glob = {
643
648
"cached_properties" : cached_properties ,
644
649
"_cached_setattr_get" : _OBJ_SETATTR .__get__ ,
645
- "original_getattr" : original_getattr ,
650
+ "original_getattr" : original_getattribute ,
646
651
}
647
652
648
653
return _make_method (
649
- "__getattr__ " ,
654
+ "__getattribute__ " ,
650
655
"\n " .join (lines ),
651
656
unique_filename ,
652
657
glob ,
@@ -948,12 +953,14 @@ def _create_slots_class(self):
948
953
if annotation is not inspect .Parameter .empty :
949
954
class_annotations [name ] = annotation
950
955
951
- original_getattr = cd .get ("__getattr__" )
952
- if original_getattr is not None :
953
- additional_closure_functions_to_update .append (original_getattr )
956
+ original_getattribute = cd .get ("__getattribute__" )
957
+ if original_getattribute is not None :
958
+ additional_closure_functions_to_update .append (
959
+ original_getattribute
960
+ )
954
961
955
- cd ["__getattr__ " ] = _make_cached_property_getattr (
956
- cached_properties , original_getattr , self ._cls
962
+ cd ["__getattribute__ " ] = _make_cached_property_getattribute (
963
+ cached_properties , original_getattribute , self ._cls
957
964
)
958
965
959
966
# We only add the names of attributes that aren't inherited.
0 commit comments