Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-64660: Don't hardcode Argument Clinic return converter result variable name #104200

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def __init__(self):
# you should check the _return_value for errors, and
# "goto exit" if there are any.
self.return_conversion = []
self.converter_retval = "_return_value"

# The C statements required to do some operations
# after the end of parsing but before cleaning up.
Expand Down Expand Up @@ -3893,15 +3894,15 @@ def __init__(self, *, py_default=None, **kwargs):
def return_converter_init(self):
pass

def declare(self, data, name="_return_value"):
def declare(self, data):
line = []
add = line.append
add(self.type)
if not self.type.endswith('*'):
add(' ')
add(name + ';')
add(data.converter_retval + ';')
data.declarations.append(''.join(line))
data.return_value = name
data.return_value = data.converter_retval

def err_occurred_if(self, expr, data):
data.return_conversion.append('if (({}) && PyErr_Occurred()) {{\n goto exit;\n}}\n'.format(expr))
Expand All @@ -3923,8 +3924,10 @@ class bool_return_converter(CReturnConverter):

def render(self, function, data):
self.declare(data)
self.err_occurred_if("_return_value == -1", data)
data.return_conversion.append('return_value = PyBool_FromLong((long)_return_value);\n')
self.err_occurred_if(f"{data.converter_retval} == -1", data)
data.return_conversion.append(
f'return_value = PyBool_FromLong((long){data.converter_retval});\n'
)

class long_return_converter(CReturnConverter):
type = 'long'
Expand All @@ -3934,9 +3937,10 @@ class long_return_converter(CReturnConverter):

def render(self, function, data):
self.declare(data)
self.err_occurred_if("_return_value == {}-1".format(self.unsigned_cast), data)
self.err_occurred_if(f"{data.converter_retval} == {self.unsigned_cast}-1", data)
data.return_conversion.append(
''.join(('return_value = ', self.conversion_fn, '(', self.cast, '_return_value);\n')))
f'return_value = {self.conversion_fn}({self.cast}{data.converter_retval});\n'
)

class int_return_converter(long_return_converter):
type = 'int'
Expand Down Expand Up @@ -3978,9 +3982,10 @@ class double_return_converter(CReturnConverter):

def render(self, function, data):
self.declare(data)
self.err_occurred_if("_return_value == -1.0", data)
self.err_occurred_if(f"{data.converter_retval} == -1.0", data)
data.return_conversion.append(
'return_value = PyFloat_FromDouble(' + self.cast + '_return_value);\n')
f'return_value = PyFloat_FromDouble({self.cast}{data.converter_retval});\n'
)

class float_return_converter(double_return_converter):
type = 'float'
Expand Down