[Python-checkins] cpython: Issue #29331: Simplified argument parsing in sorted() and list.sort().

serhiy.storchaka python-checkins at python.org
Sat Jan 21 16:05:30 EST 2017


https://hg.python.org/cpython/rev/69bd5c497a82
changeset:   106253:69bd5c497a82
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Jan 21 23:05:00 2017 +0200
summary:
  Issue #29331: Simplified argument parsing in sorted() and list.sort().

files:
  Objects/listobject.c |  30 +++++++++++++++---------------
  Python/bltinmodule.c |  14 +++++---------
  2 files changed, 20 insertions(+), 24 deletions(-)


diff --git a/Objects/listobject.c b/Objects/listobject.c
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -1912,7 +1912,7 @@
  * duplicated).
  */
 static PyObject *
-listsort(PyListObject *self, PyObject *args, PyObject *kwds)
+listsort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
 {
     MergeState ms;
     Py_ssize_t nremaining;
@@ -1922,24 +1922,11 @@
     PyObject **saved_ob_item;
     PyObject **final_ob_item;
     PyObject *result = NULL;            /* guilty until proved innocent */
-    int reverse = 0;
-    PyObject *keyfunc = NULL;
     Py_ssize_t i;
-    static char *kwlist[] = {"key", "reverse", 0};
     PyObject **keys;
 
     assert(self != NULL);
     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)
         keyfunc = NULL;
 
@@ -2088,6 +2075,19 @@
 #undef IFLT
 #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
 PyList_Sort(PyObject *v)
 {
@@ -2095,7 +2095,7 @@
         PyErr_BadInternalCall();
         return -1;
     }
-    v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
+    v = listsort_impl((PyListObject *)v, NULL, 0);
     if (v == NULL)
         return -1;
     Py_DECREF(v);
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -2126,15 +2126,11 @@
 static PyObject *
 builtin_sorted(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
 {
-    PyObject *newlist, *v, *seq, *keyfunc=NULL;
-    PyObject *callable;
-    static const char * const kwlist[] = {"", "key", "reverse", 0};
-    /* args 1-3 should match listsort in Objects/listobject.c */
-    static _PyArg_Parser parser = {"O|Oi:sorted", kwlist, 0};
-    int reverse;
-
-    if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &parser,
-                                      &seq, &keyfunc, &reverse))
+    PyObject *newlist, *v, *seq, *callable;
+
+    /* Keyword arguments are passed through list.sort() which will check
+       them. */
+    if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq))
         return NULL;
 
     newlist = PySequence_List(seq);

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list