Commit 796d16b5 authored by Stefan Behnel's avatar Stefan Behnel

reduce C code overhead for C++ std::string -> PyObject conversion helpers by...

reduce C code overhead for C++ std::string -> PyObject conversion helpers by implementing them directly in C++ and not Cython
parent b127aa60
...@@ -3075,7 +3075,12 @@ class CppClassType(CType): ...@@ -3075,7 +3075,12 @@ class CppClassType(CType):
def create_to_py_utility_code(self, env): def create_to_py_utility_code(self, env):
if self.to_py_function is not None: if self.to_py_function is not None:
return True return True
if self.cname in builtin_cpp_conversions or self.cname in cpp_string_conversions: elif self.cname in cpp_string_conversions:
env.use_utility_code(UtilityCode.load_cached("CppStringToPy", "CppSupport.cpp"))
# "PyObject" prefix gets specialised by explicit type casts in CoerceToPyTypeNode
self.to_py_function = '__Pyx_PyObject_FromStlString'
return True
elif self.cname in builtin_cpp_conversions:
X = "XYZABC" X = "XYZABC"
tags = [] tags = []
declarations = ["cdef extern from *:"] declarations = ["cdef extern from *:"]
...@@ -3089,14 +3094,8 @@ class CppClassType(CType): ...@@ -3089,14 +3094,8 @@ class CppClassType(CType):
declarations.append( declarations.append(
" cdef object %s_to_py '%s' (%s)" % ( " cdef object %s_to_py '%s' (%s)" % (
X[ix], T.to_py_function, X[ix])) X[ix], T.to_py_function, X[ix]))
if self.cname in cpp_string_conversions:
cls = 'string'
prefix = 'PyObject_' # gets specialised by explicit type casts in CoerceToPyTypeNode
tags = self.cname.replace(':', '_'),
else:
cls = self.cname[5:] cls = self.cname[5:]
prefix = '' cname = "__pyx_convert_%s_to_py_%s" % (cls, "____".join(tags))
cname = "__pyx_convert_%s%s_to_py_%s" % (prefix, cls, "____".join(tags))
context = { context = {
'template_type_declarations': '\n'.join(declarations), 'template_type_declarations': '\n'.join(declarations),
'cname': cname, 'cname': cname,
......
...@@ -16,36 +16,6 @@ cdef string {{cname}}(object o) except *: ...@@ -16,36 +16,6 @@ cdef string {{cname}}(object o) except *:
return string(data, length) return string(data, length)
#################### string.to_py ####################
#cimport cython
#from libcpp.string cimport string
cdef extern from *:
cdef cppclass string "{{type}}":
char* data()
size_t size()
cdef object __Pyx_PyObject_FromStringAndSize(char*, size_t)
cdef object __Pyx_PyBytes_FromStringAndSize(char*, size_t)
cdef object __Pyx_PyByteArray_FromStringAndSize(char*, size_t)
cdef object __Pyx_PyUnicode_FromStringAndSize(char*, size_t)
@cname("{{cname}}")
cdef inline object {{cname}}(const string& s):
return __Pyx_PyObject_FromStringAndSize(s.data(), s.size())
@cname("{{cname.replace("PyObject", "PyUnicode")}}")
cdef inline object {{cname.replace("PyObject", "PyUnicode")}}(const string& s):
return __Pyx_PyUnicode_FromStringAndSize(s.data(), s.size())
@cname("{{cname.replace("PyObject", "PyBytes")}}")
cdef inline object {{cname.replace("PyObject", "PyBytes")}}(const string& s):
return __Pyx_PyBytes_FromStringAndSize(s.data(), s.size())
@cname("{{cname.replace("PyObject", "PyByteArray")}}")
cdef inline object {{cname.replace("PyObject", "PyByteArray")}}(const string& s):
return __Pyx_PyByteArray_FromStringAndSize(s.data(), s.size())
#################### vector.from_py #################### #################### vector.from_py ####################
{{template_type_declarations}} {{template_type_declarations}}
......
...@@ -44,3 +44,29 @@ static void __Pyx_CppExn2PyErr() { ...@@ -44,3 +44,29 @@ static void __Pyx_CppExn2PyErr() {
} }
} }
#endif #endif
/////////////// CppStringToPy.proto ///////////////
static CYTHON_INLINE PyObject *__Pyx_PyObject_FromStlString(std::string const &s);
static CYTHON_INLINE PyObject *__Pyx_PyUnicode_FromStlString(std::string const &s);
static CYTHON_INLINE PyObject *__Pyx_PyBytes_FromStlString(std::string const &s);
static CYTHON_INLINE PyObject *__Pyx_PyByteArray_FromStlString(std::string const &s);
/////////////// CppStringToPy ///////////////
static CYTHON_INLINE PyObject *__Pyx_PyObject_FromStlString(std::string const &s) {
return __Pyx_PyObject_FromStringAndSize(s.data(), s.size());
}
static CYTHON_INLINE PyObject *__Pyx_PyUnicode_FromStlString(std::string const &s) {
return __Pyx_PyUnicode_FromStringAndSize(s.data(), s.size());
}
static CYTHON_INLINE PyObject *__Pyx_PyBytes_FromStlString(std::string const &s) {
return __Pyx_PyBytes_FromStringAndSize(s.data(), s.size());
}
static CYTHON_INLINE PyObject *__Pyx_PyByteArray_FromStlString(std::string const &s) {
return __Pyx_PyByteArray_FromStringAndSize(s.data(), s.size());
}
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