Python extensions that can be used in all kinds of python (jython / IronPython / etc.)

In the "old days", when there was only cpython, most extensions were written in c (as platform independent as possible) and compiled into pyd (for example, PyCrypto). Now there are Jython, IronPython and PyPy, and pyds do not work with any of them (Ironclad aside). It seems that they all support ctypes and that the best approach MAY be to create a platform independent dll or shared library, and then use ctypes to interact with it.

But I think this approach will be a little slower than the old pyd mode approach. You can also program pyd for cpython, similar to C # dll for IronPython and java class or jar for Jython (I'm not sure about PyPy. But, although this approach will appeal to platform purists, it is very laborious. Best way to take today?

+4
source share
2 answers

Currently, it seems that ctypes is really a better approach. He works today, and he is so comfortable that he will conquer (most of) the world.

For performance-critical APIs (e.g. numpy), ctypes is really problematic. The cleanest approach would probably be to port Cython to create your own IronPython / Jython / PyPy extensions.

I remember that PyPy planned to compile ctypes code for efficient shells, but, as far as I understand, nothing like that yet ...

+2
source

If you wrap an existing native library, ctypes is absolutely the way to go.

If you are trying to speed up hotspots in a Python extension, then creating a custom extension for each interpreter (and backing up in pure Python) is doable, since the bulk of the code is pure Python, which can be used together, but itโ€™s undesirable and time consuming, as you said . You can also use ctypes in this case.

+1
source

Source: https://habr.com/ru/post/1299065/


All Articles