Skip to content

Commit dc36801

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

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
@@ -3308,3 +3308,44 @@ test_preprocessor_guarded_else(PyObject *module, PyObject *Py_UNUSED(ignored))
33083308
#define TEST_PREPROCESSOR_GUARDED_ELSE_METHODDEF
33093309
#endif /* !defined(TEST_PREPROCESSOR_GUARDED_ELSE_METHODDEF) */
33103310
/*[clinic end generated code: output=3804bb18d454038c input=3fc80c9989d2f2e1]*/
3311+
3312+
/*[clinic input]
3313+
test_paramname_module
3314+
3315+
module as mod: object
3316+
[clinic start generated code]*/
3317+
3318+
PyDoc_STRVAR(test_paramname_module__doc__,
3319+
"test_paramname_module($module, /, module)\n"
3320+
"--\n"
3321+
"\n");
3322+
3323+
#define TEST_PARAMNAME_MODULE_METHODDEF \
3324+
{"test_paramname_module", (PyCFunction)(void(*)(void))test_paramname_module, METH_FASTCALL|METH_KEYWORDS, test_paramname_module__doc__},
3325+
3326+
static PyObject *
3327+
test_paramname_module_impl(PyObject *module, PyObject *mod);
3328+
3329+
static PyObject *
3330+
test_paramname_module(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
3331+
{
3332+
PyObject *return_value = NULL;
3333+
static const char * const _keywords[] = {"module", NULL};
3334+
static _PyArg_Parser _parser = {NULL, _keywords, "test_paramname_module", 0};
3335+
PyObject *argsbuf[1];
3336+
PyObject *mod;
3337+
3338+
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
3339+
if (!args) {
3340+
goto exit;
3341+
}
3342+
mod = args[0];
3343+
return_value = test_paramname_module_impl(module, mod);
3344+
3345+
exit:
3346+
return return_value;
3347+
}
3348+
3349+
static PyObject *
3350+
test_paramname_module_impl(PyObject *module, PyObject *mod)
3351+
/*[clinic end generated code: output=2a6769d34d1b2be0 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
@@ -4626,9 +4626,14 @@ def bad_node(self, node):
46264626

46274627
p = Parameter(parameter_name, kind, function=self.function, converter=converter, default=value, group=self.group)
46284628

4629-
if parameter_name in self.function.parameters:
4629+
names = [k.name for k in self.function.parameters.values()]
4630+
if parameter_name in names[1:]:
46304631
fail("You can't have two parameters named " + repr(parameter_name) + "!")
4631-
self.function.parameters[parameter_name] = p
4632+
elif names and parameter_name == names[0] and c_name is None:
4633+
fail(f"Parameter '{parameter_name}' requires a custom C name")
4634+
4635+
key = f"{parameter_name}_as_{c_name}" if c_name else parameter_name
4636+
self.function.parameters[key] = p
46324637

46334638
def parse_converter(self, annotation):
46344639
if (hasattr(ast, 'Constant') and

0 commit comments

Comments
 (0)