Rev 4773: Clean up the C code a bit, using a goto. in http://bazaar.launchpad.net/~jameinel/bzr/2.1-st-from-iterable

John Arbash Meinel john at arbash-meinel.com
Tue Oct 27 14:07:27 GMT 2009


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

------------------------------------------------------------
revno: 4773
revision-id: john at arbash-meinel.com-20091027140716-ggors1mphschb5yo
parent: mnordhoff at mattnordhoff.com-20091027092531-q0lygzntui81p7qn
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-st-from-iterable
timestamp: Tue 2009-10-27 09:07:16 -0500
message:
  Clean up the C code a bit, using a goto.
-------------- next part --------------
=== modified file 'bzrlib/_static_tuple_c.c'
--- a/bzrlib/_static_tuple_c.c	2009-10-27 09:25:31 +0000
+++ b/bzrlib/_static_tuple_c.c	2009-10-27 14:07:16 +0000
@@ -183,7 +183,7 @@
 static StaticTuple *
 StaticTuple_FromSequence(PyObject *sequence)
 {
-    StaticTuple *new;
+    StaticTuple *new = NULL;
     PyObject *as_tuple = NULL;
     PyObject *item;
     Py_ssize_t i, size;
@@ -194,17 +194,17 @@
     }
     if (!PySequence_Check(sequence)) {
         as_tuple = PySequence_Tuple(sequence);
+        if (as_tuple == NULL)
+            goto done;
         sequence = as_tuple;
     }
     size = PySequence_Size(sequence);
     if (size == -1) {
-        Py_XDECREF(as_tuple);
-        return NULL;
+        goto done;
     }
     new = StaticTuple_New(size);
     if (new == NULL) {
-        Py_XDECREF(as_tuple);
-        return NULL;
+        goto done;
     }
     for (i = 0; i < size; ++i) {
         // This returns a new reference, which we then 'steal' with 
@@ -212,11 +212,12 @@
         item = PySequence_GetItem(sequence, i);
         if (item == NULL) {
             Py_DECREF(new);
-            Py_XDECREF(as_tuple);
-            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-27 09:25:31 +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,
@@ -579,7 +581,15 @@
                           self.module.StaticTuple.from_sequence, foo='a')
 
     def test_from_sequence_iterable(self):
-        st = self.module.StaticTuple.from_sequence(iter(('foo', 'bar')))
+        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)
 



More information about the bazaar-commits mailing list