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

Function ndpointer

numpy/ctypeslib.py:233–347  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 8

test_dtypeMethod · 0.90
test_ndimMethod · 0.90
test_shapeMethod · 0.90
test_flagsMethod · 0.90
test_cacheMethod · 0.90
test_argumentsMethod · 0.90
test_returnMethod · 0.90

Calls 6

_flags_fromnumFunction · 0.85
_num_fromflagsFunction · 0.85
upperMethod · 0.80
stripMethod · 0.80
splitMethod · 0.45
joinMethod · 0.45

Tested by 8

test_dtypeMethod · 0.72
test_ndimMethod · 0.72
test_shapeMethod · 0.72
test_flagsMethod · 0.72
test_cacheMethod · 0.72
test_argumentsMethod · 0.72
test_returnMethod · 0.72