@@ -86,9 +86,7 @@ def matches(self, cpp_type: CppType) -> bool:
86
86
"""
87
87
raise NotImplementedError ()
88
88
89
- def call_method (
90
- self , res_type : CppType , cy_call_str : str , with_const : bool = True
91
- ) -> str :
89
+ def call_method (self , res_type : CppType , cy_call_str : str , with_const : bool = True ) -> str :
92
90
"""
93
91
Creates a temporary object which has the type of the current TypeConverter object.
94
92
@@ -174,9 +172,9 @@ def _code_for_instantiate_object_from_iter(cpp_type: CppType, it: str) -> str:
174
172
"shared_ptr[$cpp_type_base](new $cpp_type_base(deref(deref($it))))"
175
173
).substitute (locals ())
176
174
else :
177
- return string .Template (
178
- "shared_ptr[$cpp_type](new $cpp_type(deref($it)))"
179
- ). substitute ( locals ())
175
+ return string .Template ("shared_ptr[$cpp_type](new $cpp_type(deref($it)))" ). substitute (
176
+ locals ()
177
+ )
180
178
181
179
182
180
class VoidConverter (TypeConverterBase ):
@@ -186,9 +184,7 @@ def get_base_types(self) -> List[str]:
186
184
def matches (self , cpp_type : CppType ) -> bool :
187
185
return not cpp_type .is_ptr
188
186
189
- def call_method (
190
- self , res_type : CppType , cy_call_str : str , with_const : bool = True
191
- ) -> str :
187
+ def call_method (self , res_type : CppType , cy_call_str : str , with_const : bool = True ) -> str :
192
188
return cy_call_str
193
189
194
190
def matching_python_type (self , cpp_type : CppType ) -> str :
@@ -487,9 +483,7 @@ def input_conversion(
487
483
cleanup = ""
488
484
return code , call_as , cleanup
489
485
490
- def call_method (
491
- self , res_type : CppType , cy_call_str : str , with_const : bool = True
492
- ) -> str :
486
+ def call_method (self , res_type : CppType , cy_call_str : str , with_const : bool = True ) -> str :
493
487
return "cdef char _r = %s" % cy_call_str
494
488
495
489
def output_conversion (
@@ -518,16 +512,13 @@ def input_conversion(
518
512
self , cpp_type : CppType , argument_var : str , arg_num : int
519
513
) -> Tuple [Code , str , str ]:
520
514
code = Code ().add (
521
- "cdef const_char * input_%s = <const_char *> %s"
522
- % (argument_var , argument_var )
515
+ "cdef const_char * input_%s = <const_char *> %s" % (argument_var , argument_var )
523
516
)
524
517
call_as = "input_%s" % argument_var
525
518
cleanup = ""
526
519
return code , call_as , cleanup
527
520
528
- def call_method (
529
- self , res_type : CppType , cy_call_str : str , with_const : bool = True
530
- ) -> str :
521
+ def call_method (self , res_type : CppType , cy_call_str : str , with_const : bool = True ) -> str :
531
522
return "cdef const_char * _r = _cast_const_away(%s)" % cy_call_str
532
523
533
524
def output_conversion (
@@ -560,9 +551,7 @@ def input_conversion(
560
551
cleanup = ""
561
552
return code , call_as , cleanup
562
553
563
- def call_method (
564
- self , res_type : CppType , cy_call_str : str , with_const : bool = True
565
- ) -> str :
554
+ def call_method (self , res_type : CppType , cy_call_str : str , with_const : bool = True ) -> str :
566
555
return "cdef char * _r = _cast_const_away(%s)" % cy_call_str
567
556
568
557
def output_conversion (
@@ -612,7 +601,6 @@ def call_method(
612
601
# If t is a ref, we would like to call on the base type
613
602
t = t .base_type
614
603
elif t .is_ptr :
615
-
616
604
# Special treatment for const raw ptr
617
605
const = ""
618
606
if t .is_const :
@@ -636,7 +624,6 @@ def call_method(
636
624
def output_conversion (
637
625
self , cpp_type : CppType , input_cpp_var : str , output_py_var : str
638
626
) -> Optional [Union [Code , str ]]:
639
-
640
627
cy_clz = self .converters .cython_type (cpp_type )
641
628
642
629
# Need to ensure that type inside the raw ptr is an object and not a ref/ptr
@@ -654,7 +641,6 @@ def output_conversion(
654
641
655
642
656
643
class StdPairConverter (TypeConverterBase ):
657
-
658
644
# remark: we use list instead of tuple internally, in order to
659
645
# provide call by ref args. Python tuples are immutable.
660
646
@@ -740,10 +726,7 @@ def input_conversion(
740
726
741
727
cleanup_code = Code ()
742
728
if cpp_type .is_ref and not cpp_type .is_const :
743
- if (
744
- not i1 .is_enum
745
- and t1 .base_type in self .converters .names_of_wrapper_classes
746
- ):
729
+ if not i1 .is_enum and t1 .base_type in self .converters .names_of_wrapper_classes :
747
730
temp1 = "temp1"
748
731
cleanup_code .add (
749
732
"""
@@ -754,10 +737,7 @@ def input_conversion(
754
737
)
755
738
else :
756
739
temp1 = "%s.first" % temp_var
757
- if (
758
- not i2 .is_enum
759
- and t2 .base_type in self .converters .names_of_wrapper_classes
760
- ):
740
+ if not i2 .is_enum and t2 .base_type in self .converters .names_of_wrapper_classes :
761
741
temp2 = "temp2"
762
742
cleanup_code .add (
763
743
"""
@@ -777,15 +757,10 @@ def input_conversion(
777
757
)
778
758
return code , "%s" % temp_var , cleanup_code
779
759
780
- def call_method (
781
- self , res_type : CppType , cy_call_str : str , with_const : bool = True
782
- ) -> str :
760
+ def call_method (self , res_type : CppType , cy_call_str : str , with_const : bool = True ) -> str :
783
761
return "_r = %s" % (cy_call_str )
784
762
785
- def output_conversion (
786
- self , cpp_type : CppType , input_cpp_var : str , output_py_var : str
787
- ) -> Code :
788
-
763
+ def output_conversion (self , cpp_type : CppType , input_cpp_var : str , output_py_var : str ) -> Code :
789
764
assert not cpp_type .is_ptr
790
765
(
791
766
t1 ,
@@ -907,9 +882,7 @@ def input_conversion(
907
882
value_conv = "<%s> value" % cy_tt_value
908
883
elif tt_value .base_type in self .converters .names_of_wrapper_classes :
909
884
value_conv = "deref((<%s>value).inst.get())" % tt_value .base_type
910
- elif (
911
- tt_value .template_args is not None and tt_value .base_type == "libcpp_vector"
912
- ):
885
+ elif tt_value .template_args is not None and tt_value .base_type == "libcpp_vector" :
913
886
# Special case: the value type is a std::vector< X >, maybe something we can convert?
914
887
915
888
# code_top = """
@@ -1132,15 +1105,10 @@ def input_conversion(
1132
1105
1133
1106
return code , "deref(%s)" % temp_var , cleanup_code
1134
1107
1135
- def call_method (
1136
- self , res_type : CppType , cy_call_str : str , with_const : bool = True
1137
- ) -> str :
1108
+ def call_method (self , res_type : CppType , cy_call_str : str , with_const : bool = True ) -> str :
1138
1109
return "_r = %s" % cy_call_str
1139
1110
1140
- def output_conversion (
1141
- self , cpp_type : CppType , input_cpp_var : str , output_py_var : str
1142
- ) -> Code :
1143
-
1111
+ def output_conversion (self , cpp_type : CppType , input_cpp_var : str , output_py_var : str ) -> Code :
1144
1112
assert not cpp_type .is_ptr
1145
1113
1146
1114
tt_key , tt_value = cpp_type .template_args
@@ -1154,17 +1122,11 @@ def output_conversion(
1154
1122
not cy_tt_value .is_enum
1155
1123
and tt_value .base_type in self .converters .names_of_wrapper_classes
1156
1124
) and (
1157
- not cy_tt_key .is_enum
1158
- and tt_key .base_type in self .converters .names_of_wrapper_classes
1125
+ not cy_tt_key .is_enum and tt_key .base_type in self .converters .names_of_wrapper_classes
1159
1126
):
1160
- raise Exception (
1161
- "Converter can not handle wrapped classes as keys and values in map"
1162
- )
1127
+ raise Exception ("Converter can not handle wrapped classes as keys and values in map" )
1163
1128
1164
- elif (
1165
- not cy_tt_key .is_enum
1166
- and tt_key .base_type in self .converters .names_of_wrapper_classes
1167
- ):
1129
+ elif not cy_tt_key .is_enum and tt_key .base_type in self .converters .names_of_wrapper_classes :
1168
1130
key_conv = "deref(<%s *> (<%s> key).inst.get())" % (cy_tt_key , py_tt_key )
1169
1131
else :
1170
1132
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key , it )
@@ -1189,10 +1151,7 @@ def output_conversion(
1189
1151
locals (),
1190
1152
)
1191
1153
return code
1192
- elif (
1193
- not cy_tt_key .is_enum
1194
- and tt_key .base_type in self .converters .names_of_wrapper_classes
1195
- ):
1154
+ elif not cy_tt_key .is_enum and tt_key .base_type in self .converters .names_of_wrapper_classes :
1196
1155
value_conv = "<%s>(deref(%s).second)" % (cy_tt_value , it )
1197
1156
item_key = mangle ("itemk_" + output_py_var )
1198
1157
code = Code ().add (
@@ -1315,7 +1274,6 @@ def input_conversion(
1315
1274
locals (),
1316
1275
)
1317
1276
if cpp_type .is_ref and not cpp_type .is_const :
1318
-
1319
1277
instantiation = self ._code_for_instantiate_object_from_iter (inner , it )
1320
1278
cleanup_code = Code ().add (
1321
1279
"""
@@ -1357,15 +1315,10 @@ def input_conversion(
1357
1315
)
1358
1316
return code , "%s" % temp_var , cleanup_code
1359
1317
1360
- def call_method (
1361
- self , res_type : CppType , cy_call_str : str , with_const : bool = True
1362
- ) -> str :
1318
+ def call_method (self , res_type : CppType , cy_call_str : str , with_const : bool = True ) -> str :
1363
1319
return "_r = %s" % cy_call_str
1364
1320
1365
- def output_conversion (
1366
- self , cpp_type : CppType , input_cpp_var : str , output_py_var : str
1367
- ) -> Code :
1368
-
1321
+ def output_conversion (self , cpp_type : CppType , input_cpp_var : str , output_py_var : str ) -> Code :
1369
1322
assert not cpp_type .is_ptr
1370
1323
1371
1324
(tt ,) = cpp_type .template_args
@@ -1536,9 +1489,7 @@ def _prepare_recursive_cleanup(
1536
1489
bottommost_code .add ("del %s" % temp_var )
1537
1490
return cleanup_code
1538
1491
1539
- def _prepare_nonrecursive_precall (
1540
- self , topmost_code , cpp_type , code_top , do_deref , * a , ** kw
1541
- ):
1492
+ def _prepare_nonrecursive_precall (self , topmost_code , cpp_type , code_top , do_deref , * a , ** kw ):
1542
1493
# A) Prepare the pre-call
1543
1494
if topmost_code is not None :
1544
1495
if cpp_type .topmost_is_ref and not cpp_type .topmost_is_const :
@@ -1573,7 +1524,6 @@ def _perform_recursion(
1573
1524
* a ,
1574
1525
** kw
1575
1526
):
1576
-
1577
1527
converter = self .cr .get (tt )
1578
1528
py_type = converter .matching_python_type (tt )
1579
1529
rec_arg_num = "%s_rec" % arg_num
@@ -1749,9 +1699,7 @@ def input_conversion(
1749
1699
# Case 1: We wrap a std::vector<> with an enum base type
1750
1700
item = "item%s" % arg_num
1751
1701
if topmost_code is not None :
1752
- raise Exception (
1753
- "Recursion in std::vector<T> not yet implemented for enum"
1754
- )
1702
+ raise Exception ("Recursion in std::vector<T> not yet implemented for enum" )
1755
1703
1756
1704
code = Code ().add (
1757
1705
"""
@@ -1933,20 +1881,14 @@ def input_conversion(
1933
1881
1934
1882
return code , "%s" % temp_var , cleanup_code
1935
1883
1936
- def call_method (
1937
- self , res_type : CppType , cy_call_str : str , with_const : bool = True
1938
- ) -> str :
1939
-
1884
+ def call_method (self , res_type : CppType , cy_call_str : str , with_const : bool = True ) -> str :
1940
1885
t = self .converters .cython_type (res_type )
1941
1886
if t .is_ptr :
1942
1887
return "_r = deref(%s)" % (cy_call_str )
1943
1888
1944
1889
return "_r = %s" % (cy_call_str )
1945
1890
1946
- def output_conversion (
1947
- self , cpp_type : CppType , input_cpp_var : str , output_py_var : str
1948
- ) -> Code :
1949
-
1891
+ def output_conversion (self , cpp_type : CppType , input_cpp_var : str , output_py_var : str ) -> Code :
1950
1892
(tt ,) = cpp_type .template_args
1951
1893
inner = self .converters .cython_type (tt )
1952
1894
@@ -1991,7 +1933,6 @@ def output_conversion(
1991
1933
tt .base_type == "shared_ptr"
1992
1934
and len (set (tt .template_args [0 ].all_occuring_base_types ())) == 1
1993
1935
):
1994
-
1995
1936
inner = self .converters .cython_type (tt )
1996
1937
it = mangle ("it_" + input_cpp_var )
1997
1938
item = mangle ("item_" + output_py_var )
@@ -2184,9 +2125,7 @@ def type_check_expression(self, cpp_type: CppType, argument_var: str) -> str:
2184
2125
(tt ,) = cpp_type .template_args
2185
2126
return "isinstance(%s, %s)" % (argument_var , tt )
2186
2127
2187
- def output_conversion (
2188
- self , cpp_type : CppType , input_cpp_var : str , output_py_var : str
2189
- ) -> Code :
2128
+ def output_conversion (self , cpp_type : CppType , input_cpp_var : str , output_py_var : str ) -> Code :
2190
2129
# L.info("Output conversion for %s" % (cpp_type))
2191
2130
(tt ,) = cpp_type .template_args
2192
2131
code = Code ()
@@ -2222,18 +2161,13 @@ class ConverterRegistry(object):
2222
2161
Therefore TypeConverterBase has methods .get_base_types and .matches
2223
2162
"""
2224
2163
2225
- def __init__ (
2226
- self , instance_mapping , names_of_classes_to_wrap , names_of_enums_to_wrap
2227
- ):
2228
-
2164
+ def __init__ (self , instance_mapping , names_of_classes_to_wrap , names_of_enums_to_wrap ):
2229
2165
self .lookup = defaultdict (list )
2230
2166
2231
2167
self .names_of_wrapper_classes = list (instance_mapping .keys ())
2232
2168
# add everything with a const prefix again
2233
2169
# TODO super hack. We need to support const completely/better without hacks
2234
- self .names_of_wrapper_classes += [
2235
- "const %s" % k for k in instance_mapping .keys ()
2236
- ]
2170
+ self .names_of_wrapper_classes += ["const %s" % k for k in instance_mapping .keys ()]
2237
2171
self .names_of_classes_to_wrap = names_of_classes_to_wrap
2238
2172
self .names_of_enums_to_wrap = names_of_enums_to_wrap
2239
2173
@@ -2252,7 +2186,6 @@ def process_and_set_type_mapping(self, instance_mapping):
2252
2186
self .instance_mapping [alias ] = type_ .transformed (map_ )
2253
2187
2254
2188
def register (self , converter ):
2255
-
2256
2189
assert isinstance (converter , TypeConverterBase )
2257
2190
L .info ("register %s" % converter )
2258
2191
converter ._set_converter_registry (self )
@@ -2273,9 +2206,7 @@ def get(self, cpp_type: CppType) -> TypeConverterBase:
2273
2206
:return: TypeConverterBase
2274
2207
:except: NameError
2275
2208
"""
2276
- rv = [
2277
- conv for conv in self .lookup [cpp_type .base_type ] if conv .matches (cpp_type )
2278
- ]
2209
+ rv = [conv for conv in self .lookup [cpp_type .base_type ] if conv .matches (cpp_type )]
2279
2210
if len (rv ) < 1 :
2280
2211
raise NameError ("no converter for %s in: %s" % (cpp_type , str (self .lookup )))
2281
2212
@@ -2303,9 +2234,7 @@ def setup_converter_registry(classes_to_wrap, enums_to_wrap, instance_map):
2303
2234
names_of_classes_to_wrap = list (set (c .cpp_decl .name for c in classes_to_wrap ))
2304
2235
names_of_enums_to_wrap = list (set (c .cpp_decl .name for c in enums_to_wrap ))
2305
2236
2306
- converters = ConverterRegistry (
2307
- instance_map , names_of_classes_to_wrap , names_of_enums_to_wrap
2308
- )
2237
+ converters = ConverterRegistry (instance_map , names_of_classes_to_wrap , names_of_enums_to_wrap )
2309
2238
2310
2239
converters .register (IntegerConverter ())
2311
2240
converters .register (BooleanConverter ())
0 commit comments