diff --git a/mercurial/revset.py b/mercurial/revset.py
index 81a395447b345dd8dd7cd002e99e66c8eaa18d53_bWVyY3VyaWFsL3JldnNldC5weQ==..3553163bb736be75e68465c9c50f310954bf055a_bWVyY3VyaWFsL3JldnNldC5weQ== 100644
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -1172,9 +1172,6 @@
     result = []
     it = iter(os)
     for x in xrange(lim):
-        try:
-            y = it.next()
-            if y in ss:
-                result.append(y)
-        except (StopIteration):
+        y = next(it, None)
+        if y is None:
             break
@@ -1180,4 +1177,6 @@
             break
+        elif y in ss:
+            result.append(y)
     return baseset(result)
 
 def maxrev(repo, subset, x):