Array-checking restype/argtypes. An ndpointer instance is used to describe an ndarray in restypes and argtypes specifications. This approach is more flexible than using, for example, ``POINTER(c_double)``, since several restrictions can be specified, which are verified upon ca
(dtype=None, ndim=None, shape=None, flags=None)
| 229 | # use with ctypes argtypes mechanism |
| 230 | _pointer_type_cache = {} |
| 231 | def ndpointer(dtype=None, ndim=None, shape=None, flags=None): |
| 232 | """ |
| 233 | Array-checking restype/argtypes. |
| 234 | |
| 235 | An ndpointer instance is used to describe an ndarray in restypes |
| 236 | and argtypes specifications. This approach is more flexible than |
| 237 | using, for example, ``POINTER(c_double)``, since several restrictions |
| 238 | can be specified, which are verified upon calling the ctypes function. |
| 239 | These include data type, number of dimensions, shape and flags. If a |
| 240 | given array does not satisfy the specified restrictions, |
| 241 | a ``TypeError`` is raised. |
| 242 | |
| 243 | Parameters |
| 244 | ---------- |
| 245 | dtype : data-type, optional |
| 246 | Array data-type. |
| 247 | ndim : int, optional |
| 248 | Number of array dimensions. |
| 249 | shape : tuple of ints, optional |
| 250 | Array shape. |
| 251 | flags : str or tuple of str |
| 252 | Array flags; may be one or more of: |
| 253 | |
| 254 | - C_CONTIGUOUS / C / CONTIGUOUS |
| 255 | - F_CONTIGUOUS / F / FORTRAN |
| 256 | - OWNDATA / O |
| 257 | - WRITEABLE / W |
| 258 | - ALIGNED / A |
| 259 | - WRITEBACKIFCOPY / X |
| 260 | - UPDATEIFCOPY / U |
| 261 | |
| 262 | Returns |
| 263 | ------- |
| 264 | klass : ndpointer type object |
| 265 | A type object, which is an ``_ndtpr`` instance containing |
| 266 | dtype, ndim, shape and flags information. |
| 267 | |
| 268 | Raises |
| 269 | ------ |
| 270 | TypeError |
| 271 | If a given array does not satisfy the specified restrictions. |
| 272 | |
| 273 | Examples |
| 274 | -------- |
| 275 | >>> clib.somefunc.argtypes = [np.ctypeslib.ndpointer(dtype=np.float64, |
| 276 | ... ndim=1, |
| 277 | ... flags='C_CONTIGUOUS')] |
| 278 | ... #doctest: +SKIP |
| 279 | >>> clib.somefunc(np.array([1, 2, 3], dtype=np.float64)) |
| 280 | ... #doctest: +SKIP |
| 281 | |
| 282 | """ |
| 283 | |
| 284 | # normalize dtype to an Optional[dtype] |
| 285 | if dtype is not None: |
| 286 | dtype = _dtype(dtype) |
| 287 | |
| 288 | # normalize flags to an Optional[int] |
nothing calls this directly
no test coverage detected