Skip to content

Commit 066da48

Browse files
authored
Add a C macro to configure "Py_mod_gil" at C compile time (GH-7404)
I found a use case for setting `Py_mod_gil` differently in Py3.13 and Py3.14, but regenerating the whole C file just to switch between them is rather wasteful. So here's a new C macro `CYTHON_FREETHREADING_COMPATIBLE` that allows defining the setting from the outside. Having a directive is still useful because we don't want to require the C macro to be set. It should usually have a fixed value for a given module.
1 parent 704b09c commit 066da48

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

Cython/Compiler/ModuleNode.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,17 @@ def generate_module_preamble(self, env, options, cimported_modules, metadata, co
979979
code.putln("#endif")
980980
code.putln("")
981981

982+
code.putln("#ifdef CYTHON_FREETHREADING_COMPATIBLE")
983+
code.putln("#if CYTHON_FREETHREADING_COMPATIBLE")
984+
code.putln("#define __Pyx_FREETHREADING_COMPATIBLE Py_MOD_GIL_NOT_USED")
985+
code.putln("#else")
986+
code.putln("#define __Pyx_FREETHREADING_COMPATIBLE Py_MOD_GIL_USED")
987+
code.putln("#endif")
988+
code.putln("#else")
989+
ft_compatible = "Py_MOD_GIL_NOT_USED" if env.directives["freethreading_compatible"] else "Py_MOD_GIL_USED"
990+
code.putln(f"#define __Pyx_FREETHREADING_COMPATIBLE {ft_compatible}")
991+
code.putln("#endif")
992+
982993
c_string_type = env.directives['c_string_type']
983994
c_string_encoding = env.directives['c_string_encoding']
984995
if c_string_type not in ('bytes', 'bytearray') and not c_string_encoding:
@@ -3628,10 +3639,7 @@ def generate_pymoduledef_struct(self, env, code):
36283639
code.putln("{Py_mod_create, (void*)%s}," % Naming.pymodule_create_func_cname)
36293640
code.putln("{Py_mod_exec, (void*)%s}," % exec_func_cname)
36303641
code.putln("#if CYTHON_COMPILING_IN_CPYTHON_FREETHREADING")
3631-
gil_option = ("Py_MOD_GIL_NOT_USED"
3632-
if env.directives["freethreading_compatible"]
3633-
else "Py_MOD_GIL_USED")
3634-
code.putln("{Py_mod_gil, %s}," % gil_option)
3642+
code.putln("{Py_mod_gil, __Pyx_FREETHREADING_COMPATIBLE},")
36353643
code.putln("#endif")
36363644
code.putln("#if PY_VERSION_HEX >= 0x030C0000 && CYTHON_USE_MODULE_STATE")
36373645
subinterp_option = {

docs/src/userguide/source_files_and_compilation.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,8 @@ Cython code. Here is the list of currently supported directives:
960960
the logic and consider it safe to run. Since free-threading support
961961
is still experimental itself, this is also an experimental directive that
962962
might be changed or removed in future releases.
963+
This option can be overridden at C compile time by setting the
964+
``CYTHON_FREETHREADING_COMPATIBLE`` C macro to 1/0 for True/False.
963965

964966
``subinterpreters_compatible`` (no / shared_gil / own_gil), *default=no*
965967
If set to ``shared_gil`` or ``own_gil``, then Cython sets the
@@ -1416,6 +1418,12 @@ most important to least important:
14161418
to the constants due to reference counting. Disabled by default, but enabled
14171419
in free-threaded builds.
14181420

1421+
``CYTHON_FREETHREADING_COMPATIBLE``
1422+
In Freethreading Python runtimes, setting this to ``0`` will force enabling
1423+
the GIL when importing the module, ``1`` will keep it untouched.
1424+
The default setting depends on the ``freethreading_compatible`` directive.
1425+
This C macro allows to override the directive at C compile time.
1426+
14191427
There is a further list of macros which turn off various optimizations or language
14201428
features. Under normal circumstance Cython enables these automatically based on the
14211429
version of Python you are compiling for so there is no need to use them

0 commit comments

Comments
 (0)