Rev 4774: (Matt Nordhoff, in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Tue Oct 27 16:54:14 GMT 2009


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 4774 [merge]
revision-id: pqm at pqm.ubuntu.com-20091027165411-s996a4rrdm66jq99
parent: pqm at pqm.ubuntu.com-20091027155759-zjw6vnvs7fyk0lch
parent: john at arbash-meinel.com-20091027140716-ggors1mphschb5yo
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2009-10-27 16:54:11 +0000
message:
  (Matt Nordhoff,
  	jam) Allow StaticTuple.from_sequence() to support iterables.
modified:
  bzrlib/_static_tuple_c.c       _keys_type_c.c-20090908204220-aa346ccw4l37jzt7-1
  bzrlib/tests/test__static_tuple.py test__keys_type.py-20090908204220-aa346ccw4l37jzt7-2
=== modified file 'bzrlib/_static_tuple_c.c'
--- a/bzrlib/_static_tuple_c.c	2009-10-26 15:59:54 +0000
+++ b/bzrlib/_static_tuple_c.c	2009-10-27 14:07:16 +0000
@@ -183,7 +183,8 @@
 static StaticTuple *
 StaticTuple_FromSequence(PyObject *sequence)
 {
-    StaticTuple *new;
+    StaticTuple *new = NULL;
+    PyObject *as_tuple = NULL;
     PyObject *item;
     Py_ssize_t i, size;
 
@@ -192,16 +193,18 @@
         return (StaticTuple *)sequence;
     }
     if (!PySequence_Check(sequence)) {
-        PyErr_Format(PyExc_TypeError, "Type %s is not a sequence type",
-                     Py_TYPE(sequence)->tp_name);
-        return NULL;
+        as_tuple = PySequence_Tuple(sequence);
+        if (as_tuple == NULL)
+            goto done;
+        sequence = as_tuple;
     }
     size = PySequence_Size(sequence);
-    if (size == -1)
-        return NULL;
+    if (size == -1) {
+        goto done;
+    }
     new = StaticTuple_New(size);
     if (new == NULL) {
-        return NULL;
+        goto done;
     }
     for (i = 0; i < size; ++i) {
         // This returns a new reference, which we then 'steal' with 
@@ -209,10 +212,13 @@
         item = PySequence_GetItem(sequence, i);
         if (item == NULL) {
             Py_DECREF(new);
-            return NULL;
+            new = NULL;
+            goto done;
         }
         StaticTuple_SET_ITEM(new, i, item);
     }
+done:
+    Py_XDECREF(as_tuple);
     return (StaticTuple *)new;
 }
 

=== modified file 'bzrlib/tests/test__static_tuple.py'
--- a/bzrlib/tests/test__static_tuple.py	2009-10-21 17:54:33 +0000
+++ b/bzrlib/tests/test__static_tuple.py	2009-10-27 14:07:16 +0000
@@ -571,6 +571,8 @@
     def test_from_sequence_not_sequence(self):
         self.assertRaises(TypeError,
                           self.module.StaticTuple.from_sequence, object())
+        self.assertRaises(TypeError,
+                          self.module.StaticTuple.from_sequence, 10)
 
     def test_from_sequence_incorrect_args(self):
         self.assertRaises(TypeError,
@@ -578,6 +580,19 @@
         self.assertRaises(TypeError,
                           self.module.StaticTuple.from_sequence, foo='a')
 
+    def test_from_sequence_iterable(self):
+        st = self.module.StaticTuple.from_sequence(iter(['foo', 'bar']))
+        self.assertIsInstance(st, self.module.StaticTuple)
+        self.assertEqual(('foo', 'bar'), st)
+
+    def test_from_sequence_generator(self):
+        def generate_tuple():
+            yield 'foo'
+            yield 'bar'
+        st = self.module.StaticTuple.from_sequence(generate_tuple())
+        self.assertIsInstance(st, self.module.StaticTuple)
+        self.assertEqual(('foo', 'bar'), st)
+
     def test_pickle(self):
         st = self.module.StaticTuple('foo', 'bar')
         pickled = cPickle.dumps(st)




More information about the bazaar-commits mailing list