Skip to content

Commit 264c00a

Browse files
authored
gh-103193: Micro-optimise helper functions for inspect.getattr_static (#103195)
1 parent 119f67d commit 264c00a

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

Lib/inspect.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1772,9 +1772,9 @@ def trace(context=1):
17721772
# ------------------------------------------------ static version of getattr
17731773

17741774
_sentinel = object()
1775+
_static_getmro = type.__dict__['__mro__'].__get__
1776+
_get_dunder_dict_of_class = type.__dict__["__dict__"].__get__
17751777

1776-
def _static_getmro(klass):
1777-
return type.__dict__['__mro__'].__get__(klass)
17781778

17791779
def _check_instance(obj, attr):
17801780
instance_dict = {}
@@ -1802,10 +1802,9 @@ def _is_type(obj):
18021802
return True
18031803

18041804
def _shadowed_dict(klass):
1805-
dict_attr = type.__dict__["__dict__"]
18061805
for entry in _static_getmro(klass):
18071806
try:
1808-
class_dict = dict_attr.__get__(entry)["__dict__"]
1807+
class_dict = _get_dunder_dict_of_class(entry)["__dict__"]
18091808
except KeyError:
18101809
pass
18111810
else:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve performance of :func:`inspect.getattr_static`. Patch by Alex
2+
Waygood.

0 commit comments

Comments
 (0)