diff --git a/Include/pymacro.h b/Include/pymacro.h index cd6fc4eba9c2ed8..3335bff61531253 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -61,9 +61,15 @@ #define Py_BUILD_ASSERT_EXPR(cond) \ (sizeof(char [1 - 2*!(cond)]) - 1) -#define Py_BUILD_ASSERT(cond) do { \ - (void)Py_BUILD_ASSERT_EXPR(cond); \ - } while(0) +#if __STDC_VERSION__ >= 201112L + // Use static_assert() on C11 and newer +# define Py_BUILD_ASSERT(cond) static_assert((cond), "") +#else +# define Py_BUILD_ASSERT(cond) \ + do { \ + (void)Py_BUILD_ASSERT_EXPR(cond); \ + } while(0) +#endif /* Get the number of elements in a visible array diff --git a/Misc/NEWS.d/next/C API/2024-04-29-17-44-15.gh-issue-118124.czQQ9G.rst b/Misc/NEWS.d/next/C API/2024-04-29-17-44-15.gh-issue-118124.czQQ9G.rst new file mode 100644 index 000000000000000..c594683551a5625 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2024-04-29-17-44-15.gh-issue-118124.czQQ9G.rst @@ -0,0 +1,3 @@ +Use ``static_assert()`` in :c:macro:`Py_BUILD_ASSERT` on C11 and newer to +fix :c:macro:`Py_BUILD_ASSERT` on non-constant expressions. Patch by Victor +Stinner.