Three Talks on the C API



The Python Language Summit 2023 started with an prolonged dialogue of Python’s C API – the interface by which it’s potential to speak between code written in Python and code written in low-level languages, like C. The fullness of this interface is a big issue behind the vibrancy of Python’s ecosystem, enabling libraries comparable to NumPy and pandas which might be foundational to Python’s widespread use in knowledge science.

Three audio system had booked slots to debate the C API this yr: Mark Shannon, Guido van Rossum, and Antonio Cuni. The dialog developed naturally from one discuss to the following, so on this weblog submit, I’ll be discussing the three talks collectively.

All at sea on the C API

The C API at sea (illustration by DALL-E)

“I nonetheless don’t know what the C-API is, and I’ve been making an attempt for years!” 

Mark Shannon, CPython Core Developer

Mark Shannon spoke on the issues (as he noticed them) with Python’s C API. Shannon lamented that with each minor Python model, a slew of third-party C extensions appeared to interrupt. He argued that the foundation trigger was that the wants of C extensions weren’t adequately met by the formal C API, which had developed in a haphazard and often-unplanned means over the previous three a long time of Python’s existence.

Because of the C API’s flaws, Shannon stated, extension authors had been pressured to achieve past the formal API and into implementation particulars that had emerged as a sort of “implicit API”. The implementation particulars that constituted the brand new “implicit API” had change into so broadly depended upon that it was now unimaginable for CPython to vary some components of its code with out breaking giant components of the Python ecosystem.

Shannon believes that the brand new “implicit API” ought to be formalised in Python 3.13. This, he argues, would put an finish to the cycle of CPython releases inevitably resulting in widespread breakages in C extensions.

Sam Gross, who (amongst different issues) has contributed to pytorch, agreed with Shannon that the C API was missing in lots of areas. Gross argued that there was an excessive amount of essential performance that wasn’t uncovered to extension authors. “Tasks simply find yourself copying-and-pasting CPython C code,” Gross stated, which means the extensions broke with every new launch of CPython.

Pablo Galindo Salgado, launch supervisor for Python 3.10 and three.11, stated that the discharge course of for these variations had felt like a “recreation of whackamole” when it got here to third-party C extensions breaking. Salgado argued that CPython wanted to achieve out to the authors of extensions comparable to pytorch to assemble detailed suggestions on what core performance was lacking from the API. A number of attendees expressed frustration with a perceived tendency amongst C extension authors to instantly attain into CPython implementation particulars when one thing they wanted was lacking from the API. The results of this was that CPython core builders had been usually at nighttime about which issues the C API ought to be offering, however at the moment wasn’t. “We would not have the ability to offer you an answer,” Salgado stated, “However please come to us and inform us what your drawback is, when you’ve got an issue!”

Gross proposed that CPython ought to run third-party check suites with new variations of CPython as they had been being developed, in order that the Core Dev staff would have the ability to spot third-party breakages early and gauge the affect of their adjustments. Pytorch operated an analogous programme, Gross stated, and it had been profitable in serving to to restrict breakages of third-party pytorch fashions because the core pytorch staff developed their API.

Brandt Bucher famous, nonetheless, that the issue usually wasn’t a lot that CPython was unaware after they had been breaking third-party code – the benchmarks run in the pyperformance suite usually served as an early warning sign for breakages in C extensions. The issue was usually that CPython would supply to assist affected tasks, solely to have their assist rejected. A number of core builders had beforehand despatched pull requests to assist third-party tasks change into suitable with an upcoming model of CPython, just for their pull requests to stay unmerged for a number of months because of burned-out maintainers of those tasks.

Let’s get particular

Guido van Rossum speaks to the Language Summit on the C API
(picture by Hugo van Kemenade)

Shannon was clear about what he thought the issue with the C API was. The issue was that the C API was inadequate for the authors of C extensions, main these authors to achieve into CPython implementation particulars, resulting in an never-ending cycle of tasks breaking with every new launch of CPython. Others, nonetheless, argued that this wasn’t a lot a particular drawback however a style of issues. Every particular undertaking might need a distinct notion about which issues had been imperfect with the C API, and which issues had been lacking from the C API. Every imperfection or absence could possibly be thought of a concrete drawback in its personal means. “Issues break for everyone, however issues break in numerous methods for various individuals,” Carol Prepared argued. “We want extra granularity in our understanding of that.”

As Mark Shannon’s slot drew to an finish, Guido van Rossum opted to proceed the dialogue that Shannon had began, however sought to attract consideration to a extra exact enumeration of the difficulties C API customers had been going through.

“There’s numerous concepts right here, however I don’t know what the issue is!” 

Carol Prepared, CPython Core Developer

Simon Cross, a contributor to the HPy undertaking, reported that the HPy undertaking had, within the early levels of the undertaking, put collectively an extended checklist of the issues, as they noticed them, with the CPython C API. Cross provided to share the checklist with the Core Dev staff. Thomas Wouters, a Google worker, additionally provided to supply a listing of difficulties Google had skilled when upgrading to latest Python releases, one thing the corporate retains detailed information of. There was settlement that placing collectively a complete checklist of issues with the prevailing API was an essential first step, earlier than CPython may contemplate drawing up plans to repair the issue.

The C API discussions ended with an settlement that additional dialogue was required. events can comply with the continuing dialog at The plan is to work in direction of an informational PEP, with enter from an array of stakeholders, outlining a consensus across the issues and pitfalls within the present C API. As soon as the issues with the established order have been enumerated intimately, the neighborhood could be able to contemplate potential options.

HPy: A potential answer?

A slide from Antonio Cuni’s discuss on HPy

Whereas the C API discussions ended with an in depth dialogue of the issues within the present C API, the primary discuss of the day was the truth is by Antonio Cuni, a core developer with the HPy undertaking. HPy is another C API for Python – an API that seeks to keep away from lots of the pitfalls of the present API. The rivalry of the HPy builders is that the present C API is unhealthy for CPython, unhealthy for different implementations of Python comparable to PyPy or GraalPython, and, in the end, unhealthy for finish customers.

HPy is a specification of a brand new API and ABI for extending Python that’s Python implementation agnostic and designed to cover and summary inner particulars 

— The HPy GitHub README

Cuni started by describing the important thing objectives of the HPy undertaking:

  • An API that doesn’t leak CPython-specific implementation particulars
  • A 0% (or near 0%) efficiency overhead compared with CPython’s present C API
  • A “Common ABI” that enables compiled extension modules to make use of the identical interface to speak with PyPy (for instance) as they’d do to speak with CPython
  • An API that’s garbage-collection pleasant.

Cuni argued that if the Python ecosystem as a complete moved to utilizing HPy, as an alternative of the “official” C API, there could be dramatically fewer breakages of C extensions with every new Python launch. Cuni’s proposal was that CPython ought to “formally bless” HPy because the really helpful C API for Python.

HPy Hpy Hooray?

Simon Cross, HPy Core Developer
(picture by Hugo van Kemenade)

Cuni’s discuss was exuberant, however it was at instances considerably unclear what precisely he was asking for from the room. “The funding from CPython,” Cuni argued “could be a political funding somewhat than a technical one”. Thomas Wouters, nonetheless, argued that the CPython staff didn’t have the “ethical authority” to coerce extension authors into utilizing HPy. Hynek Schlawack agreed, arguing that it was maybe unrealistic emigrate the whole Python ecosystem in direction of HPy.

Many had been unsure about what it could even imply to “formally bless” HPy – would CPython host HPy’s documentation on Or would CPython merely add a observe to the documentation of the C API that the “official” C API was now not the really helpful option to write a C extension? Guido van Rossum emphasised {that a} top-down method from the Core Dev staff to extension authors wouldn’t work: no one wished a repeat of the decade-long transition from Python 2 to Python 3. Carol Prepared agreed that pushing C extension authors to make use of HPy could possibly be counterproductive, arguing that it was essential to recollect the affect of our selections on finish customers of Python.

Different core builders had been sceptical in regards to the fundamentals of HPy itself. HPy’s origin story lies in difficulties PyPy encountered when it making an attempt to make use of CPython’s current C API. These difficulties led to makes an attempt to create an API that could possibly be simply used on both Python implementation. Larry Hastings argued that blessing an API that was friendlier to PyPy had clear advantages to PyPy (and different implementations), however that it was much less clear the place CPython stood to achieve from this transformation.

Cuni’s response was that if the ecosystem migrated to an API that uncovered fewer implementation particulars instantly, CPython would have the ability to refactor its inner implementation way more seamlessly, as CPython would now not have to fret about breaking a mess of third-party C extensions with each inner reorganisation of code. Cuni talked about efforts to run CPython utilizing WebAssembly as a particular space the place CPython may stand to achieve, because the HPy API may work together way more simply with the Javascript rubbish collector. And Cuni additionally famous that HPy made it simple for extension authors to check whether or not they had been sticking to the API or not, one thing which is famously arduous to know with the present C API. “We don’t know all of the experimentation that this may allow,” Cuni exclaimed, nonetheless, “As a result of we haven’t carried out this transformation but!”

Mark Shannon was one other core developer expressing scepticism about HPy. Shannon argued that whereas HPy had strengths over the “official” C API, it was removed from excellent. “We should always attempt to repair CPython’s API” earlier than CPython really helpful customers swap to HPy, Shannon argued. Simon Cross, additionally of the HPy undertaking, stated that the staff welcomed suggestions about the place they may enhance. It was nonetheless simple for HPy to make adjustments, Cross argued, given that they had not but achieved widespread adoption.

Additional Studying on HPy

  1. HPy’s overview of adjustments wanted to the C API.
  2. HPy’s clarification of why the adjustments are wanted.