Skip to content

Commit 380cb8d

Browse files
authored
Update math error messages for 3.14 (2) (python#18949)
Followup to python#18534 Some more error messages for math functions were changed for Python 3.14, see python/cpython#129497. Fixes `mypyc/test/test_run.py::TestRun::run-math.test::testMathOps`
1 parent c274971 commit 380cb8d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

mypyc/lib-rt/float_ops.c

+21
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ static double CPy_MathExpectedPositiveInputError(double x) {
3434
return CPY_FLOAT_ERROR;
3535
}
3636

37+
static double CPy_MathExpectedFiniteInput(double x) {
38+
char *buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
39+
if (buf) {
40+
PyErr_Format(PyExc_ValueError, "expected a finite input, got %s", buf);
41+
PyMem_Free(buf);
42+
}
43+
return CPY_FLOAT_ERROR;
44+
}
45+
3746
double CPyFloat_FromTagged(CPyTagged x) {
3847
if (CPyTagged_CheckShort(x)) {
3948
return CPyTagged_ShortAsSsize_t(x);
@@ -48,22 +57,34 @@ double CPyFloat_FromTagged(CPyTagged x) {
4857
double CPyFloat_Sin(double x) {
4958
double v = sin(x);
5059
if (unlikely(isnan(v)) && !isnan(x)) {
60+
#if CPY_3_14_FEATURES
61+
return CPy_MathExpectedFiniteInput(x);
62+
#else
5163
return CPy_DomainError();
64+
#endif
5265
}
5366
return v;
5467
}
5568

5669
double CPyFloat_Cos(double x) {
5770
double v = cos(x);
5871
if (unlikely(isnan(v)) && !isnan(x)) {
72+
#if CPY_3_14_FEATURES
73+
return CPy_MathExpectedFiniteInput(x);
74+
#else
5975
return CPy_DomainError();
76+
#endif
6077
}
6178
return v;
6279
}
6380

6481
double CPyFloat_Tan(double x) {
6582
if (unlikely(isinf(x))) {
83+
#if CPY_3_14_FEATURES
84+
return CPy_MathExpectedFiniteInput(x);
85+
#else
6686
return CPy_DomainError();
87+
#endif
6788
}
6889
return tan(x);
6990
}

0 commit comments

Comments
 (0)