bpo-37942: Improve argument clinic float converter (GH-15470)

This commit is contained in:
Raymond Hettinger 2019-08-24 19:10:39 -07:00 committed by GitHub
parent 805f8f9afe
commit aef9ad82f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 317 additions and 112 deletions

View file

@ -1587,10 +1587,16 @@ test_float_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 1) {
goto skip_optional;
}
if (PyFloat_CheckExact(args[0])) {
a = (float) (PyFloat_AS_DOUBLE(args[0]));
}
else
{
a = (float) PyFloat_AsDouble(args[0]);
if (PyErr_Occurred()) {
if (a == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
skip_optional:
return_value = test_float_converter_impl(module, a);
@ -1600,7 +1606,7 @@ exit:
static PyObject *
test_float_converter_impl(PyObject *module, float a)
/*[clinic end generated code: output=8293566b2ec1fc52 input=259c0d98eca35034]*/
/*[clinic end generated code: output=6b9c7443d2601cea input=259c0d98eca35034]*/
/*[clinic input]
@ -1634,10 +1640,16 @@ test_double_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 1) {
goto skip_optional;
}
if (PyFloat_CheckExact(args[0])) {
a = PyFloat_AS_DOUBLE(args[0]);
}
else
{
a = PyFloat_AsDouble(args[0]);
if (PyErr_Occurred()) {
if (a == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
skip_optional:
return_value = test_double_converter_impl(module, a);
@ -1647,7 +1659,7 @@ exit:
static PyObject *
test_double_converter_impl(PyObject *module, double a)
/*[clinic end generated code: output=487081a9b8da67ab input=c6a9945706a41c27]*/
/*[clinic end generated code: output=5b7b9a0f0791b2cc input=c6a9945706a41c27]*/
/*[clinic input]

View file

@ -0,0 +1 @@
Improve ArgumentClinic converter for floats.

View file

@ -1093,10 +1093,16 @@ _ssl_RAND_add(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
goto exit;
}
}
if (PyFloat_CheckExact(args[1])) {
entropy = PyFloat_AS_DOUBLE(args[1]);
}
else
{
entropy = PyFloat_AsDouble(args[1]);
if (PyErr_Occurred()) {
if (entropy == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = _ssl_RAND_add_impl(module, &view, entropy);
exit:
@ -1476,4 +1482,4 @@ exit:
#ifndef _SSL_ENUM_CRLS_METHODDEF
#define _SSL_ENUM_CRLS_METHODDEF
#endif /* !defined(_SSL_ENUM_CRLS_METHODDEF) */
/*[clinic end generated code: output=a399d0eb393b6fab input=a9049054013a1b77]*/
/*[clinic end generated code: output=5003112e167cd948 input=a9049054013a1b77]*/

View file

@ -26,18 +26,36 @@ _statistics__normal_dist_inv_cdf(PyObject *module, PyObject *const *args, Py_ssi
if (!_PyArg_CheckPositional("_normal_dist_inv_cdf", nargs, 3, 3)) {
goto exit;
}
if (PyFloat_CheckExact(args[0])) {
p = PyFloat_AS_DOUBLE(args[0]);
}
else
{
p = PyFloat_AsDouble(args[0]);
if (PyErr_Occurred()) {
if (p == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
if (PyFloat_CheckExact(args[1])) {
mu = PyFloat_AS_DOUBLE(args[1]);
}
else
{
mu = PyFloat_AsDouble(args[1]);
if (PyErr_Occurred()) {
if (mu == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
if (PyFloat_CheckExact(args[2])) {
sigma = PyFloat_AS_DOUBLE(args[2]);
}
else
{
sigma = PyFloat_AsDouble(args[2]);
if (PyErr_Occurred()) {
if (sigma == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
_return_value = _statistics__normal_dist_inv_cdf_impl(module, p, mu, sigma);
if ((_return_value == -1.0) && PyErr_Occurred()) {
goto exit;
@ -47,4 +65,4 @@ _statistics__normal_dist_inv_cdf(PyObject *module, PyObject *const *args, Py_ssi
exit:
return return_value;
}
/*[clinic end generated code: output=ba6af124acd34732 input=a9049054013a1b77]*/
/*[clinic end generated code: output=c5826928a238326c input=a9049054013a1b77]*/

View file

@ -615,10 +615,16 @@ audioop_mul(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (width == -1 && PyErr_Occurred()) {
goto exit;
}
if (PyFloat_CheckExact(args[2])) {
factor = PyFloat_AS_DOUBLE(args[2]);
}
else
{
factor = PyFloat_AsDouble(args[2]);
if (PyErr_Occurred()) {
if (factor == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = audioop_mul_impl(module, &fragment, width, factor);
exit:
@ -671,14 +677,26 @@ audioop_tomono(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (width == -1 && PyErr_Occurred()) {
goto exit;
}
if (PyFloat_CheckExact(args[2])) {
lfactor = PyFloat_AS_DOUBLE(args[2]);
}
else
{
lfactor = PyFloat_AsDouble(args[2]);
if (PyErr_Occurred()) {
if (lfactor == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
if (PyFloat_CheckExact(args[3])) {
rfactor = PyFloat_AS_DOUBLE(args[3]);
}
else
{
rfactor = PyFloat_AsDouble(args[3]);
if (PyErr_Occurred()) {
if (rfactor == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = audioop_tomono_impl(module, &fragment, width, lfactor, rfactor);
exit:
@ -731,14 +749,26 @@ audioop_tostereo(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (width == -1 && PyErr_Occurred()) {
goto exit;
}
if (PyFloat_CheckExact(args[2])) {
lfactor = PyFloat_AS_DOUBLE(args[2]);
}
else
{
lfactor = PyFloat_AsDouble(args[2]);
if (PyErr_Occurred()) {
if (lfactor == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
if (PyFloat_CheckExact(args[3])) {
rfactor = PyFloat_AS_DOUBLE(args[3]);
}
else
{
rfactor = PyFloat_AsDouble(args[3]);
if (PyErr_Occurred()) {
if (rfactor == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = audioop_tostereo_impl(module, &fragment, width, lfactor, rfactor);
exit:
@ -1439,4 +1469,4 @@ exit:
return return_value;
}
/*[clinic end generated code: output=2b173a25726252e9 input=a9049054013a1b77]*/
/*[clinic end generated code: output=af32f4bce9c934fa input=a9049054013a1b77]*/

View file

@ -766,14 +766,26 @@ cmath_rect(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("rect", nargs, 2, 2)) {
goto exit;
}
if (PyFloat_CheckExact(args[0])) {
r = PyFloat_AS_DOUBLE(args[0]);
}
else
{
r = PyFloat_AsDouble(args[0]);
if (PyErr_Occurred()) {
if (r == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
if (PyFloat_CheckExact(args[1])) {
phi = PyFloat_AS_DOUBLE(args[1]);
}
else
{
phi = PyFloat_AsDouble(args[1]);
if (PyErr_Occurred()) {
if (phi == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = cmath_rect_impl(module, r, phi);
exit:
@ -922,18 +934,30 @@ cmath_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec
goto skip_optional_kwonly;
}
if (args[2]) {
if (PyFloat_CheckExact(args[2])) {
rel_tol = PyFloat_AS_DOUBLE(args[2]);
}
else
{
rel_tol = PyFloat_AsDouble(args[2]);
if (PyErr_Occurred()) {
if (rel_tol == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
if (!--noptargs) {
goto skip_optional_kwonly;
}
}
if (PyFloat_CheckExact(args[3])) {
abs_tol = PyFloat_AS_DOUBLE(args[3]);
}
else
{
abs_tol = PyFloat_AsDouble(args[3]);
if (PyErr_Occurred()) {
if (abs_tol == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
skip_optional_kwonly:
_return_value = cmath_isclose_impl(module, a, b, rel_tol, abs_tol);
if ((_return_value == -1) && PyErr_Occurred()) {
@ -944,4 +968,4 @@ skip_optional_kwonly:
exit:
return return_value;
}
/*[clinic end generated code: output=c7afb866e593fa45 input=a9049054013a1b77]*/
/*[clinic end generated code: output=3ab228947d1709cc input=a9049054013a1b77]*/

View file

@ -117,10 +117,16 @@ math_frexp(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
double x;
if (PyFloat_CheckExact(arg)) {
x = PyFloat_AS_DOUBLE(arg);
}
else
{
x = PyFloat_AsDouble(arg);
if (PyErr_Occurred()) {
if (x == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = math_frexp_impl(module, x);
exit:
@ -151,10 +157,16 @@ math_ldexp(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("ldexp", nargs, 2, 2)) {
goto exit;
}
if (PyFloat_CheckExact(args[0])) {
x = PyFloat_AS_DOUBLE(args[0]);
}
else
{
x = PyFloat_AsDouble(args[0]);
if (PyErr_Occurred()) {
if (x == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
i = args[1];
return_value = math_ldexp_impl(module, x, i);
@ -182,10 +194,16 @@ math_modf(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
double x;
if (PyFloat_CheckExact(arg)) {
x = PyFloat_AS_DOUBLE(arg);
}
else
{
x = PyFloat_AsDouble(arg);
if (PyErr_Occurred()) {
if (x == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = math_modf_impl(module, x);
exit:
@ -277,14 +295,26 @@ math_fmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("fmod", nargs, 2, 2)) {
goto exit;
}
if (PyFloat_CheckExact(args[0])) {
x = PyFloat_AS_DOUBLE(args[0]);
}
else
{
x = PyFloat_AsDouble(args[0]);
if (PyErr_Occurred()) {
if (x == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
if (PyFloat_CheckExact(args[1])) {
y = PyFloat_AS_DOUBLE(args[1]);
}
else
{
y = PyFloat_AsDouble(args[1]);
if (PyErr_Occurred()) {
if (y == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = math_fmod_impl(module, x, y);
exit:
@ -349,14 +379,26 @@ math_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("pow", nargs, 2, 2)) {
goto exit;
}
if (PyFloat_CheckExact(args[0])) {
x = PyFloat_AS_DOUBLE(args[0]);
}
else
{
x = PyFloat_AsDouble(args[0]);
if (PyErr_Occurred()) {
if (x == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
if (PyFloat_CheckExact(args[1])) {
y = PyFloat_AS_DOUBLE(args[1]);
}
else
{
y = PyFloat_AsDouble(args[1]);
if (PyErr_Occurred()) {
if (y == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = math_pow_impl(module, x, y);
exit:
@ -381,10 +423,16 @@ math_degrees(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
double x;
if (PyFloat_CheckExact(arg)) {
x = PyFloat_AS_DOUBLE(arg);
}
else
{
x = PyFloat_AsDouble(arg);
if (PyErr_Occurred()) {
if (x == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = math_degrees_impl(module, x);
exit:
@ -409,10 +457,16 @@ math_radians(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
double x;
if (PyFloat_CheckExact(arg)) {
x = PyFloat_AS_DOUBLE(arg);
}
else
{
x = PyFloat_AsDouble(arg);
if (PyErr_Occurred()) {
if (x == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = math_radians_impl(module, x);
exit:
@ -437,10 +491,16 @@ math_isfinite(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
double x;
if (PyFloat_CheckExact(arg)) {
x = PyFloat_AS_DOUBLE(arg);
}
else
{
x = PyFloat_AsDouble(arg);
if (PyErr_Occurred()) {
if (x == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = math_isfinite_impl(module, x);
exit:
@ -465,10 +525,16 @@ math_isnan(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
double x;
if (PyFloat_CheckExact(arg)) {
x = PyFloat_AS_DOUBLE(arg);
}
else
{
x = PyFloat_AsDouble(arg);
if (PyErr_Occurred()) {
if (x == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = math_isnan_impl(module, x);
exit:
@ -493,10 +559,16 @@ math_isinf(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
double x;
if (PyFloat_CheckExact(arg)) {
x = PyFloat_AS_DOUBLE(arg);
}
else
{
x = PyFloat_AsDouble(arg);
if (PyErr_Occurred()) {
if (x == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = math_isinf_impl(module, x);
exit:
@ -550,30 +622,54 @@ math_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject
if (!args) {
goto exit;
}
if (PyFloat_CheckExact(args[0])) {
a = PyFloat_AS_DOUBLE(args[0]);
}
else
{
a = PyFloat_AsDouble(args[0]);
if (PyErr_Occurred()) {
if (a == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
if (PyFloat_CheckExact(args[1])) {
b = PyFloat_AS_DOUBLE(args[1]);
}
else
{
b = PyFloat_AsDouble(args[1]);
if (PyErr_Occurred()) {
if (b == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
if (!noptargs) {
goto skip_optional_kwonly;
}
if (args[2]) {
if (PyFloat_CheckExact(args[2])) {
rel_tol = PyFloat_AS_DOUBLE(args[2]);
}
else
{
rel_tol = PyFloat_AsDouble(args[2]);
if (PyErr_Occurred()) {
if (rel_tol == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
if (!--noptargs) {
goto skip_optional_kwonly;
}
}
if (PyFloat_CheckExact(args[3])) {
abs_tol = PyFloat_AS_DOUBLE(args[3]);
}
else
{
abs_tol = PyFloat_AsDouble(args[3]);
if (PyErr_Occurred()) {
if (abs_tol == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
skip_optional_kwonly:
_return_value = math_isclose_impl(module, a, b, rel_tol, abs_tol);
if ((_return_value == -1) && PyErr_Occurred()) {
@ -712,4 +808,4 @@ math_comb(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
exit:
return return_value;
}
/*[clinic end generated code: output=f93cfe13ab2fdb4e input=a9049054013a1b77]*/
/*[clinic end generated code: output=9a2b3dc91eb9aadd input=a9049054013a1b77]*/

View file

@ -306,10 +306,16 @@ sys_setswitchinterval(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
double interval;
if (PyFloat_CheckExact(arg)) {
interval = PyFloat_AS_DOUBLE(arg);
}
else
{
interval = PyFloat_AsDouble(arg);
if (PyErr_Occurred()) {
if (interval == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
return_value = sys_setswitchinterval_impl(module, interval);
exit:
@ -989,4 +995,4 @@ sys_getandroidapilevel(PyObject *module, PyObject *Py_UNUSED(ignored))
#ifndef SYS_GETANDROIDAPILEVEL_METHODDEF
#define SYS_GETANDROIDAPILEVEL_METHODDEF
#endif /* !defined(SYS_GETANDROIDAPILEVEL_METHODDEF) */
/*[clinic end generated code: output=acef77d2bb8f6da9 input=a9049054013a1b77]*/
/*[clinic end generated code: output=b26faa0abdd700da input=a9049054013a1b77]*/

View file

@ -887,7 +887,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
case 'f': {/* float */
float *p = va_arg(*p_va, float *);
double dval = PyFloat_AsDouble(arg);
if (PyErr_Occurred())
if (dval == -1.0 && PyErr_Occurred())
RETURN_ERR_OCCURRED;
else
*p = (float) dval;
@ -897,7 +897,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
case 'd': {/* double */
double *p = va_arg(*p_va, double *);
double dval = PyFloat_AsDouble(arg);
if (PyErr_Occurred())
if (dval == -1.0 && PyErr_Occurred())
RETURN_ERR_OCCURRED;
else
*p = dval;

View file

@ -3106,10 +3106,16 @@ class float_converter(CConverter):
def parse_arg(self, argname, argnum):
if self.format_unit == 'f':
return """
if (PyFloat_CheckExact({argname})) {{{{
{paramname} = (float) (PyFloat_AS_DOUBLE({argname}));
}}}}
else
{{{{
{paramname} = (float) PyFloat_AsDouble({argname});
if (PyErr_Occurred()) {{{{
if ({paramname} == -1.0 && PyErr_Occurred()) {{{{
goto exit;
}}}}
}}}}
""".format(argname=argname, paramname=self.name)
return super().parse_arg(argname, argnum)
@ -3122,10 +3128,16 @@ class double_converter(CConverter):
def parse_arg(self, argname, argnum):
if self.format_unit == 'd':
return """
if (PyFloat_CheckExact({argname})) {{{{
{paramname} = PyFloat_AS_DOUBLE({argname});
}}}}
else
{{{{
{paramname} = PyFloat_AsDouble({argname});
if (PyErr_Occurred()) {{{{
if ({paramname} == -1.0 && PyErr_Occurred()) {{{{
goto exit;
}}}}
}}}}
""".format(argname=argname, paramname=self.name)
return super().parse_arg(argname, argnum)