From 06a8b0bb5ee0d01ed93315137c41c104a11e4640 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 8 Nov 2024 07:41:54 +0200 Subject: [PATCH] gh-122943: Remove the object converter for var-positional parameter (GH-126560) --- Lib/test/clinic.test.c | 24 ++++----- Lib/test/test_clinic.py | 24 ++++----- Modules/_testclinic.c | 76 ++++++++++++++-------------- Objects/typevarobject.c | 4 +- Python/bltinmodule.c | 13 ++--- Python/clinic/bltinmodule.c.h | 22 ++++---- Tools/clinic/libclinic/converters.py | 4 -- 7 files changed, 80 insertions(+), 87 deletions(-) diff --git a/Lib/test/clinic.test.c b/Lib/test/clinic.test.c index 80208862f2a..e653052e1b7 100644 --- a/Lib/test/clinic.test.c +++ b/Lib/test/clinic.test.c @@ -4135,7 +4135,7 @@ test_vararg_and_posonly a: object / - *args: object + *args: tuple [clinic start generated code]*/ @@ -4176,7 +4176,7 @@ exit: static PyObject * test_vararg_and_posonly_impl(PyObject *module, PyObject *a, PyObject *args) -/*[clinic end generated code: output=0c11c475e240869e input=9cfa748bbff09877]*/ +/*[clinic end generated code: output=0c11c475e240869e input=2c49a482f68545c0]*/ PyDoc_STRVAR(test_vararg_and_posonly__doc__, "test_vararg_and_posonly($module, a, /, *args)\n" @@ -4221,7 +4221,7 @@ test_vararg a: object - *args: object + *args: tuple [clinic start generated code]*/ @@ -4292,14 +4292,14 @@ exit: static PyObject * test_vararg_impl(PyObject *module, PyObject *a, PyObject *args) -/*[clinic end generated code: output=e7d7da6a7e008125 input=81d33815ad1bae6e]*/ +/*[clinic end generated code: output=e7d7da6a7e008125 input=7448995636d9186a]*/ /*[clinic input] test_vararg_with_default a: object - *args: object + *args: tuple b: bool = False [clinic start generated code]*/ @@ -4383,13 +4383,13 @@ exit: static PyObject * test_vararg_with_default_impl(PyObject *module, PyObject *a, PyObject *args, int b) -/*[clinic end generated code: output=46781f9920ecedcf input=6e110b54acd9b22d]*/ +/*[clinic end generated code: output=46781f9920ecedcf input=3a0f9f557ce1f712]*/ /*[clinic input] test_vararg_with_only_defaults - *args: object + *args: tuple b: bool = False c: object = ' ' @@ -4477,7 +4477,7 @@ exit: static PyObject * test_vararg_with_only_defaults_impl(PyObject *module, PyObject *args, int b, PyObject *c) -/*[clinic end generated code: output=d03daf5067039c03 input=fa56a709a035666e]*/ +/*[clinic end generated code: output=d03daf5067039c03 input=6983e66817f82924]*/ /*[clinic input] test_paramname_module @@ -4978,7 +4978,7 @@ Test_an_metho_arg_named_arg_impl(TestObj *self, int arg) /*[clinic input] Test.__init__ - *args: object + *args: tuple Varargs init method. For example, nargs is translated to PyTuple_GET_SIZE. [clinic start generated code]*/ @@ -5016,7 +5016,7 @@ exit: static int Test___init___impl(TestObj *self, PyObject *args) -/*[clinic end generated code: output=f172425cec373cd6 input=2a8bd0033c9ac772]*/ +/*[clinic end generated code: output=f172425cec373cd6 input=4b8388c4e6baab6f]*/ PyDoc_STRVAR(Test___init____doc__, "Test(*args)\n" @@ -5054,7 +5054,7 @@ exit: /*[clinic input] @classmethod Test.__new__ - *args: object + *args: tuple Varargs new method. For example, nargs is translated to PyTuple_GET_SIZE. [clinic start generated code]*/ @@ -5091,7 +5091,7 @@ exit: static PyObject * Test_impl(PyTypeObject *type, PyObject *args) -/*[clinic end generated code: output=ee1e8892a67abd4a input=70ad829df3dd9b84]*/ +/*[clinic end generated code: output=ee1e8892a67abd4a input=a8259521129cad20]*/ PyDoc_STRVAR(Test__doc__, "Test(*args)\n" diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index a4317a3ad46..3936acd81d4 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -338,7 +338,7 @@ class ClinicWholeFileTest(TestCase): my_test_func pos_arg: object - *args: object + *args: tuple * kw_arg: object [clinic start generated code]*/ @@ -353,7 +353,7 @@ class ClinicWholeFileTest(TestCase): pos_arg: object * - *args: object + *args: tuple kw_arg: object [clinic start generated code]*/ """ @@ -1816,7 +1816,7 @@ class ClinicParserTest(TestCase): foo.bar a: int * [from 3.14] - *args: object + *args: tuple b: int Docstring. """ @@ -1844,7 +1844,7 @@ class ClinicParserTest(TestCase): module foo foo.bar a: int - *args: object + *args: tuple * [from 3.14] b: int Docstring. @@ -1980,7 +1980,7 @@ class ClinicParserTest(TestCase): foo.bar x: int y: int - *args: object + *args: tuple z: int / """ @@ -2031,7 +2031,7 @@ class ClinicParserTest(TestCase): module foo foo.bar a: int - *args: object + *args: tuple / [from 3.14] b: int Docstring. @@ -2070,8 +2070,8 @@ class ClinicParserTest(TestCase): block = """ module foo foo.bar - *vararg1: object - *vararg2: object + *vararg1: tuple + *vararg2: tuple """ self.expect_failure(block, err, lineno=3) @@ -2156,11 +2156,11 @@ class ClinicParserTest(TestCase): block = """ module foo foo.bar - *vararg1: object - \t*vararg2: object + *vararg1: tuple + \t*vararg2: tuple """ err = ("Tab characters are illegal in the Clinic DSL: " - r"'\t*vararg2: object'") + r"'\t*vararg2: tuple'") self.expect_failure(block, err) def test_indent_stack_illegal_outdent(self): @@ -2505,7 +2505,7 @@ class ClinicParserTest(TestCase): err = "Vararg can't take a default value!" block = """ fn - *args: object = None + *args: tuple = None """ self.expect_failure(block, err, lineno=1) diff --git a/Modules/_testclinic.c b/Modules/_testclinic.c index 320c8ddf214..a5c4a4c40b3 100644 --- a/Modules/_testclinic.c +++ b/Modules/_testclinic.c @@ -980,13 +980,13 @@ keyword_only_parameter_impl(PyObject *module, PyObject *a) /*[clinic input] varpos - *args: object + *args: tuple [clinic start generated code]*/ static PyObject * varpos_impl(PyObject *module, PyObject *args) -/*[clinic end generated code: output=7b0b9545872bdca4 input=f87cd674145d394c]*/ +/*[clinic end generated code: output=7b0b9545872bdca4 input=ae7ccecd997aaff4]*/ { return Py_NewRef(args); } @@ -998,14 +998,14 @@ posonly_varpos a: object b: object / - *args: object + *args: tuple [clinic start generated code]*/ static PyObject * posonly_varpos_impl(PyObject *module, PyObject *a, PyObject *b, PyObject *args) -/*[clinic end generated code: output=5dae5eb2a0d623cd input=c9fd7895cfbaabba]*/ +/*[clinic end generated code: output=5dae5eb2a0d623cd input=6dd74417b62cbe67]*/ { return pack_arguments_newref(3, a, b, args); } @@ -1017,14 +1017,14 @@ posonly_req_opt_varpos a: object b: object = False / - *args: object + *args: tuple [clinic start generated code]*/ static PyObject * posonly_req_opt_varpos_impl(PyObject *module, PyObject *a, PyObject *b, PyObject *args) -/*[clinic end generated code: output=67f82f90838e166a input=a49bd64740171e1c]*/ +/*[clinic end generated code: output=67f82f90838e166a input=e08ed48926a5b760]*/ { return pack_arguments_newref(3, a, b, args); } @@ -1036,14 +1036,14 @@ posonly_poskw_varpos a: object / b: object - *args: object + *args: tuple [clinic start generated code]*/ static PyObject * posonly_poskw_varpos_impl(PyObject *module, PyObject *a, PyObject *b, PyObject *args) -/*[clinic end generated code: output=bffdb7649941c939 input=b3d7a734e0625f68]*/ +/*[clinic end generated code: output=bffdb7649941c939 input=e5a2c4cab6745ca1]*/ { return pack_arguments_newref(3, a, b, args); } @@ -1053,13 +1053,13 @@ posonly_poskw_varpos_impl(PyObject *module, PyObject *a, PyObject *b, poskw_varpos a: object - *args: object + *args: tuple [clinic start generated code]*/ static PyObject * poskw_varpos_impl(PyObject *module, PyObject *a, PyObject *args) -/*[clinic end generated code: output=2413ddfb5ef22794 input=a1dff12d00422484]*/ +/*[clinic end generated code: output=2413ddfb5ef22794 input=e78114436a07aefe]*/ { return pack_arguments_newref(2, a, args); } @@ -1069,7 +1069,7 @@ poskw_varpos_impl(PyObject *module, PyObject *a, PyObject *args) poskw_varpos_kwonly_opt a: object - *args: object + *args: tuple b: bool = False [clinic start generated code]*/ @@ -1077,7 +1077,7 @@ poskw_varpos_kwonly_opt static PyObject * poskw_varpos_kwonly_opt_impl(PyObject *module, PyObject *a, PyObject *args, int b) -/*[clinic end generated code: output=f36d35ba6133463b input=1721d43dc5f6d856]*/ +/*[clinic end generated code: output=f36d35ba6133463b input=ebecfb189f4a3380]*/ { PyObject *obj_b = b ? Py_True : Py_False; return pack_arguments_newref(3, a, args, obj_b); @@ -1088,7 +1088,7 @@ poskw_varpos_kwonly_opt_impl(PyObject *module, PyObject *a, PyObject *args, poskw_varpos_kwonly_opt2 a: object - *args: object + *args: tuple b: object = False c: object = False @@ -1097,7 +1097,7 @@ poskw_varpos_kwonly_opt2 static PyObject * poskw_varpos_kwonly_opt2_impl(PyObject *module, PyObject *a, PyObject *args, PyObject *b, PyObject *c) -/*[clinic end generated code: output=846cef62c6c40463 input=bb4b8d1577a8a408]*/ +/*[clinic end generated code: output=846cef62c6c40463 input=1aff29829431f711]*/ { return pack_arguments_newref(4, a, args, b, c); } @@ -1106,14 +1106,14 @@ poskw_varpos_kwonly_opt2_impl(PyObject *module, PyObject *a, PyObject *args, /*[clinic input] varpos_kwonly_opt - *args: object + *args: tuple b: object = False [clinic start generated code]*/ static PyObject * varpos_kwonly_opt_impl(PyObject *module, PyObject *args, PyObject *b) -/*[clinic end generated code: output=3b7bf98b091f5731 input=380fb00deec847e8]*/ +/*[clinic end generated code: output=3b7bf98b091f5731 input=1bec50dc49fca2eb]*/ { return pack_arguments_newref(2, args, b); } @@ -1122,7 +1122,7 @@ varpos_kwonly_opt_impl(PyObject *module, PyObject *args, PyObject *b) /*[clinic input] varpos_kwonly_req_opt - *args: object + *args: tuple a: object b: object = False c: object = False @@ -1132,7 +1132,7 @@ varpos_kwonly_req_opt static PyObject * varpos_kwonly_req_opt_impl(PyObject *module, PyObject *args, PyObject *a, PyObject *b, PyObject *c) -/*[clinic end generated code: output=165274e1fd037ae9 input=530794afd0690c22]*/ +/*[clinic end generated code: output=165274e1fd037ae9 input=355271f6119bb6c8]*/ { return pack_arguments_newref(4, args, a, b, c); } @@ -1219,7 +1219,7 @@ gh_32092_oob pos1: object pos2: object - *varargs: object + *varargs: tuple kw1: object = None kw2: object = None @@ -1230,7 +1230,7 @@ Proof-of-concept of GH-32092 OOB bug. static PyObject * gh_32092_oob_impl(PyObject *module, PyObject *pos1, PyObject *pos2, PyObject *varargs, PyObject *kw1, PyObject *kw2) -/*[clinic end generated code: output=ee259c130054653f input=46d15c881608f8ff]*/ +/*[clinic end generated code: output=ee259c130054653f input=63aeeca881979b91]*/ { Py_RETURN_NONE; } @@ -1240,7 +1240,7 @@ gh_32092_oob_impl(PyObject *module, PyObject *pos1, PyObject *pos2, gh_32092_kw_pass pos: object - *args: object + *args: tuple kw: object = None Proof-of-concept of GH-32092 keyword args passing bug. @@ -1250,7 +1250,7 @@ Proof-of-concept of GH-32092 keyword args passing bug. static PyObject * gh_32092_kw_pass_impl(PyObject *module, PyObject *pos, PyObject *args, PyObject *kw) -/*[clinic end generated code: output=4a2bbe4f7c8604e9 input=5c0bd5b9079a0cce]*/ +/*[clinic end generated code: output=4a2bbe4f7c8604e9 input=258987971f3ee97a]*/ { Py_RETURN_NONE; } @@ -1259,7 +1259,7 @@ gh_32092_kw_pass_impl(PyObject *module, PyObject *pos, PyObject *args, /*[clinic input] gh_99233_refcount - *args: object + *args: tuple Proof-of-concept of GH-99233 refcount error bug. @@ -1267,7 +1267,7 @@ Proof-of-concept of GH-99233 refcount error bug. static PyObject * gh_99233_refcount_impl(PyObject *module, PyObject *args) -/*[clinic end generated code: output=585855abfbca9a7f input=eecfdc2092d90dc3]*/ +/*[clinic end generated code: output=585855abfbca9a7f input=f5204359fd852613]*/ { Py_RETURN_NONE; } @@ -1295,7 +1295,7 @@ gh_99240_double_free_impl(PyObject *module, char *a, char *b) null_or_tuple_for_varargs name: object - *constraints: object + *constraints: tuple covariant: bool = False See https://github.com/python/cpython/issues/110864 @@ -1304,7 +1304,7 @@ See https://github.com/python/cpython/issues/110864 static PyObject * null_or_tuple_for_varargs_impl(PyObject *module, PyObject *name, PyObject *constraints, int covariant) -/*[clinic end generated code: output=a785b35421358983 input=c9bce186637956b3]*/ +/*[clinic end generated code: output=a785b35421358983 input=4df930e019f32878]*/ { assert(name != NULL); assert(constraints != NULL); @@ -1412,13 +1412,13 @@ _testclinic_TestClass_get_defining_class_arg_impl(PyObject *self, /*[clinic input] _testclinic.TestClass.defclass_varpos cls: defining_class - *args: object + *args: tuple [clinic start generated code]*/ static PyObject * _testclinic_TestClass_defclass_varpos_impl(PyObject *self, PyTypeObject *cls, PyObject *args) -/*[clinic end generated code: output=fad33f2d3a8d778d input=47071dcda393a7e1]*/ +/*[clinic end generated code: output=fad33f2d3a8d778d input=332043286e393d38]*/ { return PyTuple_Pack(2, cls, args); } @@ -1429,7 +1429,7 @@ _testclinic.TestClass.defclass_posonly_varpos a: object b: object / - *args: object + *args: tuple [clinic start generated code]*/ static PyObject * @@ -1437,7 +1437,7 @@ _testclinic_TestClass_defclass_posonly_varpos_impl(PyObject *self, PyTypeObject *cls, PyObject *a, PyObject *b, PyObject *args) -/*[clinic end generated code: output=1740fcf48d230b07 input=40f2e56286d4a7ef]*/ +/*[clinic end generated code: output=1740fcf48d230b07 input=191da4557203c413]*/ { return pack_arguments_newref(4, cls, a, b, args); } @@ -1447,13 +1447,13 @@ _testclinic_TestClass_defclass_posonly_varpos_impl(PyObject *self, @classmethod _testclinic.TestClass.__new__ as varpos_no_fastcall - *args: object + *args: tuple [clinic start generated code]*/ static PyObject * varpos_no_fastcall_impl(PyTypeObject *type, PyObject *args) -/*[clinic end generated code: output=04e94f2898bb2dde input=b0447ebab3e81001]*/ +/*[clinic end generated code: output=04e94f2898bb2dde input=c5d3d30a6589f97f]*/ { return Py_NewRef(args); } @@ -1466,14 +1466,14 @@ _testclinic.TestClass.__new__ as posonly_varpos_no_fastcall a: object b: object / - *args: object + *args: tuple [clinic start generated code]*/ static PyObject * posonly_varpos_no_fastcall_impl(PyTypeObject *type, PyObject *a, PyObject *b, PyObject *args) -/*[clinic end generated code: output=b0a0425719f69f5a input=d2ec37a06b3c2389]*/ +/*[clinic end generated code: output=b0a0425719f69f5a input=10f29f2c2c6bfdc4]*/ { return pack_arguments_newref(3, a, b, args); } @@ -1486,14 +1486,14 @@ _testclinic.TestClass.__new__ as posonly_req_opt_varpos_no_fastcall a: object b: object = False / - *args: object + *args: tuple [clinic start generated code]*/ static PyObject * posonly_req_opt_varpos_no_fastcall_impl(PyTypeObject *type, PyObject *a, PyObject *b, PyObject *args) -/*[clinic end generated code: output=3c44915b1a554e2d input=e9e74686a5e6a06d]*/ +/*[clinic end generated code: output=3c44915b1a554e2d input=d319302a8748147c]*/ { return pack_arguments_newref(3, a, b, args); } @@ -1506,14 +1506,14 @@ _testclinic.TestClass.__new__ as posonly_poskw_varpos_no_fastcall a: object / b: object - *args: object + *args: tuple [clinic start generated code]*/ static PyObject * posonly_poskw_varpos_no_fastcall_impl(PyTypeObject *type, PyObject *a, PyObject *b, PyObject *args) -/*[clinic end generated code: output=6ad74bed4bdc7f96 input=fa931c38184213aa]*/ +/*[clinic end generated code: output=6ad74bed4bdc7f96 input=1f8c113e749414a3]*/ { return pack_arguments_newref(3, a, b, args); } diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index 91cc37c9a72..bacb858978c 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -651,7 +651,7 @@ typevar_alloc(PyObject *name, PyObject *bound, PyObject *evaluate_bound, typevar.__new__ as typevar_new name: object(subclass_of="&PyUnicode_Type") - *constraints: object + *constraints: tuple bound: object = None default as default_value: object(c_default="&_Py_NoDefaultStruct") = typing.NoDefault covariant: bool = False @@ -665,7 +665,7 @@ static PyObject * typevar_new_impl(PyTypeObject *type, PyObject *name, PyObject *constraints, PyObject *bound, PyObject *default_value, int covariant, int contravariant, int infer_variance) -/*[clinic end generated code: output=d2b248ff074eaab6 input=836f97f631d7293a]*/ +/*[clinic end generated code: output=d2b248ff074eaab6 input=1b5b62e40c92c167]*/ { if (covariant && contravariant) { PyErr_SetString(PyExc_ValueError, diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 85a28de2bb9..a3f41190261 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -2127,7 +2127,7 @@ builtin_pow_impl(PyObject *module, PyObject *base, PyObject *exp, /*[clinic input] print as builtin_print - *args: object + *args: array sep: object(c_default="Py_None") = ' ' string inserted between values, default a space. end: object(c_default="Py_None") = '\n' @@ -2142,9 +2142,10 @@ Prints the values to a stream, or to sys.stdout by default. [clinic start generated code]*/ static PyObject * -builtin_print_impl(PyObject *module, PyObject *args, PyObject *sep, - PyObject *end, PyObject *file, int flush) -/*[clinic end generated code: output=3cfc0940f5bc237b input=c143c575d24fe665]*/ +builtin_print_impl(PyObject *module, PyObject * const *args, + Py_ssize_t args_length, PyObject *sep, PyObject *end, + PyObject *file, int flush) +/*[clinic end generated code: output=3cb7e5b66f1a8547 input=66ea4de1605a2437]*/ { int i, err; @@ -2181,7 +2182,7 @@ builtin_print_impl(PyObject *module, PyObject *args, PyObject *sep, return NULL; } - for (i = 0; i < PyTuple_GET_SIZE(args); i++) { + for (i = 0; i < args_length; i++) { if (i > 0) { if (sep == NULL) { err = PyFile_WriteString(" ", file); @@ -2193,7 +2194,7 @@ builtin_print_impl(PyObject *module, PyObject *args, PyObject *sep, return NULL; } } - err = PyFile_WriteObject(PyTuple_GET_ITEM(args, i), file, Py_PRINT_RAW); + err = PyFile_WriteObject(args[i], file, Py_PRINT_RAW); if (err) { return NULL; } diff --git a/Python/clinic/bltinmodule.c.h b/Python/clinic/bltinmodule.c.h index b47279686cc..39a23f09b7f 100644 --- a/Python/clinic/bltinmodule.c.h +++ b/Python/clinic/bltinmodule.c.h @@ -7,7 +7,6 @@ preserve # include "pycore_runtime.h" // _Py_ID() #endif #include "pycore_modsupport.h" // _PyArg_UnpackKeywords() -#include "pycore_tuple.h" // _PyTuple_FromArray() PyDoc_STRVAR(builtin___import____doc__, "__import__($module, /, name, globals=None, locals=None, fromlist=(),\n" @@ -902,8 +901,9 @@ PyDoc_STRVAR(builtin_print__doc__, {"print", _PyCFunction_CAST(builtin_print), METH_FASTCALL|METH_KEYWORDS, builtin_print__doc__}, static PyObject * -builtin_print_impl(PyObject *module, PyObject *args, PyObject *sep, - PyObject *end, PyObject *file, int flush); +builtin_print_impl(PyObject *module, PyObject * const *args, + Py_ssize_t args_length, PyObject *sep, PyObject *end, + PyObject *file, int flush); static PyObject * builtin_print(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) @@ -937,7 +937,8 @@ builtin_print(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec PyObject *argsbuf[4]; PyObject * const *fastargs; Py_ssize_t noptargs = 0 + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; - PyObject *__clinic_args = NULL; + PyObject * const *__clinic_args; + Py_ssize_t args_length; PyObject *sep = Py_None; PyObject *end = Py_None; PyObject *file = Py_None; @@ -973,16 +974,11 @@ builtin_print(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec goto exit; } skip_optional_kwonly: - __clinic_args = _PyTuple_FromArray(args, nargs); - if (__clinic_args == NULL) { - goto exit; - } - return_value = builtin_print_impl(module, __clinic_args, sep, end, file, flush); + __clinic_args = args; + args_length = nargs; + return_value = builtin_print_impl(module, __clinic_args, args_length, sep, end, file, flush); exit: - /* Cleanup for args */ - Py_XDECREF(__clinic_args); - return return_value; } @@ -1235,4 +1231,4 @@ builtin_issubclass(PyObject *module, PyObject *const *args, Py_ssize_t nargs) exit: return return_value; } -/*[clinic end generated code: output=76b27cf4164f257e input=a9049054013a1b77]*/ +/*[clinic end generated code: output=5c510ec462507656 input=a9049054013a1b77]*/ diff --git a/Tools/clinic/libclinic/converters.py b/Tools/clinic/libclinic/converters.py index 2d103c941cb..860ff8135fb 100644 --- a/Tools/clinic/libclinic/converters.py +++ b/Tools/clinic/libclinic/converters.py @@ -1251,7 +1251,3 @@ class varpos_array_converter(CConverter): def parse_arg(self, argname: str, displayname: str, *, limited_capi: bool) -> str | None: raise AssertionError('should never be called') - -# XXX: temporary -class varpos_object_converter(varpos_tuple_converter): - pass