bindings/python: implement Threads().__nonzero__
[notmuch] / bindings / python / notmuch / thread.py
index eebd6cb54724af831ccb963116e4c0c0cf18cde7..2bb30b70416e93b7723f0cbac01ae65d770da9f6 100644 (file)
@@ -141,7 +141,19 @@ class Threads(object):
         self._threads = None
         return i
 
-
+    def __nonzero__(self):
+        """Check if :class:`Threads` contains at least one more valid thread
+        
+        The existence of this function makes 'if Threads: foo' work, as
+        that will implicitely call len() exhausting the iterator if
+        __nonzero__ does not exist. This function makes `bool(Threads())`
+        work repeatedly.
+
+        :return: True if there is at least one more thread in the
+           Iterator, False if not. None on a "Out-of-memory" error.
+        """
+        return self._threads is not None and \
+            nmlib.notmuch_threads_valid(self._threads) > 0
 
     def __del__(self):
         """Close and free the notmuch Threads"""