+33
-49
lines changedFilter options
+33
-49
lines changed Original file line number Diff line number Diff line change
@@ -75,8 +75,9 @@ New Features
75
75
Other Language Changes
76
76
======================
77
77
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`.)
80
81
81
82
82
83
New Modules
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ typedef struct {
37
37
for tracebacks and debuggers; otherwise, constant de-duplication
38
38
would collapse identical functions/lambdas defined on different lines.
39
39
*/
40
-
unsigned char *co_cell2arg; /* Maps cell vars which are arguments. */
40
+
Py_ssize_t *co_cell2arg; /* Maps cell vars which are arguments. */
41
41
PyObject *co_filename; /* unicode (where it was loaded from) */
42
42
PyObject *co_name; /* unicode (name, for reference) */
43
43
PyObject *co_lnotab; /* string (encoding addr<->lineno mapping) See
@@ -84,9 +84,8 @@ typedef struct {
84
84
#define CO_FUTURE_GENERATOR_STOP 0x80000
85
85
86
86
/* 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)
90
89
91
90
/* This should be defined if a future statement modifies the syntax.
92
91
For example, when a keyword is added.
Original file line number Diff line number Diff line change
@@ -319,8 +319,7 @@ def test_odd_sizes(self):
319
319
self.assertEqual(Dot(1)._replace(d=999), (999,))
320
320
self.assertEqual(Dot(1)._fields, ('d',))
321
321
322
-
# n = 5000
323
-
n = 254 # SyntaxError: more than 255 arguments:
322
+
n = 5000
324
323
names = list(set(''.join([choice(string.ascii_letters)
325
324
for j in range(10)]) for i in range(n)))
326
325
n = len(names)
Original file line number Diff line number Diff line change
@@ -401,16 +401,9 @@ def __contains__(self, key):
401
401
self.assertNotIn((Ellipsis, Ellipsis), d)
402
402
403
403
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
408
405
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))
414
407
compile(s, '?', 'exec')
415
408
416
409
def test_mangling(self):
Original file line number Diff line number Diff line change
@@ -51,24 +51,12 @@ def testSyntaxErrorForFunctionDefinition(self):
51
51
self.assertRaisesSyntaxError("def f(p, *, (k1, k2), **kw):\n pass\n")
52
52
53
53
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")
72
60
73
61
def testTooManyPositionalErrorMessage(self):
74
62
def f(a, b=None, *, c=None):
Original file line number Diff line number Diff line change
@@ -926,7 +926,7 @@ def get_cell2(x):
926
926
def inner():
927
927
return x
928
928
return inner
929
-
check(get_cell2.__code__, size('6i13P') + 1)
929
+
check(get_cell2.__code__, size('6i13P') + calcsize('n'))
930
930
# complex
931
931
check(complex(0,1), size('2d'))
932
932
# method_descriptor (descriptor object)
Original file line number Diff line number Diff line change
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1
10
10
Core and Builtins
11
11
-----------------
12
12
13
+
- Issue #18896: Python function can now have more than 255 parameters.
14
+
collections.namedtuple() now supports tuples with more than 255 elements.
15
+
13
16
- Issue #26919: On Android, operating system data is now always encoded/decoded
14
17
to/from UTF-8, instead of the locale encoding to avoid inconsistencies with
15
18
os.fsencode() and os.fsdecode() which are already using UTF-8.
Original file line number Diff line number Diff line change
@@ -110,7 +110,7 @@ PyCode_New(int argcount, int kwonlyargcount,
110
110
PyObject *lnotab)
111
111
{
112
112
PyCodeObject *co;
113
-
unsigned char *cell2arg = NULL;
113
+
Py_ssize_t *cell2arg = NULL;
114
114
Py_ssize_t i, n_cellvars;
115
115
116
116
/* Check argument types */
@@ -142,19 +142,25 @@ PyCode_New(int argcount, int kwonlyargcount,
142
142
if (n_cellvars) {
143
143
Py_ssize_t total_args = argcount + kwonlyargcount +
144
144
((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
145
-
Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars;
146
145
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();
149
149
return NULL;
150
-
memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size);
150
+
}
151
151
/* Find cells which are also arguments. */
152
152
for (i = 0; i < n_cellvars; i++) {
153
153
Py_ssize_t j;
154
154
PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
155
+
cell2arg[i] = CO_CELL_NOT_AN_ARG;
155
156
for (j = 0; j < total_args; j++) {
156
157
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) {
158
164
cell2arg[i] = j;
159
165
used_cell2arg = true;
160
166
break;
@@ -449,7 +455,7 @@ code_sizeof(PyCodeObject *co, void *unused)
449
455
450
456
res = _PyObject_SIZE(Py_TYPE(co));
451
457
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);
453
459
return PyLong_FromSsize_t(res);
454
460
}
455
461
Original file line number Diff line number Diff line change
@@ -1411,11 +1411,6 @@ ast_for_arguments(struct compiling *c, const node *n)
1411
1411
if (!kwdefaults && nkwonlyargs)
1412
1412
return NULL;
1413
1413
1414
-
if (nposargs + nkwonlyargs > 255) {
1415
-
ast_error(c, n, "more than 255 arguments");
1416
-
return NULL;
1417
-
}
1418
-
1419
1414
/* tfpdef: NAME [':' test]
1420
1415
vfpdef: NAME
1421
1416
*/
Original file line number Diff line number Diff line change
@@ -4100,7 +4100,7 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4100
4100
vars into frame. */
4101
4101
for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
4102
4102
PyObject *c;
4103
-
int arg;
4103
+
Py_ssize_t arg;
4104
4104
/* Possibly account for the cell variable being an argument. */
4105
4105
if (co->co_cell2arg != NULL &&
4106
4106
(arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4