From 469e16521f64937001e9afa397fe2d6e0bfe5b38 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sun, 2 Apr 2023 17:51:42 +0100 Subject: [PATCH 1/2] gh-103193: Micro-optimise helper functions for `inspect.getattr_static` --- Lib/inspect.py | 7 +++---- .../Library/2023-04-02-17-51-08.gh-issue-103193.xrZbM1.rst | 2 ++ 2 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-04-02-17-51-08.gh-issue-103193.xrZbM1.rst diff --git a/Lib/inspect.py b/Lib/inspect.py index 0eceaaf9a24f5d..f6899c888ab8cb 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1772,9 +1772,9 @@ def trace(context=1): # ------------------------------------------------ static version of getattr _sentinel = object() +_static_getmro = type.__dict__['__mro__'].__get__ +_dunder_dict_descriptor = type.__dict__["__dict__"] -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 = _dunder_dict_descriptor.__get__(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 index 00000000000000..f0b76a605a5610 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-04-02-17-51-08.gh-issue-103193.xrZbM1.rst @@ -0,0 +1,2 @@ +Improve performance of :func:`inspect.getattr_static`. Patch by Alex +Waygood. From 49c0352fc30a11b5cd8893c71b49ac64fb144976 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Mon, 3 Apr 2023 16:21:56 +0100 Subject: [PATCH 2/2] Address review --- Lib/inspect.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index f6899c888ab8cb..8739c9c2572643 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1773,7 +1773,7 @@ def trace(context=1): _sentinel = object() _static_getmro = type.__dict__['__mro__'].__get__ -_dunder_dict_descriptor = type.__dict__["__dict__"] +_get_dunder_dict_of_class = type.__dict__["__dict__"].__get__ def _check_instance(obj, attr): @@ -1804,7 +1804,7 @@ def _is_type(obj): def _shadowed_dict(klass): for entry in _static_getmro(klass): try: - class_dict = _dunder_dict_descriptor.__get__(entry)["__dict__"] + class_dict = _get_dunder_dict_of_class(entry)["__dict__"] except KeyError: pass else: