Skip to content

Conversation

@ZeroIntensity
Copy link
Member

@ZeroIntensity ZeroIntensity commented Nov 4, 2025

@ZeroIntensity ZeroIntensity added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Nov 4, 2025
@bedevere-app bedevere-app bot added awaiting core review docs Documentation in the Doc dir skip news labels Nov 4, 2025
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Nov 4, 2025
@ZeroIntensity ZeroIntensity changed the title gh-141004: Document Py_RETURN_NAN and Py_RETURN_INF gh-141004: Document Py_RETURN_NAN and Py_RETURN_INF Nov 4, 2025
@encukou
Copy link
Member

encukou commented Nov 4, 2025

Macro docs should generally include the expansion (perhaps simplified).
Docs for function-like macros should include arguments.

@ZeroIntensity
Copy link
Member Author

Unfortunately, that's a little tricky here, because the definition involves Py_NAN and Py_INFINITY, which aren't documented either (and probably things we should make private).

Take a look at something like Py_RETURN_NONE for precedent. I think this is clear and fine for users.

@encukou
Copy link
Member

encukou commented Nov 4, 2025

Take a look at something like Py_RETURN_NONE for precedent. I think this is clear and fine for users.

I disagree there. I think it's clear as mud to someone who'd see it for the first time.
For Py_RETURN_NONE you can search for examples easily, but not so much for these.

Unfortunately, that's a little tricky here, because the definition involves Py_NAN and Py_INFINITY, which aren't documented either (and probably things we should make private).

The expansion can be approximate -- it should illustrate the concept for to C programmers, and be a hint for the non-C ones (who generally can't use headers and need to reimplement macros).
So you can say, for example, that “on platforms where Python's math.nan uses the C99 NAN macro, it's equivalent to: return PyFloat_FromDouble(NAN)”.

But I'd also be fine with Py_NAN & Py_INFINITY being fully public, documented as primitive values of Python's math.nan and math.inf, and (equivalently) cross-platform versions of C99's NAN and INFINITY.
(Not great for stable ABI – platforms without IEEE floats might need a more elaborate nan representation than a C float. But it's a case for practicality beating purity.)

@skirpichev
Copy link
Contributor

Macro docs should generally include the expansion (perhaps simplified).

I agree, we shouldn't use pure-Python world equivalents.

Unfortunately, that's a little tricky here, because the definition involves Py_NAN and Py_INFINITY, which aren't documented either (and probably things we should make private).

See issue thread on fate of such macros. I hope we could reference to INFINITY/NAN from the C standard.

@ZeroIntensity
Copy link
Member Author

Ok, I've updated it to include the macro expansion using C99's NAN and INFINITY. I'll adjust if we decide to make Py_NAN and Py_INFINITY public.

@skirpichev
Copy link
Contributor

Note that neither macro is used in the CPython codebase. Do we need them at all?

Both return PyFloat_FromDouble(+/-INFINITY) or return PyFloat_FromDouble(NAN) are readable enough.

Co-authored-by: Sergey B Kirpichev <[email protected]>
@ZeroIntensity
Copy link
Member Author

Note that neither macro is used in the CPython codebase. Do we need them at all?

Is it used in any third parties? (We should document the macro nonetheless, but perhaps we could deprecate it.)

@skirpichev
Copy link
Contributor

skirpichev commented Nov 5, 2025

Is it used in any third parties?

It seems so, I see few matches in the Github search. Though, nothing that can't be replaced by something like return PyFloat_FromDouble(INFINITY*sign);, ditto for Py_RETURN_NAN. I think at least soft deprecation does make sense.

@ZeroIntensity
Copy link
Member Author

If there's actual usage, I don't see why we need to deprecate these at all. They don't pose a maintenance issue.

@ZeroIntensity
Copy link
Member Author

Ok, I'm going to merge this. @encukou, if you'd like to deprecate these, let's do that in a separate PR for 3.15 only. I personally don't see any need to deprecate them.

@ZeroIntensity ZeroIntensity merged commit 5b02c6e into python:main Nov 5, 2025
28 checks passed
@ZeroIntensity ZeroIntensity deleted the document-missing-return branch November 5, 2025 22:00
@miss-islington-app
Copy link

Thanks @ZeroIntensity for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

@github-project-automation github-project-automation bot moved this from Todo to Done in Docs PRs Nov 5, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 5, 2025
…GH-141029)

(cherry picked from commit 5b02c6e)

Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Sergey B Kirpichev <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Nov 5, 2025

GH-141074 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Nov 5, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 5, 2025
…GH-141029)

(cherry picked from commit 5b02c6e)

Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Sergey B Kirpichev <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Nov 5, 2025

GH-141075 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Nov 5, 2025
ZeroIntensity added a commit that referenced this pull request Nov 5, 2025
…1029) (GH-141074)

gh-141004: Document `Py_RETURN_NAN` and `Py_RETURN_INF` (GH-141029)
(cherry picked from commit 5b02c6e)

Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Sergey B Kirpichev <[email protected]>
ZeroIntensity added a commit that referenced this pull request Nov 5, 2025
…1029) (GH-141075)

gh-141004: Document `Py_RETURN_NAN` and `Py_RETURN_INF` (GH-141029)
(cherry picked from commit 5b02c6e)

Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Sergey B Kirpichev <[email protected]>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants