Skip to content

Commit 729bfb3

Browse files
authored
gh-116417: Avoid PyFloat_AS_DOUBLE() in AC limited C API (#116568)
Argument Clinic no longer calls PyFloat_AS_DOUBLE() when the usage of the limited C API is requested.
1 parent 5b2f21f commit 729bfb3

File tree

3 files changed

+155
-19
lines changed

3 files changed

+155
-19
lines changed

Modules/_testclinic_limited.c

+36
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,46 @@ my_int_sum_impl(PyObject *module, int x, int y)
7272
}
7373

7474

75+
/*[clinic input]
76+
my_float_sum -> float
77+
78+
x: float
79+
y: float
80+
/
81+
82+
[clinic start generated code]*/
83+
84+
static float
85+
my_float_sum_impl(PyObject *module, float x, float y)
86+
/*[clinic end generated code: output=634f59a5a419cad7 input=d4b5313bdf4dc377]*/
87+
{
88+
return x + y;
89+
}
90+
91+
92+
/*[clinic input]
93+
my_double_sum -> double
94+
95+
x: double
96+
y: double
97+
/
98+
99+
[clinic start generated code]*/
100+
101+
static double
102+
my_double_sum_impl(PyObject *module, double x, double y)
103+
/*[clinic end generated code: output=a75576d9e4d8557f input=16b11c8aba172801]*/
104+
{
105+
return x + y;
106+
}
107+
108+
75109
static PyMethodDef tester_methods[] = {
76110
TEST_EMPTY_FUNCTION_METHODDEF
77111
MY_INT_FUNC_METHODDEF
78112
MY_INT_SUM_METHODDEF
113+
MY_FLOAT_SUM_METHODDEF
114+
MY_DOUBLE_SUM_METHODDEF
79115
{NULL, NULL}
80116
};
81117

Modules/clinic/_testclinic_limited.c.h

+83-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tools/clinic/clinic.py

+36-18
Original file line numberDiff line numberDiff line change
@@ -3867,19 +3867,28 @@ class float_converter(CConverter):
38673867

38683868
def parse_arg(self, argname: str, displayname: str, *, limited_capi: bool) -> str | None:
38693869
if self.format_unit == 'f':
3870-
return self.format_code("""
3871-
if (PyFloat_CheckExact({argname})) {{{{
3872-
{paramname} = (float) (PyFloat_AS_DOUBLE({argname}));
3873-
}}}}
3874-
else
3875-
{{{{
3870+
if not limited_capi:
3871+
return self.format_code("""
3872+
if (PyFloat_CheckExact({argname})) {{{{
3873+
{paramname} = (float) (PyFloat_AS_DOUBLE({argname}));
3874+
}}}}
3875+
else
3876+
{{{{
3877+
{paramname} = (float) PyFloat_AsDouble({argname});
3878+
if ({paramname} == -1.0 && PyErr_Occurred()) {{{{
3879+
goto exit;
3880+
}}}}
3881+
}}}}
3882+
""",
3883+
argname=argname)
3884+
else:
3885+
return self.format_code("""
38763886
{paramname} = (float) PyFloat_AsDouble({argname});
38773887
if ({paramname} == -1.0 && PyErr_Occurred()) {{{{
38783888
goto exit;
38793889
}}}}
3880-
}}}}
3881-
""",
3882-
argname=argname)
3890+
""",
3891+
argname=argname)
38833892
return super().parse_arg(argname, displayname, limited_capi=limited_capi)
38843893

38853894
class double_converter(CConverter):
@@ -3890,19 +3899,28 @@ class double_converter(CConverter):
38903899

38913900
def parse_arg(self, argname: str, displayname: str, *, limited_capi: bool) -> str | None:
38923901
if self.format_unit == 'd':
3893-
return self.format_code("""
3894-
if (PyFloat_CheckExact({argname})) {{{{
3895-
{paramname} = PyFloat_AS_DOUBLE({argname});
3896-
}}}}
3897-
else
3898-
{{{{
3902+
if not limited_capi:
3903+
return self.format_code("""
3904+
if (PyFloat_CheckExact({argname})) {{{{
3905+
{paramname} = PyFloat_AS_DOUBLE({argname});
3906+
}}}}
3907+
else
3908+
{{{{
3909+
{paramname} = PyFloat_AsDouble({argname});
3910+
if ({paramname} == -1.0 && PyErr_Occurred()) {{{{
3911+
goto exit;
3912+
}}}}
3913+
}}}}
3914+
""",
3915+
argname=argname)
3916+
else:
3917+
return self.format_code("""
38993918
{paramname} = PyFloat_AsDouble({argname});
39003919
if ({paramname} == -1.0 && PyErr_Occurred()) {{{{
39013920
goto exit;
39023921
}}}}
3903-
}}}}
3904-
""",
3905-
argname=argname)
3922+
""",
3923+
argname=argname)
39063924
return super().parse_arg(argname, displayname, limited_capi=limited_capi)
39073925

39083926

0 commit comments

Comments
 (0)