This is a pre-release version (latest). Go to latest (2.4.4)
{ } Raw JSON

bundles / numpy latest / numpy / ctypeslib / _ctypeslib

module

numpy.ctypeslib._ctypeslib

source: build-install/usr/lib/python3.14/site-packages/numpy/ctypeslib/_ctypeslib.py :0

Members

Additional content

ctypes Utility Functions

See Also

load_libraryLoad a C library. ndpointer : Array restype/argtype with verification. as_ctypes : Create a ctypes array from an ndarray. as_array : Create an ndarray from a ctypes array.

References

[1]

"SciPy Cookbook: ctypes", https://scipy-cookbook.readthedocs.io/items/Ctypes.html

Examples

Load the C library:

>>> _lib = np.ctypeslib.load_library('libmystuff', '.')     #doctest: +SKIP

Our result type, an ndarray that must be of type double, be 1-dimensional and is C-contiguous in memory:

>>> array_1d_double = np.ctypeslib.ndpointer(
...                          dtype=np.double,
...                          ndim=1, flags='CONTIGUOUS')    #doctest: +SKIP

Our C-function typically takes an array and updates its values in-place. For example

void foo_func(double* x, int length)
{
    int i;
    for (i = 0; i < length; i++) {
        x[i] = i*i;
    }
}

We wrap it using:

>>> _lib.foo_func.restype = None                      #doctest: +SKIP
>>> _lib.foo_func.argtypes = [array_1d_double, c_int] #doctest: +SKIP

Then, we're ready to call foo_func:

>>> out = np.empty(15, dtype=np.double)
>>> _lib.foo_func(out, len(out))                #doctest: +SKIP

See also

as_array

Create an ndarray from a ctypes array.

as_ctypes

Create a ctypes array from an ndarray.

load_library

Load a C library.

ndpointer

Array restype/argtype with verification.

Aliases

  • numpy.ctypeslib._ctypeslib