Skip to content

Conversation

@ZeroIntensity
Copy link
Member

@ZeroIntensityZeroIntensity commented Nov 4, 2025

@ZeroIntensityZeroIntensity added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Nov 4, 2025
@bedevere-appbedevere-appbot added awaiting core review docs Documentation in the Doc dir skip news labels Nov 4, 2025
@ZeroIntensityZeroIntensity changed the title gh-141004: Document Py_RETURN_NAN and Py_RETURN_INFgh-141004: Document Py_RETURN_NAN and Py_RETURN_INFNov 4, 2025
@bedevere-appbedevere-appbot mentioned this pull request Nov 4, 2025
@encukou
Copy link
Member

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

@ZeroIntensity
Copy link
MemberAuthor

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

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
Member

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
MemberAuthor

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
Member

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
MemberAuthor

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
Member

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
MemberAuthor

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
MemberAuthor

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.

@ZeroIntensityZeroIntensity merged commit 5b02c6e into python:mainNov 5, 2025
28 checks passed
@ZeroIntensityZeroIntensity 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-automationgithub-project-automationbot moved this from Todo to Done in Docs PRsNov 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

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

@bedevere-appbedevere-appbot 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

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

@bedevere-appbedevere-appbot 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 freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docsDocumentation in the Doc dirskip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants

@ZeroIntensity@encukou@skirpichev