MCPcopy Create free account
hub / github.com/numpy/numpy / as_array

Function as_array

numpy/ctypeslib.py:506–524  ·  view source on GitHub ↗

Create a numpy array from a ctypes array or POINTER. The numpy array shares the memory with the ctypes object. The shape parameter must be given if converting from a ctypes POINTER. The shape parameter is ignored if converting from a ctypes array

(obj, shape=None)

Source from the content-addressed store, hash-verified

504
505
506 def as_array(obj, shape=None):
507 """
508 Create a numpy array from a ctypes array or POINTER.
509
510 The numpy array shares the memory with the ctypes object.
511
512 The shape parameter must be given if converting from a ctypes POINTER.
513 The shape parameter is ignored if converting from a ctypes array
514 """
515 if isinstance(obj, ctypes._Pointer):
516 # convert pointers to an array of the desired shape
517 if shape is None:
518 raise TypeError(
519 'as_array() requires a shape argument when called on a '
520 'pointer')
521 p_arr_type = ctypes.POINTER(_ctype_ndarray(obj._type_, shape))
522 obj = ctypes.cast(obj, p_arr_type).contents
523
524 return asarray(obj)
525
526
527 def as_ctypes(obj):

Callers 3

test_arrayMethod · 0.90
test_pointerMethod · 0.90

Calls 3

asarrayFunction · 0.90
_ctype_ndarrayFunction · 0.85
castMethod · 0.45

Tested by 3

test_arrayMethod · 0.72
test_pointerMethod · 0.72