4 ms·
How do extensions work with interpreters written other languages? Does the interpreter still expose a C API?
by curt15 3y ago
How do extensions work with interpreters written other languages? Does the interpreter still expose a C API?
- ptspts 3y agoPyPy exposes a subset of the CPython C API, which is good enough for many C extensions, and some others can be ported with little effort. Most other Python implementations don't provide any way to use CPython C extensions. There is also the HPy API and ABI, which some C extensions target and multiple Python implementations can compile or load.
- galangalalgol 3y agoThere is a merge request up to add autogen rust bindings to hpy https://github.com/hpyproject/hpy/pull/457 https://github.com/hpyproject/hpy/pull/457
- wokwokwok 3y agoNo. …and this one is no exception -> https://github.com/RustPython/RustPython/issues/1940 https://github.com/RustPython/RustPython/issues/1940 Packages that rely on c dependencies like numpy, etc. only work if you write a custom implementation by hand; the “normal” package flat out doesn’t (and cannot) work. So basically you get no packages that use native extensions, unless the project explicitly implements support for them. Pypy is the only implementation I’m aware of that has implemented a c api that is mostly compatible (see https://doc.pypy.org/en/latest/faq.html#do-c-extension-modules-work-with-pypy https://doc.pypy.org/en/latest/faq.html#do-c-extension-modul...) For eg. Python running in a .net host or a Java host… I think this kind of compatibility would just be flat out impossible. For rust… hm… probably possible. For wasm? Definitely not.
- nickpsecurity 3y agoWould there be a way to write a bridge between the Rust interpreters and C API where calls to existing extension API could work to/from the Rust interpreter?
- UncleEntity 3y agoIIRC, PyPy does this and the translation between the native and C API is the main reason it is slow.