Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d0660a9

Browse filesBrowse files
skirpichevvstinner
andauthored
gh-101410: Customize error messages for 1-arg math functions (#129497)
This also reverts loghelper() change in 75f59bb for integer input. The error message shouldn't include argument value here. Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent bf3a0a1 commit d0660a9
Copy full SHA for d0660a9

File tree

Expand file treeCollapse file tree

5 files changed

+42
-26
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+42
-26
lines changed

‎Doc/whatsnew/3.14.rst

Copy file name to clipboardExpand all lines: Doc/whatsnew/3.14.rst
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,13 @@ logging.handlers
860860
(Contributed by Charles Machalow in :gh:`132106`.)
861861

862862

863+
math
864+
----
865+
866+
* Added more detailed error messages for domain errors in the module.
867+
(Contributed by by Charlie Zhao and Sergey B Kirpichev in :gh:`101410`.)
868+
869+
863870
mimetypes
864871
---------
865872

‎Lib/test/mathdata/ieee754.txt

Copy file name to clipboardExpand all lines: Lib/test/mathdata/ieee754.txt
+8-8Lines 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

Copy file name to clipboardExpand all lines: Lib/test/test_math.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2539,7 +2539,7 @@ def test_exception_messages(self):
25392539
"expected a positive input$"):
25402540
math.log(x)
25412541
with self.assertRaisesRegex(ValueError,
2542-
f"expected a float or nonnegative integer, got {x}"):
2542+
f"expected a noninteger or positive integer, got {x}"):
25432543
math.gamma(x)
25442544
x = 1.0
25452545
with self.assertRaisesRegex(ValueError,
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added more detailed messages for domain errors in the :mod:`math` module.

‎Modules/mathmodule.c

Copy file name to clipboardExpand all lines: Modules/mathmodule.c
+25-17Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,17 +1076,20 @@ math_2(PyObject *const *args, Py_ssize_t nargs,
10761076
}\
10771077
PyDoc_STRVAR(math_##funcname##_doc, docstring);
10781078

1079-
FUNC1(acos, acos, 0,
1079+
FUNC1D(acos, acos, 0,
10801080
"acos($module, x, /)\n--\n\n"
10811081
"Return the arc cosine (measured in radians) of x.\n\n"
1082-
"The result is between 0 and pi.")
1083-
FUNC1(acosh, acosh, 0,
1082+
"The result is between 0 and pi.",
1083+
"expected a number in range from -1 up to 1, got %s")
1084+
FUNC1D(acosh, acosh, 0,
10841085
"acosh($module, x, /)\n--\n\n"
1085-
"Return the inverse hyperbolic cosine of x.")
1086-
FUNC1(asin, asin, 0,
1086+
"Return the inverse hyperbolic cosine of x.",
1087+
"expected argument value not less than 1, got %s")
1088+
FUNC1D(asin, asin, 0,
10871089
"asin($module, x, /)\n--\n\n"
10881090
"Return the arc sine (measured in radians) of x.\n\n"
1089-
"The result is between -pi/2 and pi/2.")
1091+
"The result is between -pi/2 and pi/2.",
1092+
"expected a number in range from -1 up to 1, got %s")
10901093
FUNC1(asinh, asinh, 0,
10911094
"asinh($module, x, /)\n--\n\n"
10921095
"Return the inverse hyperbolic sine of x.")
@@ -1147,9 +1150,10 @@ FUNC2(copysign, copysign,
11471150
"Return a float with the magnitude (absolute value) of x but the sign of y.\n\n"
11481151
"On platforms that support signed zeros, copysign(1.0, -0.0)\n"
11491152
"returns -1.0.\n")
1150-
FUNC1(cos, cos, 0,
1153+
FUNC1D(cos, cos, 0,
11511154
"cos($module, x, /)\n--\n\n"
1152-
"Return the cosine of x (measured in radians).")
1155+
"Return the cosine of x (measured in radians).",
1156+
"expected a finite input, got %s")
11531157
FUNC1(cosh, cosh, 1,
11541158
"cosh($module, x, /)\n--\n\n"
11551159
"Return the hyperbolic cosine of x.")
@@ -1213,33 +1217,37 @@ math_floor(PyObject *module, PyObject *number)
12131217
FUNC1AD(gamma, m_tgamma,
12141218
"gamma($module, x, /)\n--\n\n"
12151219
"Gamma function at x.",
1216-
"expected a float or nonnegative integer, got %s")
1217-
FUNC1A(lgamma, m_lgamma,
1220+
"expected a noninteger or positive integer, got %s")
1221+
FUNC1AD(lgamma, m_lgamma,
12181222
"lgamma($module, x, /)\n--\n\n"
1219-
"Natural logarithm of absolute value of Gamma function at x.")
1220-
FUNC1(log1p, m_log1p, 0,
1223+
"Natural logarithm of absolute value of Gamma function at x.",
1224+
"expected a noninteger or positive integer, got %s")
1225+
FUNC1D(log1p, m_log1p, 0,
12211226
"log1p($module, x, /)\n--\n\n"
12221227
"Return the natural logarithm of 1+x (base e).\n\n"
1223-
"The result is computed in a way which is accurate for x near zero.")
1228+
"The result is computed in a way which is accurate for x near zero.",
1229+
"expected argument value > -1, got %s")
12241230
FUNC2(remainder, m_remainder,
12251231
"remainder($module, x, y, /)\n--\n\n"
12261232
"Difference between x and the closest integer multiple of y.\n\n"
12271233
"Return x - n*y where n*y is the closest integer multiple of y.\n"
12281234
"In the case where x is exactly halfway between two multiples of\n"
12291235
"y, the nearest even value of n is used. The result is always exact.")
1230-
FUNC1(sin, sin, 0,
1236+
FUNC1D(sin, sin, 0,
12311237
"sin($module, x, /)\n--\n\n"
1232-
"Return the sine of x (measured in radians).")
1238+
"Return the sine of x (measured in radians).",
1239+
"expected a finite input, got %s")
12331240
FUNC1(sinh, sinh, 1,
12341241
"sinh($module, x, /)\n--\n\n"
12351242
"Return the hyperbolic sine of x.")
12361243
FUNC1D(sqrt, sqrt, 0,
12371244
"sqrt($module, x, /)\n--\n\n"
12381245
"Return the square root of x.",
12391246
"expected a nonnegative input, got %s")
1240-
FUNC1(tan, tan, 0,
1247+
FUNC1D(tan, tan, 0,
12411248
"tan($module, x, /)\n--\n\n"
1242-
"Return the tangent of x (measured in radians).")
1249+
"Return the tangent of x (measured in radians).",
1250+
"expected a finite input, got %s")
12431251
FUNC1(tanh, tanh, 0,
12441252
"tanh($module, x, /)\n--\n\n"
12451253
"Return the hyperbolic tangent of x.")

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.