You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As of Cython 3.0+, it is possible to write most of the Cython code as pure Python, leveraging decorators and type annotations to convey the desired C compilation behaviour.
This would require cython at runtime, but we can shim only the annotations in order to avoid a hard dependency. @CaselIT has already implemented a prototype in this vein for SQLAlchemy.
We might want to keep .pyx for some tight-knit C character loops used for parsing parameters, but, for instance, multipart stream reader is mostly about typing with Py_ssize_t, and calling internal helpers in C, so we should be able to easily rewrite it in such mixed mode.
The main benefit is obviously that we don't need to maintain two different codebases for the same thing. Also, even if we keep dual implementations, we would be able to easily test implementation in pure Python unit tests, tracking coverage.
The text was updated successfully, but these errors were encountered:
It's also possible to import a compiled pure python cython module without using the compiled code, by doing a bit of shenanigans with the import machinery. This is useful for tests and benchmarks:
_M=TypeVar("_M", bound=ModuleType)
defload_uncompiled_module(module: _M) ->_M:
"""Load the non-compied version of a module that is also compiled with cython. """full_name=module.__name__assertmodule.__spec__parent_name=module.__spec__.parentassertparent_nameparent_module=sys.modules[parent_name]
assertparent_module.__spec__package_path=parent_module.__spec__.originassertpackage_pathandpackage_path.endswith("__init__.py")
name=full_name.split(".")[-1]
module_path=package_path.replace("__init__.py", f"{name}.py")
py_spec=importlib.util.spec_from_file_location(full_name, module_path)
assertpy_specpy_module=importlib.util.module_from_spec(py_spec)
assertpy_spec.loaderpy_spec.loader.exec_module(py_module)
returncast(_M, py_module)
One unpleasant gotcha so far is that cython needs to be imported directly in a module that is being cythonized, importing via another module/compat loader doesn't work for some reason. Probably it is AST-parsed by Cython instead of using the normal import machinery.
As of Cython 3.0+, it is possible to write most of the Cython code as pure Python, leveraging decorators and type annotations to convey the desired C compilation behaviour.
This would require
cython
at runtime, but we can shim only the annotations in order to avoid a hard dependency. @CaselIT has already implemented a prototype in this vein for SQLAlchemy.We might want to keep
.pyx
for some tight-knit C character loops used for parsing parameters, but, for instance, multipart stream reader is mostly about typing withPy_ssize_t
, and calling internal helpers in C, so we should be able to easily rewrite it in such mixed mode.The main benefit is obviously that we don't need to maintain two different codebases for the same thing. Also, even if we keep dual implementations, we would be able to easily test implementation in pure Python unit tests, tracking coverage.
The text was updated successfully, but these errors were encountered: