6.2 C
New York
Friday, November 29, 2024

Python to C: What’s new in Cython 3.1



One handy means to do that routinely is to make use of the sys._is_gil_enabled() technique to examine if the macro must be used. A recipe like this would possibly work:


macros = []
if getattr(sys, "_is_gil_enabled", None) and never sys._is_gil_enabled():
    macros = [("Py_GIL_DISABLED","1")]

ext_modules = [
    Extension(
        "compute",
        ["compute.pyx"],
        define_macros=macros
    )
]

4. Guarantee your code is thread secure

Thread security in Cython modules has all the time been the programmer’s accountability, a minimum of so far as pure C code goes. If you happen to name into the CPython interpreter utilizing Cython, even within the free-threaded construct, the interpreter will deal with reference counts for these objects (though it gained’t do something about knowledge races). However any Cython features that run concurrently should not manipulate the identical knowledge.

New sort annotations for constants, pointers, and unstable values

As Cython has developed, it has regularly transitioned away from its customized Python-but-not-quite syntax. Trendy Cython makes use of a syntax that’s primarily common Python with Cython-added sort hints, context managers, and different constructions.



Supply hyperlink

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles