summaryrefslogtreecommitdiffstats
path: root/Modules/_collectionsmodule.c
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2013-10-01 07:55:43 (GMT)
committerRaymond Hettinger <python@rcn.com>2013-10-01 07:55:43 (GMT)
commit2ff2190b6293966f76633d810dfbe2d232ff5973 (patch)
tree8eac2140c5decc70779e02017262149039ccd5d3 /Modules/_collectionsmodule.c
parent21b2933456fc3ce2d1594d704671f4fc298ca37c (diff)
downloadcpython-2ff2190b6293966f76633d810dfbe2d232ff5973.zip
cpython-2ff2190b6293966f76633d810dfbe2d232ff5973.tar.gz
cpython-2ff2190b6293966f76633d810dfbe2d232ff5973.tar.bz2
Issue #18594: Fix the fast path for collections.Counter().
The path wasn't being taken due to an over-restrictive type check.
Diffstat (limited to 'Modules/_collectionsmodule.c')
-rw-r--r--Modules/_collectionsmodule.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 34a1a90..cb16748 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -1689,10 +1689,16 @@ Count elements in the iterable, updating the mappping");
static PyObject *
_count_elements(PyObject *self, PyObject *args)
{
+ _Py_IDENTIFIER(__getitem__);
+ _Py_IDENTIFIER(__setitem__);
PyObject *it, *iterable, *mapping, *oldval;
PyObject *newval = NULL;
PyObject *key = NULL;
PyObject *one = NULL;
+ PyObject *mapping_getitem;
+ PyObject *mapping_setitem;
+ PyObject *dict_getitem;
+ PyObject *dict_setitem;
if (!PyArg_UnpackTuple(args, "_count_elements", 2, 2, &mapping, &iterable))
return NULL;
@@ -1707,7 +1713,15 @@ _count_elements(PyObject *self, PyObject *args)
return NULL;
}
- if (PyDict_CheckExact(mapping)) {
+ mapping_getitem = _PyType_LookupId(Py_TYPE(mapping), &PyId___getitem__);
+ dict_getitem = _PyType_LookupId(&PyDict_Type, &PyId___getitem__);
+ mapping_setitem = _PyType_LookupId(Py_TYPE(mapping), &PyId___setitem__);
+ dict_setitem = _PyType_LookupId(&PyDict_Type, &PyId___setitem__);
+
+ if (mapping_getitem != NULL &&
+ mapping_getitem == dict_getitem &&
+ mapping_setitem != NULL &&
+ mapping_setitem == dict_setitem) {
while (1) {
key = PyIter_Next(it);
if (key == NULL)