Issue #18594: Fix the fast path for collections.Counter().

The path wasn't being taken due to an over-restrictive type check.
This commit is contained in:
Raymond Hettinger 2013-10-01 00:55:43 -07:00
parent 21b2933456
commit 2ff2190b62
4 changed files with 20 additions and 5 deletions

View file

@ -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)