Skip to content

Commit 77a643c

Browse files
committed
gh-101410: customize error messages for 1-arg math functions
This also reverts loghelper() change in 75f59bb for integer input. The error message shouldn't include argument value here.
1 parent 10ee2d9 commit 77a643c

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

Lib/test/mathdata/ieee754.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,31 +127,31 @@ Trigonometric Functions
127127
>>> sin(INF)
128128
Traceback (most recent call last):
129129
...
130-
ValueError: math domain error
130+
ValueError: expected a finite input, got inf
131131
>>> sin(NINF)
132132
Traceback (most recent call last):
133133
...
134-
ValueError: math domain error
134+
ValueError: expected a finite input, got -inf
135135
>>> sin(NAN)
136136
nan
137137
>>> cos(INF)
138138
Traceback (most recent call last):
139139
...
140-
ValueError: math domain error
140+
ValueError: expected a finite input, got inf
141141
>>> cos(NINF)
142142
Traceback (most recent call last):
143143
...
144-
ValueError: math domain error
144+
ValueError: expected a finite input, got -inf
145145
>>> cos(NAN)
146146
nan
147147
>>> tan(INF)
148148
Traceback (most recent call last):
149149
...
150-
ValueError: math domain error
150+
ValueError: expected a finite input, got inf
151151
>>> tan(NINF)
152152
Traceback (most recent call last):
153153
...
154-
ValueError: math domain error
154+
ValueError: expected a finite input, got -inf
155155
>>> tan(NAN)
156156
nan
157157

@@ -169,11 +169,11 @@ True
169169
>>> asin(INF), asin(NINF)
170170
Traceback (most recent call last):
171171
...
172-
ValueError: math domain error
172+
ValueError: expected a number in range from -1 up to 1, got inf
173173
>>> acos(INF), acos(NINF)
174174
Traceback (most recent call last):
175175
...
176-
ValueError: math domain error
176+
ValueError: expected a number in range from -1 up to 1, got inf
177177
>>> equal(atan(INF), PI/2), equal(atan(NINF), -PI/2)
178178
(True, True)
179179

Lib/test/test_math.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2533,7 +2533,7 @@ def test_exception_messages(self):
25332533
math.log(x)
25342534
x = -123
25352535
with self.assertRaisesRegex(ValueError,
2536-
f"expected a positive input, got {x}"):
2536+
f"expected a positive input"):
25372537
math.log(x)
25382538
with self.assertRaisesRegex(ValueError,
25392539
f"expected a float or nonnegative integer, got {x}"):

