Skip to content

Commit b5f1aa5

Browse files
Add Cython coverage (aio-libs#12349)
1 parent 48ac9b1 commit b5f1aa5

5 files changed

Lines changed: 100 additions & 6 deletions

File tree

.coveragerc-cython.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[run]
2+
branch = true
3+
plugins = [
4+
'Cython.Coverage',
5+
]
6+
omit = [
7+
'site-packages',
8+
]
9+
10+
[report]
11+
exclude_also = [
12+
'if TYPE_CHECKING',
13+
'assert False',
14+
': \.\.\.(\s*#.*)?$',
15+
'^ +\.\.\.$',
16+
'pytest.fail\('
17+
]

.github/workflows/ci-cd.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,64 @@ jobs:
416416
run: python -Im pytest --no-cov --numprocesses=0 -vvvvv --codspeed
417417

418418

419+
cython-coverage:
420+
permissions:
421+
contents: read # to fetch code (actions/checkout)
422+
423+
name: Cython coverage
424+
needs: gen_llhttp
425+
runs-on: ubuntu-latest
426+
steps:
427+
- name: Checkout
428+
uses: actions/checkout@v6
429+
with:
430+
submodules: true
431+
- name: Setup Python
432+
id: python-install
433+
uses: actions/setup-python@v6
434+
with:
435+
python-version: '3.12'
436+
- name: Update pip, wheel, setuptools, build, twine
437+
run: |
438+
python -m pip install -U pip wheel setuptools build twine
439+
- name: Install dependencies
440+
run: |
441+
python -Im pip install -r requirements/test.in -c requirements/test.txt
442+
- name: Uninstall blocbuster
443+
run: python -m pip uninstall blockbuster -y
444+
- name: Restore llhttp generated files
445+
uses: actions/download-artifact@v8
446+
with:
447+
name: llhttp
448+
path: vendor/llhttp/build/
449+
- name: Cythonize with linetrace
450+
run: |
451+
make cythonize CYTHON_EXTRA="-X linetrace=True"
452+
- name: Install self
453+
env:
454+
AIOHTTP_CYTHON_TRACE: 1
455+
run: python -m pip install -e .
456+
- name: Run tests with Cython tracing
457+
env:
458+
COLOR: yes
459+
PIP_USER: 1
460+
run: >-
461+
pytest tests/test_client_functional.py tests/test_http_parser.py tests/test_http_writer.py tests/test_web_functional.py tests/test_web_response.py tests/test_websocket_parser.py
462+
--cov-config=.coveragerc-cython.toml
463+
-m 'not dev_mode and not autobahn'
464+
shell: bash
465+
- name: Turn coverage into xml
466+
run: |
467+
python -m coverage xml -o cython-coverage.xml --rcfile=.coveragerc-cython.toml
468+
- name: Upload coverage
469+
uses: codecov/codecov-action@v6
470+
with:
471+
files: ./cython-coverage.xml
472+
disable_search: true
473+
flags: cython-coverage
474+
token: ${{ secrets.CODECOV_TOKEN }}
475+
fail_ci_if_error: true
476+
419477
check: # This job does nothing and is only used for the branch protection
420478
if: always()
421479

CHANGES/12349.contrib.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added a CI job to measure Cython coverage -- by :user:`Dreamsorcerer`.

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
to-hash-one = $(dir $1).hash/$(addsuffix .hash,$(notdir $1))
44
to-hash = $(foreach fname,$1,$(call to-hash-one,$(fname)))
55

6+
CYTHON_EXTRA ?=
67
CYS := $(wildcard aiohttp/*.pyx) $(wildcard aiohttp/*.pyi) $(wildcard aiohttp/*.pxd) $(wildcard aiohttp/_websocket/*.pyx) $(wildcard aiohttp/_websocket/*.pyi) $(wildcard aiohttp/_websocket/*.pxd)
78
PYXS := $(wildcard aiohttp/*.pyx) $(wildcard aiohttp/_websocket/*.pyx)
89
CS := $(wildcard aiohttp/*.c) $(wildcard aiohttp/_websocket/*.c)
@@ -59,14 +60,14 @@ aiohttp/_find_header.c: $(call to-hash,aiohttp/hdrs.py ./tools/gen.py)
5960
# Special case for reader since we want to be able to disable
6061
# the extension with AIOHTTP_NO_EXTENSIONS
6162
aiohttp/_websocket/reader_c.c: aiohttp/_websocket/reader_c.py
62-
cython -3 -X freethreading_compatible=True -o $@ $< -I aiohttp -Werror
63+
cython -3 -X freethreading_compatible=True $(CYTHON_EXTRA) -o $@ $< -I aiohttp -Werror
6364

6465
# _find_headers generator creates _headers.pyi as well
6566
aiohttp/%.c: aiohttp/%.pyx $(call to-hash,$(CYS)) aiohttp/_find_header.c
66-
cython -3 -X freethreading_compatible=True -o $@ $< -I aiohttp -Werror
67+
cython -3 -X freethreading_compatible=True $(CYTHON_EXTRA) -o $@ $< -I aiohttp -Werror
6768

6869
aiohttp/_websocket/%.c: aiohttp/_websocket/%.pyx $(call to-hash,$(CYS))
69-
cython -3 -X freethreading_compatible=True -o $@ $< -I aiohttp -Werror
70+
cython -3 -X freethreading_compatible=True $(CYTHON_EXTRA) -o $@ $< -I aiohttp -Werror
7071

7172
vendor/llhttp/node_modules: vendor/llhttp/package.json
7273
cd vendor/llhttp; npm ci

setup.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
os.environ.get("AIOHTTP_USE_SYSTEM_DEPS", os.environ.get("USE_SYSTEM_DEPS"))
1313
)
1414
NO_EXTENSIONS: bool = bool(os.environ.get("AIOHTTP_NO_EXTENSIONS"))
15+
CYTHON_TRACING: bool = bool(os.environ.get("AIOHTTP_CYTHON_TRACE"))
1516
HERE = pathlib.Path(__file__).parent
1617
IS_GIT_REPO = (HERE / ".git").exists()
1718

@@ -54,8 +55,16 @@
5455
"include_dirs": ["vendor/llhttp/build"],
5556
}
5657

58+
cython_trace_macros = [("CYTHON_TRACE", 1)] if CYTHON_TRACING else []
59+
if cython_trace_macros:
60+
llhttp_kwargs.setdefault("define_macros", []).extend(cython_trace_macros)
61+
5762
extensions = [
58-
Extension("aiohttp._websocket.mask", ["aiohttp/_websocket/mask.c"]),
63+
Extension(
64+
"aiohttp._websocket.mask",
65+
["aiohttp/_websocket/mask.c"],
66+
define_macros=cython_trace_macros,
67+
),
5968
Extension(
6069
"aiohttp._http_parser",
6170
[
@@ -65,8 +74,16 @@
6574
],
6675
**llhttp_kwargs,
6776
),
68-
Extension("aiohttp._http_writer", ["aiohttp/_http_writer.c"]),
69-
Extension("aiohttp._websocket.reader_c", ["aiohttp/_websocket/reader_c.c"]),
77+
Extension(
78+
"aiohttp._http_writer",
79+
["aiohttp/_http_writer.c"],
80+
define_macros=cython_trace_macros,
81+
),
82+
Extension(
83+
"aiohttp._websocket.reader_c",
84+
["aiohttp/_websocket/reader_c.c"],
85+
define_macros=cython_trace_macros,
86+
),
7087
]
7188

7289

0 commit comments

Comments
 (0)