Skip to content

Commit 3e908d3

Browse files
committed
new examples`
1 parent e9b8a91 commit 3e908d3

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+

examples/extensions/ctypes/simple/test.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)