8000 Merge branch main into main-slp · stackless-dev/stackless@5f6645b · GitHub
[go: up one dir, main page]

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

Commit 5f6645b

Browse files
author
Anselm Kruis
committed
Merge branch main into main-slp
2 parents 3b9db00 + cd74e66 commit 5f6645b

File tree

14 files changed

+74
-88
lines changed

14 files changed

+74
-88
lines changed

Doc/c-api/code.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ bound into a function.
4242
4343
.. versionchanged:: 3.8
4444
An extra parameter is required (*posonlyargcount*) to support :PEP:`570`.
45+
The first parameter (*argcount*) now represents the total number of positional arguments,
46+
including positional-only.
4547
4648
.. audit-event:: code.__new__ "code filename name argcount kwonlyargcount nlocals stacksize flags"
4749

Doc/reference/datamodel.rst

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -907,24 +907,26 @@ Internal types
907907
single: co_freevars (code object attribute)
908908

909909
Special read-only attributes: :attr:`co_name` gives the function name;
910-
:attr:`co_argcount` is the number of positional arguments (including arguments
911-
with default values); :attr:`co_posonlyargcount` is the number of
912-
positional-only arguments (including arguments with default values);
913-
:attr:`co_kwonlyargcount` is the number of keyword-only arguments (including
914-
arguments with default values); :attr:`co_nlocals` is the number of local
915-
variables used by the function (including arguments); :attr:`co_varnames` is a
916-
tuple containing the names of the local variables (starting with the argument
917-
names); :attr:`co_cellvars` is a tuple containing the names of local variables
910+
:attr:`co_argcount` is the total number of positional arguments
911+
(including positional-only arguments and arguments with default values);
912+
:attr:`co_posonlyargcount` is the number of positional-only arguments
913+
(including arguments with default values); :attr:`co_kwonlyargcount` is
914+
the number of keyword-only arguments (including arguments with default
915+
values); :attr:`co_nlocals` is the number of local variables used by the
916+
function (including arguments); :attr:`co_varnames` is a tuple containing
917+
the names of the local variables (starting with the argument names);
918+
:attr:`co_cellvars` is a tuple containing the names of local variables
918919
that are referenced by nested functions; :attr:`co_freevars` is a tuple
919-
containing the names of free variables; :attr:`co_code` is a string representing
920-
the sequence of bytecode instructions; :attr:`co_consts` is a tuple containing
921-
the literals used by the bytecode; :attr:`co_names` is a tuple containing the
922-
names used by the bytecode; :attr:`co_filename` is the filename from which the
923-
code was compiled; :attr:`co_firstlineno` is the first line number of the
924-
function; :attr:`co_lnotab` is a string encoding the mapping from bytecode
925-
offsets to line numbers (for details see the source code of the interpreter);
926-
:attr:`co_stacksize` is the required stack size (including local variables);
927-
:attr:`co_flags` is an integer encoding a number of flags for the interpreter.
920+
containing the names of free variables; :attr:`co_code` is a string
921+
representing the sequence of bytecode instructions; :attr:`co_consts` is
922+
a tuple containing the literals used by the bytecode; :attr:`co_names` is
923+
a tuple containing the names used by the bytecode; :attr:`co_filename` is
924+
the filename from which the code was compiled; :attr:`co_firstlineno` is
925+
the first line number of the function; :attr:`co_lnotab` is a string
926+
encoding the mapping from bytecode offsets to line numbers (for details
927+
see the source code of the interpreter); :attr:`co_stacksize` is the
928+
required stack size (including local variables); :attr:`co_flags` is an
929+
integer encoding a number of flags for the interpreter.
928930

929931
.. index:: object: generator
930932

Doc/whatsnew/3.8.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,8 +1177,10 @@ Changes in the Python API
11771177

11781178
* :class:`types.CodeType` has a new parameter in the second position of the
11791179
constructor (*posonlyargcount*) to support positional-only arguments defined
1180-
in :pep:`570`. A new ``replace()`` method of :class:`types.CodeType` can be
1181-
used to make the code future-proof.
1180+
in :pep:`570`. The first argument (*argcount*) now represents the total
1181+
number of positional arguments (including positional-only arguments). A new
1182+
``replace()`` method of :class:`types.CodeType` can be used to make the code
1183+
future-proof.
11821184

11831185

11841186
Changes in the C API

Lib/inspect.py

Copy file name to clipboard
Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,15 +1037,11 @@ def getargs(co):
10371037

10381038
names = co.co_varnames
10391039
nargs = co.co_argcount
1040-
nposonlyargs = co.co_posonlyargcount
10411040
nkwargs = co.co_kwonlyargcount
1042-
nposargs = nargs + nposonlyargs
1043-
posonlyargs = list(names[:nposonlyargs])
1044-
args = list(names[nposonlyargs:nposonlyargs+nargs])
1045-
kwonlyargs = list(names[nposargs:nposargs+nkwargs])
1041+
args = list(names[:nargs])
1042+
kwonlyargs = list(names[nargs:nargs+nkwargs])
10461043
step = 0
10471044

1048-
nargs += nposonlyargs
10491045
nargs += nkwargs
10501046
varargs = None
10511047
if co.co_flags & CO_VARARGS:
@@ -1054,7 +1050,7 @@ def getargs(co):
10541050
varkw = None
10551051
if co.co_flags & CO_VARKEYWORDS:
10561052
varkw = co.co_varnames[nargs]
1057-
return Arguments(posonlyargs + args + kwonlyargs, varargs, varkw)
1053+
return Arguments(args + kwonlyargs, varargs, varkw)
10581054

10591055
ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
10601056

@@ -2136,11 +2132,9 @@ def _signature_from_function(cls, func, skip_bound_arg=True):
21362132
pos_count = func_code.co_argcount
21372133
arg_names = func_code.co_varnames
21382134
posonly_count = func_code.co_posonlyargcount
2139-
positional_count = posonly_count + pos_count
2140-
positional_only = tuple(arg_names[:posonly_count])
2141-
positional = tuple(arg_names[posonly_count:positional_count])
2135+
positional = arg_names[:pos_count]
21422136
keyword_only_count = func_code.co_kwonlyargcount
2143-
keyword_only = arg_names[positional_count:(positional_count + keyword_only_count)]
2137+
keyword_only = arg_names[pos_count:pos_count + keyword_only_count]
21442138
annotations = func.__annotations__
21452139
defaults = func.__defaults__
21462140
kwdefaults = func.__kwdefaults__
@@ -2152,13 +2146,11 @@ def _signature_from_function(cls, func, skip_bound_arg=True):
21522146

21532147
parameters = []
21542148

2155-
non_default_count = positional_count - pos_default_count
2156-
all_positional = positional_only + positional
2157-
2149+
non_default_count = pos_count - pos_default_count
21582150
posonly_left = posonly_count
21592151

