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