Skip to content

Commit 570a6f4

Browse files
authored
[FIX] comment parsing once and for all (#179)
* [test] add more comment parsing tests * more tests * whitespace * test fixes * err * compare code contents * test fixes * first fixes towards tests * allow empty lines in the beginning * err * Update PXDParser.py * lint codebase to LL 100 * lint tests
1 parent 39fc418 commit 570a6f4

20 files changed

+210
-402
lines changed

.github/workflows/black.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ jobs:
99
- uses: actions/checkout@v3
1010
- uses: psf/black@stable
1111
with:
12-
options: "--check --diff"
12+
options: "--line-length 100 --check --diff"

autowrap/CodeGenerator.py

Lines changed: 45 additions & 133 deletions
Large diffs are not rendered by default.

autowrap/ConversionProvider.py

Lines changed: 31 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ def matches(self, cpp_type: CppType) -> bool:
8686
"""
8787
raise NotImplementedError()
8888

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:
9290
"""
9391
Creates a temporary object which has the type of the current TypeConverter object.
9492
@@ -174,9 +172,9 @@ def _code_for_instantiate_object_from_iter(cpp_type: CppType, it: str) -> str:
174172
"shared_ptr[$cpp_type_base](new $cpp_type_base(deref(deref($it))))"
175173
).substitute(locals())
176174
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+
)
180178

181179

182180
class VoidConverter(TypeConverterBase):
@@ -186,9 +184,7 @@ def get_base_types(self) -> List[str]:
186184
def matches(self, cpp_type: CppType) -> bool:
187185
return not cpp_type.is_ptr
188186

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:
192188
return cy_call_str
193189

194190
def matching_python_type(self, cpp_type: CppType) -> str:
@@ -487,9 +483,7 @@ def input_conversion(
487483
cleanup = ""
488484
return code, call_as, cleanup
489485

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:
493487
return "cdef char _r = %s" % cy_call_str
494488

495489
def output_conversion(
@@ -518,16 +512,13 @@ def input_conversion(
518512
self, cpp_type: CppType, argument_var: str, arg_num: int
519513
) -> Tuple[Code, str, str]:
520514
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)
523516
)
524517
call_as = "input_%s" % argument_var
525518
cleanup = ""
526519
return code, call_as, cleanup
527520

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:
531522
return "cdef const_char * _r = _cast_const_away(%s)" % cy_call_str
532523

533524
def output_conversion(
@@ -560,9 +551,7 @@ def input_conversion(
560551
cleanup = ""
561552
return code, call_as, cleanup
562553

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:
566555
return "cdef char * _r = _cast_const_away(%s)" % cy_call_str
567556

568557
def output_conversion(
@@ -612,7 +601,6 @@ def call_method(
612601
# If t is a ref, we would like to call on the base type
613602
t = t.base_type
614603
elif t.is_ptr:
615-
616604
# Special treatment for const raw ptr
617605
const = ""
618606
if t.is_const:
@@ -636,7 +624,6 @@ def call_method(
636624
def output_conversion(
637625
self, cpp_type: CppType, input_cpp_var: str, output_py_var: str
638626
) -> Optional[Union[Code, str]]:
639-
640627
cy_clz = self.converters.cython_type(cpp_type)
641628

642629
# Need to ensure that type inside the raw ptr is an object and not a ref/ptr
@@ -654,7 +641,6 @@ def output_conversion(
654641

655642

656643
class StdPairConverter(TypeConverterBase):
657-
658644
# remark: we use list instead of tuple internally, in order to
659645
# provide call by ref args. Python tuples are immutable.
660646

@@ -740,10 +726,7 @@ def input_conversion(
740726

741727
cleanup_code = Code()
742728
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:
747730
temp1 = "temp1"
748731
cleanup_code.add(
749732
"""
@@ -754,10 +737,7 @@ def input_conversion(
754737
)
755738
else:
756739
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:
761741
temp2 = "temp2"
762742
cleanup_code.add(
763743
"""
@@ -777,15 +757,10 @@ def input_conversion(
777757
)
778758
return code, "%s" % temp_var, cleanup_code
779759

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:
783761
return "_r = %s" % (cy_call_str)
784762

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:
789764
assert not cpp_type.is_ptr
790765
(
791766
t1,
@@ -907,9 +882,7 @@ def input_conversion(
907882
value_conv = "<%s> value" % cy_tt_value
908883
elif tt_value.base_type in self.converters.names_of_wrapper_classes:
909884
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":
913886
# Special case: the value type is a std::vector< X >, maybe something we can convert?
914887

915888
# code_top = """
@@ -1132,15 +1105,10 @@ def input_conversion(
11321105

11331106
return code, "deref(%s)" % temp_var, cleanup_code
11341107

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:
11381109
return "_r = %s" % cy_call_str
11391110

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:
11441112
assert not cpp_type.is_ptr
11451113

11461114
tt_key, tt_value = cpp_type.template_args
@@ -1154,17 +1122,11 @@ def output_conversion(
11541122
not cy_tt_value.is_enum
11551123
and tt_value.base_type in self.converters.names_of_wrapper_classes
11561124
) 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
11591126
):
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")
11631128

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:
11681130
key_conv = "deref(<%s *> (<%s> key).inst.get())" % (cy_tt_key, py_tt_key)
11691131
else:
11701132
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it)
@@ -1189,10 +1151,7 @@ def output_conversion(
11891151
locals(),
11901152
)
11911153
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:
11961155
value_conv = "<%s>(deref(%s).second)" % (cy_tt_value, it)
11971156
item_key = mangle("itemk_" + output_py_var)
11981157
code = Code().add(
@@ -1315,7 +1274,6 @@ def input_conversion(
13151274
locals(),
13161275
)
13171276
if cpp_type.is_ref and not cpp_type.is_const:
1318-
13191277
instantiation = self._code_for_instantiate_object_from_iter(inner, it)
13201278
cleanup_code = Code().add(
13211279
"""
@@ -1357,15 +1315,10 @@ def input_conversion(
13571315
)
13581316
return code, "%s" % temp_var, cleanup_code
13591317

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:
13631319
return "_r = %s" % cy_call_str
13641320

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:
13691322
assert not cpp_type.is_ptr
13701323

13711324
(tt,) = cpp_type.template_args
@@ -1536,9 +1489,7 @@ def _prepare_recursive_cleanup(
15361489
bottommost_code.add("del %s" % temp_var)
15371490
return cleanup_code
15381491

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):
15421493
# A) Prepare the pre-call
15431494
if topmost_code is not None:
15441495
if cpp_type.topmost_is_ref and not cpp_type.topmost_is_const:
@@ -1573,7 +1524,6 @@ def _perform_recursion(
15731524
*a,
15741525
**kw
15751526
):
1576-
15771527
converter = self.cr.get(tt)
15781528
py_type = converter.matching_python_type(tt)
15791529
rec_arg_num = "%s_rec" % arg_num
@@ -1749,9 +1699,7 @@ def input_conversion(
17491699
# Case 1: We wrap a std::vector<> with an enum base type
17501700
item = "item%s" % arg_num
17511701
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")
17551703

17561704
code = Code().add(
17571705
"""
@@ -1933,20 +1881,14 @@ def input_conversion(
19331881

19341882
return code, "%s" % temp_var, cleanup_code
19351883

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:
19401885
t = self.converters.cython_type(res_type)
19411886
if t.is_ptr:
19421887
return "_r = deref(%s)" % (cy_call_str)
19431888

19441889
return "_r = %s" % (cy_call_str)
19451890

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:
19501892
(tt,) = cpp_type.template_args
19511893
inner = self.converters.cython_type(tt)
19521894

@@ -1991,7 +1933,6 @@ def output_conversion(
19911933
tt.base_type == "shared_ptr"
19921934
and len(set(tt.template_args[0].all_occuring_base_types())) == 1
19931935
):
1994-
19951936
inner = self.converters.cython_type(tt)
19961937
it = mangle("it_" + input_cpp_var)
19971938
item = mangle("item_" + output_py_var)
@@ -2184,9 +2125,7 @@ def type_check_expression(self, cpp_type: CppType, argument_var: str) -> str:
21842125
(tt,) = cpp_type.template_args
21852126
return "isinstance(%s, %s)" % (argument_var, tt)
21862127

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:
21902129
# L.info("Output conversion for %s" % (cpp_type))
21912130
(tt,) = cpp_type.template_args
21922131
code = Code()
@@ -2222,18 +2161,13 @@ class ConverterRegistry(object):
22222161
Therefore TypeConverterBase has methods .get_base_types and .matches
22232162
"""
22242163

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):
22292165
self.lookup = defaultdict(list)
22302166

22312167
self.names_of_wrapper_classes = list(instance_mapping.keys())
22322168
# add everything with a const prefix again
22332169
# 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()]
22372171
self.names_of_classes_to_wrap = names_of_classes_to_wrap
22382172
self.names_of_enums_to_wrap = names_of_enums_to_wrap
22392173

@@ -2252,7 +2186,6 @@ def process_and_set_type_mapping(self, instance_mapping):
22522186
self.instance_mapping[alias] = type_.transformed(map_)
22532187

22542188
def register(self, converter):
2255-
22562189
assert isinstance(converter, TypeConverterBase)
22572190
L.info("register %s" % converter)
22582191
converter._set_converter_registry(self)
@@ -2273,9 +2206,7 @@ def get(self, cpp_type: CppType) -> TypeConverterBase:
22732206
:return: TypeConverterBase
22742207
:except: NameError
22752208
"""
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)]
22792210
if len(rv) < 1:
22802211
raise NameError("no converter for %s in: %s" % (cpp_type, str(self.lookup)))
22812212

@@ -2303,9 +2234,7 @@ def setup_converter_registry(classes_to_wrap, enums_to_wrap, instance_map):
23032234
names_of_classes_to_wrap = list(set(c.cpp_decl.name for c in classes_to_wrap))
23042235
names_of_enums_to_wrap = list(set(c.cpp_decl.name for c in enums_to_wrap))
23052236

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)
23092238

23102239
converters.register(IntegerConverter())
23112240
converters.register(BooleanConverter())

0 commit comments

Comments
 (0)