Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.1k
gh-133895: provide C99 Annex G return values for cmath's functions#134995
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
Now this information is available as the "value" attributeof the ValueError exception object:```pycon>>> import cmath>>> try:... cmath.log(complex(-0.0, 0))... except ValueError as exc:... print(exc.value)...(-inf+3.141592653589793j)```Also uses the AC magic for return value of the cmath.rect().
CC@abhigyan631 as author of#133923 |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Misc/NEWS.d/next/Library/2025-06-01-10-38-00.gh-issue-133895.0X7c-V.rst OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Misc/NEWS.d/next/Library/2025-06-01-10-38-00.gh-issue-133895.0X7c-V.rst OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
PyObject *value = PyComplex_FromCComplex(_return_value); | ||
if (!value || PyObject_SetAttrString(exc, "value", value)) { | ||
PyErr_WriteUnraisable(NULL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
MaybeFormatUnraisable
+ some message saying we ignored the exception here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This is absolutely wrong use ofPyErr_WriteUnraisable()
/PyErr_FormatUnraisable()
. They should only be used if the error cannot be signalled back to the caller (for example if it happened in the garbage collector or inPy_DECREF()
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Oh. Thank you, TIL. It's not for exceptions that I wanted to be suppressed but still reported. (I only used it once in my life!)
PyObject *value = PyComplex_FromCComplex(_return_value); | ||
if (!value || PyObject_SetAttrString(exc, "value", value)) { | ||
PyErr_WriteUnraisable(NULL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This is absolutely wrong use ofPyErr_WriteUnraisable()
/PyErr_FormatUnraisable()
. They should only be used if the error cannot be signalled back to the caller (for example if it happened in the garbage collector or inPy_DECREF()
).
@@ -36,6 +36,15 @@ class Py_complex_protected_return_converter(CReturnConverter): | |||
data.return_conversion.append(""" | |||
if (errno == EDOM) { | |||
PyErr_SetString(PyExc_ValueError, "math domain error"); | |||
PyObject *exc = PyErr_GetRaisedException(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Note that this exception is not necessary a ValueError set above. It can be a MemoryError if allocation of string for error message failed, it can be some other error in future. It could even be a different ValueError.
So I think that the most reliable way is to create a ValueError object, set it's attribute, and only then set it byPyErr_SetObject()
.
if (errno) { | ||
PyObject *ret = math_error(); | ||
if (errno == EDOM) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
errno
can be changed inmath_error()
. Either save its value in a local variable, or checkerrno == EDOM
before callingmath_error()
.
Uh oh!
There was an error while loading.Please reload this page.
Now this information is available as the "value" attribute of the ValueError exception object:
Also uses the AC magic for return value of the cmath.rect().
📚 Documentation preview 📚:https://cpython-previews--134995.org.readthedocs.build/