8000 gh-118124: Use static_assert() in Py_BUILD_ASSERT() · vstinner/cpython@efbd483 · GitHub
[go: up one dir, main page]

Skip to content

Commit efbd483

Browse files
committed
pythongh-118124: Use static_assert() in Py_BUILD_ASSERT()
1 parent 23d0371 commit efbd483

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

Include/pymacro.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,15 @@
6161
#define Py_BUILD_ASSERT_EXPR(cond) \
6262
(sizeof(char [1 - 2*!(cond)]) - 1)
6363

64-
#define Py_BUILD_ASSERT(cond) do { \
65-
(void)Py_BUILD_ASSERT_EXPR(cond); \
66-
} while(0)
64+
#if __STDC_VERSION__ >= 201112L
65+
// Use static_assert() on C11 and newer
66+
# define Py_BUILD_ASSERT(cond) static_assert((cond), "")
67+
#else
68+
# define Py_BUILD_ASSERT(cond) \
69+
do { \
70+
(void)Py_BUILD_ASSERT_EXPR(cond); \
71+
} while(0)
72+
#endif
6773

6874
/* Get the number of elements in a visible array
6975
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Use ``static_assert()`` in :c:macro:`Py_BUILD_ASSERT` on C11 and newer to
2+
fix :c:macro:`Py_BUILD_ASSERT` on non-constant expressions. Patch by Victor
3+
Stinner.

0 commit comments

Comments
 (0)
0