Skip to content

Commit 8745d6d

Browse files
authored
Merge pull request #603 from certik/cp2
Implement cpython support in test_c_interop_03
2 parents 64828c9 + 97d0aeb commit 8745d6d

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ RUN(NAME test_math_02 LABELS cpython llvm)
154154
RUN(NAME test_c_interop_01 LABELS cpython llvm c)
155155
RUN(NAME test_c_interop_02 LABELS cpython llvm c
156156
EXTRAFILES test_c_interop_02b.c)
157-
RUN(NAME test_c_interop_03 LABELS llvm c
157+
RUN(NAME test_c_interop_03 LABELS cpython llvm c
158158
EXTRAFILES test_c_interop_03b.c)
159159
RUN(NAME test_c_interop_04 LABELS cpython llvm c
160160
EXTRAFILES test_c_interop_04b.c)

integration_tests/test_c_interop_03.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from ltypes import ccall, f32, f64, i32, i64, CPtr, pointer, Pointer, p_c_pointer
1+
from ltypes import (ccall, f32, f64, i32, i64, CPtr, pointer, Pointer,
2+
p_c_pointer, empty_c_void_p)
23

34
@ccall
45
def f_pi32_i32(x: CPtr) -> i32:
@@ -20,26 +21,27 @@ def test_c_callbacks():
2021
xi32: i32
2122
xi32 = 3
2223
p: CPtr
23-
p_c_pointer(pointer(xi32), p)
24-
print(pointer(xi32), p)
24+
p = empty_c_void_p()
25+
p_c_pointer(pointer(xi32, i32), p)
26+
print(pointer(xi32, i32), p)
2527
assert f_pi32_i32(p) == 4
2628

2729
xi64: i64
2830
xi64 = 3
29-
p_c_pointer(pointer(xi64), p)
30-
print(pointer(xi64), p)
31+
p_c_pointer(pointer(xi64, i64), p)
32+
print(pointer(xi64, i64), p)
3133
assert f_pi64_i32(p) == 4
3234

3335
xf32: f32
3436
xf32 = 3.3
35-
p_c_pointer(pointer(xf32), p)
36-
print(pointer(xf32), p)
37+
p_c_pointer(pointer(xf32, f32), p)
38+
print(pointer(xf32, f32), p)
3739
assert abs(f_pf32_i32(p)-4.3) < 1e-6
3840

3941
xf64: f64
4042
xf64 = 3.3
41-
p_c_pointer(pointer(xf64), p)
42-
print(pointer(xf64), p)
43+
p_c_pointer(pointer(xf64, f64), p)
44+
print(pointer(xf64, f64), p)
4345
assert abs(f_pf64_i32(p)-4.3) < 1e-12
4446

4547
test_c_callbacks()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,11 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
12611261
ASR::symbol_t *s = current_scope->resolve_symbol(name);
12621262
if (s) {
12631263
tmp = ASR::make_Var_t(al, x.base.base.loc, s);
1264+
} else if (name == "i32" || name == "i64" || name == "f32" || name == "f64") {
1265+
int64_t i = -1;
1266+
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc,
1267+
4, nullptr, 0));
1268+
tmp = ASR::make_IntegerConstant_t(al, x.base.base.loc, i, type);
12641269
} else {
12651270
throw SemanticError("Variable '" + name + "' not declared",
12661271
x.base.base.loc);

src/runtime/ltypes/ltypes.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,26 @@ def ccall(f):
177177
wrapped_f = CTypes(f)
178178
return wrapped_f
179179

180-
def pointer(x):
180+
def pointer(x, type=None):
181181
from numpy import ndarray
182182
if isinstance(x, ndarray):
183183
return ctypes.c_void_p(x.ctypes.data)
184184
#return x.ctypes.data_as(ctypes.POINTER(ctypes.c_int32))
185185
else:
186-
type_ = x.annotations[0]
187-
if type_ == i32:
188-
return ctypes.pointer(ctypes.c_int32(x))
186+
if type == i32:
187+
#return ctypes.c_void_p(ctypes.pointer(ctypes.c_int32(x)))
188+
#return ctypes.pointer(ctypes.c_int32(x))
189+
return ctypes.cast(ctypes.pointer(ctypes.c_int32(x)),
190+
ctypes.c_void_p)
191+
elif type == i64:
192+
return ctypes.cast(ctypes.pointer(ctypes.c_int64(x)),
193+
ctypes.c_void_p)
194+
elif type == f32:
195+
return ctypes.cast(ctypes.pointer(ctypes.c_float(x)),
196+
ctypes.c_void_p)
197+
elif type == f64:
198+
return ctypes.cast(ctypes.pointer(ctypes.c_double(x)),
199+
ctypes.c_void_p)
189200
else:
190201
raise Exception("Type not supported in pointer()")
191202

0 commit comments

Comments
 (0)