summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Include/object.h1
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_collectionsmodule.c16
-rw-r--r--Objects/typeobject.c5
4 files changed, 20 insertions, 5 deletions
diff --git a/Include/object.h b/Include/object.h
index 7f3fdb0..ef45838 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -484,6 +484,7 @@ PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
PyObject *, PyObject *);
#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
+PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *);
PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, _Py_Identifier *);
PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
#endif
diff --git a/Misc/NEWS b/Misc/NEWS
index fa5d85a..91d4029 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -58,6 +58,9 @@ Library
integer (as in Python 2). Au_read and Au_write now correctly works with file
object if start file position is not a zero.
+- Issue #18594: The fast path for collections.Counter() was never taken
+ due to an over-restrictive type check.
+
- Issue #19053: ZipExtFile.read1() with non-zero argument no more returns empty
bytes until end of data.
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)
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 3ff42da..6826cf9 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -50,9 +50,6 @@ _Py_IDENTIFIER(__name__);
_Py_IDENTIFIER(__new__);
static PyObject *
-_PyType_LookupId(PyTypeObject *type, struct _Py_Identifier *name);
-
-static PyObject *
slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
unsigned int
@@ -2620,7 +2617,7 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
return res;
}
-static PyObject *
+PyObject *
_PyType_LookupId(PyTypeObject *type, struct _Py_Identifier *name)
{
PyObject *oname;