1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
# mode: run
# tag: cpp, werror
PYTHON setup.py build_ext --inplace
PYTHON -c "import a; a.test_convert()"
######## setup.py ########
from Cython.Build.Dependencies import cythonize
from Cython.Compiler import PyrexTypes
PyrexTypes.cpp_string_conversions += ("MyString", "MyString2")
from distutils.core import setup
setup(
ext_modules = cythonize("*.pyx"),
)
######## my_string.cpp ########
#include <string>
class MyString {
public:
MyString() { }
MyString(const char* data, size_t size) : value_(data, size) { }
const char* data() const { return value_.data(); }
const size_t size() const { return value_.size(); }
private:
std::string value_;
};
class MyString2 : public MyString {
public:
MyString2() : MyString() { }
MyString2(const char* data, size_t size) : MyString(data, size) { }
};
typedef MyString MyTypedefString;
######## a.pyx ########
# distutils: language = c++
cdef extern from "my_string.cpp":
cdef cppclass MyString:
pass
cdef cppclass MyString2:
pass
ctypedef MyString2 MyTypedefString # really a MyString
def do_convert(MyString value):
return value
def do_convert2(MyString2 value):
return value
def do_convert_typedef(MyTypedefString value):
return value
def test_convert():
assert do_convert(b"abc") == b"abc"
assert do_convert(b"ab\0c") == b"ab\0c"
assert do_convert2(b"abc") == b"abc"
assert do_convert2(b"ab\0c") == b"ab\0c"
assert do_convert_typedef(b"abc") == b"abc"
assert do_convert_typedef(b"ab\0c") == b"ab\0c"
|