@@ -163,50 +163,6 @@ def ensure_legal_c_identifier(s: str) -> str:
163163 return s
164164
165165
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-
210166class CRenderData :
211167 def __init__ (self ) -> None :
212168
@@ -915,7 +871,8 @@ def parser_body(
915871 """ )
916872 for field in preamble , * fields , finale :
917873 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 )
919876
920877 fastcall = not new_or_init
921878 limited_capi = clinic .limited_capi
@@ -1570,7 +1527,7 @@ def render_option_group_parsing(
15701527 {group_booleans}
15711528 break;
15721529"""
1573- s = linear_format (s , group_booleans = lines )
1530+ s = libclinic . linear_format (s , group_booleans = lines )
15741531 s = s .format_map (d )
15751532 out .append (s )
15761533
@@ -1729,9 +1686,9 @@ def render_function(
17291686 for name , destination in clinic .destination_buffers .items ():
17301687 template = templates [name ]
17311688 if has_option_groups :
1732- template = linear_format (template ,
1689+ template = libclinic . linear_format (template ,
17331690 option_group_parsing = template_dict ['option_group_parsing' ])
1734- template = linear_format (template ,
1691+ template = libclinic . linear_format (template ,
17351692 declarations = template_dict ['declarations' ],
17361693 return_conversion = template_dict ['return_conversion' ],
17371694 initializers = template_dict ['initializers' ],
@@ -1744,10 +1701,8 @@ def render_function(
17441701
17451702 # Only generate the "exit:" label
17461703 # 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 )
17511706
17521707 s = template .format_map (template_dict )
17531708
@@ -6125,9 +6080,9 @@ def format_docstring(self) -> str:
61256080 parameters = self .format_docstring_parameters (params )
61266081 signature = self .format_docstring_signature (f , params )
61276082 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 ()
61316086
61326087 def check_remaining_star (self , lineno : int | None = None ) -> None :
61336088 assert isinstance (self .function , Function )
0 commit comments