diff options
author | Raymond Hettinger <python@rcn.com> | 2013-10-01 08:00:59 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2013-10-01 08:00:59 (GMT) |
commit | c13516b0a0fd3142b29e1f354c2080cd52948213 (patch) | |
tree | c457578d34b0dc1edf54535d83244b1636b3b87a /Modules | |
parent | 65870835a006ad613b111fe4579bd7e3354585b7 (diff) | |
parent | 2ff2190b6293966f76633d810dfbe2d232ff5973 (diff) | |
download | cpython-c13516b0a0fd3142b29e1f354c2080cd52948213.zip cpython-c13516b0a0fd3142b29e1f354c2080cd52948213.tar.gz cpython-c13516b0a0fd3142b29e1f354c2080cd52948213.tar.bz2 |
merge
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_collectionsmodule.c | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index e5dfdb4..113abc9 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -1763,10 +1763,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; @@ -1781,7 +1787,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) |