Skip to content

Argument Clinic: improve generated code for subclass_of checks #101479

Closed as not planned
@erlend-aasland

Description

@erlend-aasland

Feature or enhancement

Follow-up of #101409

Currently, the subclass_of clinic parameter is spelled out twice verbatim in the generated code. For heap type methods in isolated extension modules, the following pattern is often used:

/*[clinic input]
mymod.myclass.mymeth

    obj: object(subclass_of='clinic_state()->SomeType')

[clinic start generated code]*/

This may result in the following generated code:

if (!PyObject_TypeCheck(args[0], clinic_state()->SomeType)) {
    _PyArg_BadArgument("mymeth", "argument 'obj'", (clinic_state()->SomeType)->tp_name, args[0]);
}

Now, the second call is definitely a slow path, so this is not too bad. I still think it would be an improvement to generate code such as this:

PyTypeObject *obj_type = clinic_state()->SomeType;
if (!PyObject_TypeCheck(args[0], obj_type)) {
    _PyArg_BadArgument("mymeth", "argument 'obj'", obj_type->tp_name, args[0]);
}

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions