File tree Expand file tree Collapse file tree 3 files changed +47
-14
lines changed
examples/extensions/ctypes/simple Expand file tree Collapse file tree 3 files changed +47
-14
lines changed Original file line number Diff line number Diff line change 1+ # see https://docs.scipy.org/doc/numpy/reference/routines.ctypeslib.html
2+
3+ import numpy as np
4+ import numpy .ctypeslib as npc
5+ import ctypes as C
6+
7+ # load our extension -- this returns a ctypes library object
8+ cfunc = npc .load_library ('cfunc' , '.' )
9+
10+ # set the return type
11+ cfunc .my_subroutine .restype = C .c_int
12+
13+ # set the argument types
14+ cfunc .my_subroutine .argtypes = [npc .ndpointer (C .c_double , flags = "C_CONTIGUOUS" ),
15+ C .c_int ]
16+
17+ def my_subroutine (A ):
18+ return cfunc .my_subroutine (A , len (A ))
19+
20+ A = np .arange (10 , dtype = np .float64 )
21+ n = my_subroutine (A )
22+
23+
Original file line number Diff line number Diff line change 1+ # this version of the driver uses the normal ctypes POINTER(), so you
2+ # need to have your numpy array make itself appear as a C pointer
3+ # explicitly.
4+
5+ import numpy as np
6+ import ctypes as C
7+
8+ # load our extension -- this returns a ctypes library object
9+ cfunc = np .ctypeslib .load_library ('cfunc' , '.' )
10+
11+ # set the return type
12+ cfunc .my_subroutine .restype = C .c_int
13+
14+ # set the argument types
15+ cfunc .my_subroutine .argtypes = [C .POINTER (C .c_double ),
16+ C .c_int ]
17+
18+ def my_subroutine (A ):
19+ return cfunc .my_subroutine (A .ctypes .data_as (C .POINTER (C .c_double )), len (A ))
20+
21+ A = np .arange (10 , dtype = np .float64 )
22+ n = my_subroutine (A )
23+
24+
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments