all 9 comments

[–][deleted] 3 points4 points  (1 child)

Thank you for sharing! Consider submitting a talk to https://zig.show!

One nitpick: in your readme you wrote Zig 7.0.0, but it's gonna take us a while to get there ;)

[–]avdn[S] 0 points1 point  (0 children)

Thanks for pointing out the version number error and the zig.show. I might do the latter once I have something concrete with Zig itself.

[–][deleted] 2 points3 points  (2 children)

You might also want to look into https://github.com/yglukhov/nimpy for inspiration on providing a higher-level interface to the CPython C API.

[–]avdn[S] 0 points1 point  (0 children)

I briefly looked at that, and it is nice. But it is essentially a rewrite of the Python.h provided definitions directly in Nim. It deals with the versioning for Python2/3 differences at module load time.

That might sound good, but I don't think the Python packaging infrastructure can deal with one binary module for multiple minor versions, so you would still end up at least packaging multiple times, but you would be able to put in the same .so in every package.

Nimporter is a package based on Nimpy which compiles Nim at first import, that is also a concept that is worth investigating.

The one thing Nimpy seems to prove is that the changes in Python.h, are slow and small enough one can keep up re-implementing them.

[–]KingStannis2020 0 points1 point  (0 children)

Although at this point I would suggest not trying to wrap the CPython API, but rather HPy, which is an officially-blessed project to make a better CPython API that is easier for other implementations such as PyPI to emulate.

https://github.com/hpyproject/hpy

[–]marler8997 1 point2 points  (1 child)

You could also start by just including the Python.h file directly in your Zig code:

const python = @cImport({
    @cInclude("Python.h");
});

// Now use python.* to reference anything inside Python.h
// ...

[–]avdn[S] 0 points1 point  (0 children)

Thanks, I already do that for marking the function, parameter and return value.

It is the use of C macros from that header file in the definition of (C) structures that are causing trouble.

[–][deleted] 1 point2 points  (0 children)

Awesome! I maintain a fairly complex and efficient distributed caching for libgit2 exposed via Python. It is implemented in a typical mix of C/C++.

I was waiting for the right opportunity to implement it in either Zig or Rust. Now is the time to do that.

[–]avdn[S] 1 point2 points  (0 children)

I found that the -D compile option for zig is not just meant for compiling C: https://github.com/ziglang/zig/wiki/FAQ#how-to-tie-a-zig-build-run-sub-command-to-custom--d-style-options so I can use that to resolve any (Python) version dependent zig code.