unicode_type/string 类型是否适用于 Numba 的 cfunc?
Does unicode_type/string type work for Numba's cfunc?
我在 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。任何提示将不胜感激。
谢谢!
中回答
我在 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。任何提示将不胜感激。
谢谢!