diff --git a/.github/workflows/black.yaml b/.github/workflows/black.yaml deleted file mode 100644 index 60b6467..0000000 --- a/.github/workflows/black.yaml +++ /dev/null @@ -1,17 +0,0 @@ -name: Python Black - -on: [push, pull_request] - -jobs: - lint: - name: Python Lint - runs-on: ubuntu-latest - steps: - - name: Setup Python - uses: actions/setup-python@v4 - - name: Setup checkout - uses: actions/checkout@master - - name: Lint with Black - run: | - pip install black - black --diff --check src/witty tests diff --git a/.github/workflows/mypy.yaml b/.github/workflows/mypy.yaml deleted file mode 100644 index 495582f..0000000 --- a/.github/workflows/mypy.yaml +++ /dev/null @@ -1,18 +0,0 @@ -name: Python mypy - -on: [push, pull_request] - -jobs: - static-analysis: - name: Python mypy - runs-on: ubuntu-latest - steps: - - name: Setup Python - uses: actions/setup-python@v4 - - name: Setup checkout - uses: actions/checkout@v2 - - name: mypy - run: | - pip install . - pip install --upgrade mypy - mypy src/witty diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 384aaea..7f5bcca 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -1,25 +1,33 @@ name: Test -on: - push: +on: [push, pull_request] jobs: - test: + lint: + name: Lint runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Run pre-commit + run: pipx run pre-commit run --all-files + + test: + runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - python-version: ["3.9", "3.10"] + os: [ubuntu-latest, macos-latest, windows-latest] + python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] steps: - - uses: actions/checkout@v2 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - pip install ".[dev]" - - name: Test with pytest - run: | - pytest tests + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + pip install ".[dev]" + - name: Test with pytest + run: | + pytest --color=yes -v tests diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 17dc760..e6f0cfe 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,10 +14,12 @@ repos: - id: check-yaml - id: check-added-large-files - - repo: https://github.com/psf/black - rev: 24.10.0 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.7.2 hooks: - - id: black + - id: ruff + args: [--fix, --unsafe-fixes] + - id: ruff-format - repo: https://github.com/pre-commit/mirrors-mypy rev: v1.13.0 diff --git a/pyproject.toml b/pyproject.toml index 0dcb32f..727e634 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ requires = ["setuptools", "wheel"] name = "witty" description = "Well-in-Time Compiler for Cython Modules" readme = "README.md" -requires-python = ">=3.7" +requires-python = ">=3.9" classifiers = [ "Programming Language :: Python :: 3", ] @@ -16,12 +16,12 @@ authors = [ { email = "funkej@janelia.hhmi.org", name = "Jan Funke" }, ] dynamic = ["version"] -dependencies = ["cython"] +dependencies = ["cython", "setuptools; python_version >= '3.12'"] [project.optional-dependencies] dev = [ 'pytest', - 'black', + 'ruff', 'mypy', 'pdoc', 'pre-commit' @@ -30,3 +30,7 @@ dev = [ [project.urls] homepage = "https://github.com/funkelab/witty" repository = "https://github.com/funkelab/witty" + +[tool.ruff] +target-version = "py39" +src = ["src"] diff --git a/src/witty/__init__.py b/src/witty/__init__.py index 9d723e0..05cc9d7 100644 --- a/src/witty/__init__.py +++ b/src/witty/__init__.py @@ -1,3 +1,5 @@ from .compile_module import compile_module __version__ = "0.1" + +__all__ = ["compile_module"] diff --git a/src/witty/compile_module.py b/src/witty/compile_module.py index c388d07..cfa8a2b 100644 --- a/src/witty/compile_module.py +++ b/src/witty/compile_module.py @@ -1,14 +1,18 @@ +import os import Cython -import fcntl import hashlib import importlib.util import sys from Cython.Build import cythonize from Cython.Build.Inline import to_unicode, _get_build_extension from Cython.Utils import get_cython_cache_dir -from distutils.core import Extension from pathlib import Path +try: + from distutils.core import Extension +except ImportError: + from setuptools import Extension # type: ignore [no-redef] + def load_dynamic(module_name, module_lib): spec = importlib.util.spec_from_file_location(module_name, module_lib) @@ -111,8 +115,8 @@ def compile_module( module_dir.mkdir(parents=True, exist_ok=True) # make sure the same module is not build concurrently - with open(module_lock, "w") as lock_file: - fcntl.lockf(lock_file, fcntl.LOCK_EX) + with open(module_lock, "w") as lock_f: + lock_file(lock_f) # already compiled? if module_lib.is_file() and not force_rebuild: @@ -142,3 +146,22 @@ def compile_module( build_extension.run() return load_dynamic(module_name, module_lib) + + +if os.name == "nt": + import msvcrt + + def lock_file(file): + msvcrt.locking(file.fileno(), msvcrt.LK_LOCK, os.path.getsize(file.name)) + + def unlock_file(file): + msvcrt.locking(file.fileno(), msvcrt.LK_UNLCK, os.path.getsize(file.name)) + +else: + import fcntl + + def lock_file(file): + fcntl.lockf(file, fcntl.LOCK_EX) + + def unlock_file(file): + fcntl.lockf(file, fcntl.LOCK_UN) diff --git a/tests/test_compile.py b/tests/test_compile.py index af79bdc..c41e4fc 100644 --- a/tests/test_compile.py +++ b/tests/test_compile.py @@ -25,7 +25,7 @@ def to_vector({type} x): result = module_int.add(3, 4) assert result == 7 - assert type(result) == int + assert type(result) is int module_float = witty.compile_module( source_pxy_template.format(type="float"), language="c++" @@ -33,4 +33,4 @@ def to_vector({type} x): result = module_float.add(3, 4) assert result == 7 - assert type(result) == float + assert type(result) is float