From 5659015d3c1828e96c9318903f72a82e047f07fa Mon Sep 17 00:00:00 2001 From: Collin Anderson Date: Thu, 3 Mar 2022 00:19:11 -0500 Subject: [PATCH] Optimized lazy wrappers a bit. This avoids an extra __getattribute__() call for self._wrapped. --- django/conf/__init__.py | 5 +++-- django/utils/functional.py | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/django/conf/__init__.py b/django/conf/__init__.py index 55bf15a355..cb70a71791 100644 --- a/django/conf/__init__.py +++ b/django/conf/__init__.py @@ -88,9 +88,10 @@ class LazySettings(LazyObject): def __getattr__(self, name): """Return the value of a setting and cache it in self.__dict__.""" - if self._wrapped is empty: + if (_wrapped := self._wrapped) is empty: self._setup(name) - val = getattr(self._wrapped, name) + _wrapped = self._wrapped + val = getattr(_wrapped, name) # Special case some settings which require further modification. # This is done here for performance reasons so the modified value is cached. diff --git a/django/utils/functional.py b/django/utils/functional.py index 5827389231..fd2c3c44d6 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -262,9 +262,10 @@ empty = object() def new_method_proxy(func): def inner(self, *args): - if self._wrapped is empty: + if (_wrapped := self._wrapped) is empty: self._setup() - return func(self._wrapped, *args) + _wrapped = self._wrapped + return func(_wrapped, *args) inner._mask_wrapped = False return inner