Kaydet (Commit) 7cf8bebb authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

Issue #29331: Simplified argument parsing in sorted() and list.sort().

üst 5e65cd39
...@@ -1912,7 +1912,7 @@ reverse_sortslice(sortslice *s, Py_ssize_t n) ...@@ -1912,7 +1912,7 @@ reverse_sortslice(sortslice *s, Py_ssize_t n)
* duplicated). * duplicated).
*/ */
static PyObject * static PyObject *
listsort(PyListObject *self, PyObject *args, PyObject *kwds) listsort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
{ {
MergeState ms; MergeState ms;
Py_ssize_t nremaining; Py_ssize_t nremaining;
...@@ -1922,24 +1922,11 @@ listsort(PyListObject *self, PyObject *args, PyObject *kwds) ...@@ -1922,24 +1922,11 @@ listsort(PyListObject *self, PyObject *args, PyObject *kwds)
PyObject **saved_ob_item; PyObject **saved_ob_item;
PyObject **final_ob_item; PyObject **final_ob_item;
PyObject *result = NULL; /* guilty until proved innocent */ PyObject *result = NULL; /* guilty until proved innocent */
int reverse = 0;
PyObject *keyfunc = NULL;
Py_ssize_t i; Py_ssize_t i;
static char *kwlist[] = {"key", "reverse", 0};
PyObject **keys; PyObject **keys;
assert(self != NULL); assert(self != NULL);
assert (PyList_Check(self)); assert (PyList_Check(self));
if (args != NULL) {
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort",
kwlist, &keyfunc, &reverse))
return NULL;
if (Py_SIZE(args) > 0) {
PyErr_SetString(PyExc_TypeError,
"must use keyword argument for key function");
return NULL;
}
}
if (keyfunc == Py_None) if (keyfunc == Py_None)
keyfunc = NULL; keyfunc = NULL;
...@@ -2088,6 +2075,19 @@ keyfunc_fail: ...@@ -2088,6 +2075,19 @@ keyfunc_fail:
#undef IFLT #undef IFLT
#undef ISLT #undef ISLT
static PyObject *
listsort(PyListObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"key", "reverse", 0};
PyObject *keyfunc = NULL;
int reverse = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|$Oi:sort",
kwlist, &keyfunc, &reverse))
return NULL;
return listsort_impl(self, keyfunc, reverse);
}
int int
PyList_Sort(PyObject *v) PyList_Sort(PyObject *v)
{ {
...@@ -2095,7 +2095,7 @@ PyList_Sort(PyObject *v) ...@@ -2095,7 +2095,7 @@ PyList_Sort(PyObject *v)
PyErr_BadInternalCall(); PyErr_BadInternalCall();
return -1; return -1;
} }
v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL); v = listsort_impl((PyListObject *)v, NULL, 0);
if (v == NULL) if (v == NULL)
return -1; return -1;
Py_DECREF(v); Py_DECREF(v);
......
...@@ -2126,15 +2126,11 @@ PyDoc_STRVAR(builtin_sorted__doc__, ...@@ -2126,15 +2126,11 @@ PyDoc_STRVAR(builtin_sorted__doc__,
static PyObject * static PyObject *
builtin_sorted(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) builtin_sorted(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
{ {
PyObject *newlist, *v, *seq, *keyfunc=NULL; PyObject *newlist, *v, *seq, *callable;
PyObject *callable;
static const char * const kwlist[] = {"", "key", "reverse", 0}; /* Keyword arguments are passed through list.sort() which will check
/* args 1-3 should match listsort in Objects/listobject.c */ them. */
static _PyArg_Parser parser = {"O|Oi:sorted", kwlist, 0}; if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq))
int reverse;
if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &parser,
&seq, &keyfunc, &reverse))
return NULL; return NULL;
newlist = PySequence_List(seq); newlist = PySequence_List(seq);
......
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