gh-133895: provide C99 Annex F return values for math's functions#135008
gh-133895: provide C99 Annex F return values for math's functions#135008skirpichev wants to merge 4 commits intopython:mainfrom
Conversation
Now this information is available as the "value" attribute of the ValueError exception object: ```pycon >>> import math >>> try: ... math.gamma(-0.0) ... except ValueError as exc: ... print(exc.value) ... -inf ```
|
See also #134995 |
Misc/NEWS.d/next/Library/2025-06-01-16-10-07.gh-issue-133895.1-SE2w.rst
Outdated
Show resolved
Hide resolved
| PyObject *value = PyFloat_FromDouble(x); | ||
|
|
||
| if (value) { | ||
| PyObject_SetAttrString(exc, "value", value); |
There was a problem hiding this comment.
This call can fail. Should we do something about it? or should we simply ignore the exception? or maybe chain it?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 :-)
There was a problem hiding this comment.
Why it's bad in this case?
Don't we have an assert() somewhere that would fail if the error indicator is set?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
See cmath's pr, I did such change: #134995. Does it make sense for you?
Co-authored-by: Bénédikt Tran <[email protected]>
Now this information is available as the "value" attribute of the ValueError exception object:
📚 Documentation preview 📚: https://cpython-previews--135008.org.readthedocs.build/