Rev 4682: Add sequence functions which gives us keys[0] and len(keys) in http://bazaar.launchpad.net/~jameinel/bzr/2.1-memory-consumption

John Arbash Meinel john at arbash-meinel.com
Tue Sep 8 22:18:12 BST 2009


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

------------------------------------------------------------
revno: 4682
revision-id: john at arbash-meinel.com-20090908211758-zc31c61v7jf142zs
parent: john at arbash-meinel.com-20090908210608-4fs0t0s43smepnzo
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-memory-consumption
timestamp: Tue 2009-09-08 16:17:58 -0500
message:
  Add sequence functions which gives us keys[0] and len(keys)
-------------- next part --------------
=== modified file 'bzrlib/_keys_type_c.c'
--- a/bzrlib/_keys_type_c.c	2009-09-08 21:06:08 +0000
+++ b/bzrlib/_keys_type_c.c	2009-09-08 21:17:58 +0000
@@ -129,18 +129,22 @@
 static char Keys_doc[] =
     "C implementation of a Keys structure";
 
+
+static Py_ssize_t
+Keys_length(Keys *k)
+{
+    return (Py_ssize_t)k->num_keys;
+}
+
+
 static PyObject *
-Keys_get_key(Keys *self, PyObject *args) {
-    long offset;
-    long start;
-    long i;
-    PyObject *tpl = NULL, *obj = NULL;
+Keys_item(Keys *self, Py_ssize_t offset)
+{
+    long start, i;
+    PyObject *tpl, *obj;
 
-    if (!PyArg_ParseTuple(args, "l", &offset)) {
-        return NULL;
-    }
-    if (offset >= self->num_keys) {
-        PyErr_SetString(PyExc_IndexError, "offset out of range");
+    if (offset < 0 || offset >= self->num_keys) {
+        PyErr_SetString(PyExc_IndexError, "Keys index out of range");
         return NULL;
     }
     tpl = PyTuple_New(self->key_width);
@@ -157,9 +161,23 @@
     return tpl;
 }
 
+
+static PyObject *
+Keys_get_key(Keys *self, PyObject *args) {
+    long offset;
+    PyObject *tpl = NULL, *obj = NULL;
+
+    /* We should use "n" to indicate Py_ssize_t, however 'l' is good enough,
+     * and 'n' doesn't exist in python 2.4.
+     */
+    if (!PyArg_ParseTuple(args, "l", &offset)) {
+        return NULL;
+    }
+    return Keys_item(self, offset);
+}
+
 static char Keys_get_key_doc[] = "get_keys(offset)";
 
-
 static PyMethodDef Keys_methods[] = {
     {"get_key",
      (PyCFunction)Keys_get_key,
@@ -168,6 +186,17 @@
     {NULL, NULL} /* sentinel */
 };
 
+static PySequenceMethods Keys_as_sequence = {
+	(lenfunc)Keys_length,			/* sq_length */
+	0,		                        /* sq_concat */
+	0,		                        /* sq_repeat */
+	(ssizeargfunc)Keys_item,		/* sq_item */
+	0,		                        /* sq_slice */
+	0,					            /* sq_ass_item */
+	0,					            /* sq_ass_slice */
+	0,                              /* sq_contains */
+};
+
 static PyTypeObject KeysType = {
     PyObject_HEAD_INIT(NULL)
     0,                                           /* ob_size */
@@ -181,7 +210,7 @@
     0,                                           /* tp_compare */
     0,                                           /* tp_repr */
     0,                                           /* tp_as_number */
-    0,                                           /* tp_as_sequence */
+    &Keys_as_sequence,                            /* tp_as_sequence */
     0,                                           /* tp_as_mapping */
     0,                                           /* tp_hash */
     0,                                           /* tp_call */

=== modified file 'bzrlib/tests/test__keys_type.py'
--- a/bzrlib/tests/test__keys_type.py	2009-09-08 21:06:08 +0000
+++ b/bzrlib/tests/test__keys_type.py	2009-09-08 21:17:58 +0000
@@ -96,9 +96,14 @@
         self.assertEqual(n_refs + 1, sys.getrefcount(f))
         del f_key
         self.assertEqual(n_refs, sys.getrefcount(f))
+        self.assertEqual(2, len(k))
 
     def test_get_wide_key(self):
         k = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing')
         self.assertEqual(('foo', 'bar'), k.get_key(0))
         self.assertEqual(('baz', 'bing'), k.get_key(1))
         self.assertRaises(IndexError, k.get_key, 2)
+        self.assertEqual(('foo', 'bar'), k[0])
+        self.assertEqual(('baz', 'bing'), k[1])
+        self.assertRaises(IndexError, k.__getitem__, 2)
+        self.assertEqual(2, len(k))



More information about the bazaar-commits mailing list