[PATCH] util: subclass deque for Python 2.4 backwards compatibility

Bryan O'Sullivan bos at serpentine.com
Fri Jun 1 19:05:37 CDT 2012


# HG changeset patch
# User Bryan O'Sullivan <bryano at fb.com>
# Date 1338595531 25200
# Node ID fbd4a78dedd8ad8dd8d142c20ad686991089e53f
# Parent  8abee656e14c3d995adbc00420266fcc7c0db2c5
util: subclass deque for Python 2.4 backwards compatibility

It turns out that Python 2.4's deque type is lacking a remove method.
We can't implement remove in terms of find, because it doesn't have
find either.

diff --git a/mercurial/hbisect.py b/mercurial/hbisect.py
--- a/mercurial/hbisect.py
+++ b/mercurial/hbisect.py
@@ -11,7 +11,7 @@
 import os, error
 from i18n import _
 from node import short, hex
-import collections, util
+import util
 
 def bisect(changelog, state):
     """find the next node (if any) for testing during a bisect search.
@@ -69,7 +69,7 @@ def bisect(changelog, state):
 
     # build children dict
     children = {}
-    visit = collections.deque([badrev])
+    visit = util.deque([badrev])
     candidates = []
     while visit:
         rev = visit.popleft()
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -12,7 +12,7 @@ import tempfile, zlib, shutil
 from i18n import _
 from node import hex, nullid, short
 import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error
-import collections, context
+import context
 
 gitre = re.compile('diff --git a/(.*) b/(.*)')
 
@@ -1597,7 +1597,7 @@ def diff(repo, node1=None, node2=None, m
 
     def lrugetfilectx():
         cache = {}
-        order = collections.deque()
+        order = util.deque()
         def getfilectx(f, ctx):
             fctx = ctx.filectx(f, filelog=cache.get(f))
             if f not in cache:
diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -15,7 +15,7 @@ and O(changes) merge between branches.
 from node import bin, hex, nullid, nullrev
 from i18n import _
 import ancestor, mdiff, parsers, error, util, dagutil
-import struct, zlib, errno, collections
+import struct, zlib, errno
 
 _pack = struct.pack
 _unpack = struct.unpack
@@ -362,7 +362,7 @@ class revlog(object):
         """return the set of all nodes ancestral to a given node, including
          the node itself, stopping when stop is matched"""
         reachable = set((node,))
-        visit = collections.deque([node])
+        visit = util.deque([node])
         if stop:
             stopn = self.rev(stop)
         else:
@@ -389,7 +389,7 @@ class revlog(object):
         an ancestor of itself.  Results are in breadth-first order:
         parents of each rev in revs, then parents of those, etc.  Result
         does not include the null revision."""
-        visit = collections.deque(revs)
+        visit = util.deque(revs)
         seen = set([nullrev])
         while visit:
             for parent in self.parentrevs(visit.popleft()):
@@ -447,7 +447,7 @@ class revlog(object):
 
         # take all ancestors from heads that aren't in has
         missing = set()
-        visit = collections.deque(r for r in heads if r not in has)
+        visit = util.deque(r for r in heads if r not in has)
         while visit:
             r = visit.popleft()
             if r in missing:
diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -5,7 +5,7 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-import re, collections
+import re
 import parser, util, error, discovery, hbisect, phases
 import node
 import bookmarks as bookmarksmod
@@ -17,7 +17,7 @@ def _revancestors(repo, revs, followfirs
     """Like revlog.ancestors(), but supports followfirst."""
     cut = followfirst and 1 or None
     cl = repo.changelog
-    visit = collections.deque(revs)
+    visit = util.deque(revs)
     seen = set([node.nullrev])
     while visit:
         for parent in cl.parentrevs(visit.popleft())[:cut]:
diff --git a/mercurial/setdiscovery.py b/mercurial/setdiscovery.py
--- a/mercurial/setdiscovery.py
+++ b/mercurial/setdiscovery.py
@@ -8,7 +8,7 @@
 
 from node import nullid
 from i18n import _
-import random, collections, util, dagutil
+import random, util, dagutil
 import phases
 
 def _updatesample(dag, nodes, sample, always, quicksamplesize=0):
@@ -18,7 +18,7 @@ def _updatesample(dag, nodes, sample, al
     else:
         heads = dag.heads()
     dist = {}
-    visit = collections.deque(heads)
+    visit = util.deque(heads)
     seen = set()
     factor = 1
     while visit:
diff --git a/mercurial/treediscovery.py b/mercurial/treediscovery.py
--- a/mercurial/treediscovery.py
+++ b/mercurial/treediscovery.py
@@ -7,7 +7,7 @@
 
 from node import nullid, short
 from i18n import _
-import util, error, collections
+import util, error
 
 def findcommonincoming(repo, remote, heads=None, force=False):
     """Return a tuple (common, fetch, heads) used to identify the common
@@ -56,7 +56,7 @@ def findcommonincoming(repo, remote, hea
     # a 'branch' here is a linear segment of history, with four parts:
     # head, root, first parent, second parent
     # (a branch always has two parents (or none) by definition)
-    unknown = collections.deque(remote.branches(unknown))
+    unknown = util.deque(remote.branches(unknown))
     while unknown:
         r = []
         while unknown:
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -202,10 +202,22 @@ def cachefunc(func):
 
     return f
 
+try:
+    collections.deque.remove
+    deque = collections.deque
+except AttributeError:
+    # python 2.4 lacks deque.remove
+    class deque(collections.deque):
+        def remove(self, val):
+            for i, v in enumerate(self):
+                if v == val:
+                    del self[i]
+                    break
+
 def lrucachefunc(func):
     '''cache most recent results of function calls'''
     cache = {}
-    order = collections.deque()
+    order = deque()
     if func.func_code.co_argcount == 1:
         def f(arg):
             if arg not in cache:
@@ -865,7 +877,7 @@ class chunkbuffer(object):
         Returns less than L bytes if the iterator runs dry."""
         left = l
         buf = ''
-        queue = collections.deque(self._queue)
+        queue = deque(self._queue)
         while left > 0:
             # refill the queue
             if not queue:


More information about the Mercurial-devel mailing list