-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
Description
Feature or enhancement
Proposal:
Before argument clinic, creating method aliases in C extension classes was very easy: simply add another MethodDef for the same function:
{"replace", _PyCFunction_CAST(datetime_replace), METH_VARARGS | METH_KEYWORDS},
{"__replace__", _PyCFunction_CAST(datetime_replace), METH_VARARGS | METH_KEYWORDS},
This is very cheap and convenient. However, with argument clinic we cannot quite do this. If I do
DATETIME_DATETIME_REPLACE_METHODDEF
{"__replace__", _PyCFunction_CAST(datetime_datetime_replace), METH_FASTCALL | METH_KEYWORDS},
I have to manually copy the flags from the generated MethodDef into the one I wrote myself. This works now, but can break later if clinic generates a MethodDef with different flags. It is possible to work-around by copying the function, but this adds a maintenance burden and generates argument parsing twice, leading to code bloat.
It would be nice to have some syntax to tell argument clinic to generate the alias MethodDef. It could look like this:
/*[clinic input]
alias datetime.date.__replace__ = datetime.date.replace
[clinic start generated code]*/
which would generate:
#define DATETIME_DATETIME___REPLACE___METHODDEF \
{"__replace__", _PyCFunction_CAST(datetime_datetime_replace), METH_FASTCALL|METH_KEYWORDS, \
datetime_datetime_replace__doc__},
that can then be used as usual:
DATETIME_DATETIME_REPLACE_METHODDEF
DATETIME_DATETIME___REPLACE___METHODDEF
This is presently a problem for code.__replace__
, which is an alias for generated code.replace
1. I also ran into this in #112921, where I convert datetime.*.replace methods to use argument clinic.
CC @AlexWaygood @larryhastings @erlend-aasland
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response