@@ -140,104 +140,6 @@ def make_async_decorator(tracer, fn, *params, **kw_params):
140140 return fn
141141
142142
143- # static version of getattr backported from Python 3.7
144- try :
145- from inspect import getattr_static
146- except ImportError :
147- import types
148-
149- _sentinel = object ()
150-
151- def _static_getmro (klass ):
152- return type .__dict__ ["__mro__" ].__get__ (klass )
153-
154- def _check_instance (obj , attr ):
155- instance_dict = {}
156- try :
157- instance_dict = object .__getattribute__ (obj , "__dict__" )
158- except AttributeError :
159- pass
160- return dict .get (instance_dict , attr , _sentinel )
161-
162- def _check_class (klass , attr ):
163- for entry in _static_getmro (klass ):
164- if _shadowed_dict (type (entry )) is _sentinel :
165- try :
166- return entry .__dict__ [attr ]
167- except KeyError :
168- pass
169- return _sentinel
170-
171- def _is_type (obj ):
172- try :
173- _static_getmro (obj )
174- except TypeError :
175- return False
176- return True
177-
178- def _shadowed_dict (klass ):
179- dict_attr = type .__dict__ ["__dict__" ]
180- for entry in _static_getmro (klass ):
181- try :
182- class_dict = dict_attr .__get__ (entry )["__dict__" ]
183- except KeyError :
184- pass
185- else :
186- if not (
187- type (class_dict ) is types .GetSetDescriptorType # noqa: E721
188- and class_dict .__name__ == "__dict__" # noqa: E721,E261,W504
189- and class_dict .__objclass__ is entry # noqa: E261,W504
190- ):
191- return class_dict
192- return _sentinel
193-
194- def getattr_static (obj , attr , default = _sentinel ):
195- """Retrieve attributes without triggering dynamic lookup via the
196- descriptor protocol, __getattr__ or __getattribute__.
197-
198- Note: this function may not be able to retrieve all attributes
199- that getattr can fetch (like dynamically created attributes)
200- and may find attributes that getattr can't (like descriptors
201- that raise AttributeError). It can also return descriptor objects
202- instead of instance members in some cases. See the
203- documentation for details.
204- """
205- instance_result = _sentinel
206- if not _is_type (obj ):
207- klass = type (obj )
208- dict_attr = _shadowed_dict (klass )
209- if dict_attr is _sentinel or type (dict_attr ) is types .MemberDescriptorType : # noqa: E261,E721,W504
210- instance_result = _check_instance (obj , attr )
211- else :
212- klass = obj
213-
214- klass_result = _check_class (klass , attr )
215-
216- if instance_result is not _sentinel and klass_result is not _sentinel :
217- if (
218- _check_class (type (klass_result ), "__get__" ) is not _sentinel
219- and _check_class (type (klass_result ), "__set__" ) is not _sentinel # noqa: W504,E261,E721
220- ):
221- return klass_result
222-
223- if instance_result is not _sentinel :
224- return instance_result
225- if klass_result is not _sentinel :
226- return klass_result
227-
228- if obj is klass :
229- # for types we check the metaclass too
230- for entry in _static_getmro (type (klass )):
231- if _shadowed_dict (type (entry )) is _sentinel :
232- try :
233- return entry .__dict__ [attr ]
234- except KeyError :
235- pass
236- if default is not _sentinel :
237- return default
238- raise AttributeError (attr )
239-
240-
241143# DEV: There is `six.u()` which does something similar, but doesn't have the guard around `hasattr(s, 'decode')`
242144def to_unicode (s ):
243145 """ Return a unicode string for the given bytes or string instance. """
0 commit comments