我在Numba的documentation中看到支持str类型的某些操作。我用@jit装饰器测试了它,它当然可以工作:
In [14]: @numba.jit("boolean(unicode_type, unicode_type)")
...: def compare_str(a, b):
...: return a == b
...:
In [15]: compare_str("a","a")
Out[15]: True
我想知道这种类型是否也可用于cfuncs,因为我想拥有一个对字符串进行一些操作的C ++回调,但是尽管它实际上可以编译,但是我无法在Python中成功对其进行测试:>
In [13]: @numba.cfunc("boolean(unicode_type, unicode_type)")
...: def compare_str(a, b):
...: return a == b
...:
In [12]: compare_str.ctypes("a","a")
TypeError Traceback (most recent call last)
<ipython-input-17-63a3266c663d> in <module>
----> 1 compare_str.ctypes("a", "a")
~/.virtualenvs/py3cpp/lib/python3.6/site-packages/numba/utils.py in __get__(self, instance, type)
351 if instance is None:
352 return self
--> 353 res = instance.__dict__[self.name] = self.func(instance)
354 return res
355
~/.virtualenvs/py3cpp/lib/python3.6/site-packages/numba/ccallback.py in ctypes(self)
159 A ctypes function object representing the C callback.
160 """
--> 161 ctypes_args = [to_ctypes(ty) for ty in self._sig.args]
162 ctypes_restype = to_ctypes(self._sig.return_type)
163 functype = ctypes.CFUNCTYPE(ctypes_restype, *ctypes_args)
~/.virtualenvs/py3cpp/lib/python3.6/site-packages/numba/ccallback.py in <listcomp>(.0)
159 A ctypes function object representing the C callback.
160 """
--> 161 ctypes_args = [to_ctypes(ty) for ty in self._sig.args]
162 ctypes_restype = to_ctypes(self._sig.return_type)
163 functype = ctypes.CFUNCTYPE(ctypes_restype, *ctypes_args)
~/.virtualenvs/py3cpp/lib/python3.6/site-packages/numba/typing/ctypes_utils.py in to_ctypes(ty)
80 if ctypeobj is None:
81 raise TypeError("Cannot convert Numba type '%s' to ctypes type"
---> 82 % (ty,))
83 return ctypeobj
84
TypeError: Cannot convert Numba type 'unicode_type' to ctypes type
我正在使用numba 0.42.0和Python 3.6.7。任何提示将不胜感激。
谢谢!