-
-
Notifications
You must be signed in to change notification settings - Fork 349
Support wiring of Cython-compiled modules #965
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
Merged
ZipFile
merged 10 commits into
ets-labs:develop
from
keyz182:fix/cython-cyfunction-wiring-discovery-on-develop
May 19, 2026
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
098b920
Support wiring of Cython-compiled modules
keyz182 ca3036c
refactor: simplify Cython wiring per review
keyz182 6cde4f2
fix(test): guard pyximport import for non-Cython tox envs
keyz182 74754f6
Update docs/wiring.rst
keyz182 ce9a94e
Update docs/wiring.rst
keyz182 4721621
Update tests/unit/samples/wiringcython/cythonmodule.pyx
keyz182 9715e4e
Update tox.ini
keyz182 945ddbd
Revert "fix(test): guard pyximport import for non-Cython tox envs"
keyz182 6eeb37f
docs: note @cython.annotation_typing(False) for FastAPI views/deps
keyz182 11fa4d2
fix(test): move pyximport into test_cython, drop wiring conftest
keyz182 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| """DI container used by the Cython-compiled wiring fixture.""" | ||
|
|
||
| from dependency_injector import containers, providers | ||
|
|
||
|
|
||
| class Service: | ||
| """Simple service injected into the Cython-compiled fixture handlers.""" | ||
|
|
||
| def __init__(self, value: str = "default") -> None: | ||
| self.value = value | ||
|
|
||
| async def aget(self) -> str: | ||
| return self.value | ||
|
|
||
| def get(self) -> str: | ||
| return self.value | ||
|
|
||
|
|
||
| class Container(containers.DeclarativeContainer): | ||
| service = providers.Factory(Service, value="injected") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # cython: language_level=3, binding=True, embedsignature=True, annotation_typing=False | ||
| """Cython-compiled fixture exercising wire() against compiled handlers. | ||
|
|
||
| Compiled by tests/unit/wiring/conftest.py::pytest_configure with the four | ||
| directives FastAPI / dependency-injector codebases require: | ||
|
|
||
| binding=True — preserve descriptor semantics (so inspect.signature | ||
| + DI introspection work) | ||
| embedsignature=True — embed Python-style signature for inspect.signature | ||
| annotation_typing=False — annotations stay informational; do NOT generate | ||
| C-level isinstance checks against parameter defaults | ||
| (the FastAPI `param: str = Header(...)` pattern | ||
| relies on this) | ||
| language_level=3 — pure Python 3 semantics | ||
|
|
||
| Exercises every call shape the wiring discovery pass dispatches on: | ||
|
|
||
| - sync def at module level -> _get_sync_patched | ||
| - async def at module level -> _get_async_patched | ||
| - async def with yield (async generator) -> _get_async_gen_patched | ||
| - class with async def __call__ -> _patch_method | ||
| """ | ||
|
keyz182 marked this conversation as resolved.
Outdated
|
||
|
|
||
| from dependency_injector.wiring import Provide | ||
|
|
||
| from samples.wiringcython.container import Container, Service | ||
|
|
||
|
|
||
| def sync_handler(svc: Service = Provide[Container.service]) -> str: | ||
| return svc.get() | ||
|
|
||
|
|
||
| async def async_handler(svc: Service = Provide[Container.service]) -> str: | ||
| return await svc.aget() | ||
|
|
||
|
|
||
| async def async_gen_handler(svc: Service = Provide[Container.service]): | ||
| yield svc.get() | ||
| yield svc.get() + "_2" | ||
|
|
||
|
|
||
| class HandlerClass: | ||
| async def __call__(self, svc: Service = Provide[Container.service]) -> str: | ||
| return svc.get() | ||
|
ZipFile marked this conversation as resolved.
Outdated
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| """Build Cython test fixtures at session start. | ||
|
|
||
| The wiring test suite includes regression coverage for Cython-compiled user | ||
| modules (see tests/unit/wiring/test_cython.py). This conftest compiles the | ||
| .pyx fixture into a .so before test collection so the import succeeds. | ||
|
|
||
| If Cython or a C toolchain is unavailable, the build is skipped silently | ||
| and the cython test module raises pytest.importorskip at collection. | ||
| """ | ||
|
|
||
| import logging | ||
| import sysconfig | ||
| from pathlib import Path | ||
|
|
||
| _LOG = logging.getLogger(__name__) | ||
|
|
||
| _FIXTURE_DIR = ( | ||
| Path(__file__).resolve().parent.parent / "samples" / "wiringcython" | ||
| ) | ||
| _PYX = _FIXTURE_DIR / "cythonmodule.pyx" | ||
|
|
||
|
|
||
| def _ext_suffix() -> str: | ||
| return sysconfig.get_config_var("EXT_SUFFIX") or ".so" | ||
|
ZipFile marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| def _fixture_so_path() -> Path: | ||
| """Exact .so path the build produces under the current interpreter. | ||
|
|
||
| Includes the ABI tag (e.g. ``.cpython-313-x86_64-linux-gnu.so``) so a | ||
| stray .so built against a different Python / platform is treated as a | ||
| miss instead of skipping the build silently on cross-ABI CI runs. | ||
| """ | ||
| return _FIXTURE_DIR / f"cythonmodule{_ext_suffix()}" | ||
|
|
||
|
|
||
| def _fixture_already_built() -> bool: | ||
| so_path = _fixture_so_path() | ||
| if not so_path.exists(): | ||
| return False | ||
| return so_path.stat().st_mtime >= _PYX.stat().st_mtime | ||
|
|
||
|
|
||
| def _build_fixture() -> bool: | ||
| if not _PYX.exists(): | ||
| return False | ||
| if _fixture_already_built(): | ||
| return True | ||
| try: | ||
| from Cython.Build import cythonize | ||
| from setuptools import Extension | ||
| from setuptools.command.build_ext import build_ext | ||
| from setuptools.dist import Distribution | ||
| except ImportError as exc: | ||
|
ZipFile marked this conversation as resolved.
Outdated
|
||
| _LOG.info("Cython fixture build skipped (missing dep): %s", exc) | ||
| return False | ||
|
|
||
| # Bare module name (no dots) means setuptools writes the .so directly | ||
| # to ``<build_lib>/cythonmodule<EXT_SUFFIX>`` — no package-layout | ||
| # subdir is created. With ``build_lib`` pointed at the fixture dir, | ||
| # the .so lands next to the .pyx where | ||
| # ``from samples.wiringcython.cythonmodule import ...`` resolves via | ||
| # the ``tests/unit/conftest.py`` ``sys.path`` insertion. | ||
| # | ||
| # ``inplace`` MUST be 0 here: ``inplace=1`` ignores ``build_lib`` and | ||
| # writes next to the source tree relative to CWD, which on a clean | ||
| # checkout drops the .so at the repo root. | ||
| ext = Extension( | ||
| "cythonmodule", | ||
| sources=[str(_PYX)], | ||
| ) | ||
| ext_modules = cythonize( | ||
| [ext], | ||
| compiler_directives={ | ||
| "language_level": 3, | ||
| "binding": True, | ||
| "embedsignature": True, | ||
| "annotation_typing": False, | ||
| }, | ||
| quiet=True, | ||
| ) | ||
| dist = Distribution( | ||
| {"name": "wiringcython_fixture", "ext_modules": ext_modules} | ||
| ) | ||
| cmd = build_ext(dist) | ||
| cmd.inplace = 0 | ||
| cmd.build_lib = str(_FIXTURE_DIR) | ||
| cmd.build_temp = str(_FIXTURE_DIR / "_build") | ||
| cmd.ensure_finalized() | ||
| try: | ||
| cmd.run() | ||
| except Exception as exc: # noqa: BLE001 — surface every build failure mode | ||
| _LOG.warning("Cython fixture build failed: %s", exc) | ||
| return False | ||
|
|
||
| # Positive post-condition — the .so must be where the test importer | ||
| # will look. If setuptools silently changed layout under us, fail | ||
| # loud here instead of letting test_cython.py skip on importorskip. | ||
| so_path = _fixture_so_path() | ||
| if not so_path.exists(): | ||
| _LOG.warning( | ||
| "Cython fixture build completed but expected .so missing at %s", | ||
| so_path, | ||
| ) | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def pytest_configure(config): | ||
| """Compile cythonmodule.pyx so test_cython.py can import the .so.""" | ||
| _build_fixture() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.