Skip to content

Commit a183feb

Browse files
authored
Merge pull request #2209 from Smit-create/i-2208
Add Callable type
2 parents 3fcf814 + 46dd897 commit a183feb

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,10 @@ RUN(NAME callback_01 LABELS cpython llvm c)
724724
RUN(NAME callback_02 LABELS cpython llvm c)
725725
RUN(NAME callback_03 LABELS cpython llvm c)
726726

727+
728+
# callback_04 is to test emulation. So just run with cpython
729+
RUN(NAME callback_04 IMPORT_PATH .. LABELS cpython)
730+
727731
# Intrinsic Functions
728732
RUN(NAME intrinsics_01 LABELS cpython llvm NOFAST) # any
729733

integration_tests/callback_04.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import lpython
2+
from lpython import i32
3+
from types import FunctionType
4+
import callback_04_module
5+
6+
lpython.CTypes.emulations = {k: v for k, v in callback_04_module.__dict__.items()
7+
if isinstance(v, FunctionType)}
8+
9+
10+
def foo(x : i32) -> i32:
11+
assert x == 3
12+
print(x)
13+
return x
14+
15+
def entry_point() -> None:
16+
callback_04_module.bar(foo, 3)
17+
18+
entry_point()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from lpython import i32, Callable
2+
3+
def bar(func : Callable[[i32], i32], arg : i32) -> i32:
4+
return func(arg)

src/runtime/lpython/lpython.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ def convert_type_to_ctype(arg):
287287
return c_double_complex
288288
elif arg == bool:
289289
return ctypes.c_bool
290+
elif arg == Callable:
291+
return ctypes.PYFUNCTYPE(None)
290292
elif arg is None:
291293
raise NotImplementedError("Type cannot be None")
292294
elif isinstance(arg, Array):

0 commit comments

Comments
 (0)