Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Callable type #2209

Merged
merged 2 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,10 @@ RUN(NAME callback_01 LABELS cpython llvm c)
RUN(NAME callback_02 LABELS cpython llvm c)
RUN(NAME callback_03 LABELS cpython llvm c)


# callback_04 is to test emulation. So just run with cpython
RUN(NAME callback_04 IMPORT_PATH .. LABELS cpython)

# Intrinsic Functions
RUN(NAME intrinsics_01 LABELS cpython llvm NOFAST) # any

Expand Down
18 changes: 18 additions & 0 deletions integration_tests/callback_04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import lpython
from lpython import i32
from types import FunctionType
import callback_04_module

lpython.CTypes.emulations = {k: v for k, v in callback_04_module.__dict__.items()
if isinstance(v, FunctionType)}


def foo(x : i32) -> i32:
assert x == 3
print(x)
return x

def entry_point() -> None:
callback_04_module.bar(foo, 3)

entry_point()
4 changes: 4 additions & 0 deletions integration_tests/callback_04_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from lpython import i32, Callable

def bar(func : Callable[[i32], i32], arg : i32) -> i32:
return func(arg)
2 changes: 2 additions & 0 deletions src/runtime/lpython/lpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ def convert_type_to_ctype(arg):
return c_double_complex
elif arg == bool:
return ctypes.c_bool
elif arg == Callable:
return ctypes.PYFUNCTYPE(None)
elif arg is None:
raise NotImplementedError("Type cannot be None")
elif isinstance(arg, Array):
Expand Down