Skip to content

Commit 8acfbe7

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 8acfbe7

File tree

2 files changed

+147
-1
lines changed

2 files changed

+147
-1
lines changed

Diff for: Lib/test/clinic.test

+146
Original file line numberDiff line numberDiff line change
@@ -4102,3 +4102,149 @@ 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+
4113+
[clinic start generated code]*/
4114+
4115+
PyDoc_STRVAR(mangle1__doc__,
4116+
"mangle1($module, /, args, kwnames, return_value)\n"
4117+
"--\n"
4118+
"\n");
4119+
4120+
#define MANGLE1_METHODDEF \
4121+
{"mangle1", _PyCFunction_CAST(mangle1), METH_FASTCALL|METH_KEYWORDS, mangle1__doc__},
4122+
4123+
static PyObject *
4124+
mangle1_impl(PyObject *module, PyObject *args, PyObject *kwnames,
4125+
PyObject *return_value);
4126+
4127+
static PyObject *
4128+
mangle1(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4129+
{
4130+
PyObject *return_value = NULL;
4131+
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
4132+
4133+
#define NUM_KEYWORDS 3
4134+
static struct {
4135+
PyGC_Head _this_is_not_used;
4136+
PyObject_VAR_HEAD
4137+
PyObject *ob_item[NUM_KEYWORDS];
4138+
} _kwtuple = {
4139+
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
4140+
.ob_item = { &_Py_ID(args), &_Py_ID(kwnames), &_Py_ID(return_value), },
4141+
};
4142+
#undef NUM_KEYWORDS
4143+
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
4144+
4145+
#else // !Py_BUILD_CORE
4146+
# define KWTUPLE NULL
4147+
#endif // !Py_BUILD_CORE
4148+
4149+
static const char * const _keywords[] = {"args", "kwnames", "return_value", NULL};
4150+
static _PyArg_Parser _parser = {
4151+
.keywords = _keywords,
4152+
.fname = "mangle1",
4153+
.kwtuple = KWTUPLE,
4154+
};
4155+
#undef KWTUPLE
4156+
PyObject *argsbuf[3];
4157+
PyObject *__clinic_args;
4158+
PyObject *__clinic_kwnames;
4159+
PyObject *__clinic_return_value;
4160+
4161+
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
4162+
if (!args) {
4163+
goto exit;
4164+
}
4165+
__clinic_args = args[0];
4166+
__clinic_kwnames = args[1];
4167+
__clinic_return_value = args[2];
4168+
return_value = mangle1_impl(module, __clinic_args, __clinic_kwnames, __clinic_return_value);
4169+
4170+
exit:
4171+
return return_value;
4172+
}
4173+
4174+
static PyObject *
4175+
mangle1_impl(PyObject *module, PyObject *args, PyObject *kwnames,
4176+
PyObject *return_value)
4177+
/*[clinic end generated code: output=37822ed32b2db8c9 input=1a79ce37f3f5eaab]*/
4178+
4179+
/*[clinic input]
4180+
mangle2
4181+
4182+
args: object
4183+
kwargs: object
4184+
return_value: object
4185+
4186+
[clinic start generated code]*/
4187+
4188+
PyDoc_STRVAR(mangle2__doc__,
4189+
"mangle2($module, /, args, kwargs, return_value)\n"
4190+
"--\n"
4191+
"\n");
4192+
4193+
#define MANGLE2_METHODDEF \
4194+
{"mangle2", _PyCFunction_CAST(mangle2), METH_FASTCALL|METH_KEYWORDS, mangle2__doc__},
4195+
4196+
static PyObject *
4197+
mangle2_impl(PyObject *module, PyObject *args, PyObject *kwargs,
4198+
PyObject *return_value);
4199+
4200+
static PyObject *
4201+
mangle2(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4202+
{
4203+
PyObject *return_value = NULL;
4204+
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
4205+
4206+
#define NUM_KEYWORDS 3
4207+
static struct {
4208+
PyGC_Head _this_is_not_used;
4209+
PyObject_VAR_HEAD
4210+
PyObject *ob_item[NUM_KEYWORDS];
4211+
} _kwtuple = {
4212+
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
4213+
.ob_item = { &_Py_ID(args), &_Py_ID(kwargs), &_Py_ID(return_value), },
4214+
};
4215+
#undef NUM_KEYWORDS
4216+
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
4217+
4218+
#else // !Py_BUILD_CORE
4219+
# define KWTUPLE NULL
4220+
#endif // !Py_BUILD_CORE
4221+
4222+
static const char * const _keywords[] = {"args", "kwargs", "return_value", NULL};
4223+
static _PyArg_Parser _parser = {
4224+
.keywords = _keywords,
4225+
.fname = "mangle2",
4226+
.kwtuple = KWTUPLE,
4227+
};
4228+
#undef KWTUPLE
4229+
PyObject *argsbuf[3];
4230+
PyObject *__clinic_args;
4231+
PyObject *__clinic_kwargs;
4232+
PyObject *__clinic_return_value;
4233+
4234+
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
4235+
if (!args) {
4236+
goto exit;
4237+
}
4238+
__clinic_args = args[0];
4239+
__clinic_kwargs = args[1];
4240+
__clinic_return_value = args[2];
4241+
return_value = mangle2_impl(module, __clinic_args, __clinic_kwargs, __clinic_return_value);
4242+
4243+
exit:
4244+
return return_value;
4245+
}
4246+
4247+
static PyObject *
4248+
mangle2_impl(PyObject *module, PyObject *args, PyObject *kwargs,
4249+
PyObject *return_value)
4250+
/*[clinic end generated code: output=2ebb62aaefe7590a input=391766fee51bad7a]*/

Diff for: Tools/clinic/clinic.py

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

4444
NO_VARARG = "PY_SSIZE_T_MAX"
4545
CLINIC_PREFIX = "__clinic_"
46-
CLINIC_PREFIXED_ARGS = {"args"}
46+
CLINIC_PREFIXED_ARGS = {"args", "kwargs", "kwnames", "return_value"}
4747

4848
class Unspecified:
4949
def __repr__(self):

0 commit comments

Comments
 (0)