Paul Moore schrieb am 13.04.20 um 14:25:
On a related but different note, what is the recommended policy (assuming it's not to use the C API) for embedding Python, and for exposing the embedding app to Python as a C extension? My standard example of this is the Vim interface to Python - see https://github.com/vim/vim/blob/master/src/if_python3.c. I originally wrote this back in the Python 1.5 days, so it's *very* old, and quite likely not how I'd write it now, even using the C API. But what's the recommendation for code like that in the face of these changes, and the suggestion that using 3rd party tools is the normal way to write C extensions?
Embedding is not very well documented overall. I recently looked through the docs to collect what a user would need to know in this case, and ended up creating at least a little link collection, because I failed to find a good place to refer users to. The things people need to know from the CPython docs are scattered across different places, and lack a complete real-world-like example that "most people" could start from. (I don't think many users will pass strings into Python to execute code there.)
From Cython's PoV, the main thing that future embedders need to understand is that it's not really different from extending – you just have to start the Python runtime before doing anything else. I think there should be some help for getting that done, and then it's just executing your Python code in some module. Cython then has its ways to go back and forth from there, e.g. by writing cdef (C) functions as entry points for your application.
Cython currently doesn't really have "direct" support for embedding. You can let it generate a C main function for you to start your program, but that's not what you want in the case of vim. There's a "cython_freeze" script that generates an inittab list in addition, but it's a bit simplistic and not integrated. We have a beginners ticket for integrating it better:
What I would like to see eventually is to let users pass a list of modules into Cython's frontend (maybe cythonize(), maybe not), and then it would just generate a single distutils Extension from them that links everything together and registers all modules on import, optionally with a generated exported C function that starts up the whole thing. That seems simple enough to do and use, and you end up with a shared library that your application can load. PRs welcome. :)