Skip to content

gh-133895: provide C99 Annex F return values for math's functions #135008

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion Doc/library/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,10 @@ Constants
:exc:`ValueError` for invalid operations like ``sqrt(-1.0)`` or ``log(0.0)``
(where C99 Annex F recommends signaling invalid operation or divide-by-zero),
and :exc:`OverflowError` for results that overflow (for example,
``exp(1000.0)``). A NaN will not be returned from any of the functions
``exp(1000.0)``). The Annex F recommended result is available as
the ``value`` property of the :exc:`ValueError` exception.

A NaN will not be returned from any of the functions
above unless one or more of the input arguments was a NaN; in that case,
most functions will return a NaN, but (again following C99 Annex F) there
are some exceptions to this rule, for example ``pow(float('nan'), 0.0)`` or
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ difflib
(Contributed by Jiahao Li in :gh:`134580`.)


math
----

* Provide C99 Annex F return values for :mod:`math`'s functions as the
``value`` attribute of the :exc:`ValueError` exception object.
(Contributed by Sergey B Kirpichev in :gh:`133895`.)


shelve
------

Expand Down
18 changes: 8 additions & 10 deletions Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -2069,15 +2069,14 @@ def test_testfile(self):

func = getattr(math, fn)

if 'invalid' in flags or 'divide-by-zero' in flags:
er = 'ValueError'
elif 'overflow' in flags:
if 'overflow' in flags:
er = 'OverflowError'

try:
result = func(ar)
except ValueError:
result = 'ValueError'
except ValueError as exc:
self.assertTrue('invalid' in flags or 'divide-by-zero' in flags)
result = exc.value
except OverflowError:
result = 'OverflowError'

Expand Down Expand Up @@ -2110,15 +2109,14 @@ def test_mtestfile(self):
for id, fn, arg, expected, flags in parse_mtestfile(math_testcases):
func = getattr(math, fn)

if 'invalid' in flags or 'divide-by-zero' in flags:
expected = 'ValueError'
elif 'overflow' in flags:
if 'overflow' in flags:
expected = 'OverflowError'

try:
got = func(arg)
except ValueError:
got = 'ValueError'
except ValueError as exc:
got = exc.value
self.assertTrue('invalid' in flags or 'divide-by-zero' in flags)
except OverflowError:
got = 'OverflowError'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Provide C99 Annex F return values for :mod:`math`'s functions as the ``value``
attribute of the :exc:`ValueError` exception object. Patch by Sergey B Kirpichev.
17 changes: 17 additions & 0 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,20 @@ math_lcm_impl(PyObject *module, PyObject * const *args,
}


static void
set_exc_value(double x)
{
PyObject *exc = PyErr_GetRaisedException();
PyObject *value = PyFloat_FromDouble(x);

if (value) {
PyObject_SetAttrString(exc, "value", value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call can fail. Should we do something about it? or should we simply ignore the exception? or maybe chain it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or should we simply ignore the exception?

I don't see better option so far. (Ditto for cmath.)

BTW, maybe I should also drop if (value) { clause.

or maybe chain it?

I doubt it's appropriate here. We don't handle ValueError here. This code just alter the raised exception object.

Copy link
Member

@picnixz picnixz Jun 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, maybe I should also drop if (value) { clause.

I don't think you should, otherwise PyObject_SetAttrString will be called with an exception set (and with a NULL value it's the same as deleting the attribute)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be called with an exception set

Why it's bad in this case?

and with a NULL value it's the same as deleting the attribute

But we are already ignore errors from the PyObject_SetAttrString :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it's bad in this case?

Don't we have an assert() somewhere that would fail if the error indicator is set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I failed to find such case.

Though, we could explicitly call (twice) PyErr_Occurred() in appropriate places.

Looking on other cases of using PyErr_GetRaisedException() - I think that unraisable exceptions should be used here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See cmath's pr, I did such change: #134995. Does it make sense for you?

}
Py_XDECREF(value);
PyErr_SetRaisedException(exc);
}


/* Call is_error when errno != 0, and where x is the result libm
* returned. is_error will usually set up an exception and return
* true (1), but may return false (0) without setting up an exception.
Expand All @@ -852,6 +866,7 @@ is_error(double x, int raise_edom)
if (errno == EDOM) {
if (raise_edom) {
PyErr_SetString(PyExc_ValueError, "math domain error");
set_exc_value(x);
}
}

Expand Down Expand Up @@ -954,6 +969,7 @@ math_1(PyObject *arg, double (*func) (double), int can_overflow,
else {
PyErr_SetString(PyExc_ValueError, "math domain error");
}
set_exc_value(r);
return NULL;
}

Expand All @@ -979,6 +995,7 @@ math_1a(PyObject *arg, double (*func) (double), const char *err_msg)
PyMem_Free(buf);
}
}
set_exc_value(r);
return NULL;
}
return PyFloat_FromDouble(r);
Expand Down
Loading