-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Py_BUILD_ASSERT is broken on non-constant expression #118124
Comments
The Python C API should be usable with older C versions. |
I proposed PR gh-118398 to use static_assert() in Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR() on C11 and newer. |
I thought that |
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.
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.
Fixed in the main branch. It sounds too risky to backport the change. |
…hon#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.
Hi - sorry, a little late to this, but this change broke The check was already broken with 96e1901 before, but I only encountered it recently when trying to compile Python C++ extension that used I believe that [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57025 (should I create a new issue considering that this one is closed for a long time?) |
Do you want to propose a fix? |
It's already here: #121974 :) |
Fix check for static_assert() for C++ on some platforms.
…mos (pythonGH-121974) Fix check for static_assert() for C++ on some platforms.. (cherry picked from commit e88bd96) Co-authored-by: Jakub Kulík <Kulikjak@gmail.com>
Bug report
Bug description:
Macros
Py_BUILD_ASSERT
andPy_BUILD_ASSERT_EXPR
defined inInclude/pymacro.h
are broken whencond
is not a constant expression, becausesizeof
is allowed to apply on variable-length arrays(VLA) since C99 and with compiler extension since C89.Following code compiles on Clang and GCC without error:
Since CPython is now using C11 (PEP7) and
static_assert
is already used in other public internal headers(e.g.Include/internal/pycore_long.h
),Py_BUILD_ASSERT
should be deprecated and usestatic_assert
instead.Since they are defined in a public header of CPython without a "_Py" prefix, removing them might break third-party code.
Py_BUILD_ASSERT
can be defined tostatic_assert
in a way with deprecating warning message:Py_BUILD_ASSERT_EXPR
is used (and only used) in another macroPy_ARRAY_LENGTH
, so it can't be deprecated yet.Fixing
Py_BUILD_ASSERT_EXPR
is tricky, most because of old non-conformant MSVC1. And we need it also working in C++. The easiest fix is to change documentation so that it should be used only with constant expression or there's no assertion otherwise. But If we want to make it mandatory, here's a workaround:You can view and try the workarounds using the amazing conformance-viewer in amazing Compiler Explorer: for C and for C++.
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
Footnotes
One of the unbelievable bugs is that MSVC prior to v19.21 accepts negative-length(implicitly extended to unsigned value) arrays when used as template argument, e.g.
A<char[-1]>
. ↩The text was updated successfully, but these errors were encountered: