Skip to content

Commit 9a74d01

Browse files
pythongh-68395: Avoid naming conflicts by mangling variable names in Argument Clinic
Add all internally used variable names to CLINIC_PREFIXED_ARGS.
1 parent 80b7148 commit 9a74d01

File tree

2 files changed

+181
-1
lines changed

2 files changed

+181
-1
lines changed

Diff for: Lib/test/clinic.test

+169
Original file line numberDiff line numberDiff line change
@@ -4102,3 +4102,172 @@ exit:
41024102
static PyObject *
41034103
test_paramname_module_impl(PyObject *module, PyObject *mod)
41044104
/*[clinic end generated code: output=4a2a849ecbcc8b53 input=afefe259667f13ba]*/
4105+
4106+
/*[clinic input]
4107+
mangle1
4108+
4109+
args: object
4110+
kwnames: object
4111+
return_value: object
4112+
_keywords: object
4113+
_parser: object
4114+
argsbuf: object
4115+
fastargs: object
4116+
nargs: object
4117+
noptargs: object
4118+
4119+
[clinic start generated code]*/
4120+
4121+
PyDoc_STRVAR(mangle1__doc__,
4122+
"mangle1($module, /, args, kwnames, return_value, _keywords, _parser,\n"
4123+
" argsbuf, fastargs, nargs, noptargs)\n"
4124+
"--\n"
4125+
"\n");
4126+
4127+
#define MANGLE1_METHODDEF \
4128+
{"mangle1", _PyCFunction_CAST(mangle1), METH_FASTCALL|METH_KEYWORDS, mangle1__doc__},
4129+
4130+
static PyObject *
4131+
mangle1_impl(PyObject *module, PyObject *args, PyObject *kwnames,
4132+
PyObject *return_value, PyObject *_keywords, PyObject *_parser,
4133+
PyObject *argsbuf, PyObject *fastargs, PyObject *nargs,
4134+
PyObject *noptargs);
4135+
4136+
static PyObject *
4137+
mangle1(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4138+
{
4139+
PyObject *return_value = NULL;
4140+
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
4141+
4142+
#define NUM_KEYWORDS 9
4143+
static struct {
4144+
PyGC_Head _this_is_not_used;
4145+
PyObject_VAR_HEAD
4146+
PyObject *ob_item[NUM_KEYWORDS];
4147+
} _kwtuple = {
4148+
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
4149+
.ob_item = { &_Py_ID(args), &_Py_ID(kwnames), &_Py_ID(return_value), &_Py_ID(_keywords), &_Py_ID(_parser), &_Py_ID(argsbuf), &_Py_ID(fastargs), &_Py_ID(nargs), &_Py_ID(noptargs), },
4150+
};
4151+
#undef NUM_KEYWORDS
4152+
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
4153+
4154+
#else // !Py_BUILD_CORE
4155+
# define KWTUPLE NULL
4156+
#endif // !Py_BUILD_CORE
4157+
4158+
static const char * const _keywords[] = {"args", "kwnames", "return_value", "_keywords", "_parser", "argsbuf", "fastargs", "nargs", "noptargs", NULL};
4159+
static _PyArg_Parser _parser = {
4160+
.keywords = _keywords,
4161+
.fname = "mangle1",
4162+
.kwtuple = KWTUPLE,
4163+
};
4164+
#undef KWTUPLE
4165+
PyObject *argsbuf[9];
4166+
PyObject *__clinic_args;
4167+
PyObject *__clinic_kwnames;
4168+
PyObject *__clinic_return_value;
4169+
PyObject *__clinic__keywords;
4170+
PyObject *__clinic__parser;
4171+
PyObject *__clinic_argsbuf;
4172+
PyObject *__clinic_fastargs;
4173+
PyObject *__clinic_nargs;
4174+
PyObject *__clinic_noptargs;
4175+
4176+
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 9, 9, 0, argsbuf);
4177+
if (!args) {
4178+
goto exit;
4179+
}
4180+
__clinic_args = args[0];
4181+
__clinic_kwnames = args[1];
4182+
__clinic_return_value = args[2];
4183+
__clinic__keywords = args[3];
4184+
__clinic__parser = args[4];
4185+
__clinic_argsbuf = args[5];
4186+
__clinic_fastargs = args[6];
4187+
__clinic_nargs = args[7];
4188+
__clinic_noptargs = args[8];
4189+
return_value = mangle1_impl(module, __clinic_args, __clinic_kwnames, __clinic_return_value, __clinic__keywords, __clinic__parser, __clinic_argsbuf, __clinic_fastargs, __clinic_nargs, __clinic_noptargs);
4190+
4191+
exit:
4192+
return return_value;
4193+
}
4194+
4195+
static PyObject *
4196+
mangle1_impl(PyObject *module, PyObject *args, PyObject *kwnames,
4197+
PyObject *return_value, PyObject *_keywords, PyObject *_parser,
4198+
PyObject *argsbuf, PyObject *fastargs, PyObject *nargs,
4199+
PyObject *noptargs)
4200+
/*[clinic end generated code: output=083e5076be9987c3 input=a3ed51bdedf8a3c7]*/
4201+
4202+
/*[clinic input]
4203+
mangle2
4204+
4205+
args: object
4206+
kwargs: object
4207+
return_value: object
4208+
4209+
[clinic start generated code]*/
4210+
4211+
PyDoc_STRVAR(mangle2__doc__,
4212+
"mangle2($module, /, args, kwargs, return_value)\n"
4213+
"--\n"
4214+
"\n");
4215+
4216+
#define MANGLE2_METHODDEF \
4217+
{"mangle2", _PyCFunction_CAST(mangle2), METH_FASTCALL|METH_KEYWORDS, mangle2__doc__},
4218+
4219+
static PyObject *
4220+
mangle2_impl(PyObject *module, PyObject *args, PyObject *kwargs,
4221+
PyObject *return_value);
4222+
4223+
static PyObject *
4224+
mangle2(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4225+
{
4226+
PyObject *return_value = NULL;
4227+
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
4228+
4229+
#define NUM_KEYWORDS 3
4230+
static struct {
4231+
PyGC_Head _this_is_not_used;
4232+
PyObject_VAR_HEAD
4233+
PyObject *ob_item[NUM_KEYWORDS];
4234+
} _kwtuple = {
4235+
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
4236+
.ob_item = { &_Py_ID(args), &_Py_ID(kwargs), &_Py_ID(return_value), },
4237+
};
4238+
#undef NUM_KEYWORDS
4239+
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
4240+
4241+
#else // !Py_BUILD_CORE
4242+
# define KWTUPLE NULL
4243+
#endif // !Py_BUILD_CORE
4244+
4245+
static const char * const _keywords[] = {"args", "kwargs", "return_value", NULL};
4246+
static _PyArg_Parser _parser = {
4247+
.keywords = _keywords,
4248+
.fname = "mangle2",
4249+
.kwtuple = KWTUPLE,
4250+
};
4251+
#undef KWTUPLE
4252+
PyObject *argsbuf[3];
4253+
PyObject *__clinic_args;
4254+
PyObject *__clinic_kwargs;
4255+
PyObject *__clinic_return_value;
4256+
4257+
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
4258+
if (!args) {
4259+
goto exit;
4260+
}
4261+
__clinic_args = args[0];
4262+
__clinic_kwargs = args[1];
4263+
__clinic_return_value = args[2];
4264+
return_value = mangle2_impl(module, __clinic_args, __clinic_kwargs, __clinic_return_value);
4265+
4266+
exit:
4267+
return return_value;
4268+
}
4269+
4270+
static PyObject *
4271+
mangle2_impl(PyObject *module, PyObject *args, PyObject *kwargs,
4272+
PyObject *return_value)
4273+
/*[clinic end generated code: output=2ebb62aaefe7590a input=391766fee51bad7a]*/

Diff for: Tools/clinic/clinic.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,18 @@
4343

4444
NO_VARARG = "PY_SSIZE_T_MAX"
4545
CLINIC_PREFIX = "__clinic_"
46-
CLINIC_PREFIXED_ARGS = {"args"}
46+
CLINIC_PREFIXED_ARGS = {
47+
"_keywords",
48+
"_parser",
49+
"args",
50+
"argsbuf",
51+
"fastargs",
52+
"kwargs",
53+
"kwnames",
54+
"nargs",
55+
"noptargs",
56+
"return_value",
57+
}
4758

4859
class Unspecified:
4960
def __repr__(self):

0 commit comments

Comments
 (0)