gh-103193: Improve getattr_static
test coverage by AlexWaygood · Pull Request #104286 · python/cpython (original) (raw)
Currently, no tests would fail if we were to apply this optimisation to getattr_static
, which makes things a fair bit faster:
--- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1780,13 +1780,9 @@ def trace(context=1):
def _check_instance(obj, attr): - instance_dict = {} - try: - instance_dict = object.getattribute(obj, "dict") - except AttributeError: - pass - return dict.get(instance_dict, attr, _sentinel)
- if hasattr(obj, "dict"):
return dict.get(obj.__dict__, attr, _sentinel)
- return _sentinel
However, the optimisation would lead to incorrect behaviour. test_custom___getattribute__
, added in this PR, would correctly fail if the incorrect optimisation were applied.