Skip to content

Update math error messages for 3.14 #18534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions mypyc/lib-rt/float_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ static double CPy_MathRangeError(void) {
return CPY_FLOAT_ERROR;
}

static double CPy_MathExpectedNonNegativeInputError(double x) {
char *buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
if (buf) {
PyErr_Format(PyExc_ValueError, "expected a nonnegative input, got %s", buf);
PyMem_Free(buf);
}
return CPY_FLOAT_ERROR;
}

static double CPy_MathExpectedPositiveInputError(double x) {
char *buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
if (buf) {
PyErr_Format(PyExc_ValueError, "expected a positive input, got %s", buf);
PyMem_Free(buf);
}
return CPY_FLOAT_ERROR;
}

double CPyFloat_FromTagged(CPyTagged x) {
if (CPyTagged_CheckShort(x)) {
return CPyTagged_ShortAsSsize_t(x);
Expand Down Expand Up @@ -52,7 +70,11 @@ double CPyFloat_Tan(double x) {

double CPyFloat_Sqrt(double x) {
if (x < 0.0) {
#if CPY_3_14_FEATURES
return CPy_MathExpectedNonNegativeInputError(x);
#else
return CPy_DomainError();
#endif
}
return sqrt(x);
}
Expand All @@ -67,7 +89,11 @@ double CPyFloat_Exp(double x) {

double CPyFloat_Log(double x) {
if (x <= 0.0) {
#if CPY_3_14_FEATURES
return CPy_MathExpectedPositiveInputError(x);
#else
return CPy_DomainError();
#endif
}
return log(x);
}
Expand Down
3 changes: 3 additions & 0 deletions mypyc/lib-rt/mypyc_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,7 @@ static inline void CPyLong_SetUnsignedSize(PyLongObject *o, Py_ssize_t n) {
// Are we targeting Python 3.13 or newer?
#define CPY_3_13_FEATURES (PY_VERSION_HEX >= 0x030d0000)

// Are we targeting Python 3.14 or newer?
#define CPY_3_14_FEATURES (PY_VERSION_HEX >= 0x030e0000)

#endif
Loading