minor patch using the set builtin

Sylvain Defresne kamisama at free.fr
Thu May 19 07:24:23 BST 2005


Martin Pool <mbp at sourcefrog.net> writes:

> > I have a couple of comments:
> >  -one can use  the built-ins basestring and set instead of
> >   types.StringTypes and sets.Set respectively (see attached patch);
> 
> Actually, we can only use the set builtin in Python2.4 and later; for
> 2.3 which is still supported by bzr we need to use sets.Set.  
> (I thought they would fold down to the same thing, but apparently 
> sets.Set still gives the one written on top of a dict, which is probably
> a lot slower.)

Why don't you do something like the following to detect at runtime if
the set built-in is available, and otherwise fallback to sets.Set :

] try:
]     set ()
] except NameError, _:
]     from sets import Set as set
]     from sets import ImmutableSet as frozenset

This is implemented by the following patch (it should work correctly
with both python 2.3 and 2.4, tested with 2.4). I also removed some
import statement from body function as it can lead to deadlock.

===File ~/bzr.diff==========================================
*** modified file 'bzrlib/branch.py'
--- bzrlib/branch.py 
+++ bzrlib/branch.py 
@@ -15,9 +15,12 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
-from sets import Set
-
-import sys, os, os.path, random, time, sha, sets, types, re, shutil, tempfile
+try:
+    set ()
+except NameError, _:
+    from sets import Set as set
+
+import sys, os, os.path, random, time, sha, types, re, shutil, tempfile
 import traceback, socket, fnmatch, difflib, time
 from binascii import hexlify
 
@@ -679,7 +682,7 @@
         if to_dir_ie.kind not in ('directory', 'root_directory'):
             bailout("destination %r is not a directory" % to_abs)
 
-        to_idpath = Set(inv.get_idpath(to_dir_id))
+        to_idpath = set(inv.get_idpath(to_dir_id))
 
         for f in from_paths:
             if not tree.has_filename(f):

*** modified file 'bzrlib/check.py'
--- bzrlib/check.py 
+++ bzrlib/check.py 
@@ -21,7 +21,11 @@
 # consistency checks
 
 import sys
-from sets import Set
+
+try:
+    set ()
+except NameError, _:
+    from sets import Set as set
 
 from trace import mutter
 from errors import bailout
@@ -45,7 +49,7 @@
 
     p('history of %r' % branch.base)
     last_ptr = None
-    checked_revs = Set()
+    checked_revs = set()
     
     history = branch.revision_history()
     revno = 0
@@ -70,8 +74,8 @@
         ## TODO: Check all the required fields are present on the revision.
 
         inv = branch.get_inventory(rev.inventory_id)
-        seen_ids = Set()
-        seen_names = Set()
+        seen_ids = set()
+        seen_names = set()
 
         p('revision %d/%d file ids' % (revno, revcount))
         for file_id in inv:

*** modified file 'bzrlib/commands.py'
--- bzrlib/commands.py 
+++ bzrlib/commands.py 
@@ -17,7 +17,6 @@
 
 
 import sys, os, time, os.path
-from sets import Set
 
 import bzrlib
 from bzrlib.trace import mutter, note, log_error

*** modified file 'bzrlib/diff.py'
--- bzrlib/diff.py 
+++ bzrlib/diff.py 
@@ -14,8 +14,6 @@
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-
-from sets import Set, ImmutableSet
 
 from trace import mutter
 from errors import BzrError

*** modified file 'bzrlib/info.py'
--- bzrlib/info.py 
+++ bzrlib/info.py 
@@ -16,7 +16,11 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-from sets import Set
+try:
+    set ()
+except NameError, _:
+    from sets import Set as set
+
 import time
 
 from osutils import format_date
@@ -82,7 +86,7 @@
     history = b.revision_history()
     revno = len(history)
     print '  %8d revision%s' % (revno, plural(revno))
-    committers = Set()
+    committers = set()
     for rev in history:
         committers.add(b.get_revision(rev).committer)
     print '  %8d committer%s' % (len(committers), plural(len(committers)))

*** modified file 'bzrlib/inventory.py'
--- bzrlib/inventory.py 
+++ bzrlib/inventory.py 
@@ -21,7 +21,11 @@
 
 
 import sys, os.path, types, re
-from sets import Set
+
+try:
+    set ()
+except NameError, _:
+    from sets import Set as set
 
 try:
     from cElementTree import Element, ElementTree, SubElement
@@ -455,7 +459,7 @@
 
 
     def id_set(self):
-        return Set(self._byid)
+        return set(self._byid)
 
 
     def to_element(self):

*** modified file 'bzrlib/remotebranch.py'
--- bzrlib/remotebranch.py 
+++ bzrlib/remotebranch.py 
@@ -25,7 +25,10 @@
 
 
 import gzip
-from sets import Set
+try:
+    set ()
+except NameError, _:
+    from sets import Set as set
 from cStringIO import StringIO
 import urllib2
 
@@ -171,8 +174,8 @@
     from branch import Branch
     from inventory import Inventory
 
-    got_invs = Set()
-    got_texts = Set()
+    got_invs = set()
+    got_texts = set()
 
     print 'read history'
     history = get_url('/.bzr/revision-history').readlines()

*** modified file 'bzrlib/statcache.py'
--- bzrlib/statcache.py 
+++ bzrlib/statcache.py 
@@ -14,7 +14,12 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-import stat, os, sha, time
+try:
+    set ()
+except NameError, _:
+    from sets import Set as set
+
+import stat, os, sha, time, re
 from binascii import b2a_qp, a2b_qp
 
 from trace import mutter
@@ -136,10 +141,8 @@
         
         
 def load_cache(basedir):
-    from sets import Set
-    import re
     cache = {}
-    seen_paths = Set()
+    seen_paths = set()
 
     sha_re = re.compile(r'[a-f0-9]{40}')
 
@@ -219,14 +222,12 @@
     to_update -- Sequence of (file_id, path) pairs to check.
     """
 
-    from sets import Set
-
     stat_cnt = missing_cnt = hardcheck = change_cnt = 0
 
     # files that have been recently touched and can't be
     # committed to a persistent cache yet.
     
-    dangerfiles = Set()
+    dangerfiles = set()
     now = int(time.time())
 
     ## mutter('update statcache under %r' % basedir)

*** modified file 'bzrlib/tree.py'
--- bzrlib/tree.py 
+++ bzrlib/tree.py 
@@ -17,7 +17,6 @@
 """Tree classes, representing directory at point in time.
 """
 
-from sets import Set
 import os.path, os, fnmatch
 
 from osutils import pumpfile, filesize, quotefn, sha_file, \

============================================================

-- 
keiichi





More information about the bazaar mailing list