gh-92135: Fix _Py_reinterpret_cast() for const#92138
gh-92135: Fix _Py_reinterpret_cast() for const#92138vstinner merged 1 commit intopython:mainfrom vstinner:const_cast
Conversation
|
@tacaswell @serge-sans-paille: Does my |
|
FTR, in another project, I recently had to use two casts in order to avoid G++ warnings: void inner_set(void *);
// original code (generated warnings)
void set_func(const char *str) {
inner_set(str);
}
// new set func, still generated warnings
void improved_set_func(const char *str) {
inner_set(const_cast<void *>(str));
}
// final set func, no warnings
void set_func_without_warning(const char *str) {
const void *vptr = static_cast<const void *>(str);
inner_set(const_cast<void *>(vptr));
} |
Fix C++ compiler warnings on cast macros, like _PyObject_CAST(), when casting a constant expression to a non constant type: use const_cast<> in C++. * In C++, Py_SAFE_DOWNCAST() now uses static_cast<> rather than reinterpret_cast<>. * Add tests to the _testcppext C++ extension. * test_cppext no longer captures stdout in verbose mode.
I understand that this approach (updated First, I renamed the macro _Py_reinterpret_const_cast(), but @serge-sans-paille didn't like this name, and me neither (it's too long!). So I kept the name |
|
@tacaswell: This issue should now be fixed. Please try greenlet with on the main branch of Python. |
|
The const issues is fixed the other casting issues still remains |
I have no idea how to fix this cast. Do you have an idea? Does Maybe greenlet should be modified to cast to |
I think we need an actual C++ expert to tell us if what greenlets is doing is fully kosher or undefined behavior that just happened to work before.
python-greenlet/greenlet@50f8786 is one way that works. |
|
That's an interesting situation: greenlets are written in C++, and the object that raises issues is of type According to me, this means we need something along these lines: |
Fix C++ compiler warnings on cast macros, like _PyObject_CAST(), when
casting a constant expression to a non constant type: use
const_cast<> in C++.
reinterpret_cast<>.