Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the free-threaded build of CPython 3.13 #1456

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ env:
PY_COLORS: 1 # Recognized by the `py` package, dependency of `pytest`
PYTHONIOENCODING: utf-8
PYTHONUTF8: 1
PYTHON_LATEST: 3.12
PYTHON_LATEST: 3.13


jobs:
Expand Down Expand Up @@ -184,6 +184,7 @@ jobs:
strategy:
matrix:
pyver:
- 3.13t
- 3.13
- 3.12
- 3.11
Expand Down Expand Up @@ -231,7 +232,7 @@ jobs:

- name: Setup Python ${{ matrix.pyver }}
id: python-install
uses: actions/setup-python@v5
uses: quansight-labs/setup-python@v5
with:
python-version: ${{ matrix.pyver }}
allow-prereleases: true
Expand All @@ -241,6 +242,14 @@ jobs:
uses: py-actions/py-dependency-install@v4
with:
path: requirements/test.txt
- name: Install Cython nightly on free-threading
if: matrix.pyver == '3.13t'
run: |
python -m pip uninstall -y cython
python -m pip install cython \
--pre \
--extra-index-url \
https://pypi.anaconda.org/scientific-python-nightly-wheels/simple
- name: Determine pre-compiled compatible wheel
env:
# NOTE: When `pip` is forced to colorize output piped into `jq`,
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable-linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ env:
PY_COLORS: 1 # Recognized by the `py` package, dependency of `pytest`
PYTHONIOENCODING: utf-8
PYTHONUTF8: 1
PYTHON_LATEST: 3.12
PYTHON_LATEST: 3.13

jobs:

