gh-133895: provide C99 Annex G return values for cmath's functions#134995
gh-133895: provide C99 Annex G return values for cmath's functions#134995skirpichev wants to merge 18 commits intopython:mainfrom
Conversation
Now this information is available as the "value" attribute of 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 |
Co-authored-by: Bénédikt Tran <[email protected]>
Misc/NEWS.d/next/Library/2025-06-01-10-38-00.gh-issue-133895.0X7c-V.rst
Outdated
Show resolved
Hide resolved
Misc/NEWS.d/next/Library/2025-06-01-10-38-00.gh-issue-133895.0X7c-V.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Bénédikt Tran <[email protected]>
|
I changed exception handling along the way, suggested by @serhiy-storchaka. I hope I did that right. If so, I can adjust the math's pr. Now we add |
| exc_string = PyUnicode_FromString("math range error"); | ||
| } | ||
| if (!exc_string) { | ||
| Py_DECREF(value); |
There was a problem hiding this comment.
I'm not very fond of stealing a reference here. Maybe we can just return -1 and make the DECREF in the caller?
There was a problem hiding this comment.
Then we just have to put Py_DECREF(value) unconditionally after every set_cmath_error(value) call.
There was a problem hiding this comment.
I think it'll be a bit cleaner (I find following code that doesn't steal references much easier, even if it sometimes leads to more lines). But since you're mainly the one maintaining math/cmath, it's up to you though. I'd appreciate a small comment in the function though to know that 'value' is stolen.
There was a problem hiding this comment.
I think it'll be a bit cleaner
Maybe. Lets other reviewers decide. Meanwhile, I left a comment.
you're mainly the one maintaining math/cmath
No :-)
There was a problem hiding this comment.
Well, you're someone with many contributions in this module!
Co-authored-by: Bénédikt Tran <[email protected]>
This is a split-off pythongh-134995. The C17 standard says (cis(y) is defined as cos(y) + i sin(y)): * ccosh(+∞ + i0) returns +∞ + i0. * ccosh(+∞ + iy) returns +∞ cis(y), for finite nonzero y. and * csinh(+∞ + i0) returns +∞ + i0. * csinh(+∞ + iy) returns +∞ cis(y), for positive finite y. So far values, computed for exceptions, aren't accessible from the pure-Python world, yet we are trying to be correct in other places. The Lib/test/mathdata/cmath_testcases.txt has data points with correct numbers (see cosh0032 and sinh0032). Also, use AC magic for the rect() value.
This is a split-off pythongh-134995. The C17 standard says (cis(y) is defined as cos(y) + i sin(y)): * ccosh(+∞ + i0) returns +∞ + i0. * ccosh(+∞ + iy) returns +∞ cis(y), for finite nonzero y. and * csinh(+∞ + i0) returns +∞ + i0. * csinh(+∞ + iy) returns +∞ cis(y), for positive finite y. So far values, computed for exceptions, aren't accessible from the pure-Python world, yet we are trying to be correct in other places. The Lib/test/mathdata/cmath_testcases.txt has data points with correct numbers (see cosh0032 and sinh0032). Also, use AC magic for the rect() value.
|
The scope of this change is larger than just the |
@serhiy-storchaka, are you proposing some prior discussion in the "Ideas" section of https://discuss.python.org/? |
|
Yes, please. |
Now this information is available as the "value" attribute of raised exception objects, for example:
Also uses the AC magic for return value of the cmath.rect().
📚 Documentation preview 📚: https://cpython-previews--134995.org.readthedocs.build/