[Zodb-checkins]
SVN: ZODB/branches/alienoid-btrees_setdefault/src/BTrees/
Changed setdefault to require both arguments (Python's
Tim Peters
tim.one at comcast.net
Mon Aug 29 16:43:38 EDT 2005
Log message for revision 38149:
Changed setdefault to require both arguments (Python's
default of None for the ``default`` argument doesn't
make sense for II, OI, or IF trees; it isn't really
_useful_ for IO or OO trees either!).
Rewrote testSetdefault, and moved to the MappingBase
tests. Five tests currently fail, one for each flavor
of Bucket, because there's not yet a setdefault()
implementation for Buckets.
Changed:
U ZODB/branches/alienoid-btrees_setdefault/src/BTrees/BTreeTemplate.c
U ZODB/branches/alienoid-btrees_setdefault/src/BTrees/Interfaces.py
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:42:08 UTC (rev 38148)
+++ ZODB/branches/alienoid-btrees_setdefault/src/BTrees/BTreeTemplate.c 2005-08-29 20:43:38 UTC (rev 38149)
@@ -1704,10 +1704,10 @@
BTree_setdefault(BTree *self, PyObject *args)
{
PyObject *key;
- PyObject *failobj = Py_None; /* default */
- PyObject *value; /* return value */
+ PyObject *failobj; /* default */
+ PyObject *value; /* return value */
- if (! PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
+ if (! PyArg_UnpackTuple(args, "setdefault", 2, 2, &key, &failobj))
return NULL;
value = _BTree_get(self, key, 0);
@@ -1869,10 +1869,9 @@
"Return the value or the default if the key is not found."},
{"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"
+ "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.\n"
- "d defaults to None."},
+ "returned and inserted into the dictionary as the value of k."},
{"maxKey", (PyCFunction) BTree_maxKey, METH_VARARGS,
"maxKey([max]) -> key\n\n"
Modified: ZODB/branches/alienoid-btrees_setdefault/src/BTrees/Interfaces.py
===================================================================
--- ZODB/branches/alienoid-btrees_setdefault/src/BTrees/Interfaces.py 2005-08-29 20:42:08 UTC (rev 38148)
+++ ZODB/branches/alienoid-btrees_setdefault/src/BTrees/Interfaces.py 2005-08-29 20:43:38 UTC (rev 38149)
@@ -215,11 +215,15 @@
"""
def setdefault(key, d):
- """D.setdefault(k[, d]) -> D.get(k, d), also set D[k]=d if k not in D
+ """D.setdefault(k, d) -> D.get(k, d), also set D[k]=d if k not in D.
Return the value like get() except that if key is missing, d is both
returned and inserted into the dictionary as the value of k.
- d defaults to None.
+
+ Note that, unlike as for Python's dict.setdefault(), d is not
+ optional. Python defaults d to None, but that doesn't make sense
+ for mappings that can't have None as a value (for example, an
+ IIBTree can have only integers as values).
"""
class IBTree(IDictionaryIsh):
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:42:08 UTC (rev 38148)
+++ ZODB/branches/alienoid-btrees_setdefault/src/BTrees/tests/testBTrees.py 2005-08-29 20:43:38 UTC (rev 38149)
@@ -611,6 +611,25 @@
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
+
+ self.assertEqual(t.setdefault(1, 2), 2)
+ # That should also have associated 1 with 2 in the tree.
+ self.assert_(1 in t)
+ self.assertEqual(t[1], 2)
+ # And trying to change it again should have no effect.
+ self.assertEqual(t.setdefault(1, 666), 2)
+ self.assertEqual(t[1], 2)
+
+ # Not enough arguments.
+ self.assertRaises(TypeError, t.setdefault)
+ self.assertRaises(TypeError, t.setdefault, 1)
+ # Too many arguments.
+ self.assertRaises(TypeError, t.setdefault, 1, 2, 3)
+
class NormalSetTests(Base):
""" Test common to all set types """
@@ -1090,23 +1109,6 @@
"changed size")
break
- def testSetdefault(self):
- t = self.t
-
- # XXX This test fails for II, OI, and IF trees: they can't have
- # XXX None as a value.
- self.assert_(t.setdefault(1) is None)
- # That should also have associated 1 with None in the tree.
- self.assert_(t[1] is None)
- # And trying to change it again should have no effect.
- self.assert_(t.setdefault(1, 666) is None)
-
- # Same thing, but with an explicit default.
- self.assertEqual(t.setdefault(2, 5), 5)
- self.assertEqual(t[2], 5)
- self.assertEqual(t.setdefault(2, 666), 5)
-
-
# tests of various type errors
class TypeTest(TestCase):
More information about the Zodb-checkins
mailing list