]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-103193: Micro-optimise helper functions for `inspect.getattr_static` (#103195)
authorAlex Waygood <Alex.Waygood@Gmail.com>
Wed, 5 Apr 2023 07:27:01 +0000 (08:27 +0100)
committerGitHub <noreply@github.com>
Wed, 5 Apr 2023 07:27:01 +0000 (08:27 +0100)
Lib/inspect.py
Misc/NEWS.d/next/Library/2023-04-02-17-51-08.gh-issue-103193.xrZbM1.rst [new file with mode: 0644]

index 0eceaaf9a24f5d6b673d5a92abbd0e1aab76b726..8739c9c2572643992005bff0e20c0c5ebdf34083 100644 (file)
@@ -1772,9 +1772,9 @@ def trace(context=1):
 # ------------------------------------------------ static version of getattr
 
 _sentinel = object()
+_static_getmro = type.__dict__['__mro__'].__get__
+_get_dunder_dict_of_class = type.__dict__["__dict__"].__get__
 
-def _static_getmro(klass):
-    return type.__dict__['__mro__'].__get__(klass)
 
 def _check_instance(obj, attr):
     instance_dict = {}
@@ -1802,10 +1802,9 @@ def _is_type(obj):
     return True
 
 def _shadowed_dict(klass):
-    dict_attr = type.__dict__["__dict__"]
     for entry in _static_getmro(klass):
         try:
-            class_dict = dict_attr.__get__(entry)["__dict__"]
+            class_dict = _get_dunder_dict_of_class(entry)["__dict__"]
         except KeyError:
             pass
         else:
diff --git a/Misc/NEWS.d/next/Library/2023-04-02-17-51-08.gh-issue-103193.xrZbM1.rst b/Misc/NEWS.d/next/Library/2023-04-02-17-51-08.gh-issue-103193.xrZbM1.rst
new file mode 100644 (file)
index 0000000..f0b76a6
--- /dev/null
@@ -0,0 +1,2 @@
+Improve performance of :func:`inspect.getattr_static`. Patch by Alex
+Waygood.