From 3a855b26aed02abf87fc1163ad0d564dc3da1ea3 Mon Sep 17 00:00:00 2001 From: Shantanu Date: Sun, 17 May 2020 20:38:35 -0700 Subject: [PATCH] bpo-40665: Use Argument Clinic for the bisect module (GH-20163) --- .../2020-05-17-21-56-38.bpo-40665.msB7u5.rst | 1 + Modules/_bisectmodule.c | 236 ++++++-------- Modules/clinic/_bisectmodule.c.h | 306 ++++++++++++++++++ 3 files changed, 412 insertions(+), 131 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-05-17-21-56-38.bpo-40665.msB7u5.rst create mode 100644 Modules/clinic/_bisectmodule.c.h diff --git a/Misc/NEWS.d/next/Library/2020-05-17-21-56-38.bpo-40665.msB7u5.rst b/Misc/NEWS.d/next/Library/2020-05-17-21-56-38.bpo-40665.msB7u5.rst new file mode 100644 index 00000000000..160b2ca75d3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-17-21-56-38.bpo-40665.msB7u5.rst @@ -0,0 +1 @@ +Convert :mod:`bisect` to use Argument Clinic. \ No newline at end of file diff --git a/Modules/_bisectmodule.c b/Modules/_bisectmodule.c index 461a11f5099..82d800d9a87 100644 --- a/Modules/_bisectmodule.c +++ b/Modules/_bisectmodule.c @@ -6,6 +6,13 @@ Converted to C by Dmitry Vasiliev (dima at hlabs.spb.ru). #define PY_SSIZE_T_CLEAN #include "Python.h" +/*[clinic input] +module _bisect +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4d56a2b2033b462b]*/ + +#include "clinic/_bisectmodule.c.h" + _Py_IDENTIFIER(insert); static inline Py_ssize_t @@ -44,69 +51,63 @@ internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t return lo; } -static PyObject * -bisect_right(PyObject *self, PyObject *args, PyObject *kw) -{ - PyObject *list, *item; - Py_ssize_t lo = 0; - Py_ssize_t hi = -1; - Py_ssize_t index; - static char *keywords[] = {"a", "x", "lo", "hi", NULL}; +/*[clinic input] +_bisect.bisect_right -> Py_ssize_t - if (kw == NULL && PyTuple_GET_SIZE(args) == 2) { - list = PyTuple_GET_ITEM(args, 0); - item = PyTuple_GET_ITEM(args, 1); - } - else { - if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_right", - keywords, &list, &item, &lo, &hi)) - return NULL; - } - index = internal_bisect_right(list, item, lo, hi); - if (index < 0) - return NULL; - return PyLong_FromSsize_t(index); + a: object + x: object + lo: Py_ssize_t = 0 + hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None + +Return the index where to insert item x in list a, assuming a is sorted. + +The return value i is such that all e in a[:i] have e <= x, and all e in +a[i:] have e > x. So if x already appears in the list, i points just +beyond the rightmost x already there + +Optional args lo (default 0) and hi (default len(a)) bound the +slice of a to be searched. +[clinic start generated code]*/ + +static Py_ssize_t +_bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi) +/*[clinic end generated code: output=419e150cf1d2a235 input=e72212b282c83375]*/ +{ + return internal_bisect_right(a, x, lo, hi); } -PyDoc_STRVAR(bisect_right_doc, -"bisect_right(a, x[, lo[, hi]]) -> index\n\ -\n\ -Return the index where to insert item x in list a, assuming a is sorted.\n\ -\n\ -The return value i is such that all e in a[:i] have e <= x, and all e in\n\ -a[i:] have e > x. So if x already appears in the list, i points just\n\ -beyond the rightmost x already there\n\ -\n\ -Optional args lo (default 0) and hi (default len(a)) bound the\n\ -slice of a to be searched.\n"); +/*[clinic input] +_bisect.insort_right + + a: object + x: object + lo: Py_ssize_t = 0 + hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None + +Insert item x in list a, and keep it sorted assuming a is sorted. + +If x is already in a, insert it to the right of the rightmost x. + +Optional args lo (default 0) and hi (default len(a)) bound the +slice of a to be searched. +[clinic start generated code]*/ static PyObject * -insort_right(PyObject *self, PyObject *args, PyObject *kw) +_bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi) +/*[clinic end generated code: output=c2caa3d4cd02035a input=d1c45bfa68182669]*/ { - PyObject *list, *item, *result; - Py_ssize_t lo = 0; - Py_ssize_t hi = -1; - Py_ssize_t index; - static char *keywords[] = {"a", "x", "lo", "hi", NULL}; - - if (kw == NULL && PyTuple_GET_SIZE(args) == 2) { - list = PyTuple_GET_ITEM(args, 0); - item = PyTuple_GET_ITEM(args, 1); - } - else { - if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_right", - keywords, &list, &item, &lo, &hi)) - return NULL; - } - index = internal_bisect_right(list, item, lo, hi); + PyObject *result; + Py_ssize_t index = internal_bisect_right(a, x, lo, hi); if (index < 0) return NULL; - if (PyList_CheckExact(list)) { - if (PyList_Insert(list, index, item) < 0) + if (PyList_CheckExact(a)) { + if (PyList_Insert(a, index, x) < 0) return NULL; } else { - result = _PyObject_CallMethodId(list, &PyId_insert, "nO", index, item); + result = _PyObject_CallMethodId(a, &PyId_insert, "nO", index, x); if (result == NULL) return NULL; Py_DECREF(result); @@ -115,16 +116,6 @@ insort_right(PyObject *self, PyObject *args, PyObject *kw) Py_RETURN_NONE; } -PyDoc_STRVAR(insort_right_doc, -"insort_right(a, x[, lo[, hi]])\n\ -\n\ -Insert item x in list a, and keep it sorted assuming a is sorted.\n\ -\n\ -If x is already in a, insert it to the right of the rightmost x.\n\ -\n\ -Optional args lo (default 0) and hi (default len(a)) bound the\n\ -slice of a to be searched.\n"); - static inline Py_ssize_t internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi) { @@ -161,67 +152,64 @@ internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t h return lo; } -static PyObject * -bisect_left(PyObject *self, PyObject *args, PyObject *kw) -{ - PyObject *list, *item; - Py_ssize_t lo = 0; - Py_ssize_t hi = -1; - Py_ssize_t index; - static char *keywords[] = {"a", "x", "lo", "hi", NULL}; - if (kw == NULL && PyTuple_GET_SIZE(args) == 2) { - list = PyTuple_GET_ITEM(args, 0); - item = PyTuple_GET_ITEM(args, 1); - } - else { - if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_left", - keywords, &list, &item, &lo, &hi)) - return NULL; - } - index = internal_bisect_left(list, item, lo, hi); - if (index < 0) - return NULL; - return PyLong_FromSsize_t(index); +/*[clinic input] +_bisect.bisect_left -> Py_ssize_t + + a: object + x: object + lo: Py_ssize_t = 0 + hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None + +Return the index where to insert item x in list a, assuming a is sorted. + +The return value i is such that all e in a[:i] have e < x, and all e in +a[i:] have e >= x. So if x already appears in the list, i points just +before the leftmost x already there. + +Optional args lo (default 0) and hi (default len(a)) bound the +slice of a to be searched. +[clinic start generated code]*/ + +static Py_ssize_t +_bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi) +/*[clinic end generated code: output=af82168bc2856f24 input=2bd90f34afe5609f]*/ +{ + return internal_bisect_left(a, x, lo, hi); } -PyDoc_STRVAR(bisect_left_doc, -"bisect_left(a, x[, lo[, hi]]) -> index\n\ -\n\ -Return the index where to insert item x in list a, assuming a is sorted.\n\ -\n\ -The return value i is such that all e in a[:i] have e < x, and all e in\n\ -a[i:] have e >= x. So if x already appears in the list, i points just\n\ -before the leftmost x already there.\n\ -\n\ -Optional args lo (default 0) and hi (default len(a)) bound the\n\ -slice of a to be searched.\n"); + +/*[clinic input] +_bisect.insort_left + + a: object + x: object + lo: Py_ssize_t = 0 + hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None + +Insert item x in list a, and keep it sorted assuming a is sorted. + +If x is already in a, insert it to the left of the leftmost x. + +Optional args lo (default 0) and hi (default len(a)) bound the +slice of a to be searched. +[clinic start generated code]*/ static PyObject * -insort_left(PyObject *self, PyObject *args, PyObject *kw) +_bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi) +/*[clinic end generated code: output=9e8356c0844a182b input=bc4583308bce00cc]*/ { - PyObject *list, *item, *result; - Py_ssize_t lo = 0; - Py_ssize_t hi = -1; - Py_ssize_t index; - static char *keywords[] = {"a", "x", "lo", "hi", NULL}; - - if (kw == NULL && PyTuple_GET_SIZE(args) == 2) { - list = PyTuple_GET_ITEM(args, 0); - item = PyTuple_GET_ITEM(args, 1); - } else { - if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_left", - keywords, &list, &item, &lo, &hi)) - return NULL; - } - index = internal_bisect_left(list, item, lo, hi); + PyObject *result; + Py_ssize_t index = internal_bisect_left(a, x, lo, hi); if (index < 0) return NULL; - if (PyList_CheckExact(list)) { - if (PyList_Insert(list, index, item) < 0) + if (PyList_CheckExact(a)) { + if (PyList_Insert(a, index, x) < 0) return NULL; } else { - result = _PyObject_CallMethodId(list, &PyId_insert, "nO", index, item); + result = _PyObject_CallMethodId(a, &PyId_insert, "nO", index, x); if (result == NULL) return NULL; Py_DECREF(result); @@ -230,25 +218,11 @@ insort_left(PyObject *self, PyObject *args, PyObject *kw) Py_RETURN_NONE; } -PyDoc_STRVAR(insort_left_doc, -"insort_left(a, x[, lo[, hi]])\n\ -\n\ -Insert item x in list a, and keep it sorted assuming a is sorted.\n\ -\n\ -If x is already in a, insert it to the left of the leftmost x.\n\ -\n\ -Optional args lo (default 0) and hi (default len(a)) bound the\n\ -slice of a to be searched.\n"); - static PyMethodDef bisect_methods[] = { - {"bisect_right", (PyCFunction)(void(*)(void))bisect_right, - METH_VARARGS|METH_KEYWORDS, bisect_right_doc}, - {"insort_right", (PyCFunction)(void(*)(void))insort_right, - METH_VARARGS|METH_KEYWORDS, insort_right_doc}, - {"bisect_left", (PyCFunction)(void(*)(void))bisect_left, - METH_VARARGS|METH_KEYWORDS, bisect_left_doc}, - {"insort_left", (PyCFunction)(void(*)(void))insort_left, - METH_VARARGS|METH_KEYWORDS, insort_left_doc}, + _BISECT_BISECT_RIGHT_METHODDEF + _BISECT_INSORT_RIGHT_METHODDEF + _BISECT_BISECT_LEFT_METHODDEF + _BISECT_INSORT_LEFT_METHODDEF {NULL, NULL} /* sentinel */ }; diff --git a/Modules/clinic/_bisectmodule.c.h b/Modules/clinic/_bisectmodule.c.h new file mode 100644 index 00000000000..80ab7048f14 --- /dev/null +++ b/Modules/clinic/_bisectmodule.c.h @@ -0,0 +1,306 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +PyDoc_STRVAR(_bisect_bisect_right__doc__, +"bisect_right($module, /, a, x, lo=0, hi=None)\n" +"--\n" +"\n" +"Return the index where to insert item x in list a, assuming a is sorted.\n" +"\n" +"The return value i is such that all e in a[:i] have e <= x, and all e in\n" +"a[i:] have e > x. So if x already appears in the list, i points just\n" +"beyond the rightmost x already there\n" +"\n" +"Optional args lo (default 0) and hi (default len(a)) bound the\n" +"slice of a to be searched."); + +#define _BISECT_BISECT_RIGHT_METHODDEF \ + {"bisect_right", (PyCFunction)(void(*)(void))_bisect_bisect_right, METH_FASTCALL|METH_KEYWORDS, _bisect_bisect_right__doc__}, + +static Py_ssize_t +_bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi); + +static PyObject * +_bisect_bisect_right(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"a", "x", "lo", "hi", NULL}; + static _PyArg_Parser _parser = {NULL, _keywords, "bisect_right", 0}; + PyObject *argsbuf[4]; + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; + PyObject *a; + PyObject *x; + Py_ssize_t lo = 0; + Py_ssize_t hi = -1; + Py_ssize_t _return_value; + + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 4, 0, argsbuf); + if (!args) { + goto exit; + } + a = args[0]; + x = args[1]; + if (!noptargs) { + goto skip_optional_pos; + } + if (args[2]) { + if (PyFloat_Check(args[2])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + Py_ssize_t ival = -1; + PyObject *iobj = PyNumber_Index(args[2]); + if (iobj != NULL) { + ival = PyLong_AsSsize_t(iobj); + Py_DECREF(iobj); + } + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + lo = ival; + } + if (!--noptargs) { + goto skip_optional_pos; + } + } + if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) { + goto exit; + } +skip_optional_pos: + _return_value = _bisect_bisect_right_impl(module, a, x, lo, hi); + if ((_return_value == -1) && PyErr_Occurred()) { + goto exit; + } + return_value = PyLong_FromSsize_t(_return_value); + +exit: + return return_value; +} + +PyDoc_STRVAR(_bisect_insort_right__doc__, +"insort_right($module, /, a, x, lo=0, hi=None)\n" +"--\n" +"\n" +"Insert item x in list a, and keep it sorted assuming a is sorted.\n" +"\n" +"If x is already in a, insert it to the right of the rightmost x.\n" +"\n" +"Optional args lo (default 0) and hi (default len(a)) bound the\n" +"slice of a to be searched."); + +#define _BISECT_INSORT_RIGHT_METHODDEF \ + {"insort_right", (PyCFunction)(void(*)(void))_bisect_insort_right, METH_FASTCALL|METH_KEYWORDS, _bisect_insort_right__doc__}, + +static PyObject * +_bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi); + +static PyObject * +_bisect_insort_right(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"a", "x", "lo", "hi", NULL}; + static _PyArg_Parser _parser = {NULL, _keywords, "insort_right", 0}; + PyObject *argsbuf[4]; + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; + PyObject *a; + PyObject *x; + Py_ssize_t lo = 0; + Py_ssize_t hi = -1; + + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 4, 0, argsbuf); + if (!args) { + goto exit; + } + a = args[0]; + x = args[1]; + if (!noptargs) { + goto skip_optional_pos; + } + if (args[2]) { + if (PyFloat_Check(args[2])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + Py_ssize_t ival = -1; + PyObject *iobj = PyNumber_Index(args[2]); + if (iobj != NULL) { + ival = PyLong_AsSsize_t(iobj); + Py_DECREF(iobj); + } + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + lo = ival; + } + if (!--noptargs) { + goto skip_optional_pos; + } + } + if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) { + goto exit; + } +skip_optional_pos: + return_value = _bisect_insort_right_impl(module, a, x, lo, hi); + +exit: + return return_value; +} + +PyDoc_STRVAR(_bisect_bisect_left__doc__, +"bisect_left($module, /, a, x, lo=0, hi=None)\n" +"--\n" +"\n" +"Return the index where to insert item x in list a, assuming a is sorted.\n" +"\n" +"The return value i is such that all e in a[:i] have e < x, and all e in\n" +"a[i:] have e >= x. So if x already appears in the list, i points just\n" +"before the leftmost x already there.\n" +"\n" +"Optional args lo (default 0) and hi (default len(a)) bound the\n" +"slice of a to be searched."); + +#define _BISECT_BISECT_LEFT_METHODDEF \ + {"bisect_left", (PyCFunction)(void(*)(void))_bisect_bisect_left, METH_FASTCALL|METH_KEYWORDS, _bisect_bisect_left__doc__}, + +static Py_ssize_t +_bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi); + +static PyObject * +_bisect_bisect_left(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"a", "x", "lo", "hi", NULL}; + static _PyArg_Parser _parser = {NULL, _keywords, "bisect_left", 0}; + PyObject *argsbuf[4]; + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; + PyObject *a; + PyObject *x; + Py_ssize_t lo = 0; + Py_ssize_t hi = -1; + Py_ssize_t _return_value; + + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 4, 0, argsbuf); + if (!args) { + goto exit; + } + a = args[0]; + x = args[1]; + if (!noptargs) { + goto skip_optional_pos; + } + if (args[2]) { + if (PyFloat_Check(args[2])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + Py_ssize_t ival = -1; + PyObject *iobj = PyNumber_Index(args[2]); + if (iobj != NULL) { + ival = PyLong_AsSsize_t(iobj); + Py_DECREF(iobj); + } + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + lo = ival; + } + if (!--noptargs) { + goto skip_optional_pos; + } + } + if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) { + goto exit; + } +skip_optional_pos: + _return_value = _bisect_bisect_left_impl(module, a, x, lo, hi); + if ((_return_value == -1) && PyErr_Occurred()) { + goto exit; + } + return_value = PyLong_FromSsize_t(_return_value); + +exit: + return return_value; +} + +PyDoc_STRVAR(_bisect_insort_left__doc__, +"insort_left($module, /, a, x, lo=0, hi=None)\n" +"--\n" +"\n" +"Insert item x in list a, and keep it sorted assuming a is sorted.\n" +"\n" +"If x is already in a, insert it to the left of the leftmost x.\n" +"\n" +"Optional args lo (default 0) and hi (default len(a)) bound the\n" +"slice of a to be searched."); + +#define _BISECT_INSORT_LEFT_METHODDEF \ + {"insort_left", (PyCFunction)(void(*)(void))_bisect_insort_left, METH_FASTCALL|METH_KEYWORDS, _bisect_insort_left__doc__}, + +static PyObject * +_bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi); + +static PyObject * +_bisect_insort_left(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"a", "x", "lo", "hi", NULL}; + static _PyArg_Parser _parser = {NULL, _keywords, "insort_left", 0}; + PyObject *argsbuf[4]; + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; + PyObject *a; + PyObject *x; + Py_ssize_t lo = 0; + Py_ssize_t hi = -1; + + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 4, 0, argsbuf); + if (!args) { + goto exit; + } + a = args[0]; + x = args[1]; + if (!noptargs) { + goto skip_optional_pos; + } + if (args[2]) { + if (PyFloat_Check(args[2])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + Py_ssize_t ival = -1; + PyObject *iobj = PyNumber_Index(args[2]); + if (iobj != NULL) { + ival = PyLong_AsSsize_t(iobj); + Py_DECREF(iobj); + } + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + lo = ival; + } + if (!--noptargs) { + goto skip_optional_pos; + } + } + if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) { + goto exit; + } +skip_optional_pos: + return_value = _bisect_insort_left_impl(module, a, x, lo, hi); + +exit: + return return_value; +} +/*[clinic end generated code: output=bcbd6c77331a08f0 input=a9049054013a1b77]*/