Modules/mathmodule.c

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,17 +1089,20 @@ math_2(PyObject *const *args, Py_ssize_t nargs,
10891089
}\
10901090
PyDoc_STRVAR(math_##funcname##_doc, docstring);
10911091

1092-
FUNC1(acos, acos, 0,
1092+
FUNC1D(acos, acos, 0,
10931093
"acos($module, x, /)\n--\n\n"
10941094
"Return the arc cosine (measured in radians) of x.\n\n"
1095-
"The result is between 0 and pi.")
1096-
FUNC1(acosh, acosh, 0,
1095+
"The result is between 0 and pi.",
1096+
"expected a number in range from -1 up to 1, got %s")
1097+
FUNC1D(acosh, acosh, 0,
10971098
"acosh($module, x, /)\n--\n\n"
1098-
"Return the inverse hyperbolic cosine of x.")
1099-
FUNC1(asin, asin, 0,
1099+
"Return the inverse hyperbolic cosine of x.",
1100+
"expected argument value not less than 1, got %s")
1101+
FUNC1D(asin, asin, 0,
11001102
"asin($module, x, /)\n--\n\n"
11011103
"Return the arc sine (measured in radians) of x.\n\n"
1102-
"The result is between -pi/2 and pi/2.")
1104+
"The result is between -pi/2 and pi/2.",
1105+
"expected a number in range from -1 up to 1, got %s")
11031106
FUNC1(asinh, asinh, 0,
11041107
"asinh($module, x, /)\n--\n\n"
11051108
"Return the inverse hyperbolic sine of x.")
@@ -1161,9 +1164,10 @@ FUNC2(copysign, copysign,
11611164
"Return a float with the magnitude (absolute value) of x but the sign of y.\n\n"
11621165
"On platforms that support signed zeros, copysign(1.0, -0.0)\n"
11631166
"returns -1.0.\n")
1164-
FUNC1(cos, cos, 0,
1167+
FUNC1D(cos, cos, 0,
11651168
"cos($module, x, /)\n--\n\n"
1166-
"Return the cosine of x (measured in radians).")
1169+
"Return the cosine of x (measured in radians).",
1170+
"expected a finite input, got %s")
11671171
FUNC1(cosh, cosh, 1,
11681172
"cosh($module, x, /)\n--\n\n"
11691173
"Return the hyperbolic cosine of x.")
@@ -1229,32 +1233,36 @@ FUNC1AD(gamma, m_tgamma,
12291233
"gamma($module, x, /)\n--\n\n"
12301234
"Gamma function at x.",
12311235
"expected a float or nonnegative integer, got %s")
1232-
FUNC1A(lgamma, m_lgamma,
1236+
FUNC1AD(lgamma, m_lgamma,
12331237
"lgamma($module, x, /)\n--\n\n"
1234-
"Natural logarithm of absolute value of Gamma function at x.")
1235-
FUNC1(log1p, m_log1p, 0,
1238+
"Natural logarithm of absolute value of Gamma function at x.",
1239+
"expected a float or nonnegative integer, got %s")
1240+
FUNC1D(log1p, m_log1p, 0,
12361241
"log1p($module, x, /)\n--\n\n"
12371242
"Return the natural logarithm of 1+x (base e).\n\n"
1238-
"The result is computed in a way which is accurate for x near zero.")
1243+
"The result is computed in a way which is accurate for x near zero.",
1244+
"expected argument value > -1, got %s")
12391245
FUNC2(remainder, m_remainder,
12401246
"remainder($module, x, y, /)\n--\n\n"
12411247
"Difference between x and the closest integer multiple of y.\n\n"
12421248
"Return x - n*y where n*y is the closest integer multiple of y.\n"
12431249
"In the case where x is exactly halfway between two multiples of\n"
12441250
"y, the nearest even value of n is used. The result is always exact.")
1245-
FUNC1(sin, sin, 0,
1251+
FUNC1D(sin, sin, 0,
12461252
"sin($module, x, /)\n--\n\n"
1247-
"Return the sine of x (measured in radians).")
1253+
"Return the sine of x (measured in radians).",
1254+
"expected a finite input, got %s")
12481255
FUNC1(sinh, sinh, 1,
12491256
"sinh($module, x, /)\n--\n\n"
12501257
"Return the hyperbolic sine of x.")
12511258
FUNC1D(sqrt, sqrt, 0,
12521259
"sqrt($module, x, /)\n--\n\n"
12531260
"Return the square root of x.",
12541261
"expected a nonnegative input, got %s")
1255-
FUNC1(tan, tan, 0,
1262+
FUNC1D(tan, tan, 0,
12561263
"tan($module, x, /)\n--\n\n"
1257-
"Return the tangent of x (measured in radians).")
1264+
"Return the tangent of x (measured in radians).",
1265+
"expected a finite input, got %s")
12581266
FUNC1(tanh, tanh, 0,
12591267
"tanh($module, x, /)\n--\n\n"
12601268
"Return the hyperbolic tangent of x.")
@@ -2232,8 +2240,10 @@ loghelper(PyObject* arg, double (*func)(double))
22322240

22332241
/* Negative or zero inputs give a ValueError. */
22342242
if (!_PyLong_IsPositive((PyLongObject *)arg)) {
2235-
PyErr_Format(PyExc_ValueError,
2236-
"expected a positive input, got %S", arg);
2243+
/* The input can be an arbitrary large integer, so we
2244+
don't include it's value in the error message. */
2245+
PyErr_SetString(PyExc_ValueError,
2246+
"expected a positive input");
22372247
return NULL;
22382248
}
22392249

0 commit comments

Comments
 (0)