Browse Source

[utils][LimitedSet] Removes the need for having a `sentinel` (Issue #3102)

Ask Solem 9 years ago
parent
commit
c3ffe689ce
1 changed files with 11 additions and 9 deletions
  1. 11 9
      celery/datastructures.py

+ 11 - 9
celery/datastructures.py

@@ -51,8 +51,6 @@ REPR_LIMITED_SET = """\
 <{name}({size}): maxlen={0.maxlen}, expires={0.expires}, minlen={0.minlen}>\
 """
 
-sentinel = object()
-
 
 def force_mapping(m):
     if isinstance(m, (LazyObject, LazySettings)):
@@ -671,10 +669,7 @@ class LimitedSet(object):
 
     def _refresh_heap(self):
         """Time consuming recreating of heap. Do not run this too often."""
-        self._heap[:] = [
-            entry for entry in values(self._data)
-            if entry is not sentinel
-        ]
+        self._heap[:] = [entry for entry in values(self._data)]
         heapify(self._heap)
 
     def clear(self):
@@ -721,8 +716,11 @@ class LimitedSet(object):
 
     def discard(self, item):
         # mark an existing item as removed. If KeyError is not found, pass.
-        entry = self._data.pop(item, sentinel)
-        if entry is not sentinel:
+        try:
+            entry = self._data.pop(item)
+        except KeyError:
+            pass
+        else:
             if self._heap_overload > self.max_heap_percent_overload:
                 self._refresh_heap()
     pop_value = discard
@@ -751,7 +749,11 @@ class LimitedSet(object):
         """Remove and return the oldest item, or :const:`None` when empty."""
         while self._heap:
             _, item = heappop(self._heap)
-            if self._data.pop(item, None) is not sentinel:
+            try:
+                self._data.pop(item)
+            except KeyError:
+                pass
+            else:
                 return item
         return default