Rationale to change the Python C API — pythoncapi 0.1 documentation (original) (raw)

To be able to introduce backward incompatible changes to the C API without breaking too many C extensions, this project proposes two things:

The current C API has multiple issues:

PyPy cpyext is slow

The PyPy cpyext is slow because there is no efficient way to write a correct implementation of the current C API. For example, borrowed references is hard to optimize since the runtime cannot track the lifetime of a borrowed object.

The stable ABI is not usable

The current Python C API produces an ABI which is far from stable. The API leaks many implementation details into the ABI.

For example, using the PyTuple_GET_ITEM() macro produces machine code which uses an hardcoded offset. If the PyObject structure changes, the offset changes, and so the ABI changes.

See Relationship between the C API and the ABI.

Issues with an unstable ABI:

Performance problem: How can we make Python 2x faster?

“Leaking” implementation details into the ABI prevents many optimizations opportunities. See Optimization ideas.

Keep backward compatibility

Existing C extensions will still be supported and will not have to be modified. The old C API is not deprecated and there is no plan penalize users of the old C API.

See Backward compatibility.

The Big Carrot

Changing the C API means that authors of C extensions have to do something. To justify these changes, we need a big carrot. Examples: