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 different from the cqueue.pxd file with declarations from the C library, as both do not describe the same code. A .pxd file next to a .pyx file with the same name defines exported declarations for code in the .pyx file. As the cqueue.pxd file contains declarations of a regular C library, there must not be a .pyx file with the same name that Cython associates with it.
Yet I ran into a situation when it works properly only when the same name is given to those two files even though .pxd
is cdef extern
cpp declaration unrelated to .pyx
code.
py_test.pyx:
# distutils: language = c++
from py_test cimport Test
def f():
Test[double](2.) + 3.
zzz.pyx:
# distutils: language = c++
from py_test cimport Test
def f():
Test[double](2.) + 3.
py_test.pxd:
cdef extern from "cpp_test.h":
cdef cppclass Test[T]:
Test()
Test(T value)
T value
cdef Test[T] operator+[T](Test[T]&, T)
cpp_test.h:
template<typename T>
class Test {
public:
T value;
Test():value(0){}
Test(T value):value(value){}
~Test(){}
};
template<typename T>
Test<T> operator+(const Test<T>& x, T y) {
return Test<T>(x.value + y);
}
setup.py:
from distutils.core import setup
from Cython.Build import cythonize
setup(
name = "demo", # unused
# ext_modules = cythonize('py_test.pyx'), # ok
ext_modules = cythonize('zzz.pyx'), # Invalid operand types for '+' (Test[double]; double)
)