From 5b8e41a5b7d813b1d17a7cfcc249aba0cdb59ff5 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 5 Jun 2019 11:46:07 -0700 Subject: [PATCH 1/3] bpo-37165: Convert _count_elements to the argument clinic --- Modules/_collectionsmodule.c | 32 +++++++++++++------------- Modules/clinic/_collectionsmodule.c.h | 33 ++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index dacea3a0243914..37e74e45cd7840 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -8,9 +8,10 @@ #endif /*[clinic input] +module _collections class _tuplegetter "_tuplegetterobject *" "&tuplegetter_type" [clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ee5ed5baabe35068]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a8ece4ccad7e30ac]*/ static PyTypeObject tuplegetter_type; #include "clinic/_collectionsmodule.c.h" @@ -2228,17 +2229,24 @@ static PyTypeObject defdict_type = { /* helper function for Counter *********************************************/ -PyDoc_STRVAR(_count_elements_doc, -"_count_elements(mapping, iterable) -> None\n\ -\n\ -Count elements in the iterable, updating the mapping"); +/*[clinic input] +_collections._count_elements + + mapping: object + iterable: object + / + +Count elements in the iterable, updating the mapping +[clinic start generated code]*/ static PyObject * -_count_elements(PyObject *self, PyObject *args) +_collections__count_elements_impl(PyObject *module, PyObject *mapping, + PyObject *iterable) +/*[clinic end generated code: output=7e0c1789636b3d8f input=e79fad04534a0b45]*/ { _Py_IDENTIFIER(get); _Py_IDENTIFIER(__setitem__); - PyObject *it, *iterable, *mapping, *oldval; + PyObject *it, *oldval; PyObject *newval = NULL; PyObject *key = NULL; PyObject *bound_get = NULL; @@ -2247,9 +2255,6 @@ _count_elements(PyObject *self, PyObject *args) PyObject *mapping_setitem; PyObject *dict_setitem; - if (!PyArg_UnpackTuple(args, "_count_elements", 2, 2, &mapping, &iterable)) - return NULL; - it = PyObject_GetIter(iterable); if (it == NULL) return NULL; @@ -2509,17 +2514,12 @@ PyDoc_STRVAR(module_doc, - defaultdict: dict subclass with a default value factory\n\ "); -static struct PyMethodDef module_functions[] = { - {"_count_elements", _count_elements, METH_VARARGS, _count_elements_doc}, - {NULL, NULL} /* sentinel */ -}; - static struct PyModuleDef _collectionsmodule = { PyModuleDef_HEAD_INIT, "_collections", module_doc, -1, - module_functions, + NULL, NULL, NULL, NULL, diff --git a/Modules/clinic/_collectionsmodule.c.h b/Modules/clinic/_collectionsmodule.c.h index ed3b1b50f9b582..c3ba1a6698571d 100644 --- a/Modules/clinic/_collectionsmodule.c.h +++ b/Modules/clinic/_collectionsmodule.c.h @@ -2,6 +2,37 @@ preserve [clinic start generated code]*/ +PyDoc_STRVAR(_collections__count_elements__doc__, +"_count_elements($module, mapping, iterable, /)\n" +"--\n" +"\n" +"Count elements in the iterable, updating the mapping"); + +#define _COLLECTIONS__COUNT_ELEMENTS_METHODDEF \ + {"_count_elements", (PyCFunction)(void(*)(void))_collections__count_elements, METH_FASTCALL, _collections__count_elements__doc__}, + +static PyObject * +_collections__count_elements_impl(PyObject *module, PyObject *mapping, + PyObject *iterable); + +static PyObject * +_collections__count_elements(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + PyObject *mapping; + PyObject *iterable; + + if (!_PyArg_CheckPositional("_count_elements", nargs, 2, 2)) { + goto exit; + } + mapping = args[0]; + iterable = args[1]; + return_value = _collections__count_elements_impl(module, mapping, iterable); + +exit: + return return_value; +} + static PyObject * tuplegetter_new_impl(PyTypeObject *type, Py_ssize_t index, PyObject *doc); @@ -42,4 +73,4 @@ tuplegetter_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=51bd572577ca7111 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=9d2bfcc9df5faf35 input=a9049054013a1b77]*/ From b216ad081a71f096e63ea010499342a88eaf1c80 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 5 Jun 2019 11:48:40 -0700 Subject: [PATCH 2/3] Add blurb --- .../NEWS.d/next/Library/2019-06-05-11-48-19.bpo-37165.V_rwfE.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2019-06-05-11-48-19.bpo-37165.V_rwfE.rst diff --git a/Misc/NEWS.d/next/Library/2019-06-05-11-48-19.bpo-37165.V_rwfE.rst b/Misc/NEWS.d/next/Library/2019-06-05-11-48-19.bpo-37165.V_rwfE.rst new file mode 100644 index 00000000000000..5430a57ef54c57 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-06-05-11-48-19.bpo-37165.V_rwfE.rst @@ -0,0 +1 @@ +Converted _collections._count_elements to use the Argument Clinic. From 1c6988ac729d0075bca91c5b4583b308b0b5728b Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 5 Jun 2019 13:43:29 -0700 Subject: [PATCH 3/3] Add METHODDEF --- Modules/_collectionsmodule.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 37e74e45cd7840..45169ecd11af00 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -2514,12 +2514,17 @@ PyDoc_STRVAR(module_doc, - defaultdict: dict subclass with a default value factory\n\ "); +static struct PyMethodDef module_functions[] = { + _COLLECTIONS__COUNT_ELEMENTS_METHODDEF + {NULL, NULL} /* sentinel */ +}; + static struct PyModuleDef _collectionsmodule = { PyModuleDef_HEAD_INIT, "_collections", module_doc, -1, - NULL, + module_functions, NULL, NULL, NULL,