]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-103193: cache calls to `inspect._shadowed_dict` in `inspect.getattr_static` (...
authorAlex Waygood <Alex.Waygood@Gmail.com>
Sun, 7 May 2023 17:45:09 +0000 (18:45 +0100)
committerGitHub <noreply@github.com>
Sun, 7 May 2023 17:45:09 +0000 (18:45 +0100)
Co-authored-by: Carl Meyer <carl@oddbird.net>
Doc/whatsnew/3.12.rst
Lib/inspect.py
Lib/test/test_inspect.py

index ec04178238b6b06eaf1d87f26e7147919b6d7f0c..65b3e9ffb8072d0a290b50df6ef6c802c008313b 100644 (file)
@@ -342,8 +342,9 @@ inspect
   (Contributed by Thomas Krennwallner in :issue:`35759`.)
 
 * The performance of :func:`inspect.getattr_static` has been considerably
-  improved. Most calls to the function should be around 2x faster than they
-  were in Python 3.11. (Contributed by Alex Waygood in :gh:`103193`.)
+  improved. Most calls to the function should be at least 2x faster than they
+  were in Python 3.11, and some may be 6x faster or more. (Contributed by Alex
+  Waygood in :gh:`103193`.)
 
 pathlib
 -------
@@ -597,7 +598,7 @@ typing
   :func:`runtime-checkable protocols <typing.runtime_checkable>` has changed
   significantly. Most ``isinstance()`` checks against protocols with only a few
   members should be at least 2x faster than in 3.11, and some may be 20x
-  faster or more. However, ``isinstance()`` checks against protocols with seven
+  faster or more. However, ``isinstance()`` checks against protocols with fourteen
   or more members may be slower than in Python 3.11. (Contributed by Alex
   Waygood in :gh:`74690` and :gh:`103193`.)
 
index 95da7fb71a3997433691c34ef8ef23e0176ca3d6..a64e85e4fd67a45db1260028ef34e308c0772066 100644 (file)
@@ -1794,8 +1794,9 @@ def _check_class(klass, attr):
             return entry.__dict__[attr]
     return _sentinel
 
-def _shadowed_dict(klass):
-    for entry in _static_getmro(klass):
+@functools.lru_cache()
+def _shadowed_dict_from_mro_tuple(mro):
+    for entry in mro:
         dunder_dict = _get_dunder_dict_of_class(entry)
         if '__dict__' in dunder_dict:
             class_dict = dunder_dict['__dict__']
@@ -1805,6 +1806,9 @@ def _shadowed_dict(klass):
                 return class_dict
     return _sentinel
 
+def _shadowed_dict(klass):
+    return _shadowed_dict_from_mro_tuple(_static_getmro(klass))
+
 def getattr_static(obj, attr, default=_sentinel):
     """Retrieve attributes without triggering dynamic lookup via the
        descriptor protocol,  __getattr__ or __getattribute__.
index 42e3d709bd683f56832beae24f5b7cf441cea35e..dd0325a43e0f588c07037478c587ea2b4866d262 100644 (file)
@@ -2111,6 +2111,28 @@ class TestGetattrStatic(unittest.TestCase):
         self.assertEqual(inspect.getattr_static(foo, 'a'), 3)
         self.assertFalse(test.called)
 
+    def test_mutated_mro(self):
+        test = self
+        test.called = False
+
+        class Foo(dict):
+            a = 3
+            @property
+            def __dict__(self):
+                test.called = True
+                return {}
+
+        class Bar(dict):
+            a = 4
+
+        class Baz(Bar): pass
+
+        baz = Baz()
+        self.assertEqual(inspect.getattr_static(baz, 'a'), 4)
+        Baz.__bases__ = (Foo,)
+        self.assertEqual(inspect.getattr_static(baz, 'a'), 3)
+        self.assertFalse(test.called)
+
     def test_custom_object_dict(self):
         test = self
         test.called = False