diff -r 2d1d70b53376 Lib/test/test_bisect.py --- a/Lib/test/test_bisect.py Sun Nov 20 21:17:23 2016 +0000 +++ b/Lib/test/test_bisect.py Sun Nov 20 22:33:08 2016 +0100 @@ -30,6 +30,7 @@ class TestBisect: def setUp(self): + self.aliases = {self.module.bisect_right: [self.module.bisect]} self.precomputedCases = [ (self.module.bisect_right, [], 1, 0), (self.module.bisect_right, [1], 0, 0), @@ -114,8 +115,9 @@ def test_precomputed(self): for func, data, elem, expected in self.precomputedCases: - self.assertEqual(func(data, elem), expected) - self.assertEqual(func(UserList(data), elem), expected) + for func_alias in [func] + self.aliases.get(func, []): + self.assertEqual(func_alias(data, elem), expected) + self.assertEqual(func_alias(UserList(data), elem), expected) def test_negative_lo(self): # Issue 3301 @@ -170,24 +172,22 @@ def test_optionalSlicing(self): for func, data, elem, expected in self.precomputedCases: - for lo in range(4): - lo = min(len(data), lo) - for hi in range(3,8): - hi = min(len(data), hi) - ip = func(data, elem, lo, hi) - self.assertTrue(lo <= ip <= hi) - if func is self.module.bisect_left and ip < hi: - self.assertTrue(elem <= data[ip]) - if func is self.module.bisect_left and ip > lo: - self.assertTrue(data[ip-1] < elem) - if func is self.module.bisect_right and ip < hi: - self.assertTrue(elem < data[ip]) - if func is self.module.bisect_right and ip > lo: - self.assertTrue(data[ip-1] <= elem) - self.assertEqual(ip, max(lo, min(hi, expected))) - - def test_backcompatibility(self): - self.assertEqual(self.module.bisect, self.module.bisect_right) + for func_alias in [func] + self.aliases.get(func, []): + for lo in range(4): + lo = min(len(data), lo) + for hi in range(3,8): + hi = min(len(data), hi) + ip = func_alias(data, elem, lo, hi) + self.assertTrue(lo <= ip <= hi) + if func_alias is self.module.bisect_left and ip < hi: + self.assertTrue(elem <= data[ip]) + if func_alias is self.module.bisect_left and ip > lo: + self.assertTrue(data[ip-1] < elem) + if func_alias is self.module.bisect_right and ip < hi: + self.assertTrue(elem < data[ip]) + if func_alias is self.module.bisect_right and ip > lo: + self.assertTrue(data[ip-1] <= elem) + self.assertEqual(ip, max(lo, min(hi, expected))) def test_keyword_args(self): data = [10, 20, 30, 40, 50] @@ -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 2d1d70b53376 Modules/_bisectmodule.c --- a/Modules/_bisectmodule.c Sun Nov 20 21:17:23 2016 +0000 +++ b/Modules/_bisectmodule.c Sun Nov 20 22:33:08 2016 +0100 @@ -6,6 +6,13 @@ #define PY_SSIZE_T_CLEAN #include "Python.h" +#include "clinic/_bisectmodule.c.h" + +/*[clinic input] +module bisect +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d0e256c42a9e4c13]*/ + _Py_IDENTIFIER(insert); static Py_ssize_t @@ -44,56 +51,102 @@ return lo; } +/*[clinic input] +bisect.bisect_right + + a: 'O' + The list in which ``x`` is to be inserted. + x: 'O' + The value to be inserted. + lo: 'n' = 0 + Lower bound, defaults to 0. + hi: 'n' = -1 + Upper bound, defaults to -1 (meaning ``len(a)``). + +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 PyObject * -bisect_right(PyObject *self, PyObject *args, PyObject *kw) +bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi) +/*[clinic end generated code: output=a2fb3e3261e46954 input=404ef501d310eb53]*/ { - 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 (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_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; return PyLong_FromSsize_t(index); } -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.bisect + + a: 'O' + The list in which ``x`` is to be inserted. + x: 'O' + The value to be inserted. + lo: 'n' = 0 + Lower bound, defaults to 0. + hi: 'n' = -1 + Upper bound, defaults to -1 (meaning ``len(a)``). + +Alias for bisect_right(). + +[clinic start generated code]*/ static PyObject * -insort_right(PyObject *self, PyObject *args, PyObject *kw) +bisect_bisect_impl(PyObject *module, PyObject *a, PyObject *x, Py_ssize_t lo, + Py_ssize_t hi) +/*[clinic end generated code: output=ca65496186f7d5c1 input=f25fdfb68993d29f]*/ { - PyObject *list, *item, *result; - Py_ssize_t lo = 0; - Py_ssize_t hi = -1; + 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 * +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 *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); @@ -102,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) @@ -148,56 +216,79 @@ return lo; } +/*[clinic input] +bisect.bisect_left + + a: 'O' + The list in which ``x`` is to be inserted. + x: 'O' + The value to be inserted. + lo: 'n' = 0 + Lower bound, defaults to 0. + hi: 'n' = -1 + Upper bound, defaults to -1 (meaning ``len(a)``). + +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 PyObject * -bisect_left(PyObject *self, PyObject *args, PyObject *kw) +bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi) +/*[clinic end generated code: output=27a0228c4a0a5fa2 input=da5d94d033fd82fc]*/ { - 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 (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_left", - keywords, &list, &item, &lo, &hi)) - return NULL; - index = internal_bisect_left(list, item, lo, hi); + index = internal_bisect_left(a, x, lo, hi); if (index < 0) return NULL; return PyLong_FromSsize_t(index); } -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: '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 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=aa0228af6970ec52 input=aa95f626a96be30a]*/ { - 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_left", - keywords, &list, &item, &lo, &hi)) - return NULL; - index = internal_bisect_left(list, item, lo, hi); + 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); @@ -206,32 +297,13 @@ 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"); - -PyDoc_STRVAR(bisect_doc, "Alias for bisect_right().\n"); -PyDoc_STRVAR(insort_doc, "Alias for insort_right().\n"); - static PyMethodDef bisect_methods[] = { - {"bisect_right", (PyCFunction)bisect_right, - METH_VARARGS|METH_KEYWORDS, bisect_right_doc}, - {"bisect", (PyCFunction)bisect_right, - METH_VARARGS|METH_KEYWORDS, bisect_doc}, - {"insort_right", (PyCFunction)insort_right, - METH_VARARGS|METH_KEYWORDS, insort_right_doc}, - {"insort", (PyCFunction)insort_right, - METH_VARARGS|METH_KEYWORDS, insort_doc}, - {"bisect_left", (PyCFunction)bisect_left, - METH_VARARGS|METH_KEYWORDS, bisect_left_doc}, - {"insort_left", (PyCFunction)insort_left, - METH_VARARGS|METH_KEYWORDS, insort_left_doc}, + BISECT_BISECT_RIGHT_METHODDEF + BISECT_BISECT_METHODDEF + BISECT_INSORT_RIGHT_METHODDEF + BISECT_INSORT_METHODDEF + BISECT_BISECT_LEFT_METHODDEF + BISECT_INSORT_LEFT_METHODDEF {NULL, NULL} /* sentinel */ };