-
Notifications
You must be signed in to change notification settings - Fork 163
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
Callback crashes in emulation mode #2208
Comments
Sharing the contents of the zip files for easy reference.
no_bug $ ls
emulation.py main.py some_func.py no_bug $ cat main.py
import lpython
from types import FunctionType
import emulation
lpython.CTypes.emulations = {k: v for k, v in emulation.__dict__.items()
if isinstance(v, FunctionType)}
from some_func import entry_point
entry_point() no_bug $ cat emulation.py
from lpython import i32
def bar(arg : i32) -> i32:
print(arg) no_bug $ cat some_func.py
from lpython import ccallback, i32, ccall
@ccall
def bar(arg : i32) -> i32:
pass
@ccallback
def entry_point() -> None:
bar(3)
callback_bug $ ls
__pycache__ emulation.py main.py the_callback.py callback_bug $ cat main.py
import lpython
from types import FunctionType
import emulation
lpython.CTypes.emulations = {k: v for k, v in emulation.__dict__.items()
if isinstance(v, FunctionType)}
from the_callback import entry_point
entry_point() callback_bug $ cat emulation.py
from lpython import i32, Callable
def bar(func : Callable[[i32], i32], arg : i32) -> i32:
return func(arg) callback_bug $ cat the_callback.py
from lpython import ccallback, i32, Callable, ccall
def foo(x : i32) -> i32:
print(x)
return x
@ccall
def bar(func : Callable[[i32], i32], arg : i32) -> i32:
pass
@ccallback
def entry_point() -> None:
bar(foo, 3) |
The following diff should fix that: diff --git a/src/runtime/lpython/lpython.py b/src/runtime/lpython/lpython.py
index 4ec0faad4..eb48a8767 100644
--- a/src/runtime/lpython/lpython.py
+++ b/src/runtime/lpython/lpython.py
@@ -286,6 +286,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): |
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
callback_bug.zip
no_bug.zip
lpython version 0.18.4
Both callback_bug and no_bug (attached) contain three similar files:
We can run both programs in emulation mode, taking care to update the PYTHONPATH with the location of lpython.py:
$ PYTHONPATH=/home/emoise/lpython/src/runtime/lpython/ python main.py
no_bug works just fine:
3
the_callback fails:
Traceback (most recent call last):
File "/home/emoise/lpython/callback_bug/main.py", line 8, in
from the_callback import entry_point
File "/home/emoise/lpython/callback_bug/the_callback.py", line 8, in
def bar(func : Callable[[i32], i32], arg : i32) -> i32:
File "/home/emoise/lpython/src/runtime/lpython/lpython.py", line 471, in ccall
return CTypes(f)
File "/home/emoise/lpython/src/runtime/lpython/lpython.py", line 371, in init
arg_ctype = convert_type_to_ctype(arg_type)
File "/home/emoise/lpython/src/runtime/lpython/lpython.py", line 294, in convert_type_to_ctype
type = convert_type_to_ctype(arg._type)
File "/home/emoise/lpython/src/runtime/lpython/lpython.py", line 298, in convert_type_to_ctype
elif issubclass(arg, Enum):
TypeError: issubclass() arg 1 must be a class
The text was updated successfully, but these errors were encountered: