Skip to content

gh-116417: Avoid PyFloat_AS_DOUBLE() in AC limited C API #116568

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

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
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
36 changes: 36 additions & 0 deletions Modules/_testclinic_limited.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,46 @@ my_int_sum_impl(PyObject *module, int x, int y)
}


/*[clinic input]
my_float_sum -> float

x: float
y: float
/

[clinic start generated code]*/

static float
my_float_sum_impl(PyObject *module, float x, float y)
/*[clinic end generated code: output=634f59a5a419cad7 input=d4b5313bdf4dc377]*/
{
return x + y;
}


/*[clinic input]
my_double_sum -> double

x: double
y: double
/

[clinic start generated code]*/

static double
my_double_sum_impl(PyObject *module, double x, double y)
/*[clinic end generated code: output=a75576d9e4d8557f input=16b11c8aba172801]*/
{
return x + y;
}


static PyMethodDef tester_methods[] = {
TEST_EMPTY_FUNCTION_METHODDEF
MY_INT_FUNC_METHODDEF
MY_INT_SUM_METHODDEF
MY_FLOAT_SUM_METHODDEF
MY_DOUBLE_SUM_METHODDEF
{NULL, NULL}
};

Expand Down
84 changes: 83 additions & 1 deletion Modules/clinic/_testclinic_limited.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 36 additions & 18 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3867,19 +3867,28 @@ class float_converter(CConverter):

def parse_arg(self, argname: str, displayname: str, *, limited_capi: bool) -> str | None:
if self.format_unit == 'f':
return self.format_code("""
if (PyFloat_CheckExact({argname})) {{{{
{paramname} = (float) (PyFloat_AS_DOUBLE({argname}));
}}}}
else
{{{{
if not limited_capi:
return self.format_code("""
if (PyFloat_CheckExact({argname})) {{{{
{paramname} = (float) (PyFloat_AS_DOUBLE({argname}));
}}}}
else
{{{{
{paramname} = (float) PyFloat_AsDouble({argname});
if ({paramname} == -1.0 && PyErr_Occurred()) {{{{
goto exit;
}}}}
}}}}
""",
argname=argname)
else:
return self.format_code("""
{paramname} = (float) PyFloat_AsDouble({argname});
if ({paramname} == -1.0 && PyErr_Occurred()) {{{{
goto exit;
}}}}
}}}}
""",
argname=argname)
""",
argname=argname)
return super().parse_arg(argname, displayname, limited_capi=limited_capi)

class double_converter(CConverter):
Expand All @@ -3890,19 +3899,28 @@ class double_converter(CConverter):

def parse_arg(self, argname: str, displayname: str, *, limited_capi: bool) -> str | None:
if self.format_unit == 'd':
return self.format_code("""
if (PyFloat_CheckExact({argname})) {{{{
{paramname} = PyFloat_AS_DOUBLE({argname});
}}}}
else
{{{{
if not limited_capi:
return self.format_code("""
if (PyFloat_CheckExact({argname})) {{{{
{paramname} = PyFloat_AS_DOUBLE({argname});
}}}}
else
{{{{
{paramname} = PyFloat_AsDouble({argname});
if ({paramname} == -1.0 && PyErr_Occurred()) {{{{
goto exit;
}}}}
}}}}
""",
argname=argname)
else:
return self.format_code("""
{paramname} = PyFloat_AsDouble({argname});
if ({paramname} == -1.0 && PyErr_Occurred()) {{{{
goto exit;
}}}}
}}}}
""",
argname=argname)
""",
argname=argname)
return super().parse_arg(argname, displayname, limited_capi=limited_capi)


Expand Down