Skip to content

Commit f545c23

Browse files
lysnikolaoupre-commit-ci[bot]bdracowebknjaz
authored
Implement support for the free-threaded build of CPython 3.13 (#618)
* Implement support for the free-threaded build of CPython 3.13 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add towncrier blurbs * Fix thread safety issues with critical sections - This requires Cython nightly, so build configs have been changed to accomodate that. - Reverted some of the changes that weren't related to free-threading * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix linting * Use C++ atomics for frozen instead of critical sections * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Revert to using actions/setup-python * Update packaging/pep517_backend/_backend.py Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) <[email protected]> * Revert changes to codecov flags now that actions/setup-python is used --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: J. Nick Koston <[email protected]> Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) <[email protected]>
1 parent 4ee4583 commit f545c23

File tree

10 files changed

+36
-14
lines changed

10 files changed

+36
-14
lines changed

.github/workflows/ci-cd.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ jobs:
174174
strategy:
175175
matrix:
176176
pyver:
177+
- 3.13t
177178
- 3.13
178179
- 3.12
179180
- 3.11
@@ -238,7 +239,8 @@ jobs:
238239
- name: Install dependencies
239240
uses: py-actions/py-dependency-install@v4
240241
with:
241-
path: requirements/test.txt
242+
path: requirements/test${{
243+
matrix.pyver == '3.13t' && '-freethreading' || '' }}.txt
242244
- name: Determine pre-compiled compatible wheel
243245
env:
244246
# NOTE: When `pip` is forced to colorize output piped into `jq`,

CHANGES/618.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implemented support for the free-threaded build of CPython 3.13 -- by :user:`lysnikolaou`.

CHANGES/618.packaging.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Started building wheels for the free-threaded build of CPython 3.13 -- by :user:`lysnikolaou`.

frozenlist/_frozenlist.pyx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
1+
# cython: freethreading_compatible = True
2+
# distutils: language = c++
3+
4+
from cpython.bool import PyBool_FromLong
5+
from libcpp.atomic cimport atomic
6+
17
import types
28
from collections.abc import MutableSequence
39

410

511
cdef class FrozenList:
612
__class_getitem__ = classmethod(types.GenericAlias)
713

8-
cdef readonly bint frozen
14+
cdef atomic[bint] _frozen
915
cdef list _items
1016

1117
def __init__(self, items=None):
12-
self.frozen = False
18+
self._frozen.store(False)
1319
if items is not None:
1420
items = list(items)
1521
else:
1622
items = []
1723
self._items = items
1824

25+
@property
26+
def frozen(self):
27+
return PyBool_FromLong(self._frozen.load())
28+
1929
cdef object _check_frozen(self):
20-
if self.frozen:
30+
if self._frozen.load():
2131
raise RuntimeError("Cannot modify frozen list.")
2232

2333
cdef inline object _fast_len(self):
2434
return len(self._items)
2535

2636
def freeze(self):
27-
self.frozen = True
37+
self._frozen.store(True)
2838

2939
def __getitem__(self, index):
3040
return self._items[index]
@@ -103,11 +113,11 @@ cdef class FrozenList:
103113
return self._items.count(item)
104114

105115
def __repr__(self):
106-
return '<FrozenList(frozen={}, {!r})>'.format(self.frozen,
116+
return '<FrozenList(frozen={}, {!r})>'.format(self._frozen.load(),
107117
self._items)
108118

109119
def __hash__(self):
110-
if self.frozen:
120+
if self._frozen.load():
111121
return hash(tuple(self._items))
112122
else:
113123
raise RuntimeError("Cannot hash unfrozen list.")

packaging/pep517_backend/_backend.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import annotations
55

66
import os
7+
import sysconfig
78
from contextlib import contextmanager, nullcontext, suppress
89
from functools import partial
910
from pathlib import Path
@@ -372,10 +373,12 @@ def get_requires_for_build_wheel(
372373
stacklevel=999,
373374
)
374375

375-
c_ext_build_deps = [] if is_pure_python_build else [
376-
'Cython ~= 3.0.0; python_version >= "3.12"',
377-
'Cython; python_version < "3.12"',
378-
]
376+
if is_pure_python_build:
377+
c_ext_build_deps = []
378+
elif sysconfig.get_config_var('Py_GIL_DISABLED'):
379+
c_ext_build_deps = ['Cython ~= 3.1.0a1']
380+
else:
381+
c_ext_build_deps = ['Cython >= 3.0.12']
379382

380383
return _setuptools_get_requires_for_build_wheel(
381384
config_settings=config_settings,

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ linetrace = "True" # Implies `profile=True`
6363
#error_on_uninitialized = "True"
6464

6565
[tool.cibuildwheel]
66+
enable = ["cpython-freethreading"]
6667
build-frontend = "build"
6768
before-test = [
6869
# NOTE: Attempt to have pip pre-compile PyYAML wheel with our build

requirements/cython-freethreading.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cython==3.1.0a1

requirements/test-freethreading.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-r cython-freethreading.txt
2+
-r test-pure.txt

requirements/test-pure.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage==7.6.1
2+
pytest==7.4.3
3+
pytest-cov==4.1.0

requirements/test.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
-r cython.txt
2-
coverage==7.6.1
3-
pytest==7.4.3
4-
pytest-cov==4.1.0
2+
-r test-pure.txt

0 commit comments

Comments
 (0)