Rev 4768: Implement support for lots of types. in http://bazaar.launchpad.net/~jameinel/bzr/2.1-st-concat

John Arbash Meinel john at arbash-meinel.com
Wed Oct 21 06:02:51 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr/2.1-st-concat

------------------------------------------------------------
revno: 4768
revision-id: john at arbash-meinel.com-20091021050235-6m79io53pe18dd0m
parent: john at arbash-meinel.com-20091021044749-gaadnlcy92ybn0ke
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-st-concat
timestamp: Wed 2009-10-21 00:02:35 -0500
message:
  Implement support for lots of types.
  
  StaticTuple can now contain:
    StaticTuple, str, unicode, bool, int, long, and float types.
  I don't know if there are any other builtin types that we could
  safely hold. 'object()' is one possibility, though potentially
  we could mess that one up.
-------------- next part --------------
=== modified file 'bzrlib/_static_tuple_c.c'
--- a/bzrlib/_static_tuple_c.c	2009-10-21 04:32:57 +0000
+++ b/bzrlib/_static_tuple_c.c	2009-10-21 05:02:35 +0000
@@ -240,14 +240,20 @@
                 " should not have a NULL entry.");
             return 0;
         }
-        if (!PyString_CheckExact(obj)) {
-            if (!StaticTuple_CheckExact(obj)) {
-                PyErr_Format(PyExc_TypeError, "StaticTuple(...)"
-                    " requires that all items are strings or StaticTuples"
-                    " not %s.", Py_TYPE(obj)->tp_name);
-                return 0;
-            }
-        }
+        if (PyString_CheckExact(obj)
+            || StaticTuple_CheckExact(obj)
+            || obj == Py_None
+            || PyBool_Check(obj)
+            || PyInt_Check(obj)
+            || PyLong_Check(obj)
+            || PyFloat_Check(obj)
+            || PyUnicode_Check(obj)
+            ) continue;
+        PyErr_Format(PyExc_TypeError, "StaticTuple(...)"
+            " requires that all items are one of"
+            " str, StaticTuple, None, bool, int, long, float, or unicode"
+            " not %s.", Py_TYPE(obj)->tp_name);
+        return 0;
     }
     return 1;
 }
@@ -479,27 +485,14 @@
             /* Both are StaticTuple types, so recurse */
             result = StaticTuple_richcompare(v_obj, w_obj, Py_EQ);
         } else {
-            /* Not the same type, obviously they won't compare equal */
-            break;
+            /* Fall back to generic richcompare */
+            result = PyObject_RichCompare(v_obj, w_obj, Py_EQ);
         }
         if (result == NULL) {
             return NULL; /* There seems to be an error */
         }
-        if (result == Py_NotImplemented) {
-            Py_DECREF(result);
-            /* One side must have had a string and the other a StaticTuple.
-             * This clearly means that they are not equal.
-             */
-            if (op == Py_EQ) {
-                Py_INCREF(Py_False);
-                return Py_False;
-            }
-            result = PyObject_RichCompare(v_obj, w_obj, Py_EQ);
-        }
         if (result == Py_False) {
-            /* This entry is not identical
-             * Shortcut for Py_EQ
-             */
+            // This entry is not identical, Shortcut for Py_EQ
             if (op == Py_EQ) {
                 return result;
             }
@@ -553,8 +546,7 @@
         /* Both are StaticTuple types, so recurse */
         return StaticTuple_richcompare(v_obj, w_obj, op);
     } else {
-        Py_INCREF(Py_NotImplemented);
-        return Py_NotImplemented;
+        return PyObject_RichCompare(v_obj, w_obj, op);
     }
 }
 

=== modified file 'bzrlib/_static_tuple_py.py'
--- a/bzrlib/_static_tuple_py.py	2009-10-21 04:47:49 +0000
+++ b/bzrlib/_static_tuple_py.py	2009-10-21 05:02:35 +0000
@@ -33,11 +33,11 @@
     def __init__(self, *args):
         """Create a new 'StaticTuple'"""
         for bit in args:
-            if type(bit) not in (str, StaticTuple, unicode, int, float,
+            if type(bit) not in (str, StaticTuple, unicode, int, long, float,
                                  None.__class__, bool):
                 raise TypeError('StaticTuple can only point to'
-                    ' StaticTuple, str, unicode, int, float, bool, or None'
-                    ' not %s' % (type(bit),))
+                    ' StaticTuple, str, unicode, int, long, float, bool, or'
+                    ' None not %s' % (type(bit),))
         num_keys = len(args)
         if num_keys < 0 or num_keys > 255:
             raise ValueError('must have 1 => 256 key bits')

=== modified file 'bzrlib/tests/test__static_tuple.py'
--- a/bzrlib/tests/test__static_tuple.py	2009-10-21 04:47:49 +0000
+++ b/bzrlib/tests/test__static_tuple.py	2009-10-21 05:02:35 +0000
@@ -228,6 +228,9 @@
     def test_holds_int(self):
         k1 = self.module.StaticTuple(1)
 
+    def test_holds_long(self):
+        k1 = self.module.StaticTuple(2L**65)
+
     def test_holds_float(self):
         k1 = self.module.StaticTuple(1.2)
 
@@ -243,7 +246,8 @@
         self.assertCompareEqual(k1, k1)
         k2 = self.module.StaticTuple(k1, k1)
         self.assertCompareEqual(k2, k2)
-        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, True, k1)
+        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
+                                     k1)
         self.assertCompareEqual(k3, k3)
 
     def test_compare_equivalent_obj(self):
@@ -253,8 +257,10 @@
         k3 = self.module.StaticTuple(k1, k2)
         k4 = self.module.StaticTuple(k2, k1)
         self.assertCompareEqual(k1, k2)
-        k5 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, True, k1)
-        k6 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, True, k1)
+        k5 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
+                                     k1)
+        k6 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
+                                     k1)
         self.assertCompareEqual(k5, k6)
         k7 = self.module.StaticTuple(None)
         k8 = self.module.StaticTuple(None)
@@ -340,7 +346,8 @@
 
     def test_compare_different_types(self):
         k1 = self.module.StaticTuple('foo', 'bar')
-        k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, True, k1)
+        k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
+                                     k1)
         self.assertCompareNoRelation(k1, k2)
         k3 = self.module.StaticTuple('foo')
         self.assertCompareDifferent(k3, k1)
@@ -398,8 +405,9 @@
         as_tuple2 = (('foo', 'bar', 'baz', 'bing'),)
         self.assertEqual(hash(k2), hash(as_tuple2))
 
-        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, True, k)
-        as_tuple3 = ('foo', 1, None, u'\xb5', 1.2, True, k)
+        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
+                                     k)
+        as_tuple3 = ('foo', 1, None, u'\xb5', 1.2, 2**65, True, k)
         self.assertEqual(hash(as_tuple3), hash(k3))
 
     def test_slice(self):



More information about the bazaar-commits mailing list