FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-133895: provide C99 Annex F return values for math's functions by skirpichev · Pull Request #135008 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (3) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 4 additions & 1 deletion Doc/library/math.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,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
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.15.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ math
* Add :func:`math.isnormal` and :func:`math.issubnormal` functions.
(Contributed by Sergey B Kirpichev in :gh:`132908`.)

* 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`.)
* Add :func:`math.fmax`, :func:`math.fmin` and :func:`math.signbit` functions.
(Contributed by Bénédikt Tran in :gh:`135853`.)

Expand Down
18 changes: 8 additions & 10 deletions Lib/test/test_math.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2190,15 +2190,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 @@ -2231,15 +2230,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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

picnixz Jun 1, 2025 •
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Back | FazBrowse Home | New Git URL