[Python-checkins] cpython (merge 3.6 -> default): Issue #29327: Fixed a crash when pass the iterable keyword argument to sorted().

serhiy.storchaka python-checkins at python.org
Fri Jan 20 01:35:37 EST 2017


https://hg.python.org/cpython/rev/f44f44b14dfc
changeset:   106246:f44f44b14dfc
parent:      106244:cb6d4df2bd9b
parent:      106245:1827b64cfce8
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Fri Jan 20 08:35:18 2017 +0200
summary:
  Issue #29327: Fixed a crash when pass the iterable keyword argument to sorted().

files:
  Lib/test/test_builtin.py |  10 ++++++++++
  Misc/NEWS                |   3 +++
  Python/bltinmodule.c     |   3 ++-
  3 files changed, 15 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -1627,6 +1627,16 @@
         self.assertEqual(data, sorted(copy, reverse=1))
         self.assertNotEqual(data, copy)
 
+    def test_bad_arguments(self):
+        # Issue #29327: The first argument is positional-only.
+        sorted([])
+        with self.assertRaises(TypeError):
+            sorted(iterable=[])
+        # Other arguments are keyword-only
+        sorted([], key=None)
+        with self.assertRaises(TypeError):
+            sorted([], None)
+
     def test_inputtypes(self):
         s = 'abracadabra'
         types = [list, tuple, str]
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #29327: Fixed a crash when pass the iterable keyword argument to
+  sorted().
+
 - Issue #29034: Fix memory leak and use-after-free in os module (path_converter).
 
 - Issue #29159: Fix regression in bytes(x) when x.__index__() raises Exception.
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -2128,7 +2128,7 @@
 {
     PyObject *newlist, *v, *seq, *keyfunc=NULL;
     PyObject *callable;
-    static const char * const kwlist[] = {"iterable", "key", "reverse", 0};
+    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;
@@ -2147,6 +2147,7 @@
         return NULL;
     }
 
+    assert(nargs >= 1);
     v = _PyObject_FastCallKeywords(callable, args + 1, nargs - 1, kwnames);
     Py_DECREF(callable);
     if (v == NULL) {

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


More information about the Python-checkins mailing list