Skip to content

Commit 5bb8b91

Browse files
Issue #18896: Python function can now have more than 255 parameters.
collections.namedtuple() now supports tuples with more than 255 elements.
1 parent 14d8b96 commit 5bb8b91

File tree

10 files changed

+33
-49
lines changed

10 files changed

+33
-49
lines changed

Doc/whatsnew/3.7.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ New Features
7575
Other Language Changes
7676
======================
7777

78-
* More than 255 arguments can now be passed to a function.
79-
(Contributed by Serhiy Storchaka in :issue:`12844`.)
78+
* More than 255 arguments can now be passed to a function, and a function can
79+
now have more than 255 parameters.
80+
(Contributed by Serhiy Storchaka in :issue:`12844` and :issue:`18896`.)
8081

8182

8283
New Modules

Include/code.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ typedef struct {
3737
for tracebacks and debuggers; otherwise, constant de-duplication
3838
would collapse identical functions/lambdas defined on different lines.
3939
*/
40-
unsigned char *co_cell2arg; /* Maps cell vars which are arguments. */
40+
Py_ssize_t *co_cell2arg; /* Maps cell vars which are arguments. */
4141
PyObject *co_filename; /* unicode (where it was loaded from) */
4242
PyObject *co_name; /* unicode (name, for reference) */
4343
PyObject *co_lnotab; /* string (encoding addr<->lineno mapping) See
@@ -84,9 +84,8 @@ typedef struct {
8484
#define CO_FUTURE_GENERATOR_STOP 0x80000
8585

8686
/* This value is found in the co_cell2arg array when the associated cell
87-
variable does not correspond to an argument. The maximum number of
88-
arguments is 255 (indexed up to 254), so 255 work as a special flag.*/
89-
#define CO_CELL_NOT_AN_ARG 255
87+
variable does not correspond to an argument. */
88+
#define CO_CELL_NOT_AN_ARG (-1)
9089

9190
/* This should be defined if a future statement modifies the syntax.
9291
For example, when a keyword is added.

Lib/test/test_collections.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,7 @@ def test_odd_sizes(self):
319319
self.assertEqual(Dot(1)._replace(d=999), (999,))
320320
self.assertEqual(Dot(1)._fields, ('d',))
321321

322-
# n = 5000
323-
n = 254 # SyntaxError: more than 255 arguments:
322+
n = 5000
324323
names = list(set(''.join([choice(string.ascii_letters)
325324
for j in range(10)]) for i in range(n)))
326325
n = len(names)

Lib/test/test_compile.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -401,16 +401,9 @@ def __contains__(self, key):
401401
self.assertNotIn((Ellipsis, Ellipsis), d)
402402

403403
def test_annotation_limit(self):
404-
# 16 bits are available for # of annotations, but only 8 bits are
405-
# available for the parameter count, hence 255
406-
# is the max. Ensure the result of too many annotations is a
407-
# SyntaxError.
404+
# more than 255 annotations, should compile ok
408405
s = "def f(%s): pass"
409-
s %= ', '.join('a%d:%d' % (i,i) for i in range(256))
410-
self.assertRaises(SyntaxError, compile, s, '?', 'exec')
411-
# Test that the max # of annotations compiles.
412-
s = "def f(%s): pass"
413-
s %= ', '.join('a%d:%d' % (i,i) for i in range(255))
406+
s %= ', '.join('a%d:%d' % (i,i) for i in range(300))
414407
compile(s, '?', 'exec')
415408

416409
def test_mangling(self):

Lib/test/test_keywordonlyarg.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,12 @@ def testSyntaxErrorForFunctionDefinition(self):
5151
self.assertRaisesSyntaxError("def f(p, *, (k1, k2), **kw):\n pass\n")
5252

5353
def testSyntaxForManyArguments(self):
54-
fundef = "def f("
55-
for i in range(255):
56-
fundef += "i%d, "%i
57-
fundef += "*, key=100):\n pass\n"
58-
self.assertRaisesSyntaxError(fundef)
59-
60-
fundef2 = "def foo(i,*,"
61-
for i in range(255):
62-
fundef2 += "i%d, "%i
63-
fundef2 += "lastarg):\n pass\n"
64-
self.assertRaisesSyntaxError(fundef2)
65-
66-
# exactly 255 arguments, should compile ok
67-
fundef3 = "def f(i,*,"
68-
for i in range(253):
69-
fundef3 += "i%d, "%i
70-
fundef3 += "lastarg):\n pass\n"
71-
compile(fundef3, "<test>", "single")
54+
# more than 255 positional arguments, should compile ok
55+
fundef = "def f(%s):\n pass\n" % ', '.join('i%d' % i for i in range(300))
56+
compile(fundef, "<test>", "single")
57+
# more than 255 keyword-only arguments, should compile ok
58+
fundef = "def f(*, %s):\n pass\n" % ', '.join('i%d' % i for i in range(300))
59+
compile(fundef, "<test>", "single")
7260

7361
def testTooManyPositionalErrorMessage(self):
7462
def f(a, b=None, *, c=None):

Lib/test/test_sys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ def get_cell2(x):
926926
def inner():
927927
return x
928928
return inner
929-
check(get_cell2.__code__, size('6i13P') + 1)
929+
check(get_cell2.__code__, size('6i13P') + calcsize('n'))
930930
# complex
931931
check(complex(0,1), size('2d'))
932932
# method_descriptor (descriptor object)

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 #18896: Python function can now have more than 255 parameters.
14+
collections.namedtuple() now supports tuples with more than 255 elements.
15+
1316
- Issue #26919: On Android, operating system data is now always encoded/decoded
1417
to/from UTF-8, instead of the locale encoding to avoid inconsistencies with
1518
os.fsencode() and os.fsdecode() which are already using UTF-8.

Objects/codeobject.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ PyCode_New(int argcount, int kwonlyargcount,
110110
PyObject *lnotab)
111111
{
112112
PyCodeObject *co;
113-
unsigned char *cell2arg = NULL;
113+
Py_ssize_t *cell2arg = NULL;
114114
Py_ssize_t i, n_cellvars;
115115

116116
/* Check argument types */
@@ -142,19 +142,25 @@ PyCode_New(int argcount, int kwonlyargcount,
142142
if (n_cellvars) {
143143
Py_ssize_t total_args = argcount + kwonlyargcount +
144144
((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
145-
Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars;
146145
bool used_cell2arg = false;
147-
cell2arg = PyMem_MALLOC(alloc_size);
148-
if (cell2arg == NULL)
146+
cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
147+
if (cell2arg == NULL) {
148+
PyErr_NoMemory();
149149
return NULL;
150-
memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size);
150+
}
151151
/* Find cells which are also arguments. */
152152
for (i = 0; i < n_cellvars; i++) {
153153
Py_ssize_t j;
154154
PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
155+
cell2arg[i] = CO_CELL_NOT_AN_ARG;
155156
for (j = 0; j < total_args; j++) {
156157
PyObject *arg = PyTuple_GET_ITEM(varnames, j);
157-
if (!PyUnicode_Compare(cell, arg)) {
158+
int cmp = PyUnicode_Compare(cell, arg);
159+
if (cmp == -1 && PyErr_Occurred()) {
160+
PyMem_FREE(cell2arg);
161+
return NULL;
162+
}
163+
if (cmp == 0) {
158164
cell2arg[i] = j;
159165
used_cell2arg = true;
160166
break;
@@ -449,7 +455,7 @@ code_sizeof(PyCodeObject *co, void *unused)
449455

450456
res = _PyObject_SIZE(Py_TYPE(co));
451457
if (co->co_cell2arg != NULL && co->co_cellvars != NULL)
452-
res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char);
458+
res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
453459
return PyLong_FromSsize_t(res);
454460
}
455461

Python/ast.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,11 +1411,6 @@ ast_for_arguments(struct compiling *c, const node *n)
14111411
if (!kwdefaults && nkwonlyargs)
14121412
return NULL;
14131413

1414-
if (nposargs + nkwonlyargs > 255) {
1415-
ast_error(c, n, "more than 255 arguments");
1416-
return NULL;
1417-
}
1418-
14191414
/* tfpdef: NAME [':' test]
14201415
vfpdef: NAME
14211416
*/

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4100,7 +4100,7 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
41004100
vars into frame. */
41014101
for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
41024102
PyObject *c;
4103-
int arg;
4103+
Py_ssize_t arg;
41044104
/* Possibly account for the cell variable being an argument. */
41054105
if (co->co_cell2arg != NULL &&
41064106
(arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {

0 commit comments

Comments
 (0)