Commit e2aa8aec authored by Robert Bradshaw's avatar Robert Bradshaw

Support multiple custom string conversions.

parent c67e8954
...@@ -3057,6 +3057,7 @@ class CppClassType(CType): ...@@ -3057,6 +3057,7 @@ class CppClassType(CType):
X[ix], X[ix], T.from_py_function, except_clause)) X[ix], X[ix], T.from_py_function, except_clause))
if self.cname in cpp_string_conversions: if self.cname in cpp_string_conversions:
cls = 'string' cls = 'string'
tags = self.cname.replace(':', '_'),
else: else:
cls = self.cname[5:] cls = self.cname[5:]
cname = '__pyx_convert_%s_from_py_%s' % (cls, '____'.join(tags)) cname = '__pyx_convert_%s_from_py_%s' % (cls, '____'.join(tags))
...@@ -3090,6 +3091,7 @@ class CppClassType(CType): ...@@ -3090,6 +3091,7 @@ class CppClassType(CType):
X[ix], T.to_py_function, X[ix])) X[ix], T.to_py_function, X[ix]))
if self.cname in cpp_string_conversions: if self.cname in cpp_string_conversions:
cls = 'string' cls = 'string'
tags = self.cname.replace(':', '_'),
else: else:
cls = self.cname[5:] cls = self.cname[5:]
cname = "__pyx_convert_%s_to_py_%s" % (cls, "____".join(tags)) cname = "__pyx_convert_%s_to_py_%s" % (cls, "____".join(tags))
......
...@@ -7,7 +7,7 @@ PYTHON -c "import a; a.test_convert()" ...@@ -7,7 +7,7 @@ PYTHON -c "import a; a.test_convert()"
from Cython.Build.Dependencies import cythonize from Cython.Build.Dependencies import cythonize
from Cython.Compiler import PyrexTypes from Cython.Compiler import PyrexTypes
PyrexTypes.cpp_string_conversions += ("MyString",) PyrexTypes.cpp_string_conversions += ("MyString", "MyString2")
from distutils.core import setup from distutils.core import setup
...@@ -29,6 +29,12 @@ class MyString { ...@@ -29,6 +29,12 @@ class MyString {
std::string value_; std::string value_;
}; };
class MyString2 : public MyString {
public:
MyString2() : MyString() { }
MyString2(const char* data, size_t size) : MyString(data, size) { }
};
######## a.pyx ######## ######## a.pyx ########
# distutils: language = c++ # distutils: language = c++
...@@ -37,9 +43,19 @@ cdef extern from "my_string.cpp": ...@@ -37,9 +43,19 @@ cdef extern from "my_string.cpp":
cdef cppclass MyString: cdef cppclass MyString:
pass pass
cdef extern from "my_string.cpp":
cdef cppclass MyString2:
pass
def do_convert(MyString value): def do_convert(MyString value):
return value return value
def do_convert2(MyString2 value):
return value
def test_convert(): def test_convert():
assert do_convert(b"abc") == b"abc" assert do_convert(b"abc") == b"abc"
assert do_convert(b"ab\0c") == b"ab\0c" 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"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment