Skip to content

Commit ebe95fd

Browse files
Issue #26305: Argument Clinic now escapes braces. No need to double them.
1 parent efe7256 commit ebe95fd

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

Modules/cmathmodule.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ class Py_complex_protected_return_converter(CReturnConverter):
2727
self.declare(data)
2828
data.return_conversion.append("""
2929
PyFPE_END_PROTECT(_return_value);
30-
if (errno == EDOM) {{
30+
if (errno == EDOM) {
3131
PyErr_SetString(PyExc_ValueError, "math domain error");
3232
goto exit;
33-
}}
34-
else if (errno == ERANGE) {{
33+
}
34+
else if (errno == ERANGE) {
3535
PyErr_SetString(PyExc_OverflowError, "math range error");
3636
goto exit;
37-
}}
38-
else {{
37+
}
38+
else {
3939
return_value = PyComplex_FromCComplex(_return_value);
40-
}}
40+
}
4141
""".strip())
4242
[python start generated code]*/
43-
/*[python end generated code: output=da39a3ee5e6b4b0d input=231019039a6fbb9a]*/
43+
/*[python end generated code: output=da39a3ee5e6b4b0d input=345daa075b1028e7]*/
4444

4545
#if (FLT_RADIX != 2 && FLT_RADIX != 16)
4646
#error "Modules/cmathmodule.c expects FLT_RADIX to be 2 or 16"

Tools/clinic/clinic.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ def rstrip_lines(s):
176176
text.pop()
177177
return output()
178178

179+
def format_escape(s):
180+
# double up curly-braces, this string will be used
181+
# as part of a format_map() template later
182+
s = s.replace('{', '{{')
183+
s = s.replace('}', '}}')
184+
return s
185+
179186
def linear_format(s, **kwargs):
180187
"""
181188
Perform str.format-like substitution, except:
@@ -996,7 +1003,7 @@ def render_option_group_parsing(self, f, template_dict):
9961003
count_min = sys.maxsize
9971004
count_max = -1
9981005

999-
add("switch (PyTuple_GET_SIZE(args)) {{\n")
1006+
add("switch (PyTuple_GET_SIZE(args)) {\n")
10001007
for subset in permute_optional_groups(left, required, right):
10011008
count = len(subset)
10021009
count_min = min(count_min, count)
@@ -1012,7 +1019,6 @@ def render_option_group_parsing(self, f, template_dict):
10121019
d = {}
10131020
d['count'] = count
10141021
d['name'] = f.name
1015-
d['groups'] = sorted(group_ids)
10161022
d['format_units'] = "".join(p.converter.format_unit for p in subset)
10171023

10181024
parse_arguments = []
@@ -1039,8 +1045,8 @@ def render_option_group_parsing(self, f, template_dict):
10391045
s = ' PyErr_SetString(PyExc_TypeError, "{} requires {} to {} arguments");\n'
10401046
add(s.format(f.full_name, count_min, count_max))
10411047
add(' goto exit;\n')
1042-
add("}}")
1043-
template_dict['option_group_parsing'] = output()
1048+
add("}")
1049+
template_dict['option_group_parsing'] = format_escape(output())
10441050

10451051
def render_function(self, clinic, f):
10461052
if not f:
@@ -1135,16 +1141,16 @@ def render_function(self, clinic, f):
11351141
f.return_converter.render(f, data)
11361142
template_dict['impl_return_type'] = f.return_converter.type
11371143

1138-
template_dict['declarations'] = "\n".join(data.declarations)
1144+
template_dict['declarations'] = format_escape("\n".join(data.declarations))
11391145
template_dict['initializers'] = "\n\n".join(data.initializers)
11401146
template_dict['modifications'] = '\n\n'.join(data.modifications)
11411147
template_dict['keywords'] = '"' + '", "'.join(data.keywords) + '"'
11421148
template_dict['format_units'] = ''.join(data.format_units)
11431149
template_dict['parse_arguments'] = ', '.join(data.parse_arguments)
11441150
template_dict['impl_parameters'] = ", ".join(data.impl_parameters)
11451151
template_dict['impl_arguments'] = ", ".join(data.impl_arguments)
1146-
template_dict['return_conversion'] = "".join(data.return_conversion).rstrip()
1147-
template_dict['cleanup'] = "".join(data.cleanup)
1152+
template_dict['return_conversion'] = format_escape("".join(data.return_conversion).rstrip())
1153+
template_dict['cleanup'] = format_escape("".join(data.cleanup))
11481154
template_dict['return_value'] = data.return_value
11491155

11501156
# used by unpack tuple code generator
@@ -2439,12 +2445,7 @@ def declaration(self):
24392445
declaration.append('\nPy_ssize_clean_t ')
24402446
declaration.append(self.length_name())
24412447
declaration.append(';')
2442-
s = "".join(declaration)
2443-
# double up curly-braces, this string will be used
2444-
# as part of a format_map() template later
2445-
s = s.replace("{", "{{")
2446-
s = s.replace("}", "}}")
2447-
return s
2448+
return "".join(declaration)
24482449

24492450
def initialize(self):
24502451
"""

0 commit comments

Comments
 (0)