[PATCH 2 of 6] ancestors: add a __nonzero__ method

Pierre-Yves David pierre-yves.david at ens-lyon.org
Mon Aug 18 16:50:00 CDT 2014


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1408143470 25200
#      Fri Aug 15 15:57:50 2014 -0700
# Node ID 45f5b72530ad0be0c6ae2af17af70f461274c9e4
# Parent  cef89d04868a41c6e1a7951e9f2add046fcc681c
ancestors: add a __nonzero__ method

This allows to use the object in conditional the same way we can use list.

diff --git a/mercurial/ancestor.py b/mercurial/ancestor.py
--- a/mercurial/ancestor.py
+++ b/mercurial/ancestor.py
@@ -244,10 +244,18 @@ class lazyancestors(object):
         if inclusive:
             self._containsseen = set(revs)
         else:
             self._containsseen = set()
 
+    def __nonzero__(self):
+        """False if the set is empty, True otherwise."""
+        try:
+            iter(self).next()
+            return True
+        except StopIteration:
+            return False
+
     def __iter__(self):
         """Generate the ancestors of _initrevs in reverse topological order.
 
         If inclusive is False, yield a sequence of revision numbers starting
         with the parents of each revision in revs, i.e., each revision is *not*


More information about the Mercurial-devel mailing list