@@ -1689,10 +1689,16 @@ Count elements in the iterable, updating the mappping");
16891689static PyObject *
16901690_count_elements (PyObject * self , PyObject * args )
16911691{
1692+ _Py_IDENTIFIER (__getitem__ );
1693+ _Py_IDENTIFIER (__setitem__ );
16921694 PyObject * it , * iterable , * mapping , * oldval ;
16931695 PyObject * newval = NULL ;
16941696 PyObject * key = NULL ;
16951697 PyObject * one = NULL ;
1698+ PyObject * mapping_getitem ;
1699+ PyObject * mapping_setitem ;
1700+ PyObject * dict_getitem ;
1701+ PyObject * dict_setitem ;
16961702
16971703 if (!PyArg_UnpackTuple (args , "_count_elements" , 2 , 2 , & mapping , & iterable ))
16981704 return NULL ;
@@ -1707,7 +1713,15 @@ _count_elements(PyObject *self, PyObject *args)
17071713 return NULL ;
17081714 }
17091715
1710- if (PyDict_CheckExact (mapping )) {
1716+ mapping_getitem = _PyType_LookupId (Py_TYPE (mapping ), & PyId___getitem__ );
1717+ dict_getitem = _PyType_LookupId (& PyDict_Type , & PyId___getitem__ );
1718+ mapping_setitem = _PyType_LookupId (Py_TYPE (mapping ), & PyId___setitem__ );
1719+ dict_setitem = _PyType_LookupId (& PyDict_Type , & PyId___setitem__ );
1720+
1721+ if (mapping_getitem != NULL &&
1722+ mapping_getitem == dict_getitem &&
1723+ mapping_setitem != NULL &&
1724+ mapping_setitem == dict_setitem ) {
17111725 while (1 ) {
17121726 key = PyIter_Next (it );
17131727 if (key == NULL )
0 commit comments