[Python-3000-checkins] r53723 - in python/branches/p3yk: Lib/test/test_dictviews.py Objects/dictobject.c
guido.van.rossum
python-3000-checkins at python.org
Sat Feb 10 05:54:20 CET 2007
Author: guido.van.rossum
Date: Sat Feb 10 05:54:19 2007
New Revision: 53723
Modified:
python/branches/p3yk/Lib/test/test_dictviews.py
python/branches/p3yk/Objects/dictobject.c
Log:
Endow dict views with a proper length method.
Modified: python/branches/p3yk/Lib/test/test_dictviews.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_dictviews.py (original)
+++ python/branches/p3yk/Lib/test/test_dictviews.py Sat Feb 10 05:54:19 2007
@@ -7,16 +7,19 @@
d = {1: 10, "a": "ABC"}
keys = d.KEYS()
self.assertEqual(set(keys), {1, "a"})
+ self.assertEqual(len(keys), 2)
def test_dict_items(self):
d = {1: 10, "a": "ABC"}
items = d.ITEMS()
self.assertEqual(set(items), {(1, 10), ("a", "ABC")})
+ self.assertEqual(len(items), 2)
def test_dict_values(self):
d = {1: 10, "a": "ABC"}
values = d.VALUES()
self.assertEqual(set(values), {10, "ABC"})
+ self.assertEqual(len(values), 2)
def test_main():
test_support.run_unittest(DictSetTest)
Modified: python/branches/p3yk/Objects/dictobject.c
==============================================================================
--- python/branches/p3yk/Objects/dictobject.c (original)
+++ python/branches/p3yk/Objects/dictobject.c Sat Feb 10 05:54:19 2007
@@ -2354,13 +2354,13 @@
PyObject_Del(ds);
}
-static PyObject *
-dictview_length_hint(dictviewobject *ds)
+static Py_ssize_t
+dictview_len(dictviewobject *ds)
{
Py_ssize_t len = 0;
if (ds->ds_dict != NULL)
len = ds->ds_dict->ma_used;
- return PyInt_FromSize_t(len);
+ return len;
}
static PyObject *
@@ -2397,9 +2397,18 @@
return dictiter_new(ds->ds_dict, &PyDictIterKey_Type);
}
+static PySequenceMethods dictkeys_as_sequence = {
+ (lenfunc)dictview_len, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ (objobjproc)0, /* sq_contains */
+};
+
static PyMethodDef dictkeys_methods[] = {
- {"__length_hint__", (PyCFunction)dictview_length_hint, METH_NOARGS,
- length_hint_doc},
{NULL, NULL} /* sentinel */
};
@@ -2417,7 +2426,7 @@
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
- 0, /* tp_as_sequence */
+ &dictkeys_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
@@ -2454,9 +2463,18 @@
return dictiter_new(ds->ds_dict, &PyDictIterItem_Type);
}
+static PySequenceMethods dictitems_as_sequence = {
+ (lenfunc)dictview_len, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ (objobjproc)0, /* sq_contains */
+};
+
static PyMethodDef dictitems_methods[] = {
- {"__length_hint__", (PyCFunction)dictview_length_hint, METH_NOARGS,
- length_hint_doc},
{NULL, NULL} /* sentinel */
};
@@ -2474,7 +2492,7 @@
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
- 0, /* tp_as_sequence */
+ &dictitems_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
@@ -2511,9 +2529,18 @@
return dictiter_new(ds->ds_dict, &PyDictIterValue_Type);
}
+static PySequenceMethods dictvalues_as_sequence = {
+ (lenfunc)dictview_len, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ (objobjproc)0, /* sq_contains */
+};
+
static PyMethodDef dictvalues_methods[] = {
- {"__length_hint__", (PyCFunction)dictview_length_hint, METH_NOARGS,
- length_hint_doc},
{NULL, NULL} /* sentinel */
};
@@ -2531,7 +2558,7 @@
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
- 0, /* tp_as_sequence */
+ &dictvalues_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
More information about the Python-3000-checkins
mailing list