From 7ccec0b5369f94c51bdf487ac274a68c4b9bdfb9 Mon Sep 17 00:00:00 2001 From: Ask Solem Date: Thu, 7 Jul 2016 18:42:30 -0700 Subject: [PATCH] Avoid expensive try: except in cached_property --- kombu/utils/__init__.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/kombu/utils/__init__.py b/kombu/utils/__init__.py index 964f64dd..2ddc4a80 100644 --- a/kombu/utils/__init__.py +++ b/kombu/utils/__init__.py @@ -309,16 +309,12 @@ class cached_property(object): value = self.__set(obj, value) obj.__dict__[self.__name__] = value - def __delete__(self, obj): + def __delete__(self, obj, _sentinel=object()): if obj is None: return self - try: - value = obj.__dict__.pop(self.__name__) - except KeyError: - pass - else: - if self.__del is not None: - self.__del(obj, value) + value = obj.__dict__.pop(self.__name__, _sentinel) + if self.__del is not None and value is not _sentinel: + self.__del(obj, value) def setter(self, fset): return self.__class__(self.__get, fset, self.__del)