Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python+Numpy modules in free pascal

Developing a module (.pyd) for Python in free pascal is fairly easy, see Developing Python Modules with Pascal. But if I want to interface with numpy, this is not that easy. When using C to interface with numpy, you have to add #include <numpy/arrayobject.h> to the code, and also call import_array(); in the initialization function.

Is there a way to interface with numpy in pascal?

EDIT1

As mentioned in the comments under @wilberforce answer, the import_array function which is defined in the header files just imports multiarray.pyd module into the current interpreter and does some checking. It is easily translated into pascal and it works.

The numpy C-API functions are not present initially in pythonXX.dll, so they can't be linked statically. Static or dynamic linking with multiarray.pyd is not working for me.

So the updated question is: Is there a way to access the C-API functions embedded in multiarray.pyd from code which is not C?

like image 505
hdrz Avatar asked Nov 11 '22 18:11

hdrz


1 Answers

Treat the numpy library like any other C library from Pascal's point of view - you need to include the header and declare import_array as an external cdecl function.

This guide covers the details.

You already need to have done some of this in order to have written a Python extension module so your Pascal code can use the Python API functions to interact with Python objects. You can see this in the cdecl; external PythonLib; modifiers in the example you link to. It's possible this wasn't clear while you were doing it.

like image 150
babbageclunk Avatar answered Nov 15 '22 06:11

babbageclunk