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