mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
gh-101410: Customize error messages for 1-arg math functions (#129497)
This also reverts loghelper() change in 75f59bb629
for integer
input. The error message shouldn't include argument value here.
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
bf3a0a1c0f
commit
d0660a9a40
5 changed files with 42 additions and 26 deletions
|
@ -860,6 +860,13 @@ logging.handlers
|
||||||
(Contributed by Charles Machalow in :gh:`132106`.)
|
(Contributed by Charles Machalow in :gh:`132106`.)
|
||||||
|
|
||||||
|
|
||||||
|
math
|
||||||
|
----
|
||||||
|
|
||||||
|
* Added more detailed error messages for domain errors in the module.
|
||||||
|
(Contributed by by Charlie Zhao and Sergey B Kirpichev in :gh:`101410`.)
|
||||||
|
|
||||||
|
|
||||||
mimetypes
|
mimetypes
|
||||||
---------
|
---------
|
||||||
|
|
||||||
|
|
|
@ -127,31 +127,31 @@ Trigonometric Functions
|
||||||
>>> sin(INF)
|
>>> sin(INF)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: math domain error
|
ValueError: expected a finite input, got inf
|
||||||
>>> sin(NINF)
|
>>> sin(NINF)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: math domain error
|
ValueError: expected a finite input, got -inf
|
||||||
>>> sin(NAN)
|
>>> sin(NAN)
|
||||||
nan
|
nan
|
||||||
>>> cos(INF)
|
>>> cos(INF)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: math domain error
|
ValueError: expected a finite input, got inf
|
||||||
>>> cos(NINF)
|
>>> cos(NINF)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: math domain error
|
ValueError: expected a finite input, got -inf
|
||||||
>>> cos(NAN)
|
>>> cos(NAN)
|
||||||
nan
|
nan
|
||||||
>>> tan(INF)
|
>>> tan(INF)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: math domain error
|
ValueError: expected a finite input, got inf
|
||||||
>>> tan(NINF)
|
>>> tan(NINF)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: math domain error
|
ValueError: expected a finite input, got -inf
|
||||||
>>> tan(NAN)
|
>>> tan(NAN)
|
||||||
nan
|
nan
|
||||||
|
|
||||||
|
@ -169,11 +169,11 @@ True
|
||||||
>>> asin(INF), asin(NINF)
|
>>> asin(INF), asin(NINF)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: math domain error
|
ValueError: expected a number in range from -1 up to 1, got inf
|
||||||
>>> acos(INF), acos(NINF)
|
>>> acos(INF), acos(NINF)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: math domain error
|
ValueError: expected a number in range from -1 up to 1, got inf
|
||||||
>>> equal(atan(INF), PI/2), equal(atan(NINF), -PI/2)
|
>>> equal(atan(INF), PI/2), equal(atan(NINF), -PI/2)
|
||||||
(True, True)
|
(True, True)
|
||||||
|
|
||||||
|
|
|
@ -2539,7 +2539,7 @@ class MathTests(unittest.TestCase):
|
||||||
"expected a positive input$"):
|
"expected a positive input$"):
|
||||||
math.log(x)
|
math.log(x)
|
||||||
with self.assertRaisesRegex(ValueError,
|
with self.assertRaisesRegex(ValueError,
|
||||||
f"expected a float or nonnegative integer, got {x}"):
|
f"expected a noninteger or positive integer, got {x}"):
|
||||||
math.gamma(x)
|
math.gamma(x)
|
||||||
x = 1.0
|
x = 1.0
|
||||||
with self.assertRaisesRegex(ValueError,
|
with self.assertRaisesRegex(ValueError,
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Added more detailed messages for domain errors in the :mod:`math` module.
|
|
@ -1076,17 +1076,20 @@ math_2(PyObject *const *args, Py_ssize_t nargs,
|
||||||
}\
|
}\
|
||||||
PyDoc_STRVAR(math_##funcname##_doc, docstring);
|
PyDoc_STRVAR(math_##funcname##_doc, docstring);
|
||||||
|
|
||||||
FUNC1(acos, acos, 0,
|
FUNC1D(acos, acos, 0,
|
||||||
"acos($module, x, /)\n--\n\n"
|
"acos($module, x, /)\n--\n\n"
|
||||||
"Return the arc cosine (measured in radians) of x.\n\n"
|
"Return the arc cosine (measured in radians) of x.\n\n"
|
||||||
"The result is between 0 and pi.")
|
"The result is between 0 and pi.",
|
||||||
FUNC1(acosh, acosh, 0,
|
"expected a number in range from -1 up to 1, got %s")
|
||||||
|
FUNC1D(acosh, acosh, 0,
|
||||||
"acosh($module, x, /)\n--\n\n"
|
"acosh($module, x, /)\n--\n\n"
|
||||||
"Return the inverse hyperbolic cosine of x.")
|
"Return the inverse hyperbolic cosine of x.",
|
||||||
FUNC1(asin, asin, 0,
|
"expected argument value not less than 1, got %s")
|
||||||
|
FUNC1D(asin, asin, 0,
|
||||||
"asin($module, x, /)\n--\n\n"
|
"asin($module, x, /)\n--\n\n"
|
||||||
"Return the arc sine (measured in radians) of x.\n\n"
|
"Return the arc sine (measured in radians) of x.\n\n"
|
||||||
"The result is between -pi/2 and pi/2.")
|
"The result is between -pi/2 and pi/2.",
|
||||||
|
"expected a number in range from -1 up to 1, got %s")
|
||||||
FUNC1(asinh, asinh, 0,
|
FUNC1(asinh, asinh, 0,
|
||||||
"asinh($module, x, /)\n--\n\n"
|
"asinh($module, x, /)\n--\n\n"
|
||||||
"Return the inverse hyperbolic sine of x.")
|
"Return the inverse hyperbolic sine of x.")
|
||||||
|
@ -1147,9 +1150,10 @@ FUNC2(copysign, copysign,
|
||||||
"Return a float with the magnitude (absolute value) of x but the sign of y.\n\n"
|
"Return a float with the magnitude (absolute value) of x but the sign of y.\n\n"
|
||||||
"On platforms that support signed zeros, copysign(1.0, -0.0)\n"
|
"On platforms that support signed zeros, copysign(1.0, -0.0)\n"
|
||||||
"returns -1.0.\n")
|
"returns -1.0.\n")
|
||||||
FUNC1(cos, cos, 0,
|
FUNC1D(cos, cos, 0,
|
||||||
"cos($module, x, /)\n--\n\n"
|
"cos($module, x, /)\n--\n\n"
|
||||||
"Return the cosine of x (measured in radians).")
|
"Return the cosine of x (measured in radians).",
|
||||||
|
"expected a finite input, got %s")
|
||||||
FUNC1(cosh, cosh, 1,
|
FUNC1(cosh, cosh, 1,
|
||||||
"cosh($module, x, /)\n--\n\n"
|
"cosh($module, x, /)\n--\n\n"
|
||||||
"Return the hyperbolic cosine of x.")
|
"Return the hyperbolic cosine of x.")
|
||||||
|
@ -1213,23 +1217,26 @@ math_floor(PyObject *module, PyObject *number)
|
||||||
FUNC1AD(gamma, m_tgamma,
|
FUNC1AD(gamma, m_tgamma,
|
||||||
"gamma($module, x, /)\n--\n\n"
|
"gamma($module, x, /)\n--\n\n"
|
||||||
"Gamma function at x.",
|
"Gamma function at x.",
|
||||||
"expected a float or nonnegative integer, got %s")
|
"expected a noninteger or positive integer, got %s")
|
||||||
FUNC1A(lgamma, m_lgamma,
|
FUNC1AD(lgamma, m_lgamma,
|
||||||
"lgamma($module, x, /)\n--\n\n"
|
"lgamma($module, x, /)\n--\n\n"
|
||||||
"Natural logarithm of absolute value of Gamma function at x.")
|
"Natural logarithm of absolute value of Gamma function at x.",
|
||||||
FUNC1(log1p, m_log1p, 0,
|
"expected a noninteger or positive integer, got %s")
|
||||||
|
FUNC1D(log1p, m_log1p, 0,
|
||||||
"log1p($module, x, /)\n--\n\n"
|
"log1p($module, x, /)\n--\n\n"
|
||||||
"Return the natural logarithm of 1+x (base e).\n\n"
|
"Return the natural logarithm of 1+x (base e).\n\n"
|
||||||
"The result is computed in a way which is accurate for x near zero.")
|
"The result is computed in a way which is accurate for x near zero.",
|
||||||
|
"expected argument value > -1, got %s")
|
||||||
FUNC2(remainder, m_remainder,
|
FUNC2(remainder, m_remainder,
|
||||||
"remainder($module, x, y, /)\n--\n\n"
|
"remainder($module, x, y, /)\n--\n\n"
|
||||||
"Difference between x and the closest integer multiple of y.\n\n"
|
"Difference between x and the closest integer multiple of y.\n\n"
|
||||||
"Return x - n*y where n*y is the closest integer multiple of y.\n"
|
"Return x - n*y where n*y is the closest integer multiple of y.\n"
|
||||||
"In the case where x is exactly halfway between two multiples of\n"
|
"In the case where x is exactly halfway between two multiples of\n"
|
||||||
"y, the nearest even value of n is used. The result is always exact.")
|
"y, the nearest even value of n is used. The result is always exact.")
|
||||||
FUNC1(sin, sin, 0,
|
FUNC1D(sin, sin, 0,
|
||||||
"sin($module, x, /)\n--\n\n"
|
"sin($module, x, /)\n--\n\n"
|
||||||
"Return the sine of x (measured in radians).")
|
"Return the sine of x (measured in radians).",
|
||||||
|
"expected a finite input, got %s")
|
||||||
FUNC1(sinh, sinh, 1,
|
FUNC1(sinh, sinh, 1,
|
||||||
"sinh($module, x, /)\n--\n\n"
|
"sinh($module, x, /)\n--\n\n"
|
||||||
"Return the hyperbolic sine of x.")
|
"Return the hyperbolic sine of x.")
|
||||||
|
@ -1237,9 +1244,10 @@ FUNC1D(sqrt, sqrt, 0,
|
||||||
"sqrt($module, x, /)\n--\n\n"
|
"sqrt($module, x, /)\n--\n\n"
|
||||||
"Return the square root of x.",
|
"Return the square root of x.",
|
||||||
"expected a nonnegative input, got %s")
|
"expected a nonnegative input, got %s")
|
||||||
FUNC1(tan, tan, 0,
|
FUNC1D(tan, tan, 0,
|
||||||
"tan($module, x, /)\n--\n\n"
|
"tan($module, x, /)\n--\n\n"
|
||||||
"Return the tangent of x (measured in radians).")
|
"Return the tangent of x (measured in radians).",
|
||||||
|
"expected a finite input, got %s")
|
||||||
FUNC1(tanh, tanh, 0,
|
FUNC1(tanh, tanh, 0,
|
||||||
"tanh($module, x, /)\n--\n\n"
|
"tanh($module, x, /)\n--\n\n"
|
||||||
"Return the hyperbolic tangent of x.")
|
"Return the hyperbolic tangent of x.")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue