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)
| 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): |