1

I want to use numpy C array api (https://numpy.org/doc/stable/reference/c-api/array.html) using ctypes python library. Similarly to how I can use python c api with ctypes. For example, I can reference PyLong_FromLong function like this:

ctypes.pythonapi.PyLong_FromLong

Is this possible to do? Is there some compiled .so file that I can load like this

np_api = ctypes.CDLL(file_path)

to be able to use numpy api?

4
  • @JérômeRichard Not exactly. stackoverflow.com/questions/71195319/… your answer is very close, but it creates memory leakage. I actually want to pass ownership of data to numpy array so that there is no memory leakage. I thought that I can do this if I try to use Numpy C api directly. Commented May 29, 2022 at 16:42
  • Did you read the WARNING note in the comments about freeing the memory? I remember this problem and using free solved it. AFAIK Numpy should not free the buffer because it does not know how it was allocated and there are many allocator. One could even return a pointer to a memory-mapped buffer (possibly owned by a remote process). Thus, AFAIK this is how the API works (which does not mean there is a leak). Commented May 29, 2022 at 16:55
  • First of all one would ask why is this necessary, as from a technical point of view it makes no sense. NumPy .dlls export the extension module API so that this very scenario is avoided. Commented Jun 1, 2022 at 13:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.