Skip to content

Commit c0b7868

Browse files
[3.11] gh-94430: Allow params named module or self with custom C names in AC (GH-94431) (#94649)
(cherry picked from commit 8bbd70b) Co-authored-by: Erlend Egeberg Aasland <[email protected]>
1 parent 74c953d commit c0b7868

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

Lib/test/clinic.test

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3524,3 +3524,44 @@ static PyObject *
35243524
test_vararg_with_only_defaults_impl(PyObject *module, PyObject *args, int b,
35253525
PyObject *c)
35263526
/*[clinic end generated code: output=7e393689e6ce61a3 input=fa56a709a035666e]*/
3527+
3528+
/*[clinic input]
3529+
test_paramname_module
3530+
3531+
module as mod: object
3532+
[clinic start generated code]*/
3533+
3534+
PyDoc_STRVAR(test_paramname_module__doc__,
3535+
"test_paramname_module($module, /, module)\n"
3536+
"--\n"
3537+
"\n");
3538+
3539+
#define TEST_PARAMNAME_MODULE_METHODDEF \
3540+
{"test_paramname_module", _PyCFunction_CAST(test_paramname_module), METH_FASTCALL|METH_KEYWORDS, test_paramname_module__doc__},
3541+
3542+
static PyObject *
3543+
test_paramname_module_impl(PyObject *module, PyObject *mod);
3544+
3545+
static PyObject *
3546+
test_paramname_module(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
3547+
{
3548+
PyObject *return_value = NULL;
3549+
static const char * const _keywords[] = {"module", NULL};
3550+
static _PyArg_Parser _parser = {NULL, _keywords, "test_paramname_module", 0};
3551+
PyObject *argsbuf[1];
3552+
PyObject *mod;
3553+
3554+
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
3555+
if (!args) {
3556+
goto exit;
3557+
}
3558+
mod = args[0];
3559+
return_value = test_paramname_module_impl(module, mod);
3560+
3561+
exit:
3562+
return return_value;
3563+
}
3564+
3565+
static PyObject *
3566+
test_paramname_module_impl(PyObject *module, PyObject *mod)
3567+
/*[clinic end generated code: output=23379a7ffa65c514 input=afefe259667f13ba]*/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Allow parameters named ``module`` and ``self`` with custom C names in Argument
2+
Clinic. Patch by Erlend E. Aasland

Tools/clinic/clinic.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4721,9 +4721,14 @@ def bad_node(self, node):
47214721

47224722
p = Parameter(parameter_name, kind, function=self.function, converter=converter, default=value, group=self.group)
47234723

4724-
if parameter_name in self.function.parameters:
4724+
names = [k.name for k in self.function.parameters.values()]
4725+
if parameter_name in names[1:]:
47254726
fail("You can't have two parameters named " + repr(parameter_name) + "!")
4726-
self.function.parameters[parameter_name] = p
4727+
elif names and parameter_name == names[0] and c_name is None:
4728+
fail(f"Parameter '{parameter_name}' requires a custom C name")
4729+
4730+
key = f"{parameter_name}_as_{c_name}" if c_name else parameter_name
4731+
self.function.parameters[key] = p
47274732

47284733
def parse_converter(self, annotation):
47294734
if (hasattr(ast, 'Constant') and

0 commit comments

Comments
 (0)