Skip to content

Commit 99b8847

Browse files
gh-64660: Do not hardcode the name of the returned variable (GH-155268)
Return converters had to hardcode "return_value", because declare() sets data.return_value to the variable which receives the value returned by the impl. The name of the variable returned by the parsing function is now available as data.parser_retval.
1 parent 116caab commit 99b8847

6 files changed

Lines changed: 23 additions & 12 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Argument Clinic return converters no longer need to hardcode the name of the
2+
variable returned by the parsing function.
3+
It is now available as ``data.parser_retval``.

PC/msvcrtmodule.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,18 @@ class byte_char_return_converter(CReturnConverter):
6565
data.declarations.append('char s[1];')
6666
data.return_value = 's[0]'
6767
data.return_conversion.append(
68-
'return_value = PyBytes_FromStringAndSize(s, 1);\n')
68+
f'{data.parser_retval} = PyBytes_FromStringAndSize(s, 1);\n')
6969
7070
class wchar_t_return_converter(CReturnConverter):
7171
type = 'wchar_t'
7272
7373
def render(self, function, data):
7474
self.declare(data)
7575
data.return_conversion.append(
76-
'return_value = PyUnicode_FromOrdinal(_return_value);\n')
76+
f'{data.parser_retval} = '
77+
f'PyUnicode_FromOrdinal({data.converter_retval});\n')
7778
[python start generated code]*/
78-
/*[python end generated code: output=da39a3ee5e6b4b0d input=ff031be44ab3250d]*/
79+
/*[python end generated code: output=da39a3ee5e6b4b0d input=ed7a4a045a6d0496]*/
7980

8081
/*[clinic input]
8182
module msvcrt

Tools/clinic/libclinic/clanguage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ def render_function(
525525
template_dict['cleanup'] = libclinic.format_escape("".join(data.cleanup))
526526

527527
template_dict['return_value'] = data.return_value
528+
template_dict['parser_retval'] = data.parser_retval
528529
template_dict['lock'] = "\n".join(data.lock)
529530
template_dict['unlock'] = "\n".join(data.unlock)
530531

Tools/clinic/libclinic/codegen.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,17 @@ def __init__(self) -> None:
4747
# The arguments to the impl function at the time it's called.
4848
self.impl_arguments: list[str] = []
4949

50+
# The name of the variable which is returned by the parser.
51+
self.parser_retval = "return_value"
52+
5053
# For return converters: the name of the variable that
5154
# should receive the value returned by the impl.
5255
self.return_value = "return_value"
5356

5457
# For return converters: the code to convert the return
5558
# value from the parse function. This is also where
56-
# you should check the _return_value for errors, and
57-
# "goto exit" if there are any.
59+
# you should check the value returned by the impl for errors,
60+
# and "goto exit" if there are any.
5861
self.return_conversion: list[str] = []
5962
self.converter_retval = "_return_value"
6063

Tools/clinic/libclinic/parse_args.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def select_prototypes(self) -> None:
320320
self.docstring_prototype = ''
321321
self.docstring_definition = ''
322322
self.methoddef_define = METHODDEF_PROTOTYPE_DEFINE
323-
self.return_value_declaration = "PyObject *return_value = NULL;"
323+
self.return_value_declaration = "PyObject *{parser_retval} = NULL;"
324324

325325
if self.is_new_or_init() and not self.func.docstring:
326326
pass
@@ -331,7 +331,7 @@ def select_prototypes(self) -> None:
331331
elif self.func.kind is SETTER:
332332
if self.func.docstring:
333333
fail("docstrings are only supported for @getter, not @setter")
334-
self.return_value_declaration = "int {return_value};"
334+
self.return_value_declaration = "int {parser_retval};"
335335
self.methoddef_define = SETTERDEF_PROTOTYPE_DEFINE
336336
else:
337337
self.docstring_prototype = DOCSTRING_PROTOTYPE_VAR
@@ -372,7 +372,7 @@ def parser_body(
372372
373373
{exit_label}
374374
{cleanup}
375-
return return_value;
375+
return {parser_retval};
376376
}}
377377
""")
378378
for field in preamble, *fields, finale:
@@ -861,7 +861,7 @@ def handle_new_or_init(self) -> None:
861861
if self.func.kind is METHOD_NEW:
862862
self.parser_prototype = PARSER_PROTOTYPE_KEYWORD
863863
else:
864-
self.return_value_declaration = "int return_value = -1;"
864+
self.return_value_declaration = "int {parser_retval} = -1;"
865865
self.parser_prototype = PARSER_PROTOTYPE_KEYWORD___INIT__
866866

867867
fields: list[str] = list(self.parser_body_fields)

Tools/clinic/libclinic/return_converters.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ def render(self, function: Function, data: CRenderData) -> None:
110110
self.declare(data)
111111
self.err_occurred_if(f"{data.converter_retval} == -1", data)
112112
data.return_conversion.append(
113-
f'return_value = PyBool_FromLong((long){data.converter_retval});\n'
113+
f'{data.parser_retval} = '
114+
f'PyBool_FromLong((long){data.converter_retval});\n'
114115
)
115116

116117

@@ -124,7 +125,8 @@ def render(self, function: Function, data: CRenderData) -> None:
124125
self.declare(data)
125126
self.err_occurred_if(f"{data.converter_retval} == {self.unsigned_cast}-1", data)
126127
data.return_conversion.append(
127-
f'return_value = {self.conversion_fn}({self.cast}{data.converter_retval});\n'
128+
f'{data.parser_retval} = '
129+
f'{self.conversion_fn}({self.cast}{data.converter_retval});\n'
128130
)
129131

130132

@@ -164,7 +166,8 @@ def render(self, function: Function, data: CRenderData) -> None:
164166
self.declare(data)
165167
self.err_occurred_if(f"{data.converter_retval} == -1.0", data)
166168
data.return_conversion.append(
167-
f'return_value = PyFloat_FromDouble({self.cast}{data.converter_retval});\n'
169+
f'{data.parser_retval} = '
170+
f'PyFloat_FromDouble({self.cast}{data.converter_retval});\n'
168171
)
169172

170173

0 commit comments

Comments
 (0)