Rev 4687: Implement an as_tuples() function, and use it to implement repr(). in http://bazaar.launchpad.net/~jameinel/bzr/2.1-memory-consumption

John Arbash Meinel john at arbash-meinel.com
Wed Sep 9 16:20:52 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr/2.1-memory-consumption

------------------------------------------------------------
revno: 4687
revision-id: john at arbash-meinel.com-20090909152038-j5ggmryfyznohcas
parent: john at arbash-meinel.com-20090908214716-mvi8eze2i6nf8bul
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-memory-consumption
timestamp: Wed 2009-09-09 10:20:38 -0500
message:
  Implement an as_tuples() function, and use it to implement repr().
-------------- next part --------------
=== modified file 'bzrlib/_keys_type_c.c'
--- a/bzrlib/_keys_type_c.c	2009-09-08 21:47:16 +0000
+++ b/bzrlib/_keys_type_c.c	2009-09-09 15:20:38 +0000
@@ -40,6 +40,7 @@
 
 /* Forward declaration */
 extern PyTypeObject KeysType;
+static PyObject *Keys_item(Keys *self, Py_ssize_t offset);
 
 static void
 Keys_dealloc(Keys *keys)
@@ -130,6 +131,46 @@
     return (PyObject *)self;
 }
 
+static PyObject *
+Keys_as_tuples(Keys *self)
+{
+    PyObject *as_tuple = NULL;
+    PyObject *a_key = NULL;
+    Py_ssize_t i;
+
+    as_tuple = PyTuple_New(self->ob_size);
+    if (as_tuple == NULL) {
+        return NULL;
+    }
+    for (i = 0; i < self->ob_size; ++i) {
+        a_key = Keys_item(self, i);
+        if (a_key == NULL) {
+            goto Err;
+        }
+        PyTuple_SET_ITEM(as_tuple, i, a_key);
+    }
+    return as_tuple;
+Err:
+    Py_DECREF(as_tuple);
+    return NULL;
+}
+
+static PyObject *
+Keys_repr(Keys *self)
+{
+    PyObject *as_tpl;
+    PyObject *result;
+
+    as_tpl = Keys_as_tuples(self);
+    if (as_tpl == NULL) {
+        return NULL;
+    }
+    result = PyObject_Repr(as_tpl);
+Done:
+    Py_DECREF(as_tpl);
+    return result;
+}
+
 static char Keys_doc[] =
     "C implementation of a Keys structure."
     "\n This is used as Keys(width, key_bit_1, key_bit_2, key_bit_3, ...)"
@@ -176,6 +217,7 @@
 static char Keys_get_key_doc[] = "get_keys(offset)";
 
 static PyMethodDef Keys_methods[] = {
+    {"as_tuples", (PyCFunction)Keys_as_tuples, METH_VARARGS},
     {NULL, NULL} /* sentinel */
 };
 
@@ -202,7 +244,7 @@
     0,                                           /* tp_setattr */
     0,                                           /* tp_compare */
     // TODO: implement repr() and possibly str()
-    0,                                           /* tp_repr */
+    Keys_repr,                                           /* tp_repr */
     0,                                           /* tp_as_number */
     &Keys_as_sequence,                           /* tp_as_sequence */
     0,                                           /* tp_as_mapping */

=== modified file 'bzrlib/tests/test__keys_type.py'
--- a/bzrlib/tests/test__keys_type.py	2009-09-08 21:35:01 +0000
+++ b/bzrlib/tests/test__keys_type.py	2009-09-09 15:20:38 +0000
@@ -108,3 +108,15 @@
         self.assertEqual(('baz', 'bing'), k[1])
         self.assertRaises(IndexError, k.__getitem__, 2)
         self.assertEqual(2, len(k))
+
+    def test_as_tuple(self):
+        k = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing')
+        if getattr(k, 'as_tuples', None) is not None:
+            t = k.as_tuples()
+        else:
+            t = k # The pure-python form is in tuples already
+        self.assertEqual((('foo', 'bar'), ('baz', 'bing')), t)
+
+    def test_repr(self):
+        k = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing')
+        self.assertEqual("(('foo', 'bar'), ('baz', 'bing'))", repr(k))



More information about the bazaar-commits mailing list