Cython 3.0 released after nearly 5 years, but beware breaking changes

Cython 3.0 released after nearly 5 years, but beware breaking changes

Cython 3.0 has been declared done by co-developer Dr Stefan Behnel, after what he said was “close to five long years,” with many new features, including “Python 3 syntax and semantics by default”.

Cython is a compiler and Python-based programming language that outputs native code, with potentially much faster execution than the CPython interpreter. It is used both to optimize Python applications and to create modules that can be used by CPython, enabling performance-critical code to be compiled while the main part of an application runs in the interpreter.

According to the release notes, “Cython 3.0.0 has been a very large effort that cleaned up many old warts, introduced many new features, and introduces a couple of intentional behaviour changes.” Cython now supports all versions of Python 3, including “experimental” support for the forthcoming CPython 3.12, but drops support for Python 2.6. Cython semantics are now aligned with Python 3 for division, power operator, print, classes, types and subscripting. This is a breaking change but can be reverted by setting a language_level compiler directive. A migration guide to this and other breaking changes is here.

Exception handling is improved in this release, with Cython-implemented C functions propagating exceptions by default, where they used to swallow them “if the user forgot to add an except declaration to the signature.” The notes add that this was a long-standing source of bugs; but since it is a breaking change, it can be disabled. Compatibility with both C and C++ is improved.

Compatibility issues between Cython 2.x and Cython 3.0 are already causing problems. Users attempting to install the AWS CLI (command line interface) via the pip package manger got an error: “Getting requirements to build wheel: finished with status ‘error’.” This was traced to pip downloading the latest version of Cython, which is a dependency of PyYAML, a package used by the AWC CLI, causing the install to fail immediately after 3.0 was released. The fix was to add a build constraint to continue using Cython 2.x, described by the PyYAML team as “a stopgap measure to ensure the libyaml extension build continues to function whenever Cython 3.0 is released.”

It was not just the AWS CLI that was impacted. Another user said “This is causing a number of products in our organization to be unbuildable. Please consider this another plea for a quick merge and release of this now critical PR.”

Cython is now over 21 years old, if you date it from its predecessor, Pyrex, which was first released in April 2002. Pyrex was described by its developer Greg Ewing as “more or less Python with C data types added. You can write functions which freely intermix operations on Python and C data, with all Python reference counting and error checking completely automated.”

On its 20th anniversary last year, Behnel described Cython’s history, and how it has become “a major corner stone of the Python data ecosystem.” According to Behnel, Cython’s uses include “sending robots to Mars, scaling up Django websites to a thousand million users, building climate models,” and countless others.