Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import cython function to cython script

Let us have script foo.pyx with function in it:

def hello():
    cdef int* i = <int *> malloc(sizeof(int))
    i[0] = 1
    trol(i)
    print i

and script with function noo.pyx:

cdef trol(int * i):
    i[0] = 42

the question is, how do I now import the trol function from file noo.pyx to foo.pyx, so I can use it in hello function.

This is only model example, but I think, that it illustrate the problem fair enough.

I tried simple

from noo import trol

but that throws "Cannot convert 'int *' to Python object"

Edit: I would better add, that this example will work just fine if I put both functions to same file.

like image 526
Jendas Avatar asked Jan 27 '26 11:01

Jendas


1 Answers

This seems like something obvious to try, but did you try:

from noo cimport trol

If you use import instead of cimport, I think it will try to cast trol as a python function and generate the error you're getting.

like image 188
Paul R Avatar answered Jan 30 '26 02:01

Paul R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!