From cb580bac935c111727ce177ae9b56109c5fce4b3 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sun, 7 May 2023 15:31:52 +0100 Subject: [PATCH 1/4] gh-103193: cache calls to inspect._shadowed_dict --- Lib/inspect.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/inspect.py b/Lib/inspect.py index 95da7fb71a3997..deade558cc4c2b 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1794,6 +1794,7 @@ def _check_class(klass, attr): return entry.__dict__[attr] return _sentinel +@functools.lru_cache() def _shadowed_dict(klass): for entry in _static_getmro(klass): dunder_dict = _get_dunder_dict_of_class(entry) From c26587c462935935eb68172dc179d1d9298e3864 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sun, 7 May 2023 16:36:45 +0100 Subject: [PATCH 2/4] Use mro tuple as the cache key --- Lib/inspect.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index deade558cc4c2b..a64e85e4fd67a4 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1795,8 +1795,8 @@ def _check_class(klass, attr): return _sentinel @functools.lru_cache() -def _shadowed_dict(klass): - for entry in _static_getmro(klass): +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__'] @@ -1806,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__. From b3cc123ad1ca8d4371a912cdb0d364b962fb016f Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sun, 7 May 2023 16:46:11 +0100 Subject: [PATCH 3/4] Add regression test for if a class has its mro mutated(!) --- Lib/test/test_inspect.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 42e3d709bd683f..dd0325a43e0f58 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -2111,6 +2111,28 @@ def __dict__(self): 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 From 2f4b42faaffdc0952ee69158160ccc87a78080a9 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sun, 7 May 2023 17:22:06 +0100 Subject: [PATCH 4/4] Update perf numbers in whatsnew --- Doc/whatsnew/3.12.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index ec04178238b6b0..65b3e9ffb8072d 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -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 ` 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`.)