Skip to content

Commit 1896cf2

Browse files
committed
[sipify] Better cleanup of method state
1 parent 35f5280 commit 1896cf2

1 file changed

Lines changed: 28 additions & 22 deletions

File tree

‎scripts/sipify.py‎

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ def __init__(self):
134134
self.deprecated_message = None
135135
self.method_py_name: Optional[str] = None
136136

137+
def reset_method_state(self):
138+
"""
139+
Should be called immediately after processing (or skipping) a method
140+
"""
141+
self.comment = ""
142+
self.deprecated_message = ""
143+
self.return_type = ""
144+
137145
def current_fully_qualified_class_name(self) -> str:
138146
return ".".join(
139147
_c
@@ -1732,7 +1740,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
17321740
CONTEXT.current_line = (
17331741
f"%{re.match(r'^ *[/]*% *(.*)$', CONTEXT.current_line).group(1)}"
17341742
)
1735-
CONTEXT.comment = ""
1743+
CONTEXT.reset_method_state()
17361744
dbg_info("do not process SIP code")
17371745
while not re.match(r"^ *[/]*% *End", CONTEXT.current_line):
17381746
write_output("COD", CONTEXT.current_line + "\n")
@@ -1764,7 +1772,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
17641772
CONTEXT.current_line = (
17651773
f"%{re.match(r'^ *% *(.*)$', CONTEXT.current_line).group(1)}"
17661774
)
1767-
CONTEXT.comment = ""
1775+
CONTEXT.reset_method_state()
17681776
write_output("COD", CONTEXT.current_line + "\n")
17691777
continue
17701778

@@ -1773,7 +1781,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
17731781
CONTEXT.current_line = (
17741782
f"%{re.match(r'^ *% (.*)$', CONTEXT.current_line).group(1)}"
17751783
)
1776-
CONTEXT.comment = ""
1784+
CONTEXT.reset_method_state()
17771785
write_output("COD", CONTEXT.current_line)
17781786
continue
17791787

@@ -1791,7 +1799,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
17911799
elif nesting_index == 0 and re.match(
17921800
r"^\s*#(endif|else)", CONTEXT.current_line
17931801
):
1794-
CONTEXT.comment = ""
1802+
CONTEXT.reset_method_state()
17951803
break
17961804
elif nesting_index != 0 and re.match(
17971805
r"^\s*#endif", CONTEXT.current_line
@@ -1830,7 +1838,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
18301838
CONTEXT.ifdef_nesting_idx += 1
18311839
elif re.match(r"^\s*#endif", CONTEXT.current_line):
18321840
if CONTEXT.ifdef_nesting_idx == 0:
1833-
CONTEXT.comment = ""
1841+
CONTEXT.reset_method_state()
18341842
CONTEXT.sip_run = False
18351843
break
18361844
else:
@@ -1883,7 +1891,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
18831891
if match:
18841892
if match.group("external"):
18851893
dbg_info("do not skip external forward declaration")
1886-
CONTEXT.comment = ""
1894+
CONTEXT.reset_method_state()
18871895
else:
18881896
dbg_info("skipping forward declaration")
18891897
continue
@@ -1988,7 +1996,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
19881996
if args.python_output:
19891997
CONTEXT.output_python.append(f"{pyop}\n")
19901998

1991-
CONTEXT.comment = ""
1999+
CONTEXT.reset_method_state()
19922000
continue
19932001

19942002
# Detect comment block
@@ -2177,7 +2185,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
21772185
exit_with_error("expecting { after class definition")
21782186
CONTEXT.bracket_nesting_idx[-1] += 1
21792187

2180-
CONTEXT.comment = ""
2188+
CONTEXT.reset_method_state()
21812189
CONTEXT.header_code = True
21822190
CONTEXT.access[-1] = Visibility.Private
21832191
continue
@@ -2217,8 +2225,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
22172225
Visibility.Public
22182226
) # Top level should stay public
22192227

2220-
CONTEXT.comment = ""
2221-
CONTEXT.return_type = ""
2228+
CONTEXT.reset_method_state()
22222229
CONTEXT.private_section_line = ""
22232230

22242231
dbg_info(f"new bracket balance: {CONTEXT.bracket_nesting_idx}")
@@ -2228,27 +2235,27 @@ def cpp_to_python_signature(cpp_function: str) -> str:
22282235
CONTEXT.access[-1] = Visibility.Private
22292236
CONTEXT.last_access_section_line = CONTEXT.current_line
22302237
CONTEXT.private_section_line = CONTEXT.current_line
2231-
CONTEXT.comment = ""
2238+
CONTEXT.reset_method_state()
22322239
dbg_info("going private")
22332240
continue
22342241

22352242
elif re.match(r"^\s*(public( slots)?):.*$", CONTEXT.current_line):
22362243
dbg_info("going public")
22372244
CONTEXT.last_access_section_line = CONTEXT.current_line
22382245
CONTEXT.access[-1] = Visibility.Public
2239-
CONTEXT.comment = ""
2246+
CONTEXT.reset_method_state()
22402247

22412248
elif re.match(r"^\s*signals:.*$", CONTEXT.current_line):
22422249
dbg_info("going public for signals")
22432250
CONTEXT.last_access_section_line = CONTEXT.current_line
22442251
CONTEXT.access[-1] = Visibility.Signals
2245-
CONTEXT.comment = ""
2252+
CONTEXT.reset_method_state()
22462253

22472254
elif re.match(r"^\s*(protected)( slots)?:.*$", CONTEXT.current_line):
22482255
dbg_info("going protected")
22492256
CONTEXT.last_access_section_line = CONTEXT.current_line
22502257
CONTEXT.access[-1] = Visibility.Protected
2251-
CONTEXT.comment = ""
2258+
CONTEXT.reset_method_state()
22522259

22532260
elif (
22542261
CONTEXT.access[-1] == Visibility.Private and "SIP_FORCE" in CONTEXT.current_line
@@ -2259,15 +2266,15 @@ def cpp_to_python_signature(cpp_function: str) -> str:
22592266
CONTEXT.private_section_line = ""
22602267

22612268
elif any(x == Visibility.Private for x in CONTEXT.access) and not CONTEXT.sip_run:
2262-
CONTEXT.comment = ""
2269+
CONTEXT.reset_method_state()
22632270
continue
22642271

22652272
# Skip operators
22662273
if CONTEXT.access[-1] != Visibility.Private and re.search(
22672274
r"operator(=|<<|>>|->)\s*\(", CONTEXT.current_line
22682275
):
22692276
dbg_info("skip operator")
2270-
CONTEXT.comment = ""
2277+
CONTEXT.reset_method_state()
22712278
detect_and_remove_following_body_or_initializerlist()
22722279
continue
22732280

@@ -2283,7 +2290,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
22832290
CONTEXT.comment = process_doxygen_line(match.group(1))
22842291
CONTEXT.comment = CONTEXT.comment.rstrip()
22852292
elif not re.search(r"\*/", CONTEXT.input_lines[CONTEXT.line_idx - 1]):
2286-
CONTEXT.comment = ""
2293+
CONTEXT.reset_method_state()
22872294
continue
22882295

22892296
# Handle Q_DECLARE_FLAGS in Qt6
@@ -2680,7 +2687,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
26802687
if match:
26812688
CONTEXT.current_line = f"{match.group('staticconst')};"
26822689
if match.group("static") is None:
2683-
CONTEXT.comment = ""
2690+
CONTEXT.reset_method_state()
26842691

26852692
if match.group("endingchar") == "|":
26862693
dbg_info("multiline const static assignment")
@@ -3022,7 +3029,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
30223029
CONTEXT.current_line,
30233030
):
30243031
dbg_info(f"removing deleted function {CONTEXT.current_line}")
3025-
CONTEXT.comment = ""
3032+
CONTEXT.reset_method_state()
30263033
continue
30273034

30283035
# remove export macro from struct definition
@@ -3133,7 +3140,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
31333140
CONTEXT.current_line = (
31343141
f"{CONTEXT.indent}const QgsSettingsEntryEnumFlag_{var_name} {var_name};"
31353142
)
3136-
CONTEXT.comment = ""
3143+
CONTEXT.reset_method_state()
31373144
write_output("ENF", f"{prep_line}\n", "prepend")
31383145

31393146
write_output("NOR", f"{CONTEXT.current_line}\n")
@@ -3386,8 +3393,7 @@ def cpp_to_python_signature(cpp_function: str) -> str:
33863393
] = doc_string
33873394
write_output("CM4", f"{doc_prepend}%End\n")
33883395

3389-
CONTEXT.comment = ""
3390-
CONTEXT.return_type = ""
3396+
CONTEXT.reset_method_state()
33913397
if CONTEXT.is_override_or_make_private == PrependType.MakePrivate:
33923398
write_output("MKP", CONTEXT.last_access_section_line)
33933399
CONTEXT.is_override_or_make_private = PrependType.NoPrepend

0 commit comments

Comments
 (0)