2

I have done this function that uses ctypes to create an object with a buffer protocol that points to a specified address:

import numpy as np

def np_buffer_from_address(shape, dtype, address):
    import ctypes
    return np.ndarray(shape, dtype = dtype, buffer = ctypes.c_void_p.from_address(address))

But I wanted to know if I can do this without using ctypes. If you can do it directly with numpy

Mano-Wii
  • 592
  • 5
  • 19
  • What's the source of the `address`? In the link I show that you can use the `.data` attribute of another array. https://stackoverflow.com/a/39377877/901925 – hpaulj Sep 27 '17 at 16:22
  • 1
    `numpy.ctypes` might help; https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ndarray.ctypes.html – hpaulj Sep 27 '17 at 16:32

1 Answers1

2

You can avoid ctypes by using just the standardized __array_interface__, here's an example how it works: Creating a NumPy array directly from __array_interface__ (in fact, numpy.ctypeslib.as_array does the same under the hood, setting typestr and data appropriately)

lomereiter
  • 356
  • 1
  • 5