Expand Down
1 change: 1 addition & 0 deletions CHANGES/1456.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implemented support for the free-threaded build of CPython 3.13 -- by :user:`lysnikolaou`.
1 change: 1 addition & 0 deletions CHANGES/1456.packaging.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Started building wheels for the free-threaded build of CPython 3.13 -- by :user:`lysnikolaou`.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ graft docs
graft CHANGES
graft requirements
graft tests
graft scripts
global-exclude *.pyc
global-exclude *.cache
exclude yarl/*.c
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ linetrace = "True" # Implies `profile=True`

[tool.cibuildwheel]
build-frontend = "build"
enable = ["cpython-freethreading"]
before-test = [
# NOTE: Attempt to have pip pre-compile PyYAML wheel with our build
# NOTE: constraints unset. The hope is that pip will cache that wheel
Expand Down Expand Up @@ -91,3 +92,8 @@ pure-python = "false"

[tool.cibuildwheel.windows]
before-test = [] # Windows cmd has different syntax and pip chooses wheels

[[tool.cibuildwheel.overrides]]
select = "cp313t-*"
build-frontend = "build; args: --no-isolation"
before-build = "bash {package}/scripts/cibw_before_build.sh"
7 changes: 7 additions & 0 deletions scripts/cibw_before_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# TODO: Delete when there's a PyPI Cython release (3.1.0) that supports free-threaded Python 3.13.
FREE_THREADED_BUILD="$(python -c"import sysconfig; print(bool(sysconfig.get_config_var('Py_GIL_DISABLED')))")"
if [[ $FREE_THREADED_BUILD == "True" ]]; then
python -m pip install -U pip
python -m pip install -i https://pypi.anaconda.org/scientific-python-nightly-wheels/simple cython
python -m pip install setuptools expandvars
fi
47 changes: 23 additions & 24 deletions yarl/_quoting_c.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# cython: language_level=3
# cython: language_level=3, freethreading_compatible=True

from cpython.exc cimport PyErr_NoMemory
from cpython.mem cimport PyMem_Free, PyMem_Malloc, PyMem_Realloc
Expand All @@ -25,7 +25,6 @@ cdef str ALLOWED = UNRESERVED + SUB_DELIMS_WITHOUT_QS
cdef str QS = '+&=;'

DEF BUF_SIZE = 8 * 1024 # 8KiB
cdef char BUFFER[BUF_SIZE]

cdef inline Py_UCS4 _to_hex(uint8_t v) noexcept:
if v < 10:
Expand All @@ -49,14 +48,14 @@ cdef inline int _is_lower_hex(Py_UCS4 v) noexcept:
return 'a' <= v <= 'f'


cdef inline Py_UCS4 _restore_ch(Py_UCS4 d1, Py_UCS4 d2):
cdef inline long _restore_ch(Py_UCS4 d1, Py_UCS4 d2):
cdef int digit1 = _from_hex(d1)
if digit1 < 0:
return <Py_UCS4>-1
return -1
cdef int digit2 = _from_hex(d2)
if digit2 < 0:
return <Py_UCS4>-1
return <Py_UCS4>(digit1 << 4 | digit2)
return -1
return digit1 << 4 | digit2


cdef uint8_t ALLOWED_TABLE[16]
Expand Down Expand Up @@ -91,15 +90,18 @@ cdef struct Writer:


cdef inline void _init_writer(Writer* writer):
writer.buf = &BUFFER[0]
cdef char *buf = <char *> PyMem_Malloc(BUF_SIZE)
if buf == NULL:
PyErr_NoMemory()
return
writer.buf = buf
writer.size = BUF_SIZE
writer.pos = 0
writer.changed = 0


cdef inline void _release_writer(Writer* writer):
if writer.buf != BUFFER:
PyMem_Free(writer.buf)
PyMem_Free(writer.buf)


cdef inline int _write_char(Writer* writer, Py_UCS4 ch, bint changed):
Expand All @@ -109,17 +111,10 @@ cdef inline int _write_char(Writer* writer, Py_UCS4 ch, bint changed):
if writer.pos == writer.size:
# reallocate
size = writer.size + BUF_SIZE
if writer.buf == BUFFER:
buf = <char*>PyMem_Malloc(size)
if buf == NULL:
PyErr_NoMemory()
return -1
memcpy(buf, writer.buf, writer.size)
else:
buf = <char*>PyMem_Realloc(writer.buf, size)
if buf == NULL:
PyErr_NoMemory()
return -1
buf = <char*>PyMem_Realloc(writer.buf, size)
if buf == NULL:
PyErr_NoMemory()
return -1
writer.buf = buf
writer.size = size
writer.buf[writer.pos] = <char>ch
Expand Down Expand Up @@ -255,18 +250,20 @@ cdef class _Quoter:
Writer *writer
):
cdef Py_UCS4 ch
cdef long chl
cdef int changed
cdef Py_ssize_t idx = 0

while idx < length:
ch = PyUnicode_READ(kind, data, idx)
idx += 1
if ch == '%' and self._requote and idx <= length - 2:
ch = _restore_ch(
chl = _restore_ch(
PyUnicode_READ(kind, data, idx),
PyUnicode_READ(kind, data, idx + 1)
)
if ch != <Py_UCS4>-1:
if chl != -1:
ch = <Py_UCS4>chl
idx += 2
if ch < 128:
if bit_at(self._protected_table, ch):
Expand Down Expand Up @@ -342,6 +339,7 @@ cdef class _Unquoter:
cdef Py_ssize_t consumed
cdef str unquoted
cdef Py_UCS4 ch = 0
cdef long chl = 0
cdef Py_ssize_t idx = 0
cdef Py_ssize_t start_pct
cdef int kind = PyUnicode_KIND(val)
Expand All @@ -352,11 +350,12 @@ cdef class _Unquoter:
idx += 1
if ch == '%' and idx <= length - 2:
changed = 1
ch = _restore_ch(
chl = _restore_ch(
PyUnicode_READ(kind, data, idx),
PyUnicode_READ(kind, data, idx + 1)
)
if ch != <Py_UCS4>-1:
if chl != -1:
ch = <Py_UCS4>chl
idx += 2
assert buflen < 4
buffer[buflen] = ch
Expand Down
Loading