gh-118124: Use static_assert() in Py_BUILD_ASSERT() on C11 (#118398) · python/cpython@587388f · GitHub
Skip to content

Commit 587388f

Browse files
authored
gh-118124: Use static_assert() in Py_BUILD_ASSERT() on C11 (#118398)
Use static_assert() in Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR() on C11 and newer and C++11 and newer. Add tests to test_cext and test_cppext.
1 parent 6999d68 commit 587388f

4 files changed

Lines changed: 45 additions & 16 deletions

File tree

Include/pymacro.h

Lines changed: 33 additions & 16 deletions

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

Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)