Kaydet (Commit) cc9a951f authored tarafından Raymond Hettinger's avatar Raymond Hettinger

SF #1313496: bisect C replacement doesn't accept named args

üst 892a33f8
...@@ -130,6 +130,16 @@ class TestBisect(unittest.TestCase): ...@@ -130,6 +130,16 @@ class TestBisect(unittest.TestCase):
def test_backcompatibility(self): def test_backcompatibility(self):
self.assertEqual(bisect, bisect_right) 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): class TestInsort(unittest.TestCase):
......
...@@ -34,15 +34,16 @@ internal_bisect_right(PyObject *list, PyObject *item, int lo, int hi) ...@@ -34,15 +34,16 @@ internal_bisect_right(PyObject *list, PyObject *item, int lo, int hi)
} }
static PyObject * static PyObject *
bisect_right(PyObject *self, PyObject *args) bisect_right(PyObject *self, PyObject *args, PyObject *kw)
{ {
PyObject *list, *item; PyObject *list, *item;
int lo = 0; int lo = 0;
int hi = -1; int hi = -1;
int index; int index;
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
if (!PyArg_ParseTuple(args, "OO|ii:bisect_right", if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|ii:bisect_right",
&list, &item, &lo, &hi)) keywords, &list, &item, &lo, &hi))
return NULL; return NULL;
index = internal_bisect_right(list, item, lo, hi); index = internal_bisect_right(list, item, lo, hi);
if (index < 0) if (index < 0)
...@@ -51,7 +52,7 @@ bisect_right(PyObject *self, PyObject *args) ...@@ -51,7 +52,7 @@ bisect_right(PyObject *self, PyObject *args)
} }
PyDoc_STRVAR(bisect_right_doc, PyDoc_STRVAR(bisect_right_doc,
"bisect_right(list, item[, lo[, hi]]) -> index\n\ "bisect_right(a, x[, lo[, hi]]) -> index\n\
\n\ \n\
Return the index where to insert item x in list a, assuming a is sorted.\n\ Return the index where to insert item x in list a, assuming a is sorted.\n\
\n\ \n\
...@@ -63,15 +64,16 @@ Optional args lo (default 0) and hi (default len(a)) bound the\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"); slice of a to be searched.\n");
static PyObject * static PyObject *
insort_right(PyObject *self, PyObject *args) insort_right(PyObject *self, PyObject *args, PyObject *kw)
{ {
PyObject *list, *item, *result; PyObject *list, *item, *result;
int lo = 0; int lo = 0;
int hi = -1; int hi = -1;
int index; int index;
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
if (!PyArg_ParseTuple(args, "OO|ii:insort_right", if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|ii:insort_right",
&list, &item, &lo, &hi)) keywords, &list, &item, &lo, &hi))
return NULL; return NULL;
index = internal_bisect_right(list, item, lo, hi); index = internal_bisect_right(list, item, lo, hi);
if (index < 0) if (index < 0)
...@@ -91,7 +93,7 @@ insort_right(PyObject *self, PyObject *args) ...@@ -91,7 +93,7 @@ insort_right(PyObject *self, PyObject *args)
} }
PyDoc_STRVAR(insort_right_doc, PyDoc_STRVAR(insort_right_doc,
"insort_right(list, item[, lo[, hi]])\n\ "insort_right(a, x[, lo[, hi]])\n\
\n\ \n\
Insert item x in list a, and keep it sorted assuming a is sorted.\n\ Insert item x in list a, and keep it sorted assuming a is sorted.\n\
\n\ \n\
...@@ -129,15 +131,16 @@ internal_bisect_left(PyObject *list, PyObject *item, int lo, int hi) ...@@ -129,15 +131,16 @@ internal_bisect_left(PyObject *list, PyObject *item, int lo, int hi)
} }
static PyObject * static PyObject *
bisect_left(PyObject *self, PyObject *args) bisect_left(PyObject *self, PyObject *args, PyObject *kw)
{ {
PyObject *list, *item; PyObject *list, *item;
int lo = 0; int lo = 0;
int hi = -1; int hi = -1;
int index; int index;
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
if (!PyArg_ParseTuple(args, "OO|ii:bisect_left", if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|ii:bisect_left",
&list, &item, &lo, &hi)) keywords, &list, &item, &lo, &hi))
return NULL; return NULL;
index = internal_bisect_left(list, item, lo, hi); index = internal_bisect_left(list, item, lo, hi);
if (index < 0) if (index < 0)
...@@ -146,7 +149,7 @@ bisect_left(PyObject *self, PyObject *args) ...@@ -146,7 +149,7 @@ bisect_left(PyObject *self, PyObject *args)
} }
PyDoc_STRVAR(bisect_left_doc, PyDoc_STRVAR(bisect_left_doc,
"bisect_left(list, item[, lo[, hi]]) -> index\n\ "bisect_left(a, x[, lo[, hi]]) -> index\n\
\n\ \n\
Return the index where to insert item x in list a, assuming a is sorted.\n\ Return the index where to insert item x in list a, assuming a is sorted.\n\
\n\ \n\
...@@ -158,15 +161,16 @@ Optional args lo (default 0) and hi (default len(a)) bound the\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"); slice of a to be searched.\n");
static PyObject * static PyObject *
insort_left(PyObject *self, PyObject *args) insort_left(PyObject *self, PyObject *args, PyObject *kw)
{ {
PyObject *list, *item, *result; PyObject *list, *item, *result;
int lo = 0; int lo = 0;
int hi = -1; int hi = -1;
int index; int index;
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
if (!PyArg_ParseTuple(args, "OO|ii:insort_left", if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|ii:insort_left",
&list, &item, &lo, &hi)) keywords, &list, &item, &lo, &hi))
return NULL; return NULL;
index = internal_bisect_left(list, item, lo, hi); index = internal_bisect_left(list, item, lo, hi);
if (index < 0) if (index < 0)
...@@ -186,7 +190,7 @@ insort_left(PyObject *self, PyObject *args) ...@@ -186,7 +190,7 @@ insort_left(PyObject *self, PyObject *args)
} }
PyDoc_STRVAR(insort_left_doc, PyDoc_STRVAR(insort_left_doc,
"insort_left(list, item[, lo[, hi]])\n\ "insort_left(a, x[, lo[, hi]])\n\
\n\ \n\
Insert item x in list a, and keep it sorted assuming a is sorted.\n\ Insert item x in list a, and keep it sorted assuming a is sorted.\n\
\n\ \n\
...@@ -200,17 +204,17 @@ PyDoc_STRVAR(insort_doc, "Alias for insort_right().\n"); ...@@ -200,17 +204,17 @@ PyDoc_STRVAR(insort_doc, "Alias for insort_right().\n");
static PyMethodDef bisect_methods[] = { static PyMethodDef bisect_methods[] = {
{"bisect_right", (PyCFunction)bisect_right, {"bisect_right", (PyCFunction)bisect_right,
METH_VARARGS, bisect_right_doc}, METH_VARARGS|METH_KEYWORDS, bisect_right_doc},
{"bisect", (PyCFunction)bisect_right, {"bisect", (PyCFunction)bisect_right,
METH_VARARGS, bisect_doc}, METH_VARARGS|METH_KEYWORDS, bisect_doc},
{"insort_right", (PyCFunction)insort_right, {"insort_right", (PyCFunction)insort_right,
METH_VARARGS, insort_right_doc}, METH_VARARGS|METH_KEYWORDS, insort_right_doc},
{"insort", (PyCFunction)insort_right, {"insort", (PyCFunction)insort_right,
METH_VARARGS, insort_doc}, METH_VARARGS|METH_KEYWORDS, insort_doc},
{"bisect_left", (PyCFunction)bisect_left, {"bisect_left", (PyCFunction)bisect_left,
METH_VARARGS, bisect_left_doc}, METH_VARARGS|METH_KEYWORDS, bisect_left_doc},
{"insort_left", (PyCFunction)insort_left, {"insort_left", (PyCFunction)insort_left,
METH_VARARGS, insort_left_doc}, METH_VARARGS|METH_KEYWORDS, insort_left_doc},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment