Fri. Aug 14th, 2020

Cython 3.0a6 releases: C-Extensions for Python

3 min read

Cython is a programming language that makes writing C extensions for the Python language as easy as Python itself. It aims to become a superset of the Python language which gives it high-level, object-oriented, functional, and dynamic programming. Its main feature on top of these is support for optional static type declarations as part of the language. The source code gets translated into optimized C/C++ code and compiled as Python extension modules. This allows for both very fast program execution and tight integration with external C libraries, while keeping up the high programmer productivity for which the Python language is well known.

The primary Python execution environment is commonly referred to as CPython, as it is written in C. Other major implementations use Java (Jython [Jython]), C# (IronPython [IronPython]) and Python itself (PyPy [PyPy]). Written in C, CPython has been conducive to wrapping many external libraries that interface through the C language. It has, however, remained non trivial to write the necessary glue code in C, especially for programmers who are more fluent in a high-level language like Python than in a close-to-the-metal language like C.

Changelog Cython v3.0a6

Features added

  • Special methods for binary operators now follow Python semantics. Rather than e.g. a single __add__ method for cdef classes, where “self” can be either the first or second argument, one can now define both __add__ and __radd__ as for standard Python classes. This behavior can be disabled with the c_api_binop_methods directive to return to the previous semantics in Cython code (available from Cython 0.29.20), or the reversed method (__radd__) can be implemented in addition to an existing two-sided operator method (__add__) to get a backwards compatible implementation. (Github issue #2056)
  • No/single argument functions now accept keyword arguments by default in order to comply with Python semantics. The marginally faster calling conventions METH_NOARGS and METH_O that reject keyword arguments are still available with the directive @cython.always_allow_keywords(False). (Github issue #3090)
  • For-in-loop iteration over bytearray and memory views is optimised. Patch by David Woods. (Github issue #2227)
  • Type inference now works for memory views and slices. Patch by David Woods. (Github issue #2227)
  • The @returns() decorator propagates exceptions by default for suitable C return types when no @exceptval() is defined. (Github issues #3625, #3664)
  • A low-level inline function total_seconds(timedelta) was added to cpython.datetime to bypass the Python method call. Note that this function is not guaranteed to give exactly the same results for very large time intervals. Patch by Brock Mendel. (Github issue #3616)
  • Type inference now understands that a, *b = x assigns a list to b.
  • Limited API support was improved. Patches by Matthias Braun. (Github issues #3693, #3707)
  • The Cython CodeWriter can now handle more syntax constructs. Patch by Tao He. (Github issue #3514)

Bugs fixed

  • The construct for x in cpp_function_call() failed to compile. Patch by David Woods. (Github issue #3663)
  • C++ references failed to compile when used as Python object indexes. Patch by David Woods. (Github issue #3754)
  • The C++ typeid() function was allowed in C mode. Patch by Celelibi. (Github issue #3637)
  • repr() was assumed to return str instead of unicode with language_level=3. (Github issue #3736)
  • Includes all bug-fixes from the 0.29.21 release.

Other changes

  • The numpy declarations were updated. Patch by Brock Mendel. (Github issue #3630)
  • The names of Cython’s internal types (functions, generator, coroutine, etc.) are now qualified with the module name of the internal Cython module that is used for sharing them across Cython implemented modules, for example _cython_3_0a5.coroutine. This was done to avoid making them look like homeless builtins, to help with debugging, and in order to avoid a CPython warning according to https://bugs.python.org/issue20204

Download