Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Draft
skirpichev wants to merge8 commits intopython:main
base:main
Choose a base branch
Loading
fromskirpichev:cmath-ValueError/133895

Conversation

skirpichev
Copy link
Contributor

@skirpichevskirpichev commentedJun 1, 2025
edited by github-actionsbot
Loading

Now this information is available as the "value" attribute of the ValueError exception object:

>>>import cmath>>>try:...     cmath.log(complex(-0.0,0))...exceptValueErroras 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" 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().
@skirpichev
Copy link
ContributorAuthor

CC@abhigyan631 as author of#133923

@skirpichevskirpichev changed the titlegh-133895: provide C99 Annex G return values for cmath functionsgh-133895: provide C99 Annex G return values for cmath's functionsJun 1, 2025
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);
Copy link
Member

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?

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

Copy link
Member

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

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

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

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

@skirpichevskirpichev marked this pull request as draftJune 2, 2025 09:06
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka left review comments

@picnixzpicnixzpicnixz left review comments

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@skirpichev@serhiy-storchaka@picnixz

[8]ページ先頭

©2009-2025 Movatter.jp