[Zodb-checkins] SVN: ZODB/branches/alienoid-btrees_setdefault/src/BTrees/ Added setdefault method implementation and basic unit tests.

Ruslan Spivak rspivak at nuxeo.com
Sat Aug 27 20:01:08 EDT 2005


Log message for revision 38122:
  Added setdefault method implementation and basic unit tests.

Changed:
  U   ZODB/branches/alienoid-btrees_setdefault/src/BTrees/BTreeTemplate.c
  U   ZODB/branches/alienoid-btrees_setdefault/src/BTrees/tests/testBTrees.py

-=-
Modified: ZODB/branches/alienoid-btrees_setdefault/src/BTrees/BTreeTemplate.c
===================================================================
--- ZODB/branches/alienoid-btrees_setdefault/src/BTrees/BTreeTemplate.c	2005-08-27 22:17:18 UTC (rev 38121)
+++ ZODB/branches/alienoid-btrees_setdefault/src/BTrees/BTreeTemplate.c	2005-08-28 00:01:08 UTC (rev 38122)
@@ -1700,6 +1700,29 @@
     return _BTree_get(self, key, 1);
 }
 
+static PyObject *
+BTree_setdefault(BTree *self, PyObject *args)
+{
+        PyObject *key, *d = Py_None, *r;
+
+        UNLESS (PyArg_ParseTuple(args, "O|O", &key, &d)) return NULL;
+        if ((r = _BTree_get(self, key, 0)))
+                return r;
+        UNLESS (PyErr_ExceptionMatches(PyExc_KeyError)) return NULL;
+        PyErr_Clear();
+
+        Py_INCREF(d);
+        if (d == Py_None)
+                return d;
+
+        if (BTree_setitem(self, key, d) < 0) {
+                Py_DECREF(d);
+                return NULL;
+        }
+        return d;
+}
+
+
 /* Search BTree self for key.  This is the sq_contains slot of the
  * PySequenceMethods.
  *
@@ -1838,6 +1861,12 @@
      "get(key[, default=None]) -> Value for key or default\n\n"
      "Return the value or the default if the key is not found."},
 
+    {"setdefault", (PyCFunction) BTree_setdefault, METH_VARARGS,
+     "D.setdefault(k[, x]) -> D.get(k, x), also set D[k] = x if k not in D\n\n"
+     "Return the value like get() except that if key is missing, x is both\n"
+     "returned and inserted into the dictionary as the value of k.\n"
+     "x defaults to None."},
+
     {"maxKey", (PyCFunction) BTree_maxKey,	METH_VARARGS,
      "maxKey([max]) -> key\n\n"
      "Return the largest key in the BTree.  If max is specified, return\n"

Modified: ZODB/branches/alienoid-btrees_setdefault/src/BTrees/tests/testBTrees.py
===================================================================
--- ZODB/branches/alienoid-btrees_setdefault/src/BTrees/tests/testBTrees.py	2005-08-27 22:17:18 UTC (rev 38121)
+++ ZODB/branches/alienoid-btrees_setdefault/src/BTrees/tests/testBTrees.py	2005-08-28 00:01:08 UTC (rev 38122)
@@ -1090,6 +1090,14 @@
                                               "changed size")
                 break
 
+    def testSetdefault(self):
+        t = self.t
+        self.assert_(t.setdefault(1) is None)
+        self.assertEqual(t.setdefault(1, 5), 5)
+        self.assert_(t[1] == 5)
+        self.assertEqual(t.setdefault(1, 3), 5)
+
+
 # tests of various type errors
 
 class TypeTest(TestCase):



More information about the Zodb-checkins mailing list