From c1adc06352ce8a62d82b051fe2312fa9c724667a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 5 Oct 2026 14:31:18 +0200 Subject: [PATCH] gh-158810: Use _Py_TYPEOF in Py_ARRAY_LENGTH() Fix the compiler warning "extension used" when building test_cext with clang. * test_cext no longer overrides the CC environment variable if it's already defined. * test_cext now also logs __STRICT_ANSI__ macro. --- Include/pymacro.h | 6 +++--- Lib/test/test_cext/extension.c | 5 +++++ Lib/test/test_cext/runtests.py | 7 ++++++- Lib/test/test_cext/setup.py | 2 +- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Include/pymacro.h b/Include/pymacro.h index 979e6563742b98..122d26315b51b9 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -214,12 +214,12 @@ // // gh-158810: Do not use __builtin_types_compatible_p() in strict C ANSI mode // and on C++. -#if (defined(__GNUC__) && __GNUC__ >= 4 \ +#if (defined(__GNUC__) && __GNUC__ >= 4 && defined(_Py_TYPEOF) \ && !defined(__STRICT_ANSI__) && !defined(__cplusplus)) #define Py_ARRAY_LENGTH(array) \ (sizeof(array) / sizeof((array)[0]) \ - + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \ - typeof(&(array)[0])))) + + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(_Py_TYPEOF(array), \ + _Py_TYPEOF(&(array)[0])))) #else #define Py_ARRAY_LENGTH(array) \ (sizeof(array) / sizeof((array)[0])) diff --git a/Lib/test/test_cext/extension.c b/Lib/test/test_cext/extension.c index e392b66f19cc4a..f1528b4df7776f 100644 --- a/Lib/test/test_cext/extension.c +++ b/Lib/test/test_cext/extension.c @@ -388,6 +388,11 @@ module_exec(PyObject *module) return -1; } #endif +#ifdef __STRICT_ANSI__ + if (PyModule_AddIntConstant(module, "__STRICT_ANSI__", 1) < 0) { + return -1; + } +#endif #ifdef __cplusplus if (PyModule_AddIntMacro(module, __cplusplus) < 0) { return -1; diff --git a/Lib/test/test_cext/runtests.py b/Lib/test/test_cext/runtests.py index 86aab671200493..e8288ee5e11e44 100644 --- a/Lib/test/test_cext/runtests.py +++ b/Lib/test/test_cext/runtests.py @@ -28,7 +28,12 @@ def main(): testmod = importlib.import_module(module_name) newline = False - for name in ('__STDC_VERSION__', '__cplusplus', '_MSVC_LANG'): + for name in ( + '__STDC_VERSION__', + '__STRICT_ANSI__', + '__cplusplus', + '_MSVC_LANG', + ): try: value = getattr(testmod, name) except AttributeError: diff --git a/Lib/test/test_cext/setup.py b/Lib/test/test_cext/setup.py index 58bee3e255a4b3..c5706d23748e53 100644 --- a/Lib/test/test_cext/setup.py +++ b/Lib/test/test_cext/setup.py @@ -135,7 +135,7 @@ def main(): # option emits a C++ compiler warning. Remove "-std11" option from the # CC command. cmd = (sysconfig.get_config_var('CC') or '') - if cmd is not None: + if cmd is not None and 'CC' not in os.environ: if support.MS_WINDOWS: std_prefix = '/std' else: