diff -r c144bf6c0ff7 Lib/test/test_builtin.py --- a/Lib/test/test_builtin.py Thu Jan 19 19:38:13 2017 +0200 +++ b/Lib/test/test_builtin.py Thu Jan 19 20:26:32 2017 +0200 @@ -1627,6 +1627,16 @@ class TestSorted(unittest.TestCase): 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 -r c144bf6c0ff7 Python/bltinmodule.c --- a/Python/bltinmodule.c Thu Jan 19 19:38:13 2017 +0200 +++ b/Python/bltinmodule.c Thu Jan 19 20:26:32 2017 +0200 @@ -2128,7 +2128,7 @@ builtin_sorted(PyObject *self, PyObject { 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 @@ builtin_sorted(PyObject *self, PyObject return NULL; } + assert(nargs >= 1); v = _PyObject_FastCallKeywords(callable, args + 1, nargs - 1, kwnames); Py_DECREF(callable); if (v == NULL) {