Skip to content

Commit

Permalink
pythongh-118124: Use static_assert() in Py_BUILD_ASSERT()
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Apr 29, 2024
1 parent 23d0371 commit efbd483
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Include/pymacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit efbd483

Please sign in to comment.