diff -r 66b9a29d2696 Lib/test/test_bisect.py --- a/Lib/test/test_bisect.py Sun Nov 20 21:16:44 2016 +0100 +++ b/Lib/test/test_bisect.py Sun Nov 20 22:15:07 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] diff -r 66b9a29d2696 Modules/_bisectmodule.c --- a/Modules/_bisectmodule.c Sun Nov 20 21:16:44 2016 +0100 +++ b/Modules/_bisectmodule.c Sun Nov 20 22:15:07 2016 +0100 @@ -51,35 +51,64 @@ 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 * +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]*/ +{ + return bisect_bisect_right_impl(module, a, x, lo, hi); +} static PyObject * insort_right(PyObject *self, PyObject *args, PyObject *kw) @@ -229,14 +258,11 @@ 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}, + BISECT_BISECT_RIGHT_METHODDEF + BISECT_BISECT_METHODDEF {"insort_right", (PyCFunction)insort_right, METH_VARARGS|METH_KEYWORDS, insort_right_doc}, {"insort", (PyCFunction)insort_right,