From ae44ccb8ebb608b59e78f830f18742f208f4885e Mon Sep 17 00:00:00 2001 From: Sumit Kumar Das Date: Mon, 26 Oct 2026 18:54:37 +0000 Subject: [PATCH 1/3] gh-160719: Define _Py_STDC_VERSION in pyport.h 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 --- Include/pymacro.h | 7 +------ Include/pyport.h | 9 +++++++++ .../C_API/2026-10-26-19-10-00.gh-issue-160719.F9F7fw.rst | 5 +++++ 3 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/C_API/2026-10-26-19-10-00.gh-issue-160719.F9F7fw.rst diff --git a/Include/pymacro.h b/Include/pymacro.h index 73521be00fcd1ad..cde2a5fd05ad585 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -1,12 +1,7 @@ #ifndef Py_PYMACRO_H #define Py_PYMACRO_H -// _Py_STDC_VERSION: the version of the C standard. -// gh-160208: The C++ standard allows __STDC_VERSION__ to be defined to an -// arbitrary value, which means we can't use __STDC_VERSION__ on C++. -#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. // gh-91782: On FreeBSD 12, if the _POSIX_C_SOURCE and _XOPEN_SOURCE macros are // defined, disables C11 support and does not define diff --git a/Include/pyport.h b/Include/pyport.h index 3de7fb3d317521a..821e68ee89e23f7 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -8,6 +8,15 @@ # error "Python's source code assumes C's unsigned char is an 8-bit type" #endif +// _Py_STDC_VERSION: the version of the C standard. +// gh-160208: The C++ standard allows __STDC_VERSION__ to be defined to an +// arbitrary value, which means we can't use __STDC_VERSION__ on C++. +// gh-160719: Defined here, rather than in pymacro.h, because this file uses it +// and is included before pymacro.h. +#if !defined(__cplusplus) && defined(__STDC_VERSION__) +# define _Py_STDC_VERSION __STDC_VERSION__ +#endif + // Preprocessor check for a builtin preprocessor function. Always return 0 // if __has_builtin() macro is not defined. diff --git a/Misc/NEWS.d/next/C_API/2026-10-26-19-10-00.gh-issue-160719.F9F7fw.rst b/Misc/NEWS.d/next/C_API/2026-10-26-19-10-00.gh-issue-160719.F9F7fw.rst new file mode 100644 index 000000000000000..622afb2d70cfee0 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-10-26-19-10-00.gh-issue-160719.F9F7fw.rst @@ -0,0 +1,5 @@ +Fix the C11 and C23 code paths in :file:`pyport.h` never being used: the +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``. From b990992fc6a3ee15ac9a2cb92016c549d0f834ac Mon Sep 17 00:00:00 2001 From: Sumit Kumar Das <151006536+skdas20@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:24:49 +0000 Subject: [PATCH 2/3] Address review: drop extra comments and NEWS entry --- Include/pymacro.h | 2 -- Include/pyport.h | 2 -- .../C_API/2026-10-26-19-10-00.gh-issue-160719.F9F7fw.rst | 5 ----- 3 files changed, 9 deletions(-) delete mode 100644 Misc/NEWS.d/next/C_API/2026-10-26-19-10-00.gh-issue-160719.F9F7fw.rst diff --git a/Include/pymacro.h b/Include/pymacro.h index cde2a5fd05ad585..16589ffe0fdd351 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -1,8 +1,6 @@ #ifndef Py_PYMACRO_H #define Py_PYMACRO_H -// _Py_STDC_VERSION is defined in pyport.h, which is included before this file. - // gh-91782: On FreeBSD 12, if the _POSIX_C_SOURCE and _XOPEN_SOURCE macros are // defined, disables C11 support and does not define // the static_assert() macro. diff --git a/Include/pyport.h b/Include/pyport.h index 821e68ee89e23f7..a075dff5c789e79 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -11,8 +11,6 @@ // _Py_STDC_VERSION: the version of the C standard. // gh-160208: The C++ standard allows __STDC_VERSION__ to be defined to an // arbitrary value, which means we can't use __STDC_VERSION__ on C++. -// gh-160719: Defined here, rather than in pymacro.h, because this file uses it -// and is included before pymacro.h. #if !defined(__cplusplus) && defined(__STDC_VERSION__) # define _Py_STDC_VERSION __STDC_VERSION__ #endif diff --git a/Misc/NEWS.d/next/C_API/2026-10-26-19-10-00.gh-issue-160719.F9F7fw.rst b/Misc/NEWS.d/next/C_API/2026-10-26-19-10-00.gh-issue-160719.F9F7fw.rst deleted file mode 100644 index 622afb2d70cfee0..000000000000000 --- a/Misc/NEWS.d/next/C_API/2026-10-26-19-10-00.gh-issue-160719.F9F7fw.rst +++ /dev/null @@ -1,5 +0,0 @@ -Fix the C11 and C23 code paths in :file:`pyport.h` never being used: the -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``. From 570b9b3abd7fdb2a24156a5ad86001b416731ee2 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 16 Nov 2026 22:23:23 +0100 Subject: [PATCH 3/3] Apply suggestion from @vstinner --- Include/pyport.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Include/pyport.h b/Include/pyport.h index a075dff5c789e79..e742ecd37d3305e 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -11,7 +11,7 @@ // _Py_STDC_VERSION: the version of the C standard. // gh-160208: The C++ standard allows __STDC_VERSION__ to be defined to an // arbitrary value, which means we can't use __STDC_VERSION__ on C++. -#if !defined(__cplusplus) && defined(__STDC_VERSION__) +#if defined(__STDC_VERSION__) && !defined(__cplusplus) # define _Py_STDC_VERSION __STDC_VERSION__ #endif