17

I have written a small cython code that is

#t3.pyx
from libc.stdlib cimport atoi

cdef int fun(char *s):
        return atoi(s)

the setup.py file is

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules=cythonize("t3.pyx"))

I run setup.py using this command

python setup.py build_ext --inplace

This gives me

Compiling t3.pyx because it changed.
Cythonizing t3.pyx
running build_ext
building 't3' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-     prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-  strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c    t3.c -o build/temp.linux-x86_64-2.7/t3.o
t3.c:556:12: warning: ‘__pyx_f_2t3_fun’ defined but not used [-Wunused-function]
 static int __pyx_f_2t3_fun(char *__pyx_v_s) {
        ^
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-Bsymbolic-functions -Wl,-z,relro -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/t3.o -o /home/debesh/Documents/cython/t3/t3.so

When I run in the python interpreter it shows me

>>> import t3
>>> t3.fun('1234')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'fun'
>>> 
cel
  • 30,017
  • 18
  • 97
  • 117
Debesh Mohanty
  • 469
  • 1
  • 5
  • 18
  • 4
    For future readers, you want to use `cpdef` not `cdef` if you want to import the function in a regular Python script – Jon Jun 07 '17 at 21:58
  • The chances are you should use `def` rather than `cpdef`. Either way the code inside the function is compiled for both and runs at the same speed. `cpdef` is _only_ useful in the small number of cases where you want to call a function in a performance critical loop in Cython _and_ you want the same function available in Python. Otherwise it's restrictive. – DavidW Dec 13 '20 at 21:17

1 Answers1

17

The problem here is that you defined your method with cdef instead of def. cdef methods can only be called from cython code.

You can find detailed information in the Python functions vs. C functions section of the docs.

Python functions are defined using the def statement, as in Python. They take Python objects as parameters and return Python objects.

C functions are defined using the new cdef statement. They take either Python objects or C values as parameters, and can return either Python objects or C values.

and the important part:

Within a Cython module, Python functions and C functions can call each other freely, but only Python functions can be called from outside the module by interpreted Python code. So, any functions that you want to “export” from your Cython module must be declared as Python functions using def.

cel
  • 30,017
  • 18
  • 97
  • 117