Rev 4757: Change the pure-python version of StaticTuple in http://bazaar.launchpad.net/~jameinel/bzr/2.1-static-tuple

John Arbash Meinel john at arbash-meinel.com
Wed Oct 7 17:07:06 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr/2.1-static-tuple

------------------------------------------------------------
revno: 4757
revision-id: john at arbash-meinel.com-20091007160652-cvcgqllfgml48uq4
parent: john at arbash-meinel.com-20091007155725-vq1jsr92sk1vmidx
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-static-tuple
timestamp: Wed 2009-10-07 11:06:52 -0500
message:
  Change the pure-python version of StaticTuple
  
  wrather than 'has_a(tuple)' it now 'is_a(tuple)'. Making use of
  inheritance to avoid wasting memory on having 2 objects.
-------------- next part --------------
=== modified file 'bzrlib/_static_tuple_py.py'
--- a/bzrlib/_static_tuple_py.py	2009-10-01 19:06:35 +0000
+++ b/bzrlib/_static_tuple_py.py	2009-10-07 16:06:52 +0000
@@ -21,50 +21,32 @@
 """
 
 
-class StaticTuple(object):
+class StaticTuple(tuple):
     """A static type, similar to a tuple of strings."""
 
-    __slots__ = ('_tuple',)
-
     def __new__(cls, *args):
+        # Make the empty StaticTuple a singleton
         if not args and _empty_tuple is not None:
             return _empty_tuple
-        return object.__new__(cls)
+        return tuple.__new__(cls, args)
 
     def __init__(self, *args):
         """Create a new 'StaticTuple'"""
         for bit in args:
-            if not isinstance(bit, str) and not isinstance(bit, StaticTuple):
+            if type(bit) not in (str, StaticTuple):
                 raise TypeError('key bits must be strings or StaticTuple')
         num_keys = len(args)
         if num_keys < 0 or num_keys > 255:
             raise ValueError('must have 1 => 256 key bits')
-        self._tuple = args
-
-    def __repr__(self):
-        return repr(self._tuple)
-
-    def __hash__(self):
-        return hash(self._tuple)
-
-    def __eq__(self, other):
-        if isinstance(other, StaticTuple):
-            return self._tuple == other._tuple
-        if isinstance(other, tuple):
-            return other == self._tuple
-        return NotImplemented
-
-    def __len__(self):
-        return len(self._tuple)
-
-    def __cmp__(self, other):
-        return cmp(self._tuple, other)
-
-    def __getitem__(self, idx):
-        return self._tuple[idx]
+        # We don't need to pass args to tuple.__init__, because that was
+        # already handled in __new__.
+        tuple.__init__(self)
+
+    # def __repr__(self):
+    #     return tuple.__repr__(self)
 
     def as_tuple(self):
-        return self._tuple
+        return self
 
     def intern(self):
         return _interned_tuples.setdefault(self, self)

=== modified file 'bzrlib/tests/test__static_tuple.py'
--- a/bzrlib/tests/test__static_tuple.py	2009-10-07 15:27:24 +0000
+++ b/bzrlib/tests/test__static_tuple.py	2009-10-07 16:06:52 +0000
@@ -273,37 +273,27 @@
         strs = ['foo', 'bar', 'baz', 'bing']
         k = self.module.StaticTuple(*strs)
         if self.module is _static_tuple_py:
-            # The python version references objects slightly different than the
-            # compiled version
-            self.assertEqual([k._tuple, _static_tuple_py.StaticTuple],
-                             scanner.get_referents(k))
-            self.assertEqual(sorted(strs),
-                             sorted(scanner.get_referents(k._tuple)))
+            refs = strs + [self.module.StaticTuple]
         else:
-            self.assertEqual(sorted(strs), sorted(scanner.get_referents(k)))
+            refs = strs
+        self.assertEqual(sorted(refs), sorted(scanner.get_referents(k)))
 
     def test_nested_referents(self):
         self.requireFeature(Meliae)
+        from meliae import scanner
+        strs = ['foo', 'bar', 'baz', 'bing']
+        k1 = self.module.StaticTuple(*strs[:2])
+        k2 = self.module.StaticTuple(*strs[2:])
+        k3 = self.module.StaticTuple(k1, k2)
+        refs = [k1, k2]
+        if self.module is _static_tuple_py:
+            refs.append(self.module.StaticTuple)
+        self.assertEqual(sorted(refs),
+                         sorted(scanner.get_referents(k3)))
 
     def test_empty_is_singleton(self):
-        self.requireFeature(Meliae)
-        from meliae import scanner
         key = self.module.StaticTuple()
         self.assertIs(key, self.module._empty_tuple)
-        strs = ['foo', 'bar', 'baz', 'bing']
-        k1 = self.module.StaticTuple(*strs[:2])
-        k2 = self.module.StaticTuple(*strs[2:])
-        k3 = self.module.StaticTuple(k1, k2)
-        if self.module is _static_tuple_py:
-            # The python version references objects slightly different than the
-            # compiled version
-            self.assertEqual([k3._tuple, _static_tuple_py.StaticTuple],
-                             scanner.get_referents(k3))
-            self.assertEqual(sorted([k1, k2]),
-                             sorted(scanner.get_referents(k3._tuple)))
-        else:
-            self.assertEqual(sorted([k1, k2]),
-                             sorted(scanner.get_referents(k3)))
 
     def test_intern(self):
         unique_str1 = 'unique str ' + osutils.rand_chars(20)



More information about the bazaar-commits mailing list