Skip to content

Commit 1ec6b21

Browse files
authored
Merge pull request #2126 from certik/bindc_fixes
Add a test for a struct from a C pointer
2 parents f2500fa + 7701d26 commit 1ec6b21

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ RUN(NAME bindc_04 LABELS llvm c NOFAST)
374374
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)
377+
RUN(NAME bindc_10 LABELS cpython llvm c NOFAST)
378+
RUN(NAME bindc_11 LABELS cpython) # This is CPython test only
377379
RUN(NAME exit_01 LABELS cpython llvm c NOFAST)
378380
RUN(NAME exit_02 FAIL LABELS cpython llvm c NOFAST)
379381
RUN(NAME exit_03 LABELS cpython llvm c wasm wasm_x86 wasm_x64)

integration_tests/bindc_10.py

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

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)