gh-91320: Add _Py_reinterpret_cast() macro (#91959)

Fix C++ compiler warnings about "old-style cast"
(g++ -Wold-style-cast) in the Python C API.  Use C++
reinterpret_cast<> and static_cast<> casts when the Python C API is
used in C++.

Example of fixed warning:

    Include/object.h:107:43: error: use of old-style cast to
    ‘PyObject*’ {aka ‘struct _object*’} [-Werror=old-style-cast]
    #define _PyObject_CAST(op) ((PyObject*)(op))

Add _Py_reinterpret_cast() and _Py_static_cast() macros.
This commit is contained in:
Victor Stinner 2022-04-27 10:40:57 +02:00 committed by GitHub
parent f882d33778
commit 29e2245ad5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 42 additions and 17 deletions

View file

@ -104,7 +104,7 @@ struct _object {
};
/* Cast argument to PyObject* type. */
#define _PyObject_CAST(op) ((PyObject*)(op))
#define _PyObject_CAST(op) _Py_reinterpret_cast(PyObject*, (op))
typedef struct {
PyObject ob_base;
@ -112,7 +112,7 @@ typedef struct {
} PyVarObject;
/* Cast argument to PyVarObject* type. */
#define _PyVarObject_CAST(op) ((PyVarObject*)(op))
#define _PyVarObject_CAST(op) _Py_reinterpret_cast(PyVarObject*, (op))
// Test if the 'x' object is the 'y' object, the same as "x is y" in Python.
@ -780,7 +780,8 @@ static inline int PyType_Check(PyObject *op) {
# define PyType_Check(op) PyType_Check(_PyObject_CAST(op))
#endif
#define _PyType_CAST(op) (assert(PyType_Check(op)), (PyTypeObject*)(op))
#define _PyType_CAST(op) \
(assert(PyType_Check(op)), _Py_reinterpret_cast(PyTypeObject*, (op)))
static inline int PyType_CheckExact(PyObject *op) {
return Py_IS_TYPE(op, &PyType_Type);