Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

skirpichev
Copy link
Contributor

@skirpichev skirpichev commented Jun 1, 2025

Now this information is available as the "value" attribute of raised exception objects, for example:

>>> 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().


📚 Documentation preview 📚: https://cpython-previews--134995.org.readthedocs.build/

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().
@skirpichev
Copy link
Contributor Author

CC @abhigyan631 as author of #133923

@skirpichev skirpichev changed the title gh-133895: provide C99 Annex G return values for cmath functions gh-133895: provide C99 Annex G return values for cmath's functions Jun 1, 2025
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@skirpichev skirpichev marked this pull request as draft June 2, 2025 09:06
@skirpichev skirpichev marked this pull request as ready for review June 5, 2025 04:40
@skirpichev
Copy link
Contributor Author

skirpichev commented Jun 5, 2025

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 value to OverflowError's too. This reveals two bugs (in sinh and cosh), fixed.

@skirpichev skirpichev requested a review from picnixz June 5, 2025 05:06
exc_string = PyUnicode_FromString("math range error");
}
if (!exc_string) {
Py_DECREF(value);
Copy link
Member

Choose a reason for hiding this comment

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

I'm not very fond of stealing a reference here. Maybe we can just return -1 and make the DECREF in the caller?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then we just have to put Py_DECREF(value) unconditionally after every set_cmath_error(value) call.

Copy link
Member

Choose a reason for hiding this comment

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

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.

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 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 :-)

Copy link
Member

Choose a reason for hiding this comment

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

Well, you're someone with many contributions in this module!

skirpichev and others added 2 commits June 8, 2025 15:44
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants