Rev 4792: Add a static_tuple.as_tuples() helper. in http://bazaar.launchpad.net/~jameinel/bzr/2.1.0b4-builder-no-keys

John Arbash Meinel john at arbash-meinel.com
Sat Nov 28 21:54:10 GMT 2009


At http://bazaar.launchpad.net/~jameinel/bzr/2.1.0b4-builder-no-keys

------------------------------------------------------------
revno: 4792
revision-id: john at arbash-meinel.com-20091128215408-d9jtxbxyiklmiurh
parent: john at arbash-meinel.com-20091107015811-apybkqd40koa4b98
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1.0b4-builder-no-keys
timestamp: Sat 2009-11-28 15:54:08 -0600
message:
  Add a static_tuple.as_tuples() helper.
  
  It will pass over an iterable, and ensure that it and all children
  are simple tuples. Useful for formatting.
  Also fix a small bug in the reference checking code that assumes
  the values passed in are the same type, but sometimes they are a
  list rather than a tuple.
-------------- next part --------------
=== modified file 'bzrlib/_static_tuple_py.py'
--- a/bzrlib/_static_tuple_py.py	2009-10-27 03:39:16 +0000
+++ b/bzrlib/_static_tuple_py.py	2009-11-28 21:54:08 +0000
@@ -58,7 +58,7 @@
         return StaticTuple.from_sequence(tuple.__add__(self,other))
 
     def as_tuple(self):
-        return self
+        return tuple(self)
 
     def intern(self):
         return _interned_tuples.setdefault(self, self)

=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2009-11-04 22:32:13 +0000
+++ b/bzrlib/builtins.py	2009-11-28 21:54:08 +0000
@@ -43,6 +43,7 @@
     reconfigure,
     rename_map,
     revision as _mod_revision,
+    static_tuple,
     symbol_versioning,
     transport,
     ui,
@@ -431,8 +432,7 @@
         for node in bt.iter_all_entries():
             # Node is made up of:
             # (index, key, value, [references])
-            refs_as_tuples = tuple([tuple([tuple(ref) for ref in ref_list])
-                                   for ref_list in node[3]])
+            refs_as_tuples = static_tuple.as_tuples(node[3])
             as_tuple = (tuple(node[1]), node[2], refs_as_tuples)
             self.outf.write('%s\n' % (as_tuple,))
 

=== modified file 'bzrlib/groupcompress.py'
--- a/bzrlib/groupcompress.py	2009-10-23 15:46:01 +0000
+++ b/bzrlib/groupcompress.py	2009-11-28 21:54:08 +0000
@@ -31,6 +31,7 @@
     knit,
     osutils,
     pack,
+    static_tuple,
     trace,
     )
 from bzrlib.btree_index import BTreeBuilder
@@ -1877,8 +1878,11 @@
         if not random_id:
             present_nodes = self._get_entries(keys)
             for (index, key, value, node_refs) in present_nodes:
-                if node_refs != keys[key][1]:
-                    details = '%s %s %s' % (key, (value, node_refs), keys[key])
+                # Sometimes these are passed as a list rather than a tuple
+                node_refs = static_tuple.as_tuples(node_refs)
+                passed = static_tuple.as_tuples(keys[key])
+                if node_refs != passed[1]:
+                    details = '%s %s %s' % (key, (value, node_refs), passed)
                     if self._inconsistency_fatal:
                         raise errors.KnitCorrupt(self, "inconsistent details"
                                                  " in add_records: %s" %

=== modified file 'bzrlib/knit.py'
--- a/bzrlib/knit.py	2009-10-29 05:54:49 +0000
+++ b/bzrlib/knit.py	2009-11-28 21:54:08 +0000
@@ -69,6 +69,7 @@
     lru_cache,
     pack,
     progress,
+    static_tuple,
     trace,
     tsort,
     tuned_gzip,
@@ -2944,10 +2945,15 @@
         if not random_id:
             present_nodes = self._get_entries(keys)
             for (index, key, value, node_refs) in present_nodes:
+                parents = node_refs[:1]
+                # Sometimes these are passed as a list rather than a tuple
+                passed = static_tuple.as_tuples(keys[key])
+                passed_parents = passed([1][:1])
                 if (value[0] != keys[key][0][0] or
-                    node_refs[:1] != keys[key][1][:1]):
+                    parents != passed_parents):
+                    node_refs = static_tuple.as_tuples(node_refs)
                     raise KnitCorrupt(self, "inconsistent details in add_records"
-                        ": %s %s" % ((value, node_refs), keys[key]))
+                        ": %s %s" % ((value, node_refs), passed))
                 del keys[key]
         result = []
         if self._parents:

=== modified file 'bzrlib/static_tuple.py'
--- a/bzrlib/static_tuple.py	2009-11-02 17:39:39 +0000
+++ b/bzrlib/static_tuple.py	2009-11-28 21:54:08 +0000
@@ -40,3 +40,17 @@
     if type(obj) is not StaticTuple:
         raise TypeError('We expected a StaticTuple not a %s' % (type(obj),))
     return obj
+
+
+def as_tuples(obj):
+    """Ensure that the object and any referenced objects are plain tuples.
+
+    :param obj: a list, tuple or StaticTuple
+    :return: a plain tuple instance, with all children also being tuples.
+    """
+    result = []
+    for item in obj:
+        if isinstance(item, (tuple, list, StaticTuple)):
+            item = as_tuples(item)
+        result.append(item)
+    return tuple(result)

=== modified file 'bzrlib/tests/test__static_tuple.py'
--- a/bzrlib/tests/test__static_tuple.py	2009-11-02 17:15:20 +0000
+++ b/bzrlib/tests/test__static_tuple.py	2009-11-28 21:54:08 +0000
@@ -148,9 +148,34 @@
         k = self.module.StaticTuple('foo')
         t = k.as_tuple()
         self.assertEqual(('foo',), t)
+        self.assertIsInstance(t, tuple)
+        self.assertFalse(isinstance(t, self.module.StaticTuple))
         k = self.module.StaticTuple('foo', 'bar')
         t = k.as_tuple()
         self.assertEqual(('foo', 'bar'), t)
+        k2 = self.module.StaticTuple(1, k)
+        t = k2.as_tuple()
+        self.assertIsInstance(t, tuple)
+        # For pickling to work, we need to keep the sub-items as StaticTuple so
+        # that it knows that they also need to be converted.
+        self.assertIsInstance(t[1], self.module.StaticTuple)
+        self.assertEqual((1, ('foo', 'bar')), t)
+
+    def test_as_tuples(self):
+        k1 = self.module.StaticTuple('foo', 'bar')
+        t = static_tuple.as_tuples(k1)
+        self.assertIsInstance(t, tuple)
+        self.assertEqual(('foo', 'bar'), t)
+        k2 = self.module.StaticTuple(1, k1)
+        t = static_tuple.as_tuples(k2)
+        self.assertIsInstance(t, tuple)
+        self.assertIsInstance(t[1], tuple)
+        self.assertEqual((1, ('foo', 'bar')), t)
+        mixed = (1, k1)
+        t = static_tuple.as_tuples(mixed)
+        self.assertIsInstance(t, tuple)
+        self.assertIsInstance(t[1], tuple)
+        self.assertEqual((1, ('foo', 'bar')), t)
 
     def test_len(self):
         k = self.module.StaticTuple()



More information about the bazaar-commits mailing list