diff options
author | Raymond Hettinger <python@rcn.com> | 2005-10-05 11:39:12 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2005-10-05 11:39:12 (GMT) |
commit | cc9a951f35c99f21b76c39e8c202cb467a412038 (patch) | |
tree | 7fc5a0cf33a6b8305d7bcbdc488b622992973bfd | |
parent | 892a33f85aca8e399509c2c673a02124d24d1991 (diff) | |
download | cpython-cc9a951f35c99f21b76c39e8c202cb467a412038.zip cpython-cc9a951f35c99f21b76c39e8c202cb467a412038.tar.gz cpython-cc9a951f35c99f21b76c39e8c202cb467a412038.tar.bz2 |
SF #1313496: bisect C replacement doesn't accept named args
-rw-r--r-- | Lib/test/test_bisect.py | 10 | ||||
-rw-r--r-- | Modules/_bisectmodule.c | 48 |
2 files changed, 36 insertions, 22 deletions
diff --git a/Lib/test/test_bisect.py b/Lib/test/test_bisect.py index 87448b3..f6e24a6 100644 --- a/Lib/test/test_bisect.py +++ b/Lib/test/test_bisect.py @@ -130,6 +130,16 @@ class TestBisect(unittest.TestCase): def test_backcompatibility(self): self.assertEqual(bisect, bisect_right) + def test_keyword_args(self): + data = [10, 20, 30, 40, 50] + self.assertEqual(bisect_left(a=data, x=25, lo=1, hi=3), 2) + self.assertEqual(bisect_right(a=data, x=25, lo=1, hi=3), 2) + self.assertEqual(bisect(a=data, x=25, lo=1, hi=3), 2) + insort_left(a=data, x=25, lo=1, hi=3) + insort_right(a=data, x=25, lo=1, hi=3) + insort(a=data, x=25, lo=1, hi=3) + self.assertEqual(data, [10, 20, 25, 25, 25, 30, 40, 50]) + #============================================================================== class TestInsort(unittest.TestCase): diff --git a/Modules/_bisectmodule.c b/Modules/_bisectmodule.c index f4016fe..0d29462 100644 --- a/Modules/_bisectmodule.c +++ b/Modules/_bisectmodule.c @@ -34,15 +34,16 @@ internal_bisect_right(PyObject *list, PyObject *item, int lo, int hi) } static PyObject * -bisect_right(PyObject *self, PyObject *args) +bisect_right(PyObject *self, PyObject *args, PyObject *kw) { PyObject *list, *item; int lo = 0; int hi = -1; int index; + static char *keywords[] = {"a", "x", "lo", "hi", NULL}; - if (!PyArg_ParseTuple(args, "OO|ii:bisect_right", - &list, &item, &lo, &hi)) + if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|ii:bisect_right", + keywords, &list, &item, &lo, &hi)) return NULL; index = internal_bisect_right(list, item, lo, hi); if (index < 0) @@ -51,7 +52,7 @@ bisect_right(PyObject *self, PyObject *args) } PyDoc_STRVAR(bisect_right_doc, -"bisect_right(list, item[, lo[, hi]]) -> index\n\ +"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\ @@ -63,15 +64,16 @@ Optional args lo (default 0) and hi (default len(a)) bound the\n\ slice of a to be searched.\n"); static PyObject * -insort_right(PyObject *self, PyObject *args) +insort_right(PyObject *self, PyObject *args, PyObject *kw) { PyObject *list, *item, *result; int lo = 0; int hi = -1; int index; + static char *keywords[] = {"a", "x", "lo", "hi", NULL}; - if (!PyArg_ParseTuple(args, "OO|ii:insort_right", - &list, &item, &lo, &hi)) + if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|ii:insort_right", + keywords, &list, &item, &lo, &hi)) return NULL; index = internal_bisect_right(list, item, lo, hi); if (index < 0) @@ -91,7 +93,7 @@ insort_right(PyObject *self, PyObject *args) } PyDoc_STRVAR(insort_right_doc, -"insort_right(list, item[, lo[, hi]])\n\ +"insort_right(a, x[, lo[, hi]])\n\ \n\ Insert item x in list a, and keep it sorted assuming a is sorted.\n\ \n\ @@ -129,15 +131,16 @@ internal_bisect_left(PyObject *list, PyObject *item, int lo, int hi) } static PyObject * -bisect_left(PyObject *self, PyObject *args) +bisect_left(PyObject *self, PyObject *args, PyObject *kw) { PyObject *list, *item; int lo = 0; int hi = -1; int index; + static char *keywords[] = {"a", "x", "lo", "hi", NULL}; - if (!PyArg_ParseTuple(args, "OO|ii:bisect_left", - &list, &item, &lo, &hi)) + if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|ii:bisect_left", + keywords, &list, &item, &lo, &hi)) return NULL; index = internal_bisect_left(list, item, lo, hi); if (index < 0) @@ -146,7 +149,7 @@ bisect_left(PyObject *self, PyObject *args) } PyDoc_STRVAR(bisect_left_doc, -"bisect_left(list, item[, lo[, hi]]) -> index\n\ +"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\ @@ -158,15 +161,16 @@ Optional args lo (default 0) and hi (default len(a)) bound the\n\ slice of a to be searched.\n"); static PyObject * -insort_left(PyObject *self, PyObject *args) +insort_left(PyObject *self, PyObject *args, PyObject *kw) { PyObject *list, *item, *result; int lo = 0; int hi = -1; int index; + static char *keywords[] = {"a", "x", "lo", "hi", NULL}; - if (!PyArg_ParseTuple(args, "OO|ii:insort_left", - &list, &item, &lo, &hi)) + if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|ii:insort_left", + keywords, &list, &item, &lo, &hi)) return NULL; index = internal_bisect_left(list, item, lo, hi); if (index < 0) @@ -186,7 +190,7 @@ insort_left(PyObject *self, PyObject *args) } PyDoc_STRVAR(insort_left_doc, -"insort_left(list, item[, lo[, hi]])\n\ +"insort_left(a, x[, lo[, hi]])\n\ \n\ Insert item x in list a, and keep it sorted assuming a is sorted.\n\ \n\ @@ -200,17 +204,17 @@ PyDoc_STRVAR(insort_doc, "Alias for insort_right().\n"); static PyMethodDef bisect_methods[] = { {"bisect_right", (PyCFunction)bisect_right, - METH_VARARGS, bisect_right_doc}, + METH_VARARGS|METH_KEYWORDS, bisect_right_doc}, {"bisect", (PyCFunction)bisect_right, - METH_VARARGS, bisect_doc}, + METH_VARARGS|METH_KEYWORDS, bisect_doc}, {"insort_right", (PyCFunction)insort_right, - METH_VARARGS, insort_right_doc}, + METH_VARARGS|METH_KEYWORDS, insort_right_doc}, {"insort", (PyCFunction)insort_right, - METH_VARARGS, insort_doc}, + METH_VARARGS|METH_KEYWORDS, insort_doc}, {"bisect_left", (PyCFunction)bisect_left, - METH_VARARGS, bisect_left_doc}, + METH_VARARGS|METH_KEYWORDS, bisect_left_doc}, {"insort_left", (PyCFunction)insort_left, - METH_VARARGS, insort_left_doc}, + METH_VARARGS|METH_KEYWORDS, insort_left_doc}, {NULL, NULL} /* sentinel */ }; |