# HG changeset patch # User Julien Palard # Date 1479676915 -3600 # Sun Nov 20 22:21:55 2016 +0100 # Node ID c941a85f5169d0d140faa1caa686518e736ec06a # Parent ad4e6894f98e43cae878322dfd8cb93dd3498737 Argument Clinic for insort and insort_right. diff -r ad4e6894f98e -r c941a85f5169 Lib/test/test_bisect.py --- a/Lib/test/test_bisect.py Sun Nov 20 22:15:26 2016 +0100 +++ b/Lib/test/test_bisect.py Sun Nov 20 22:21:55 2016 +0100 @@ -216,13 +216,11 @@ if digit in "02468": f = self.module.insort_left else: - f = self.module.insort_right + f = choice((self.module.insort_right, + self.module.insort)) f(insorted, digit) self.assertEqual(sorted(insorted), insorted) - def test_backcompatibility(self): - self.assertEqual(self.module.insort, self.module.insort_right) - def test_listDerived(self): class List(list): data = [] diff -r ad4e6894f98e -r c941a85f5169 Modules/_bisectmodule.c --- a/Modules/_bisectmodule.c Sun Nov 20 22:15:26 2016 +0100 +++ b/Modules/_bisectmodule.c Sun Nov 20 22:21:55 2016 +0100 @@ -110,26 +110,43 @@ return bisect_bisect_right_impl(module, a, x, lo, hi); } +/*[clinic input] +bisect.insort_right + + a: 'O' + The list in which ``x`` will be inserted. + x: 'O' + The value to insert. + lo: 'n' = 0 + Lower bound, defaults to 0. + hi: 'n' = -1 + Upper bound, defaults to -1 (meaning ``len(a)``). + +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=6e0b99c731a11c1a input=21a0e6d0b8384fa2]*/ { - PyObject *list, *item, *result; - Py_ssize_t lo = 0; - Py_ssize_t hi = -1; + PyObject *result; Py_ssize_t index; - static char *keywords[] = {"a", "x", "lo", "hi", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_right", - keywords, &list, &item, &lo, &hi)) - return NULL; - index = internal_bisect_right(list, item, lo, hi); + 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); @@ -138,15 +155,30 @@ 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"); +/*[clinic input] +bisect.insort + + a: 'O' + The list in which ``x`` will be inserted. + x: 'O' + The value to insert. + lo: 'n' = 0 + Lower bound, defaults to 0. + hi: 'n' = -1 + Upper bound, defaults to -1 (meaning ``len(a)``). + +Alias for insort_right(). + +[clinic start generated code]*/ + +static PyObject * +bisect_insort_impl(PyObject *module, PyObject *a, PyObject *x, Py_ssize_t lo, + Py_ssize_t hi) +/*[clinic end generated code: output=38192d1100aa564e input=c6d71c3ed2f8f2da]*/ +{ + return bisect_insort_right_impl(module, a, x, lo, hi); +} + static Py_ssize_t internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi) @@ -258,15 +290,11 @@ Optional args lo (default 0) and hi (default len(a)) bound the\n\ slice of a to be searched.\n"); -PyDoc_STRVAR(insort_doc, "Alias for insort_right().\n"); - static PyMethodDef bisect_methods[] = { BISECT_BISECT_RIGHT_METHODDEF BISECT_BISECT_METHODDEF - {"insort_right", (PyCFunction)insort_right, - METH_VARARGS|METH_KEYWORDS, insort_right_doc}, - {"insort", (PyCFunction)insort_right, - METH_VARARGS|METH_KEYWORDS, insort_doc}, + BISECT_INSORT_RIGHT_METHODDEF + BISECT_INSORT_METHODDEF BISECT_BISECT_LEFT_METHODDEF {"insort_left", (PyCFunction)insort_left, METH_VARARGS|METH_KEYWORDS, insort_left_doc},