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

Callback crashes in emulation mode #2208

Closed
emoise-gsi opened this issue Jul 24, 2023 · 2 comments · Fixed by #2209
Closed

Callback crashes in emulation mode #2208

emoise-gsi opened this issue Jul 24, 2023 · 2 comments · Fixed by #2209
Assignees

Comments

@emoise-gsi
Copy link

callback_bug.zip
no_bug.zip

lpython version 0.18.4

Both callback_bug and no_bug (attached) contain three similar files:

  • main.py, which drives the program
  • emulation.py which contains the emulation implementation
  • the_callback.py / some_func.py

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

@Shaikh-Ubaid
Copy link
Collaborator

Sharing the contents of the zip files for easy reference.

no_bug.zip contents:

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.zip contents:

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)

@Smit-create
Copy link
Collaborator

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):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants