← 输入框|github.com|刷新
Skip to content

Callback crashes in emulation mode #2208

Closed

Description

@emoise-gsi

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

Activity

  1. ubaidsk commented on Jul 24, 2023

    @ubaidsk
    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)
  2. Smit-create commented on Jul 24, 2023

    @Smit-create
    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):
  3. self-assigned this
    on Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions