Skip to content

Commit 204ccd3

Browse files
committed
C++ support; test_cext and test_cppext
1 parent 91389b5 commit 204ccd3

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

Include/pymacro.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
5050
# define Py_BUILD_ASSERT_EXPR(cond) \
51-
(sizeof(struct { _Static_assert(cond, #cond); int dummy; }), \
51+
((void)sizeof(struct { int dummy; _Static_assert(cond, #cond); }), \
5252
0)
5353
#else
5454
/* Assert a build-time dependency, as an expression.
@@ -68,9 +68,13 @@
6868
(sizeof(char [1 - 2*!(cond)]) - 1)
6969
#endif
7070

71-
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
71+
#if ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) \
72+
|| (defined(__cplusplus) && __cplusplus >= 201103L))
7273
// Use static_assert() on C11 and newer
73-
# define Py_BUILD_ASSERT(cond) static_assert((cond), #cond)
74+
# define Py_BUILD_ASSERT(cond) \
75+
do { \
76+
static_assert((cond), #cond); \
77+
} while (0)
7478
#else
7579
# define Py_BUILD_ASSERT(cond) \
7680
do { \

Lib/test/test_cext/extension.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ _testcext_exec(PyObject *module)
4444
return -1;
4545
}
4646
#endif
47+
48+
// test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR()
49+
Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
50+
assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);
51+
4752
return 0;
4853
}
4954

Lib/test/test_cppext/extension.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ _testcppext_exec(PyObject *module)
225225
if (!result) return -1;
226226
Py_DECREF(result);
227227

228+
// test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR()
229+
Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
230+
assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);
231+
228232
return 0;
229233
}
230234

0 commit comments

Comments
 (0)