From 56c80f89cafadcd7050eff2c9bf34bf890ee93fc Mon Sep 17 00:00:00 2001
From: Lysandros Nikolaou <lisandrosnik@gmail.com>
Date: Tue, 16 Jul 2024 18:42:04 +0200
Subject: [PATCH] ENH: Globally enable Cython free-threading directive (#59248)

* ENH: Globally enable Cython free-threading directive

This is the Cython equivalent of adding a `Py_mod_gil` slot with
`Py_MOD_GIL_NOT_USED` like we did in #59135.

* Use add_project_arguments

* Mark json with Py_MOD_GIL_NOT_USED & remove PYTHON_GIL env var from ci test job
---
 .github/workflows/unit-tests.yml               | 2 --
 meson.build                                    | 5 +++++
 pandas/_libs/src/vendored/ujson/python/ujson.c | 4 ++++
 3 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml
index ddb6ecbe83..a9585c1745 100644
--- a/.github/workflows/unit-tests.yml
+++ b/.github/workflows/unit-tests.yml
@@ -388,8 +388,6 @@ jobs:
 
       - name: Run Tests
         uses: ./.github/actions/run-tests
-        env:
-          PYTHON_GIL: 0
 
   # NOTE: this job must be kept in sync with the Pyodide build job in wheels.yml
   emscripten:
diff --git a/meson.build b/meson.build
index 06623a305a..efe543b7a2 100644
--- a/meson.build
+++ b/meson.build
@@ -44,6 +44,11 @@ else
     meson.add_dist_script(py, versioneer, '-o', '_version_meson.py')
 endif
 
+cy = meson.get_compiler('cython')
+if cy.version().version_compare('>=3.1.0')
+  add_project_arguments('-Xfreethreading_compatible=true', language : 'cython')
+endif
+
 # Needed by pandas.test() when it looks for the pytest ini options
 py.install_sources(
     'pyproject.toml',
diff --git a/pandas/_libs/src/vendored/ujson/python/ujson.c b/pandas/_libs/src/vendored/ujson/python/ujson.c
index 075411a23b..f369d122a3 100644
--- a/pandas/_libs/src/vendored/ujson/python/ujson.c
+++ b/pandas/_libs/src/vendored/ujson/python/ujson.c
@@ -384,6 +384,10 @@ PyMODINIT_FUNC PyInit_json(void) {
     return NULL;
   }
 
+#ifdef Py_GIL_DISABLED
+  PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED);
+#endif
+
 #ifndef PYPY_VERSION
   PyObject *mod_decimal = PyImport_ImportModule("decimal");
   if (mod_decimal) {
-- 
GitLab