Commit ad0a4bf8 authored by Robert Bradshaw's avatar Robert Bradshaw

Use exception values in C++ conversion, consolidate boilerplate.

parent eb3e5e6e
......@@ -3019,18 +3019,34 @@ class CppClassType(CType):
if self.from_py_function is not None:
return True
if self.cname in builtin_cpp_conversions:
X = "XYZABC"
tags = []
context = {}
declarations = ["cdef extern from *:"]
for ix, T in enumerate(self.templates or []):
if T.is_pyobject or not T.create_from_py_utility_code(env):
return False
tags.append(T.specialization_name())
# TODO: exception values
context["T%s" % ix] = T.declaration_code("")
context["T%s_from_py" % ix] = T.from_py_function
if T.exception_value is not None:
except_clause = T.exception_value
if T.exception_check:
except_clause = "? %s" % except_clause
declarations.append(
" ctypedef %s %s '%s'" % (
T.declaration_code("", for_display=True), X[ix], T.declaration_code("")))
else:
except_clause = "*"
declarations.append(
" ctypedef struct %s '%s':\n pass" % (
X[ix], T.declaration_code("")))
declarations.append(
" cdef %s %s_from_py '%s' (object) except %s" % (
X[ix], X[ix], T.from_py_function, except_clause))
cls = self.cname[5:]
cname = "__pyx_convert_%s_from_py_%s" % (cls, "____".join(tags))
context['cname'] = cname
cname = '__pyx_convert_%s_from_py_%s' % (cls, '____'.join(tags))
context = {
'template_type_declarations': '\n'.join(declarations),
'cname': cname
}
from UtilityCode import CythonUtilityCode
env.use_utility_code(CythonUtilityCode.load(cls + ".from_py", "CppConvert.pyx", context=context))
self.from_py_function = cname
......@@ -3040,17 +3056,25 @@ class CppClassType(CType):
if self.to_py_function is not None:
return True
if self.cname in builtin_cpp_conversions:
X = "XYZABC"
tags = []
context = {}
declarations = ["cdef extern from *:"]
for ix, T in enumerate(self.templates or []):
if not T.create_to_py_utility_code(env):
return False
tags.append(T.specialization_name())
context["T%s" % ix] = T.declaration_code("")
context["T%s_to_py" % ix] = T.to_py_function
declarations.append(
" ctypedef struct %s '%s':\n pass" % (
X[ix], T.declaration_code("")))
declarations.append(
" cdef object %s_to_py '%s' (%s)" % (
X[ix], T.to_py_function, X[ix]))
cls = self.cname[5:]
cname = "__pyx_convert_%s_to_py_%s" % (cls, "____".join(tags))
context['cname'] = cname
context = {
'template_type_declarations': '\n'.join(declarations),
'cname': cname
}
from UtilityCode import CythonUtilityCode
env.use_utility_code(CythonUtilityCode.load(cls + ".to_py", "CppConvert.pyx", context=context))
self.to_py_function = cname
......
......@@ -28,10 +28,7 @@ cdef object {{cname}}(string& s):
#################### vector.from_py ####################
cdef extern from *:
ctypedef struct X "{{T0}}":
pass
cdef X X_from_py "{{T0_from_py}}" (object) except *
{{template_type_declarations}}
cdef extern from *:
cdef cppclass vector "std::vector" [T]:
......@@ -47,10 +44,7 @@ cdef vector[X] {{cname}}(object o) except *:
#################### vector.to_py ####################
cdef extern from *:
ctypedef struct X "{{T0}}":
pass
cdef object X_to_py "{{T0_to_py}}" (X)
{{template_type_declarations}}
cdef extern from *:
cdef cppclass vector "const std::vector" [T]:
......@@ -64,10 +58,7 @@ cdef object {{cname}}(vector[X]& v):
#################### list.from_py ####################
cdef extern from *:
ctypedef struct X "{{T0}}":
pass
cdef X X_from_py "{{T0_from_py}}" (object) except *
{{template_type_declarations}}
cdef extern from *:
cdef cppclass cpp_list "std::list" [T]:
......@@ -85,10 +76,7 @@ cdef cpp_list[X] {{cname}}(object o) except *:
cimport cython
cdef extern from *:
ctypedef struct X "{{T0}}":
pass
cdef object X_to_py "{{T0_to_py}}" (X)
{{template_type_declarations}}
cdef extern from *:
cdef cppclass cpp_list "std::list" [T]:
......@@ -113,10 +101,7 @@ cdef object {{cname}}(const_cpp_list[X]& v):
#################### set.from_py ####################
cdef extern from *:
ctypedef struct X "{{T0}}":
pass
cdef X X_from_py "{{T0_from_py}}" (object) except *
{{template_type_declarations}}
cdef extern from *:
cdef cppclass set "std::set" [T]:
......@@ -134,10 +119,7 @@ cdef set[X] {{cname}}(object o) except *:
cimport cython
cdef extern from *:
ctypedef struct X "{{T0}}":
pass
cdef object X_to_py "{{T0_to_py}}" (X)
{{template_type_declarations}}
cdef extern from *:
cdef cppclass cpp_set "std::set" [T]:
......@@ -161,13 +143,7 @@ cdef object {{cname}}(const_cpp_set[X]& s):
#################### pair.from_py ####################
cdef extern from *:
ctypedef struct X "{{T0}}":
pass
ctypedef struct Y "{{T1}}":
pass
cdef X X_from_py "{{T0_from_py}}" (object) except *
cdef Y Y_from_py "{{T1_from_py}}" (object) except *
{{template_type_declarations}}
cdef extern from *:
cdef cppclass pair "std::pair" [T, U]:
......@@ -181,13 +157,7 @@ cdef pair[X,Y] {{cname}}(object o) except *:
#################### pair.to_py ####################
cdef extern from *:
ctypedef struct X "{{T0}}":
pass
ctypedef struct Y "{{T1}}":
pass
cdef object X_to_py "{{T0_to_py}}" (X)
cdef object Y_to_py "{{T1_to_py}}" (Y)
{{template_type_declarations}}
cdef extern from *:
cdef cppclass pair "const std::pair" [T, U]:
......@@ -201,13 +171,7 @@ cdef object {{cname}}(pair[X,Y]& p):
#################### map.from_py ####################
cdef extern from *:
ctypedef struct X "{{T0}}":
pass
ctypedef struct Y "{{T1}}":
pass
cdef X X_from_py "{{T0_from_py}}" (object) except *
cdef Y Y_from_py "{{T1_from_py}}" (object) except *
{{template_type_declarations}}
cdef extern from *:
cdef cppclass pair "std::pair" [T, U]:
......@@ -236,13 +200,7 @@ cdef map[X,Y] {{cname}}(object o) except *:
cimport cython
cdef extern from *:
ctypedef struct X "{{T0}}":
pass
ctypedef struct Y "{{T1}}":
pass
cdef object X_to_py "{{T0_to_py}}" (X)
cdef object Y_to_py "{{T1_to_py}}" (Y)
{{template_type_declarations}}
cdef extern from *:
cdef cppclass map "std::map" [T, U]:
......
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