# Patchwork [1,of,7] dagop: unnest inner generator of revancestors()

Submitter Yuya Nishihara June 22, 2017, 3:52 p.m. mbox | patch /patch/21612/ Accepted show

Yuya Nishihara - June 22, 2017, 3:52 p.m.
```# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1497706403 -32400
#      Sat Jun 17 22:33:23 2017 +0900
# Node ID a8dd4bd0604af65ee06cd942e9af4b7971725f43
dagop: unnest inner generator of revancestors()

This just moves iterate() to module-level function.
```

## Patch

```diff --git a/mercurial/dagop.py b/mercurial/dagop.py
--- a/mercurial/dagop.py
+++ b/mercurial/dagop.py
@@ -20,43 +20,43 @@  from . import (
baseset = smartset.baseset
generatorset = smartset.generatorset

-def revancestors(repo, revs, followfirst):
-    """Like revlog.ancestors(), but supports followfirst."""
+def _genrevancestors(repo, revs, followfirst):
if followfirst:
cut = 1
else:
cut = None
cl = repo.changelog
+    revs.sort(reverse=True)
+    irevs = iter(revs)
+    h = []

-    def iterate():
-        revs.sort(reverse=True)
-        irevs = iter(revs)
-        h = []
-
-        inputrev = next(irevs, None)
-        if inputrev is not None:
-            heapq.heappush(h, -inputrev)
+    inputrev = next(irevs, None)
+    if inputrev is not None:
+        heapq.heappush(h, -inputrev)

-        seen = set()
-        while h:
-            current = -heapq.heappop(h)
-            if current == inputrev:
-                inputrev = next(irevs, None)
-                if inputrev is not None:
-                    heapq.heappush(h, -inputrev)
-            if current not in seen:
-                yield current
-                try:
-                    for parent in cl.parentrevs(current)[:cut]:
-                        if parent != node.nullrev:
-                            heapq.heappush(h, -parent)
-                except error.WdirUnsupported:
-                    for parent in repo[current].parents()[:cut]:
-                        if parent.rev() != node.nullrev:
-                            heapq.heappush(h, -parent.rev())
+    seen = set()
+    while h:
+        current = -heapq.heappop(h)
+        if current == inputrev:
+            inputrev = next(irevs, None)
+            if inputrev is not None:
+                heapq.heappush(h, -inputrev)
+        if current not in seen:
+            yield current
+            try:
+                for parent in cl.parentrevs(current)[:cut]:
+                    if parent != node.nullrev:
+                        heapq.heappush(h, -parent)
+            except error.WdirUnsupported:
+                for parent in repo[current].parents()[:cut]:
+                    if parent.rev() != node.nullrev:
+                        heapq.heappush(h, -parent.rev())

-    return generatorset(iterate(), iterasc=False)
+def revancestors(repo, revs, followfirst):
+    """Like revlog.ancestors(), but supports followfirst."""
+    gen = _genrevancestors(repo, revs, followfirst)
+    return generatorset(gen, iterasc=False)

def revdescendants(repo, revs, followfirst):
"""Like revlog.descendants() but supports followfirst."""

```