Skip to content

Commit e816e72

Browse files
committed
Refactor output_conversion
1 parent 0465f23 commit e816e72

File tree

2 files changed

+27
-65
lines changed

2 files changed

+27
-65
lines changed

autowrap/ConversionProvider.py

Lines changed: 26 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,89 +1108,51 @@ def output_conversion(
11081108

11091109
it = mangle("it_" + input_cpp_var)
11101110

1111+
cython_text = """
1112+
|$output_py_var = dict()
1113+
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
1114+
"""
11111115
# Code for key that is wrapped
11121116
if not cy_tt_key.is_enum and self.is_wrapper_class(py_tt_key.base_type):
11131117
key_conv = "deref(<%s *> (<%s> key).inst.get())" % (cy_tt_key, py_tt_key)
11141118
value_conv = "<%s>(deref(%s).second)" % (cy_tt_value, it)
11151119
item_key = mangle("itemk_" + output_py_var)
1116-
code = Code().add(
1117-
"""
1118-
|$output_py_var = dict()
1119-
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
1120-
|cdef $py_tt_key $item_key
1121-
|while $it != $input_cpp_var.end():
1122-
| #$output_py_var[$key_conv] = $value_conv
1123-
| $item_key = $py_tt_key.__new__($py_tt_key)
1124-
| $item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))
1125-
| # $output_py_var[$key_conv] = $value_conv
1126-
| $output_py_var[$item_key] = $value_conv
1127-
| inc($it)
1128-
""",
1129-
locals(),
1130-
)
1131-
return code
1132-
else:
1133-
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it)
1134-
1120+
cython_text += "\n|cdef $py_tt_key $item_key"
1121+
cython_text += "\n|while $it != $input_cpp_var.end():"
1122+
cython_text += "\n|\t$item_key = $py_tt_key.__new__($py_tt_key)"
1123+
cython_text += "\n|\t$item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))"
1124+
cython_text += "\n|\t$output_py_var[$item_key] = $value_conv"
11351125
# Code for value that is wrapped
1136-
if not cy_tt_value.is_enum and self.is_wrapper_class(tt_value.base_type):
1126+
elif not cy_tt_value.is_enum and self.is_wrapper_class(tt_value.base_type):
11371127
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it)
1138-
cy_tt = tt_value.base_type
1128+
cy_tt_base = tt_value.base_type
11391129
item = mangle("item_" + output_py_var)
1140-
code = Code().add(
1141-
"""
1142-
|$output_py_var = dict()
1143-
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
1144-
|cdef $cy_tt $item
1145-
|while $it != $input_cpp_var.end():
1146-
| $item = $cy_tt.__new__($cy_tt)
1147-
| $item.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))
1148-
| $output_py_var[$key_conv] = $item
1149-
| inc($it)
1150-
""",
1151-
locals(),
1152-
)
1153-
return code
1130+
cython_text += "\n|cdef $cy_tt_base $item"
1131+
cython_text += "\n|while $it != $input_cpp_var.end():"
1132+
cython_text += "\n|\t$item = $cy_tt_base.__new__($cy_tt_base)"
1133+
cython_text += "\n|\t$item.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))"
1134+
cython_text += "\n|\t$output_py_var[$key_conv] = $item"
11541135
# Code for value AND key that is wrapped
11551136
elif (
11561137
not cy_tt_value.is_enum
11571138
and self.is_wrapper_class(tt_value.base_type)
11581139
and self.is_wrapper_class(py_tt_key.base_type)
11591140
):
1160-
key_conv = "deref(<%s *> (<%s> key).inst.get())" % (cy_tt_key, py_tt_key)
1161-
cy_tt = tt_value.base_type
11621141
item_key = mangle("itemk_" + output_py_var)
11631142
item_val = mangle("item_" + output_py_var)
1164-
code = Code().add(
1165-
"""
1166-
|$output_py_var = dict()
1167-
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
1168-
|cdef $py_tt_key $item_key
1169-
|while $it != $input_cpp_var.end():
1170-
| #$output_py_var[$key_conv] = $value_conv
1171-
| $item_key = $py_tt_key.__new__($py_tt_key)
1172-
| $item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))
1173-
| $item_val = $cy_tt.__new__($cy_tt)
1174-
| $item_val.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))
1175-
| inc($it)
1176-
""",
1177-
locals(),
1178-
)
1179-
return code
1143+
cython_text += "\n|cdef $py_tt_key $item_key"
1144+
cython_text += "\n|while $it != $input_cpp_var.end():"
1145+
cython_text += "\n|\t$item_key = $py_tt_key.__new__($py_tt_key)"
1146+
cython_text += "\n|\t$item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))"
1147+
cython_text += "\n|\t$item_val.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))"
11801148
else:
11811149
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it)
11821150
value_conv = "<%s>(deref(%s).second)" % (cy_tt_value, it)
1183-
code = Code().add(
1184-
"""
1185-
|$output_py_var = dict()
1186-
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
1187-
|while $it != $input_cpp_var.end():
1188-
| $output_py_var[$key_conv] = $value_conv
1189-
| inc($it)
1190-
""",
1191-
locals(),
1192-
)
1193-
return code
1151+
cython_text += "\n|while $it != $input_cpp_var.end():"
1152+
cython_text += "\n|\t$output_py_var[$key_conv] = $value_conv"
1153+
cython_text += "\n|\tinc($it)"
1154+
cython_text = cython_text.replace("\t", " ")
1155+
return Code().add(cython_text, locals())
11941156

11951157

11961158
class StdSetConverter(TypeConverterBase):

tests/test_files/inherited.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Generated with autowrap 0.22.9 and Cython (Parser) 3.0.0a10
1+
#Generated with autowrap 0.22.9 and Cython (Parser) 0.29.30
22
#cython: c_string_encoding=ascii
33
#cython: embedsignature=False
44
from enum import Enum as _PyEnum

0 commit comments

Comments
 (0)