Skip to content

Commit 8f26f43

Browse files
authored
PYTHON-4450 Support free-threaded Python 3.13t with no-GIL (mongodb#1906)
1 parent 2895e84 commit 8f26f43

File tree

8 files changed

+47
-5
lines changed

8 files changed

+47
-5
lines changed

.github/workflows/dist.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
# Note: the default manylinux is manylinux2014
6868
run: |
6969
python -m pip install -U pip
70-
python -m pip install "cibuildwheel>=2.17,<3"
70+
python -m pip install "cibuildwheel>=2.20,<3"
7171
7272
- name: Build wheels
7373
env:
@@ -89,6 +89,9 @@ jobs:
8989
ls wheelhouse/*cp310*.whl
9090
ls wheelhouse/*cp311*.whl
9191
ls wheelhouse/*cp312*.whl
92+
ls wheelhouse/*cp313*.whl
93+
# Free-threading builds:
94+
ls wheelhouse/*cp313t*.whl
9295
9396
- uses: actions/upload-artifact@v4
9497
with:

.github/workflows/test-python.yml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,18 @@ jobs:
5151
strategy:
5252
matrix:
5353
os: [ubuntu-20.04]
54-
python-version: ["3.9", "pypy-3.9", "3.13"]
54+
python-version: ["3.9", "pypy-3.9", "3.13", "3.13t"]
5555
name: CPython ${{ matrix.python-version }}-${{ matrix.os }}
5656
steps:
5757
- uses: actions/checkout@v4
58-
- name: Setup Python
58+
- if: ${{ matrix.python-version == '3.13t' }}
59+
name: Setup free-threaded Python
60+
uses: deadsnakes/[email protected]
61+
with:
62+
python-version: 3.13
63+
nogil: true
64+
- if: ${{ matrix.python-version != '3.13t' }}
65+
name: Setup Python
5966
uses: actions/setup-python@v5
6067
with:
6168
python-version: ${{ matrix.python-version }}
@@ -65,9 +72,13 @@ jobs:
6572
- name: Install dependencies
6673
run: |
6774
pip install -U pip
68-
if [ "${{ matrix.python-version }}" == "3.13" ]; then
75+
if [[ "${{ matrix.python-version }}" == "3.13" ]]; then
6976
pip install --pre cffi setuptools
7077
pip install --no-build-isolation hatch
78+
elif [[ "${{ matrix.python-version }}" == "3.13t" ]]; then
79+
# Hatch can't be installed on 3.13t, use pytest directly.
80+
pip install .
81+
pip install -r requirements/test.txt
7182
else
7283
pip install hatch
7384
fi
@@ -77,7 +88,11 @@ jobs:
7788
mongodb-version: 6.0
7889
- name: Run tests
7990
run: |
80-
hatch run test:test
91+
if [[ "${{ matrix.python-version }}" == "3.13t" ]]; then
92+
pytest -v --durations=5 --maxfail=10
93+
else
94+
hatch run test:test
95+
fi
8196
8297
doctest:
8398
runs-on: ubuntu-latest

bson/_cbsonmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,6 +3184,9 @@ static PyModuleDef_Slot _cbson_slots[] = {
31843184
{Py_mod_exec, _cbson_exec},
31853185
#if defined(Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED)
31863186
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED},
3187+
#endif
3188+
#if PY_VERSION_HEX >= 0x030D0000
3189+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
31873190
#endif
31883191
{0, NULL},
31893192
};

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ PyMongo 4.11 brings a number of changes including:
1212

1313
- Dropped support for Python 3.8.
1414
- Dropped support for MongoDB 3.6.
15+
- Added support for free-threaded Python with the GIL disabled. For more information see:
16+
`Free-threaded CPython <https://docs.python.org/3.13/whatsnew/3.13.html#whatsnew313-free-threaded-cpython>`_.
1517

1618
Issues Resolved
1719
...............

pymongo/_cmessagemodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,9 @@ static PyModuleDef_Slot _cmessage_slots[] = {
10221022
{Py_mod_exec, _cmessage_exec},
10231023
#ifdef Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED
10241024
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED},
1025+
#endif
1026+
#if PY_VERSION_HEX >= 0x030D0000
1027+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
10251028
#endif
10261029
{0, NULL},
10271030
};

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ partial_branches = ["if (.*and +)*not _use_c( and.*)*:"]
236236
directory = "htmlcov"
237237

238238
[tool.cibuildwheel]
239+
# Enable free-threaded support
240+
free-threaded-support = true
239241
skip = "pp* *-musllinux*"
240242
build-frontend = "build"
241243
test-command = "python {project}/tools/fail_if_no_c.py"

test/asynchronous/test_client_context.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ def test_setdefaultencoding_worked(self):
6161

6262
self.assertEqual(sys.getdefaultencoding(), os.environ["SETDEFAULTENCODING"])
6363

64+
def test_free_threading_is_enabled(self):
65+
if "free-threading build" not in sys.version:
66+
raise SkipTest("this test requires the Python free-threading build")
67+
68+
# If the GIL is enabled then pymongo or one of our deps does not support free-threading.
69+
self.assertFalse(sys._is_gil_enabled()) # type: ignore[attr-defined]
70+
6471

6572
if __name__ == "__main__":
6673
unittest.main()

test/test_client_context.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ def test_setdefaultencoding_worked(self):
6161

6262
self.assertEqual(sys.getdefaultencoding(), os.environ["SETDEFAULTENCODING"])
6363

64+
def test_free_threading_is_enabled(self):
65+
if "free-threading build" not in sys.version:
66+
raise SkipTest("this test requires the Python free-threading build")
67+
68+
# If the GIL is enabled then pymongo or one of our deps does not support free-threading.
69+
self.assertFalse(sys._is_gil_enabled()) # type: ignore[attr-defined]
70+
6471

6572
if __name__ == "__main__":
6673
unittest.main()

0 commit comments

Comments
 (0)