Skip to content

gh-160719: Define _Py_STDC_VERSION in pyport.h - #160733

Open
skdas20 wants to merge 3 commits into
python:mainfrom
skdas20:gh-160719-stdc-version
Open

skdas20 wants to merge 3 commits into
python:mainfrom
skdas20:gh-160719-stdc-version

Conversation

@skdas20

@skdas20 skdas20 commented Oct 26, 2026

Copy link
Copy Markdown

pyport.h uses _Py_STDC_VERSION to pick _Py_thread_local, _Py_NULL and _Py_TYPEOF, but the macro is defined in pymacro.h, which Python.h includes after pyport.h. So those C11/C23 branches are never taken. This moves the definition into pyport.h.

Macro values with #include "Python.h" (cc -E -dM):

Compiler Macro Before After
gcc 13, -std=c11 _Py_thread_local __thread _Thread_local
clang 18, -std=c23 _Py_NULL NULL nullptr
clang 18, -std=c23 _Py_TYPEOF(expr) __typeof__(expr) typeof(expr)

./python -m test test_cext -u cpu passes on Linux (the MSVC-only test is skipped).

This doesn't touch the inverted static_assert fallback condition in pymacro.h, which gh-160705 already fixes.

🤖 Generated with Claude Code

pyport.h uses _Py_STDC_VERSION to select _Py_thread_local, _Py_NULL and
_Py_TYPEOF, but the macro was defined in pymacro.h, which Python.h
includes after pyport.h. The C11 and C23 branches were therefore never
taken. Move the definition to pyport.h.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@NotorandXor256

NotorandXor256 commented Oct 27, 2026 •

Copy link
Copy Markdown

hello @skdas20, can i help with unsuccessful test
I am a first time contributor

@vstinner vstinner added the needs backport to 3.15 bugs and security fixes label Nov 15, 2026
Comment thread Include/pymacro.h Outdated
#if !defined(__cplusplus) && defined(__STDC_VERSION__)
# define _Py_STDC_VERSION __STDC_VERSION__
#endif
// _Py_STDC_VERSION is defined in pyport.h, which is included before this file.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't tink that this comment is useful. We don't do it for other macros. I suggest removing it.

private ``_Py_STDC_VERSION`` macro was defined in :file:`pymacro.h`, which is
included after :file:`pyport.h`, so ``_Py_thread_local``, ``_Py_NULL`` and
``_Py_TYPEOF`` fell back to compiler extensions instead of ``_Thread_local``,
``nullptr`` and ``typeof``.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure that it's useful to end users to know about the fix. It's not a big deal to use another implementation if C23 detection was broken. I suggest removing the Changelog entry.

Note: Sadly, the _Py_STDC_VERSION change landed in Python 3.15.2 release.

Comment thread Include/pyport.h Outdated
Comment on lines +14 to +15
// gh-160719: Defined here, rather than in pymacro.h, because this file uses it
// and is included before pymacro.h.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think that this remark/note is useful.

@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@bedevere-app

bedevere-app Bot commented Nov 16, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@skdas20

skdas20 commented Nov 16, 2026

Copy link
Copy Markdown
Author

Thanks @vstinner, done: I removed the extra comments and the NEWS entry. The change is now just moving the _Py_STDC_VERSION definition into pyport.h.

Comment thread Include/pyport.h Resolved
@bedevere-app

bedevere-app Bot commented Nov 16, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@vstinner

Copy link
Copy Markdown
Member

@skdas20: Would you mind to sign the Python CLA? See #160733 (comment) comment for details.

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

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