@@ -35,6 +35,59 @@ compiler directive. It can be enabled either per module as a directive
3535` -Xfreethreading_compatible=True ` to the Cython arguments via the project's
3636build system.
3737
38+ Here are a few examples of how to globally enable the directive in a few popular
39+ build systems:
40+
41+ === "setuptools"
42+
43+ When using setuptools, you can pass the `compiler_directives` keyword argument
44+ to `cythonize`:
45+
46+ ```python
47+ from Cython.Compiler.Version import version as cython_version
48+ from packaging.version import Version
49+
50+ compiler_directives = {...}
51+ if Version(cython_version) >= Version("3.1.0a1"):
52+ compiler_directives["freethreading_compatible"] = True
53+
54+ setup(
55+ ext_modules=cythonize(
56+ extensions,
57+ compiler_directives=compiler_directives,
58+ )
59+ )
60+ ```
61+
62+ === "Meson"
63+
64+ When using Meson, you can add the directive to the `cython_args` you're
65+ passing to `py.extension_module`:
66+
67+ ```meson
68+ cy = meson.get_compiler('cython')
69+
70+ cython_args = [...]
71+ if cy.version().version_compare('>=3.1.0')
72+ cython_args += ['-Xfreethreading_compatible=True']
73+ endif
74+
75+ py.extension_module('modulename'
76+ 'source.pyx',
77+ cython_args: cython_args,
78+ ...
79+ )
80+ ```
81+
82+ You can also globally add the directive for all Cython extension modules:
83+
84+ ```meson
85+ cy = meson.get_compiler('cython')
86+ if cy.version().version_compare('>=3.1.0')
87+ add_global_arguments('-Xfreethreading_compatible=true', language : 'cython')
88+ endif
89+ ```
90+
3891C or C++ extension modules using multi-phase initialization can specify the
3992[ ` Py_mod_gil ` ] ( https://docs.python.org/3.13/c-api/module.html#c.Py_mod_gil )
4093module slot like this:
0 commit comments