Patchwork [2,of,6] ancestors: add a __nonzero__ method

login
register
mail settings
Submitter Pierre-Yves David
Date Aug. 18, 2014, 9:50 p.m.
Message ID <45f5b72530ad0be0c6ae.1408398600@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/5471/
State Accepted
Headers show

Comments

Pierre-Yves David - Aug. 18, 2014, 9:50 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@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.

Patch

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*