Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 299dc23

Browse files
Issue python#29327: Fixed a crash when pass the iterable keyword argument to sorted().
2 parents aecbef4 + 398ef5c commit 299dc23

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Lib/test/test_builtin.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,16 @@ def test_basic(self):
16271627
self.assertEqual(data, sorted(copy, reverse=1))
16281628
self.assertNotEqual(data, copy)
16291629

1630+
def test_bad_arguments(self):
1631+
# Issue #29327: The first argument is positional-only.
1632+
sorted([])
1633+
with self.assertRaises(TypeError):
1634+
sorted(iterable=[])
1635+
# Other arguments are keyword-only
1636+
sorted([], key=None)
1637+
with self.assertRaises(TypeError):
1638+
sorted([], None)
1639+
16301640
def test_inputtypes(self):
16311641
s = 'abracadabra'
16321642
types = [list, tuple, str]

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #29327: Fixed a crash when pass the iterable keyword argument to
14+
sorted().
15+
1316
- Issue #29034: Fix memory leak and use-after-free in os module (path_converter).
1417

1518
- Issue #29159: Fix regression in bytes(x) when x.__index__() raises Exception.

Python/bltinmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2128,7 +2128,7 @@ builtin_sorted(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwna
21282128
{
21292129
PyObject *newlist, *v, *seq, *keyfunc=NULL;
21302130
PyObject *callable;
2131-
static const char * const kwlist[] = {"iterable", "key", "reverse", 0};
2131+
static const char * const kwlist[] = {"", "key", "reverse", 0};
21322132
/* args 1-3 should match listsort in Objects/listobject.c */
21332133
static _PyArg_Parser parser = {"O|Oi:sorted", kwlist, 0};
21342134
int reverse;
@@ -2147,6 +2147,7 @@ builtin_sorted(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwna
21472147
return NULL;
21482148
}
21492149

2150+
assert(nargs >= 1);
21502151
v = _PyObject_FastCallKeywords(callable, args + 1, nargs - 1, kwnames);
21512152
Py_DECREF(callable);
21522153
if (v == NULL) {

0 commit comments

Comments
 (0)