Skip to content

Commit 662935a

Browse files
committed
Add a CPython test for using NumPy to allocate
1 parent de65afc commit 662935a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ RUN(NAME bindc_07 LABELS cpython llvm c NOFAST)
375375
RUN(NAME bindc_08 LABELS cpython llvm c)
376376
RUN(NAME bindc_09 LABELS cpython llvm c)
377377
RUN(NAME bindc_10 LABELS cpython llvm c)
378+
RUN(NAME bindc_11 LABELS cpython) # This is CPython test only
378379
RUN(NAME exit_01 LABELS cpython llvm c NOFAST)
379380
RUN(NAME exit_02 FAIL LABELS cpython llvm c NOFAST)
380381
RUN(NAME exit_03 LABELS cpython llvm c wasm wasm_x86 wasm_x64)

integration_tests/bindc_11.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import numpy, ctypes
2+
from lpython import (i64, i16, CPtr, c_p_pointer, Pointer, sizeof, packed,
3+
dataclass, ccallable, ccall, i32)
4+
5+
global_arrays = []
6+
7+
8+
def alloc(buf_size:i64) -> CPtr:
9+
xs = numpy.empty(buf_size, dtype=numpy.uint8)
10+
global_arrays.append(xs)
11+
p = ctypes.c_void_p(xs.ctypes.data)
12+
return ctypes.cast(p.value, ctypes.c_void_p)
13+
14+
15+
@ccallable
16+
@packed
17+
@dataclass
18+
class S:
19+
a: i16
20+
b: i64
21+
22+
23+
def main():
24+
p1: CPtr = alloc(sizeof(S))
25+
print(p1)
26+
p2: Pointer[S] = c_p_pointer(p1, S)
27+
p2.a = i16(5)
28+
p2.b = i64(4)
29+
print(p2.a, p2.b)
30+
assert p2.a == i16(5)
31+
assert p2.b == i64(4)
32+
33+
34+
main()

0 commit comments

Comments
 (0)