Skip to content

Commit

Permalink
Merge pull request #2 from tlambert03/linting
Browse files Browse the repository at this point in the history
linting, ruff, python3.12+, tests
  • Loading branch information
funkey authored Nov 7, 2024
2 parents c57c6c7 + 44eff2d commit 3e98088
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 62 deletions.
17 changes: 0 additions & 17 deletions .github/workflows/black.yaml

This file was deleted.

18 changes: 0 additions & 18 deletions .github/workflows/mypy.yaml

This file was deleted.

38 changes: 23 additions & 15 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand All @@ -16,12 +16,12 @@ authors = [
{ email = "[email protected]", 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'
Expand All @@ -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"]
2 changes: 2 additions & 0 deletions src/witty/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .compile_module import compile_module

__version__ = "0.1"

__all__ = ["compile_module"]
31 changes: 27 additions & 4 deletions src/witty/compile_module.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ 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++"
)
result = module_float.add(3, 4)

assert result == 7
assert type(result) == float
assert type(result) is float

0 comments on commit 3e98088

Please sign in to comment.