Skip to content

Commit

Permalink
gh-68395: Avoid naming conflicts by mangling variable names in Argume…
Browse files Browse the repository at this point in the history
…nt Clinic

Add all internally used variable names to CLINIC_PREFIXED_ARGS.
  • Loading branch information
erlend-aasland committed May 1, 2023
1 parent 80b7148 commit 8acfbe7
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 1 deletion.
146 changes: 146 additions & 0 deletions Lib/test/clinic.test
Original file line number Diff line number Diff line change
Expand Up @@ -4102,3 +4102,149 @@ exit:
static PyObject *
test_paramname_module_impl(PyObject *module, PyObject *mod)
/*[clinic end generated code: output=4a2a849ecbcc8b53 input=afefe259667f13ba]*/

/*[clinic input]
mangle1

args: object
kwnames: object
return_value: object

[clinic start generated code]*/

PyDoc_STRVAR(mangle1__doc__,
"mangle1($module, /, args, kwnames, return_value)\n"
"--\n"
"\n");

#define MANGLE1_METHODDEF \
{"mangle1", _PyCFunction_CAST(mangle1), METH_FASTCALL|METH_KEYWORDS, mangle1__doc__},

static PyObject *
mangle1_impl(PyObject *module, PyObject *args, PyObject *kwnames,
PyObject *return_value);

static PyObject *
mangle1(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)

#define NUM_KEYWORDS 3
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(args), &_Py_ID(kwnames), &_Py_ID(return_value), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)

#else // !Py_BUILD_CORE
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE

static const char * const _keywords[] = {"args", "kwnames", "return_value", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "mangle1",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[3];
PyObject *__clinic_args;
PyObject *__clinic_kwnames;
PyObject *__clinic_return_value;

args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
if (!args) {
goto exit;
}
__clinic_args = args[0];
__clinic_kwnames = args[1];
__clinic_return_value = args[2];
return_value = mangle1_impl(module, __clinic_args, __clinic_kwnames, __clinic_return_value);

exit:
return return_value;
}

static PyObject *
mangle1_impl(PyObject *module, PyObject *args, PyObject *kwnames,
PyObject *return_value)
/*[clinic end generated code: output=37822ed32b2db8c9 input=1a79ce37f3f5eaab]*/

/*[clinic input]
mangle2

args: object
kwargs: object
return_value: object

[clinic start generated code]*/

PyDoc_STRVAR(mangle2__doc__,
"mangle2($module, /, args, kwargs, return_value)\n"
"--\n"
"\n");

#define MANGLE2_METHODDEF \
{"mangle2", _PyCFunction_CAST(mangle2), METH_FASTCALL|METH_KEYWORDS, mangle2__doc__},

static PyObject *
mangle2_impl(PyObject *module, PyObject *args, PyObject *kwargs,
PyObject *return_value);

static PyObject *
mangle2(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)

#define NUM_KEYWORDS 3
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(args), &_Py_ID(kwargs), &_Py_ID(return_value), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)

#else // !Py_BUILD_CORE
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE

static const char * const _keywords[] = {"args", "kwargs", "return_value", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "mangle2",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[3];
PyObject *__clinic_args;
PyObject *__clinic_kwargs;
PyObject *__clinic_return_value;

args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
if (!args) {
goto exit;
}
__clinic_args = args[0];
__clinic_kwargs = args[1];
__clinic_return_value = args[2];
return_value = mangle2_impl(module, __clinic_args, __clinic_kwargs, __clinic_return_value);

exit:
return return_value;
}

static PyObject *
mangle2_impl(PyObject *module, PyObject *args, PyObject *kwargs,
PyObject *return_value)
/*[clinic end generated code: output=2ebb62aaefe7590a input=391766fee51bad7a]*/
2 changes: 1 addition & 1 deletion Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

NO_VARARG = "PY_SSIZE_T_MAX"
CLINIC_PREFIX = "__clinic_"
CLINIC_PREFIXED_ARGS = {"args"}
CLINIC_PREFIXED_ARGS = {"args", "kwargs", "kwnames", "return_value"}

class Unspecified:
def __repr__(self):
Expand Down

0 comments on commit 8acfbe7

Please sign in to comment.