Skip to content

Commit 4ab2ae9

Browse files
committed
Refactor pure vs cython builds
1 parent 6fb793d commit 4ab2ae9

File tree

5 files changed

+56
-83
lines changed

5 files changed

+56
-83
lines changed

.github/workflows/tests.yml

+15-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ jobs:
88
matrix:
99
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
1010
os: [ubuntu-20.04, macos-12, windows-2019]
11+
cython: [false, true]
1112
runs-on: ${{ matrix.os }}
1213
steps:
1314
- uses: actions/checkout@v4
@@ -20,27 +21,29 @@ jobs:
2021
with:
2122
poetry-version: 1.8.4
2223

23-
- name: Install Dependencies
24+
- name: Install Dependencies (Pure Python)
25+
if: ${{ !matrix.cython }}
2426
run: poetry install --without dev,docs
2527

26-
- name: Run Tests (Pure Python with coverage)
27-
run: poetry run pytest --cov=iscc_core --cov-report=xml -q tests
28+
- name: Install Dependencies (with Cython)
29+
if: ${{ matrix.cython }}
30+
run: poetry install --without dev,docs --extras cython
2831

29-
- name: Build Extension modules
30-
run: poetry install --without dev,docs --extras turbo
31-
32-
- name: Run Tests (With Extension Modules)
33-
run: poetry run pytest -q tests --turbo
32+
- name: Run Tests
33+
run: |
34+
poetry run python -c "import iscc_core; print(f'Using Cython: {iscc_core.USING_CYTHON}')"
35+
poetry run pytest -v tests
3436
3537
- name: Upload coverage to Codecov
3638
uses: codecov/codecov-action@v3
37-
if: matrix.os == 'ubuntu-20.04' && matrix.python-version == '3.7'
39+
if: matrix.os == 'ubuntu-20.04' && matrix.python-version == '3.9' && !matrix.cython
3840

3941
- name: Build Wheel
42+
if: ${{ matrix.cython }}
4043
run: poetry build -f wheel
4144

4245
- name: Test Wheel Installation
43-
if: runner.os != 'Windows'
46+
if: ${{ matrix.cython && runner.os != 'Windows' }}
4447
run: |
4548
python -m venv venv
4649
source venv/bin/activate
@@ -49,7 +52,7 @@ jobs:
4952
python -c "import iscc_core; print(iscc_core.turbo())"
5053
5154
- name: Test Wheel Installation on Windows
52-
if: runner.os == 'Windows'
55+
if: ${{ matrix.cython && runner.os == 'Windows' }}
5356
run: |
5457
python -m venv venv
5558
venv\Scripts\Activate.ps1
@@ -59,6 +62,7 @@ jobs:
5962
python -c "import iscc_core; print(iscc_core.turbo())"
6063
6164
- name: Collect Wheel
65+
if: ${{ matrix.cython }}
6266
uses: actions/upload-artifact@v3
6367
with:
6468
path: dist/*.whl

build.py

+24-56
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,32 @@
11
# -*- coding: utf-8 -*-
2-
"""
3-
Build cython extension modules.
2+
import os
3+
from setuptools import Extension
44

5-
The shared library can also be built manually using the command:
6-
$ cythonize -X language_level=3 -a -i ./iscc_core/cdc.py
7-
$ cythonize -X language_level=3 -a -i ./iscc_core/minhash.py
8-
$ cythonize -X language_level=3 -a -i ./iscc_core/simhash.py
9-
$ cythonize -X language_level=3 -a -i ./iscc_core/dct.py
10-
$ cythonize -X language_level=3 -a -i ./iscc_core/wtahash.py
11-
"""
12-
try:
13-
from Cython.Build import cythonize, build_ext
14-
except ImportError:
15-
# dummy build function for poetry
16-
def build(setup_kwargs):
17-
pass
185

19-
else:
6+
def build(setup_kwargs):
7+
try:
8+
from Cython.Build import cythonize
209

21-
class build_ext_gracefull(build_ext):
22-
def run(self):
23-
try:
24-
print("Trying to compile C accelerator modules")
25-
super().run()
26-
print("Successfully comiled C accelerator modules")
27-
except Exception as e:
28-
print(e)
29-
print("********************************************************************")
30-
print("Failed to compile C accelerator module, falling back to pure python.")
31-
print("********************************************************************")
10+
use_cython = True
11+
except ImportError:
12+
use_cython = False
3213

33-
def build_extensions(self):
34-
try:
35-
print("Trying to compile C accelerator modules")
36-
super().build_extensions()
37-
print("Successfully comiled C accelerator modules")
38-
except Exception as e:
39-
print(e)
40-
print("********************************************************************")
41-
print("Failed to compile C accelerator module, falling back to pure python.")
42-
print("********************************************************************")
43-
44-
def build(setup_kwargs):
14+
if use_cython:
4515
try:
46-
setup_kwargs.update(
47-
dict(
48-
ext_modules=cythonize(
49-
[
50-
"iscc_core/cdc.py",
51-
"iscc_core/minhash.py",
52-
"iscc_core/simhash.py",
53-
"iscc_core/dct.py",
54-
"iscc_core/wtahash.py",
55-
]
56-
),
57-
cmdclass=dict(build_ext=build_ext_gracefull),
58-
)
16+
ext_modules = cythonize(
17+
[
18+
Extension("iscc_core.cdc", ["iscc_core/cdc.py"]),
19+
Extension("iscc_core.minhash", ["iscc_core/minhash.py"]),
20+
Extension("iscc_core.simhash", ["iscc_core/simhash.py"]),
21+
Extension("iscc_core.dct", ["iscc_core/dct.py"]),
22+
Extension("iscc_core.wtahash", ["iscc_core/wtahash.py"]),
23+
],
24+
compiler_directives={"language_level": "3"},
5925
)
26+
setup_kwargs.update({"ext_modules": ext_modules})
27+
print("Cython modules prepared for compilation")
6028
except Exception as e:
61-
print(e)
62-
print("********************************************************************")
63-
print("Failed to compile C accelerator module, falling back to pure python.")
64-
print("********************************************************************")
29+
print(f"Failed to prepare Cython modules: {e}")
30+
print("Falling back to pure Python")
31+
else:
32+
print("Cython not available, using pure Python")

iscc_core/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
import sys
2+
3+
4+
def _using_cython_modules():
5+
modules = ["cdc", "minhash", "simhash", "dct", "wtahash"]
6+
return any(
7+
getattr(sys.modules.get(f"iscc_core.{module}"), "__file__", "").endswith((".so", ".pyd"))
8+
for module in modules
9+
)
10+
11+
12+
USING_CYTHON = _using_cython_modules()
13+
114
__version__ = "1.1.0"
215
from iscc_core.options import core_opts, conformant_options
316

iscc_core/check.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
"""Inspect lib environment/installation"""
3-
import inspect
4-
from loguru import logger as log
3+
from iscc_core import USING_CYTHON
54

65

76
__all__ = ["turbo"]
@@ -10,12 +9,4 @@
109
def turbo(): # pragma: no cover
1110
# type: () -> bool
1211
"""Check whether all optional cython extensions have been compiled to native modules."""
13-
from iscc_core import cdc, minhash, simhash, dct, wtahash
14-
15-
modules = (cdc, minhash, simhash, dct, wtahash)
16-
for module in modules:
17-
module_file = inspect.getfile(module)
18-
log.debug(f"Module {module.__name__} file: {module_file}")
19-
if module_file.endswith(".py") or module_file.endswith(".pyc"):
20-
return False
21-
return True
12+
return USING_CYTHON

tests/test_check.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,5 @@
22
import iscc_core as ic
33

44

5-
def test_check_turbo(turbo):
6-
if turbo is False:
7-
assert ic.turbo() is False
8-
else:
9-
assert ic.turbo() is True
5+
def test_check_turbo():
6+
assert ic.USING_CYTHON == ic.turbo()

0 commit comments

Comments
 (0)