21602152
# Non-keyword-only parameters w/o defaults.
2161-
for name in all_positional[:non_default_count]:
2153+
for name in positional[:non_default_count]:
21622154
kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD
21632155
annotation = annotations.get(name, _empty)
21642156
parameters.append(Parameter(name, annotation=annotation,
@@ -2167,7 +2159,7 @@ def _signature_from_function(cls, func, skip_bound_arg=True):
21672159
posonly_left -= 1
21682160

21692161
# ... w/ defaults.
2170-
for offset, name in enumerate(all_positional[non_default_count:]):
2162+
for offset, name in enumerate(positional[non_default_count:]):
21712163
kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD
21722164
annotation = annotations.get(name, _empty)
21732165
parameters.append(Parameter(name, annotation=annotation,
@@ -2178,7 +2170,7 @@ def _signature_from_function(cls, func, skip_bound_arg=True):
21782170

21792171
# *args
21802172
if func_code.co_flags & CO_VARARGS:
2181-
name = arg_names[positional_count + keyword_only_count]
2173+
name = arg_names[pos_count + keyword_only_count]
21822174
annotation = annotations.get(name, _empty)
21832175
parameters.append(Parameter(name, annotation=annotation,
21842176
kind=_VAR_POSITIONAL))
@@ -2195,7 +2187,7 @@ def _signature_from_function(cls, func, skip_bound_arg=True):
21952187
default=default))
21962188
# **kwargs
21972189
if func_code.co_flags & CO_VARKEYWORDS:
2198-
index = positional_count + keyword_only_count
2190+
index = pos_count + keyword_only_count
21992191
if func_code.co_flags & CO_VARARGS:
22002192
index += 1
22012193

Lib/pdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ def do_args(self, arg):
11321132
"""
11331133
co = self.curframe.f_code
11341134
dict = self.curframe_locals
1135-
n = co.co_argcount + co.co_posonlyargcount + co.co_kwonlyargcount
1135+
n = co.co_argcount + co.co_kwonlyargcount
11361136
if co.co_flags & inspect.CO_VARARGS: n = n+1
11371137
if co.co_flags & inspect.CO_VARKEYWORDS: n = n+1
11381138
for i in range(n):

Lib/test/test_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
113113
>>> dump(posonly_args.__code__)
114114
name: posonly_args
115-
argcount: 1
115+
argcount: 3
116116
posonlyargcount: 2
117117
kwonlyargcount: 0
118118
names: ()

Lib/test/test_dis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ def f(c=c):
640640
code_info_tricky = """\
641641
Name: tricky
642642
Filename: (.*)
643-
Argument count: 3
643+
Argument count: 5
644644
Positional-only arguments: 2
645645
Kw-only arguments: 3
646646
Number of locals: 10

Lib/test/test_positional_only_arg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ def test_pos_only_definition(self):
100100
def f(a, b, c, /, d, e=1, *, f, g=2):
101101
pass
102102

103-
self.assertEqual(2, f.__code__.co_argcount) # 2 "standard args"
103+
self.assertEqual(5, f.__code__.co_argcount) # 3 posonly + 2 "standard args"
104104
self.assertEqual(3, f.__code__.co_posonlyargcount)
105105
self.assertEqual((1,), f.__defaults__)
106106

107107
def f(a, b, c=1, /, d=2, e=3, *, f, g=4):
108108
pass
109109

110-
self.assertEqual(2, f.__code__.co_argcount) # 2 "standard args"
110+
self.assertEqual(5, f.__code__.co_argcount) # 3 posonly + 2 "standard args"
111111
self.assertEqual(3, f.__code__.co_posonlyargcount)
112112
self.assertEqual((1, 2, 3), f.__defaults__)
113113

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Make the *co_argcount* attribute of code objects represent the total number
2+
of positional arguments (including positional-only arguments). The value of
3+
*co_posonlyargcount* can be used to distinguish which arguments are
4+
positional only, and the difference (*co_argcount* - *co_posonlyargcount*)
5+
is the number of positional-or-keyword arguments. Patch by Pablo Galindo.

Objects/call.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,14 @@ _PyFunction_FastCallDict(PyObject *func, PyObject *const *args, Py_ssize_t nargs
354354
(co->co_flags & ~PyCF_MASK) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
355355
{
356356
/* Fast paths */
357-
if (argdefs == NULL && co->co_argcount + co->co_posonlyargcount == nargs) {
357+
if (argdefs == NULL && co->co_argcount == nargs) {
358358
STACKLESS_PROMOTE_ALL();
359359
result = function_code_fastcall(co, args, nargs, globals);
360360
STACKLESS_ASSERT();
361361
return result;
362362
}
363363
else if (nargs == 0 && argdefs != NULL
364-
&& co->co_argcount + co->co_posonlyargcount == PyTuple_GET_SIZE(argdefs)) {
364+
&& co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
365365
/* function called with no arguments, but all parameters have
366366
a default value: use default values as arguments .*/
367367
args = _PyTuple_ITEMS(argdefs);
@@ -452,14 +452,14 @@ _PyFunction_Vectorcall(PyObject *func, PyObject* const* stack,
452452
if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
453453
(co->co_flags & ~PyCF_MASK) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
454454
{
455-
if (argdefs == NULL && co->co_argcount + co->co_posonlyargcount== nargs) {
455+
if (argdefs == NULL && co->co_argcount == nargs) {
456456
STACKLESS_PROMOTE_ALL();
457457
result = function_code_fastcall(co, stack, nargs, globals);
458458
STACKLESS_ASSERT();
459459
return result;
460460
}
461461
else if (nargs == 0 && argdefs != NULL
462-
&& co->co_argcount + co->co_posonlyargcount == PyTuple_GET_SIZE(argdefs)) {
462+
&& co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
463463
/* function called with no arguments, but all parameters have
464464
a default value: use default values as arguments .*/
465465
stack = _PyTuple_ITEMS(argdefs);

Objects/codeobject.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ PyCode_New(int argcount, int posonlyargcount, int kwonlyargcount,
114114
Py_ssize_t i, n_cellvars, n_varnames, total_args;
115115

116116
/* Check argument types */
117-
if (argcount < 0 || posonlyargcount < 0 || kwonlyargcount < 0 ||
118-
nlocals < 0 || stacksize < 0 || flags < 0 ||
117+
if (argcount < posonlyargcount || posonlyargcount < 0 ||
118+
kwonlyargcount < 0 || nlocals < 0 ||
119+
stacksize < 0 || flags < 0 ||
119120
code == NULL || !PyBytes_Check(code) ||
120121
consts == NULL || !PyTuple_Check(consts) ||
121122
names == NULL || !PyTuple_Check(names) ||
@@ -152,11 +153,9 @@ PyCode_New(int argcount, int posonlyargcount, int kwonlyargcount,
152153
}
153154

154155
n_varnames = PyTuple_GET_SIZE(varnames);
155-
if (posonlyargcount + argcount <= n_varnames
156-
&& kwonlyargcount <= n_varnames) {
156+
if (argcount <= n_varnames && kwonlyargcount <= n_varnames) {
157157
/* Never overflows. */
158-
total_args = (Py_ssize_t)posonlyargcount + (Py_ssize_t)argcount
159-
+ (Py_ssize_t)kwonlyargcount +
158+
total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount +
160159
((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
161160
}
162161
else {

Objects/typeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8123,7 +8123,7 @@ super_init(PyObject *self, PyObject *args, PyObject *kwds)
81238123
"super(): no code object");
81248124
return -1;
81258125
}
8126-
if (co->co_posonlyargcount + co->co_argcount == 0) {
8126+
if (co->co_argcount == 0) {
81278127
PyErr_SetString(PyExc_RuntimeError,
81288128
"super(): no arguments");
81298129
return -1;

Python/ceval.c

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4257,10 +4257,10 @@ missing_arguments(PyThreadState *tstate, PyCodeObject *co,
42574257
return;
42584258
if (positional) {
42594259
start = 0;
4260-
end = co->co_posonlyargcount + co->co_argcount - defcount;
4260+
end = co->co_argcount - defcount;
42614261
}
42624262
else {
4263-
start = co->co_posonlyargcount + co->co_argcount;
4263+
start = co->co_argcount;
42644264
end = start + co->co_kwonlyargcount;
42654265
}
42664266
for (i = start; i < end; i++) {
@@ -4288,25 +4288,23 @@ too_many_positional(PyThreadState *tstate, PyCodeObject *co,
42884288
Py_ssize_t kwonly_given = 0;
42894289
Py_ssize_t i;
42904290
PyObject *sig, *kwonly_sig;
4291-
Py_ssize_t co_posonlyargcount = co->co_posonlyargcount;
42924291
Py_ssize_t co_argcount = co->co_argcount;
4293-
Py_ssize_t total_positional = co_argcount + co_posonlyargcount;
42944292

42954293
assert((co->co_flags & CO_VARARGS) == 0);
42964294
/* Count missing keyword-only args. */
4297-
for (i = total_positional; i < total_positional + co->co_kwonlyargcount; i++) {
4295+
for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
42984296
if (GETLOCAL(i) != NULL) {
42994297
kwonly_given++;
43004298
}
43014299
}
43024300
if (defcount) {
4303-
Py_ssize_t atleast = total_positional - defcount;
4301+
Py_ssize_t atleast = co_argcount - defcount;
43044302
plural = 1;
4305-
sig = PyUnicode_FromFormat("from %zd to %zd", atleast, total_positional);
4303+
sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
43064304
}
43074305
else {
4308-
plural = (total_positional != 1);
4309-
sig = PyUnicode_FromFormat("%zd", total_positional);
4306+
plural = (co_argcount != 1);
4307+
sig = PyUnicode_FromFormat("%zd", co_argcount);
43104308
}
43114309
if (sig == NULL)
43124310
return;
@@ -4418,7 +4416,7 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
44184416
PyObject *retval = NULL;
44194417
PyObject **fastlocals, **freevars;
44204418
PyObject *x, *u;
4421-
const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount + co->co_posonlyargcount;
4419+
const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
44224420
Py_ssize_t i, j, n;
44234421
PyObject *kwdict;
44244422

@@ -4454,9 +4452,9 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
44544452
kwdict = NULL;
44554453
}
44564454

4457-
/* Copy positional only arguments into local variables */
4458-
if (argcount > co->co_argcount + co->co_posonlyargcount) {
4459-
n = co->co_posonlyargcount;
4455+
/* Copy all positional arguments into local variables */
4456+
if (argcount > co->co_argcount) {
4457+
n = co->co_argcount;
44604458
}
44614459
else {
44624460
n = argcount;
@@ -4467,20 +4465,6 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
44674465
SETLOCAL(j, x);
44684466
}
44694467

4470-
4471-
/* Copy positional arguments into local variables */
4472-
if (argcount > co->co_argcount + co->co_posonlyargcount) {
4473-
n += co->co_argcount;
4474-
}
4475-
else {
4476-
n = argcount;
4477-
}
4478-
for (i = j; i < n; i++) {
4479-
x = args[i];
4480-
Py_INCREF(x);
4481-
SETLOCAL(i, x);
4482-
}
4483-
44844468
/* Pack other positional arguments into the *args argument */
44854469
if (co->co_flags & CO_VARARGS) {
44864470
u = _PyTuple_FromArray(args + n, argcount - n);
@@ -4560,14 +4544,14 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
45604544
}
45614545

45624546
/* Check the number of positional arguments */
4563-
if ((argcount > co->co_argcount + co->co_posonlyargcount) && !(co->co_flags & CO_VARARGS)) {
4547+
if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
45644548
too_many_positional(tstate, co, argcount, defcount, fastlocals);
45654549
goto fail;
45664550
}
45674551

45684552
/* Add missing positional arguments (copy default values from defs) */
4569-
if (argcount < co->co_posonlyargcount + co->co_argcount) {
4570-
Py_ssize_t m = co->co_posonlyargcount + co->co_argcount - defcount;
4553+
if (argcount < co->co_argcount) {
4554+
Py_ssize_t m = co->co_argcount - defcount;
45714555
Py_ssize_t missing = 0;
45724556
for (i = argcount; i < m; i++) {
45734557
if (GETLOCAL(i) == NULL) {
@@ -4594,7 +4578,7 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
45944578
/* Add missing keyword arguments (copy default values from kwdefs) */
45954579
if (co->co_kwonlyargcount > 0) {
45964580
Py_ssize_t missing = 0;
4597-
for (i = co->co_posonlyargcount + co->co_argcount; i < total_args; i++) {
4581+
for (i = co->co_argcount; i < total_args; i++) {
45984582
PyObject *name;
45994583
if (GETLOCAL(i) != NULL)
46004584
continue;

0 commit comments

Comments
 (0)
0