Failed to call C function with "char*" argument.

Hi,

using the last numba version (0.55.1), i’m unable to call a c func with char* argument.

from numba import njit
from cffi import FFI

ffi = FFI()

ffi.cdef("int strlen(char*);")
lib = ffi.dlopen(None)
strlen = lib.strlen

print("cffi ", strlen(b"cffi"))

@njit
def n():
    print("numb", strlen(b"numba"))

n()

I got the following output:

cffi  4
Traceback (most recent call last):
  File "test__numba_str.py", line 16, in <module>
    n()
  File "site-packages/numba/core/dispatcher.py", line 415, in _compile_for_args
    error_rewrite(e, 'typing')
  File "site-packages/numba/core/dispatcher.py", line 358, in error_rewrite
    reraise(type(e), e, None)
  File "site-packages/numba/core/utils.py", line 80, in reraise
    raise value.with_traceback(tb)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function ExternalFunctionPointer((int8*,) -> int32) found for signature:
 
 >>> ExternalFunctionPointer(readonly bytes(uint8, 1d, C))
 
There are 2 candidate implementations:
  - Of which 2 did not match due to:
  Type Restricted Function in function 'unknown': File: unknown: Line unknown.
    With argument(s): '(readonly bytes(uint8, 1d, C))':
   No match for registered cases:
    * (int8*,) -> int32

During: resolving callee type: ExternalFunctionPointer((int8*,) -> int32)
During: typing of call at test__numba_str.py (14)


File "test__numba_str.py", line 14:
def n():
    print("numb", strlen(b"numba"))

Any help is welcome :wink:

Cross-reference to original Github Issue: Failed to call C function with "char*" argument. · Issue #8113 · numba/numba · GitHub

I had to call a C function with a char* argument, hacked together something like this that seemed to work.

@ims_jit
def get_char_ptr(string: str):
    res = np.zeros(len(string) + 1, dtype=np.uint8)
    for i, char in enumerate(string):
        res[i] = ord(char)
    return res, cast_int_to_int8_ptr(res.ctypes.data)