Skip to content

Argument clinic: add support for creating method aliases #113270

@eltoder

Description

@eltoder

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.replace1. 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

Footnotes

  1. https://github.com/python/cpython/blob/893c9ccf48eacb02fa6ae93632f2d0cb6778dbb6/Objects/codeobject.c#L2180

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions