Skip to content

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
@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 added a commit to skirpichev/cpython that referenced this pull request Jun 17, 2025
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.
skirpichev added a commit to skirpichev/cpython that referenced this pull request Jun 17, 2025
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.
@skirpichev skirpichev requested a review from AA-Turner as a code owner October 10, 2025 04:24
@serhiy-storchaka
Copy link
Member

The scope of this change is larger than just the math module. The decision cannot be made by the reviewers of this PR alone. I'm not sure this is PEP worthy, but at least it's worth discussing with a wider audience.

@skirpichev
Copy link
Contributor Author

but at least it's worth discussing with a wider audience

@serhiy-storchaka, are you proposing some prior discussion in the "Ideas" section of https://discuss.python.org/?

@serhiy-storchaka
Copy link
Member

Yes, please.

@skirpichev skirpichev closed this Oct 14, 2025
@skirpichev skirpichev deleted the cmath-ValueError/133895 branch October 14, 2025 11:32
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