mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
gh-92135: Fix _Py_reinterpret_cast() for const (#92138)
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.
This commit is contained in:
parent
b11243e85e
commit
031397063e
5 changed files with 62 additions and 14 deletions
|
@ -43,8 +43,7 @@ typedef PyObject *(*PyCMethod)(PyObject *, PyTypeObject *, PyObject *const *,
|
|||
// it triggers an undefined behavior when Python calls it with 2 parameters
|
||||
// (bpo-33012).
|
||||
#define _PyCFunction_CAST(func) \
|
||||
_Py_reinterpret_cast(PyCFunction, \
|
||||
_Py_reinterpret_cast(void(*)(void), (func)))
|
||||
_Py_reinterpret_cast(PyCFunction, _Py_reinterpret_cast(void(*)(void), (func)))
|
||||
|
||||
PyAPI_FUNC(PyCFunction) PyCFunction_GetFunction(PyObject *);
|
||||
PyAPI_FUNC(PyObject *) PyCFunction_GetSelf(PyObject *);
|
||||
|
|
|
@ -182,9 +182,9 @@ PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
|
|||
PyAPI_FUNC(void) PyObject_GC_Del(void *);
|
||||
|
||||
#define PyObject_GC_New(type, typeobj) \
|
||||
_Py_reinterpret_cast(type*, _PyObject_GC_New(typeobj))
|
||||
_Py_reinterpret_cast(type*, _PyObject_GC_New(typeobj))
|
||||
#define PyObject_GC_NewVar(type, typeobj, n) \
|
||||
_Py_reinterpret_cast(type*, _PyObject_GC_NewVar((typeobj), (n)))
|
||||
_Py_reinterpret_cast(type*, _PyObject_GC_NewVar((typeobj), (n)))
|
||||
|
||||
PyAPI_FUNC(int) PyObject_GC_IsTracked(PyObject *);
|
||||
PyAPI_FUNC(int) PyObject_GC_IsFinalized(PyObject *);
|
||||
|
|
|
@ -14,10 +14,20 @@
|
|||
#endif
|
||||
|
||||
|
||||
// Macro to use C++ static_cast<> and reinterpret_cast<> in the Python C API
|
||||
// Macro to use C++ static_cast<>, reinterpret_cast<> and const_cast<>
|
||||
// in the Python C API.
|
||||
//
|
||||
// In C++, _Py_reinterpret_cast(type, expr) converts a constant expression to a
|
||||
// non constant type using const_cast<type>. For example,
|
||||
// _Py_reinterpret_cast(PyObject*, op) can convert a "const PyObject*" to
|
||||
// "PyObject*".
|
||||
//
|
||||
// The type argument must not be constant. For example, in C++,
|
||||
// _Py_reinterpret_cast(const PyObject*, expr) fails with a compiler error.
|
||||
#ifdef __cplusplus
|
||||
# define _Py_static_cast(type, expr) static_cast<type>(expr)
|
||||
# define _Py_reinterpret_cast(type, expr) reinterpret_cast<type>(expr)
|
||||
# define _Py_reinterpret_cast(type, expr) \
|
||||
const_cast<type>(reinterpret_cast<const type>(expr))
|
||||
#else
|
||||
# define _Py_static_cast(type, expr) ((type)(expr))
|
||||
# define _Py_reinterpret_cast(type, expr) ((type)(expr))
|
||||
|
@ -307,10 +317,10 @@ extern "C" {
|
|||
*/
|
||||
#ifdef Py_DEBUG
|
||||
# define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
|
||||
(assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
|
||||
(assert(_Py_static_cast(WIDE, _Py_static_cast(NARROW, (VALUE))) == (VALUE)), \
|
||||
_Py_static_cast(NARROW, (VALUE)))
|
||||
#else
|
||||
# define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
|
||||
_Py_reinterpret_cast(NARROW, (VALUE))
|
||||
# define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) _Py_static_cast(NARROW, (VALUE))
|
||||
#endif
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue