Skip to content

PyUnstable_ExecutableKinds is useless #152105

Description

@ZeroIntensity

We have some unstable APIs related to frame executables. We were looking into documenting them in #143490. These are defined at the bottom of frame.c:

cpython/Python/frame.c

Lines 153 to 159 in 0fb82b4

const PyTypeObject *const PyUnstable_ExecutableKinds[PyUnstable_EXECUTABLE_KINDS+1] = {
[PyUnstable_EXECUTABLE_KIND_SKIP] = &_PyNone_Type,
[PyUnstable_EXECUTABLE_KIND_PY_FUNCTION] = &PyCode_Type,
[PyUnstable_EXECUTABLE_KIND_BUILTIN_FUNCTION] = &PyMethod_Type,
[PyUnstable_EXECUTABLE_KIND_METHOD_DESCRIPTOR] = &PyMethodDescr_Type,
[PyUnstable_EXECUTABLE_KINDS] = NULL,
};

The idea is that users can use these to determine the type of f_executable without accessing it -- but it seems we forgot to add an API to actually get one of the PyUnstable_EXECUTABLE_KIND* macros from a frame object! That means the only way to an entry out of PyUnstable_ExecutableKinds is to know the type in advance, like this:

int
get_executable_kind(PyFrameObject *frame)
{
    _PyInterpreterFrame *f = frame->f_frame;
    PyObject *exec = PyStackRef_AsPyObjectBorrow(f->f_executable);

    if (PyCode_Check(exec)) {
        return PyUnstable_EXECUTABLE_KIND_PY_FUNCTION;
    }
    if (PyMethod_Check(exec)) {
        return PyUnstable_EXECUTABLE_KIND_BUILTIN_FUNCTION;
    }
    if (Py_IS_TYPE(exec, &PyMethodDescr_Type)) {
        return PyUnstable_EXECUTABLE_KIND_METHOD_DESCRIPTOR;
    }

    return PyUnstable_EXECUTABLE_KIND_SKIP;
}

But, if you already know the type, then you have no reason to access PyUnstable_ExecutableKinds! For the future, let's decide on one of two options:

  1. If this is still intended to be useful, then let's add the get_executable_kind function described above as an unstable C API.
  2. Otherwise, let's just remove this in 3.16.

cc @markshannon (author of #105727, where these were added), @encukou

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions