[Zodb-checkins]
SVN: ZODB/branches/alienoid-btrees_setdefault/src/BTrees/
Implement setdefault() for buckets.
Tim Peters
tim.one at comcast.net
Mon Aug 29 17:13:42 EDT 2005
Log message for revision 38150:
Implement setdefault() for buckets.
Minor docstring fiddling.
Remove the XXX on testSetdefault() (which noted that
the test failed for the 5 bucket types -- those tests
pass now).
Changed:
U ZODB/branches/alienoid-btrees_setdefault/src/BTrees/BTreeTemplate.c
U ZODB/branches/alienoid-btrees_setdefault/src/BTrees/BucketTemplate.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-29 20:43:38 UTC (rev 38149)
+++ ZODB/branches/alienoid-btrees_setdefault/src/BTrees/BTreeTemplate.c 2005-08-29 21:13:42 UTC (rev 38150)
@@ -1871,7 +1871,7 @@
{"setdefault", (PyCFunction) BTree_setdefault, METH_VARARGS,
"D.setdefault(k, d) -> D.get(k, d), also set D[k]=d if k not in D.\n\n"
"Return the value like get() except that if key is missing, d is both\n"
- "returned and inserted into the dictionary as the value of k."},
+ "returned and inserted into the BTree as the value of k."},
{"maxKey", (PyCFunction) BTree_maxKey, METH_VARARGS,
"maxKey([max]) -> key\n\n"
Modified: ZODB/branches/alienoid-btrees_setdefault/src/BTrees/BucketTemplate.c
===================================================================
--- ZODB/branches/alienoid-btrees_setdefault/src/BTrees/BucketTemplate.c 2005-08-29 20:43:38 UTC (rev 38149)
+++ ZODB/branches/alienoid-btrees_setdefault/src/BTrees/BucketTemplate.c 2005-08-29 21:13:42 UTC (rev 38150)
@@ -1257,7 +1257,36 @@
return _bucket_get(self, key, 1);
}
+static PyObject *
+bucket_setdefault(Bucket *self, PyObject *args)
+{
+ PyObject *key;
+ PyObject *failobj; /* default */
+ PyObject *value; /* return value */
+ int dummy_changed; /* in order to call _bucket_set */
+ if (! PyArg_UnpackTuple(args, "setdefault", 2, 2, &key, &failobj))
+ return NULL;
+
+ value = _bucket_get(self, key, 0);
+ if (value != NULL)
+ return value;
+
+ /* The key isn't in the bucket. If that's not due to a KeyError exception,
+ * pass back the unexpected exception.
+ */
+ if (! PyErr_ExceptionMatches(PyExc_KeyError))
+ return NULL;
+ PyErr_Clear();
+
+ /* Associate `key` with `failobj` in the bucket, and return `failobj`. */
+ value = failobj;
+ if (_bucket_set(self, key, failobj, 0, 0, &dummy_changed) < 0)
+ value = NULL;
+ Py_XINCREF(value);
+ return value;
+}
+
/* Search bucket self for key. This is the sq_contains slot of the
* PySequenceMethods.
*
@@ -1481,6 +1510,11 @@
"get(key[,default]) -- Look up a value\n\n"
"Return the default (or None) if the key is not found."},
+ {"setdefault", (PyCFunction) bucket_setdefault, METH_VARARGS,
+ "D.setdefault(k, d) -> D.get(k, d), also set D[k]=d if k not in D.\n\n"
+ "Return the value like get() except that if key is missing, d is both\n"
+ "returned and inserted into the bucket as the value of k."},
+
{"iterkeys", (PyCFunction) Bucket_iterkeys, METH_KEYWORDS,
"B.iterkeys([min[,max]]) -> an iterator over the keys of B"},
Modified: ZODB/branches/alienoid-btrees_setdefault/src/BTrees/tests/testBTrees.py
===================================================================
--- ZODB/branches/alienoid-btrees_setdefault/src/BTrees/tests/testBTrees.py 2005-08-29 20:43:38 UTC (rev 38149)
+++ ZODB/branches/alienoid-btrees_setdefault/src/BTrees/tests/testBTrees.py 2005-08-29 21:13:42 UTC (rev 38150)
@@ -611,8 +611,6 @@
excludemax=True)),
f([1]))
- # XXX This test fails for all bucket types, since they haven't
- # XXX implented setdefault yet.
def testSetdefault(self):
t = self.t
More information about the Zodb-checkins
mailing list