私はCython(0.15.2)を使ってPython(2.6.5)の拡張機能を作成しています。私はpxdファイルとpyxファイルを作成しました。ここに私のPYXファイルの内容は以下のとおりです。フリー関数のCythonコンパイルエラー(Pythonオブジェクトの引数を 'FooBar *'型に変換できません)
cimport capifuncs
cdef class myArray:
cdef capifuncs.myArray *_my_array
def __cinit__(self, size):
self._my_array = capifuncs.array_new(size)
if (self._my_array is NULL):
raise MemoryError()
def __dealloc__(self):
if self._my_array is not NULL:
capifuncs.array_free(self._my_array)
def __bool__(self):
return not capifuncs.IsEmpty(self._my_array)
##############################
# Array methods #
##############################
cpdef getItem(self, unsigned int pos):
if capifuncs.IsEmpty(self._my_array):
raise IndexError("Array is empty")
#if():
# raise IndexError("Array bounds exceeded")
return capifuncs.array_get_item(self._my_array, pos)
cpdef setItem(self, unsigned int pos, double val):
if capifuncs.IsEmpty(self._my_array):
raise IndexError("Array is empty")
#if():
# raise IndexError("Array bounds exceeded")
capifuncs.array_set_item(self._my_array, pos, val)
# Free functions
cpdef long someCAPIFuncCall(capifuncs.FooBar *fb, capifuncs.myArray *arr, long start, long stop):
return capifuncs.doSomethingInteresting(fb, arr, start, stop)
私は自由(すなわち、非メンバー)関数定義文をコメントアウトした場合、コードが正しくコンパイルおよび拡張が生成されます。しかし、私はそれをアンコメントしてファイルをコンパイルしようとした場合、私は次のようなエラーメッセージが出ます:
cafuncs.pyx:64:23: Cannot convert Python object argument to type 'FooBar *'
を何本の原因である、と私はそれをどのように修正するのですか? cpdef
として定義
はい、私はPythonからC関数を呼び出せるようにしたい - 拡張を構築する理由は、PythonのCライブラリのデータ型と関数を使用できるようにするためです。 PythonからC関数を呼び出すことができます(Pythonから) –
私はcpdefの代わりにcdefを使用するとコードがコンパイルされますが、次の警告メッセージが表示されます:警告: '__pyx_f_6clib_doSomethingInteresting'使用されていない。 Unsuprisingly、私はPythonから関数を使用しようとすると、私はエラーAttributeErrorを取得します: 'モジュール'オブジェクトに属性がありません 'doSomethingInteresting' –
@ HomunculusReticulli。私の更新された答えを見てください。基本的には、Cの型のラッパーを作成する必要があります。 – ekhumoro