Passing A Single Integer By Reference To Cython?
Solution 1:
As the error says: you're passing a pointer where it expects the plain type (C++ don't really behave like pointers - they can't be dereferenced etc.). I think the following should work:
defmyTest(int[:] val):
testFn(val[0])
Your main
function will also have issues since list is not a buffer type (so cannot be used with a memoryview). Fix that by converting it to a numpy array or Python array
def main():
val = np.array([0],dtype=np.int) # or array.arraymyTest(val)
print(val)
Solution 2:
&int
(needed for testFn
) and int *
(your &val[0]
) are not quite the same, that explains the compiler error.
Even if one could change a python-integer in-place, one should not do it. A possible solution is to pass a python-integer to the wrapper-function and return the result of the wrapped function as a new python-integer:
def myTest(int val):
testFn(val)
returnval
Behind the scenes cython casts python-integer to a c-style integer and after the call of myTest
back to a (new) python-integer. However, val
is an independent (local) variable and not a reference to the original python-integer, so by changing it we don't change the original:
>>>a=6>>>b=myTest(a)
val: 6
>>>a,b
(6,-1)
Post a Comment for "Passing A Single Integer By Reference To Cython?"