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
How to call a cython function from within @njit compiled code? #5403
Comments
Thanks for the report. Just wondering what it is you want to achieve? If If you are new to Numba, perhaps take a look at the 5 minute guide first and/or click one of the |
@stuartarchibald Yes, it is already a compiled cython function, but the code that's calling it is not, and that's the code I want to JIT! |
@tsoernes you have a couple of options.
|
@stuartarchibald A great oversight of mine, but the function is a (wrapper to a?) compiled C function (https://github.com/ztane/python-Levenshtein) and not a Cython function.
|
@tsoernes ok, then you can perhaps bind using |
@stuartarchibald Okay. The function returns a Python list of tuples so I guess I'm out of luck since I cannot build a supported ctype. |
@tsoernes ok, how about instead using an |
@stuartarchibald Does it work when the return type is a list of tuples? I've tried this, without knowing quite how to annotate the return type.
|
@tsoernes what is being returned, something like |
Here's an example of creating a tuple in an from numba import njit, objmode
import numpy as np
@njit
def foo():
with objmode(ret = 'Tuple((unicode_type, int64, int64))'):
ret = ('foo', 1, 2)
return ret + ('bar',)
print(foo()) For lists, I'd recommend avoiding reflected-lists, and use typed listed instead, example: from numba import njit, objmode
from numba.typed import List
import numpy as np
@njit
def foo():
with objmode(l = 'ListType(Tuple((unicode_type, int64, int64)))'):
ret = ('foo', 1, 2)
l = List()
l.append(ret)
return l
print(foo()) docs on why: http://numba.pydata.org/numba-doc/latest/reference/pysupported.html#list |
My mistake. The return type is always a List of tuples with 3 Ints. Still no luck with the above example:
|
Perhaps try this: from Levenshtein import matching_blocks, editops
from numba import njit, objmode
import numpy as np
from numba import typeof, cfunc
import numpy as np
from numba.typed import List
str_ty = typeof('x')
block_ty = typeof([('x', 1, 2)])
@njit
def matching_blocks2(edit_operations, source_length, destination_length):
with objmode(mb='ListType(Tuple((int64, int64, int64)))'):
l_edit_operations = [_ for _ in edit_operations]
x = matching_blocks(l_edit_operations, source_length,
destination_length)
mb = List()
mb.extend(x)
return mb
a, b = 'spam', 'park'
print(matching_blocks2(tuple(editops(a, b)), a, b)) the |
|
Is the code above not working for you? |
It works! I had a type in the code. Thanks for the help! |
No problem, thanks for using Numba. |
where
matching_blocks
is a Cython function, yieldsIs there any way to work around that? The
__text_signature__
field is not writeable and providing a signature of the function tonjit
does not help.The text was updated successfully, but these errors were encountered: