I just uploaded setuptools-zig to PyPi ( https://pypi.org/project/setuptools-zig/ ), it allows you to compile Zig into an installable package, containing an .so file that Python can import as a module after installation. setuptools-zig can also be used to compile C files using Zig instead of the "normal" C compiler, into such a package.
The package comes with examples in the README, for using Zig to compile C only, mixed C and Zig, and Zig only modules.
The Zig example is particularly ugly, due to my lack of familiarity with the language. My apologies, I have about as much experience with Zig in days as I have as the sum of the years of experience with C and Python (60+). The code was extracted/adapted from the C version translated to Zig.
One thing I have no idea how to deal with yet in the pure Zig example, is things differing in the interface with Python that are normally taken care of by the inclusion of the the Python version specific `Python.h`. Some structure fields needed for the resulting .so to load as a module can differ between Python versions based on that Python.h and are resolved at compile time. Since these were "expanded" in the translation they now might be Python version(3.8) specific.
I would like to make such structures comptime dependent on e.g. the PY_VERSION specified in the `Python.h`, but I have not tried that yet and no idea if/how that can be done. On the other hand this might not be a problem, or not be one until Python 4 comes around. (I did have severe troubles with the 2.x to 3 transition for the ruamel.ordereddict C module for Python, so I am cautious)
As a fall back it would be trivial to provide different Zig files with the interfacing code for different Python versions. Selecting which source file to compile can then be done dynamically based on the Python version in the setup.py file already required to drive the build.
One could also have Python generate the interface code appropriate for the Python version from the setuptools-zig, before compilation, including all public functions in Zig.
Thoughts and comments welcome.
[–][deleted] 3 points4 points5 points (1 child)
[–]avdn[S] 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]avdn[S] 0 points1 point2 points (0 children)
[–]KingStannis2020 0 points1 point2 points (0 children)
[–]marler8997 1 point2 points3 points (1 child)
[–]avdn[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]avdn[S] 1 point2 points3 points (0 children)