@@ -163,50 +163,6 @@ def ensure_legal_c_identifier(s: str) -> str:
163
163
return s
164
164
165
165
166
- def linear_format (s : str , ** kwargs : str ) -> str :
167
- """
168
- Perform str.format-like substitution, except:
169
- * The strings substituted must be on lines by
170
- themselves. (This line is the "source line".)
171
- * If the substitution text is empty, the source line
172
- is removed in the output.
173
- * If the field is not recognized, the original line
174
- is passed unmodified through to the output.
175
- * If the substitution text is not empty:
176
- * Each line of the substituted text is indented
177
- by the indent of the source line.
178
- * A newline will be added to the end.
179
- """
180
- lines = []
181
- for line in s .split ('\n ' ):
182
- indent , curly , trailing = line .partition ('{' )
183
- if not curly :
184
- lines .extend ([line , "\n " ])
185
- continue
186
-
187
- name , curly , trailing = trailing .partition ('}' )
188
- if not curly or name not in kwargs :
189
- lines .extend ([line , "\n " ])
190
- continue
191
-
192
- if trailing :
193
- fail (f"Text found after {{{ name } }} block marker! "
194
- "It must be on a line by itself." )
195
- if indent .strip ():
196
- fail (f"Non-whitespace characters found before {{{ name } }} block marker! "
197
- "It must be on a line by itself." )
198
-
199
- value = kwargs [name ]
200
- if not value :
201
- continue
202
-
203
- stripped = [line .rstrip () for line in value .split ("\n " )]
204
- value = textwrap .indent ("\n " .join (stripped ), indent )
205
- lines .extend ([value , "\n " ])
206
-
207
- return "" .join (lines [:- 1 ])
208
-
209
-
210
166
class CRenderData :
211
167
def __init__ (self ) -> None :
212
168
@@ -915,7 +871,8 @@ def parser_body(
915
871
""" )
916
872
for field in preamble , * fields , finale :
917
873
lines .append (field )
918
- return linear_format ("\n " .join (lines ), parser_declarations = declarations )
874
+ return libclinic .linear_format ("\n " .join (lines ),
875
+ parser_declarations = declarations )
919
876
920
877
fastcall = not new_or_init
921
878
limited_capi = clinic .limited_capi
@@ -1570,7 +1527,7 @@ def render_option_group_parsing(
1570
1527
{group_booleans}
1571
1528
break;
1572
1529
"""
1573
- s = linear_format (s , group_booleans = lines )
1530
+ s = libclinic . linear_format (s , group_booleans = lines )
1574
1531
s = s .format_map (d )
1575
1532
out .append (s )
1576
1533
@@ -1729,9 +1686,9 @@ def render_function(
1729
1686
for name , destination in clinic .destination_buffers .items ():
1730
1687
template = templates [name ]
1731
1688
if has_option_groups :
1732
- template = linear_format (template ,
1689
+ template = libclinic . linear_format (template ,
1733
1690
option_group_parsing = template_dict ['option_group_parsing' ])
1734
- template = linear_format (template ,
1691
+ template = libclinic . linear_format (template ,
1735
1692
declarations = template_dict ['declarations' ],
1736
1693
return_conversion = template_dict ['return_conversion' ],
1737
1694
initializers = template_dict ['initializers' ],
@@ -1744,10 +1701,8 @@ def render_function(
1744
1701
1745
1702
# Only generate the "exit:" label
1746
1703
# if we have any gotos
1747
- need_exit_label = "goto exit;" in template
1748
- template = linear_format (template ,
1749
- exit_label = "exit:" if need_exit_label else ''
1750
- )
1704
+ label = "exit:" if "goto exit;" in template else ""
1705
+ template = libclinic .linear_format (template , exit_label = label )
1751
1706
1752
1707
s = template .format_map (template_dict )
1753
1708
@@ -6125,9 +6080,9 @@ def format_docstring(self) -> str:
6125
6080
parameters = self .format_docstring_parameters (params )
6126
6081
signature = self .format_docstring_signature (f , params )
6127
6082
docstring = "\n " .join (lines )
6128
- return linear_format (docstring ,
6129
- signature = signature ,
6130
- parameters = parameters ).rstrip ()
6083
+ return libclinic . linear_format (docstring ,
6084
+ signature = signature ,
6085
+ parameters = parameters ).rstrip ()
6131
6086
6132
6087
def check_remaining_star (self , lineno : int | None = None ) -> None :
6133
6088
assert isinstance (self .function , Function )
0 commit comments