Skip to content Skip to sidebar Skip to footer

Cython: Counter-tutorial Behavior

Tutorial says .pyx and .pxd files should not have the same name unless .pyx is the realization of the definitions from .pxd file. Note that the name of the .pyx file must be diffe

Solution 1:

The reason the tutorial says that the .pyx and .pxd files should not have the same name is that a .pyx file will automatically do the equivalent of from pxd_file_with_the_same_name cimport * if it finds a file with the same name.

So the reason to avoid having unrelated files with the same name is just to avoid confusing behaviour where something gets cimported that you don't expect.


This specific case has to do with how Cython imports non-member C++ operators which is frankly a bit buggy. See this previous question.

The non-member operator will only be available to Cython if it's cimported into the current module scope (i.e. just importing the associated class won't do it). Unfortunately there's no way to cimport it directly that I know of, but if you change zzz.pyx to start with

from py_test cimport *

then the *does managed to cimport the operator and it should work. Because py_test.pyx does this implicitly then it works without further changes.


(With respect to your previous question about complex which motivated this I tried messing around with this kind of idea and couldn't immediately get it to work, but perhaps you'll have more luck than me)

Solution 2:

Another way of cimporting operator+ is like this:

zzz.pyx

# distutils: language = c++from py_test cimport Test, add

def f():
    add(Test[double](2.), 3.)

pytest.pxd:

cdef extern from "cpp_test.h": 
    cdef cppclass Test[T]:
        Test()
        Test(T value)T value
    cdef Test[T] add "operator+"[T](Test[T]&,T)

In certain cases it might be preferable over cimport *, yet it is not ideal as it alters the semantics of the operator invocation.

Post a Comment for "Cython: Counter-tutorial Behavior"