diff options
author | Victor Stinner <vstinner@python.org> | 2022-04-27 08:40:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-27 08:40:57 (GMT) |
commit | 29e2245ad5d28b565fe0d0d667d283708c6e832c (patch) | |
tree | 8147203346a87146036c0087893f910409d886b0 /Include/cpython | |
parent | f882d33778ee2625ab32d90e28edb6878fb8af93 (diff) | |
download | cpython-29e2245ad5d28b565fe0d0d667d283708c6e832c.zip cpython-29e2245ad5d28b565fe0d0d667d283708c6e832c.tar.gz cpython-29e2245ad5d28b565fe0d0d667d283708c6e832c.tar.bz2 |
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.
Diffstat (limited to 'Include/cpython')
-rw-r--r-- | Include/cpython/abstract.h | 3 | ||||
-rw-r--r-- | Include/cpython/listobject.h | 3 | ||||
-rw-r--r-- | Include/cpython/methodobject.h | 6 | ||||
-rw-r--r-- | Include/cpython/tupleobject.h | 3 | ||||
-rw-r--r-- | Include/cpython/unicodeobject.h | 9 |
5 files changed, 16 insertions, 8 deletions
diff --git a/Include/cpython/abstract.h b/Include/cpython/abstract.h index bdc0c49..42f766d 100644 --- a/Include/cpython/abstract.h +++ b/Include/cpython/abstract.h @@ -50,7 +50,8 @@ PyAPI_FUNC(PyObject *) _PyObject_MakeTpCall( PyObject *const *args, Py_ssize_t nargs, PyObject *keywords); -#define PY_VECTORCALL_ARGUMENTS_OFFSET ((size_t)1 << (8 * sizeof(size_t) - 1)) +#define PY_VECTORCALL_ARGUMENTS_OFFSET \ + (_Py_static_cast(size_t, 1) << (8 * sizeof(size_t) - 1)) static inline Py_ssize_t PyVectorcall_NARGS(size_t n) diff --git a/Include/cpython/listobject.h b/Include/cpython/listobject.h index 0cd6921..298bfcb 100644 --- a/Include/cpython/listobject.h +++ b/Include/cpython/listobject.h @@ -25,7 +25,8 @@ PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *); PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out); /* Cast argument to PyListObject* type. */ -#define _PyList_CAST(op) (assert(PyList_Check(op)), (PyListObject *)(op)) +#define _PyList_CAST(op) \ + (assert(PyList_Check(op)), _Py_reinterpret_cast(PyListObject*, (op))) // Macros and static inline functions, trading safety for speed diff --git a/Include/cpython/methodobject.h b/Include/cpython/methodobject.h index 46d1777..68cecfe 100644 --- a/Include/cpython/methodobject.h +++ b/Include/cpython/methodobject.h @@ -8,9 +8,11 @@ PyAPI_DATA(PyTypeObject) PyCMethod_Type; #define PyCMethod_Check(op) PyObject_TypeCheck(op, &PyCMethod_Type) #define _PyCFunctionObject_CAST(func) \ - (assert(PyCFunction_Check(func)), (PyCFunctionObject *)(func)) + (assert(PyCFunction_Check(func)), \ + _Py_reinterpret_cast(PyCFunctionObject*, (func))) #define _PyCMethodObject_CAST(func) \ - (assert(PyCMethod_Check(func)), (PyCMethodObject *)(func)) + (assert(PyCMethod_Check(func)), \ + _Py_reinterpret_cast(PyCMethodObject*, (func))) /* Macros for direct access to these values. Type checks are *not* done, so use with care. */ diff --git a/Include/cpython/tupleobject.h b/Include/cpython/tupleobject.h index fd15ecd..8089e24 100644 --- a/Include/cpython/tupleobject.h +++ b/Include/cpython/tupleobject.h @@ -14,7 +14,8 @@ PyAPI_FUNC(int) _PyTuple_Resize(PyObject **, Py_ssize_t); PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *); /* Cast argument to PyTupleObject* type. */ -#define _PyTuple_CAST(op) (assert(PyTuple_Check(op)), (PyTupleObject *)(op)) +#define _PyTuple_CAST(op) \ + (assert(PyTuple_Check(op)), _Py_reinterpret_cast(PyTupleObject*, (op))) // Macros and static inline functions, trading safety for speed diff --git a/Include/cpython/unicodeobject.h b/Include/cpython/unicodeobject.h index 2712c58..0397f12 100644 --- a/Include/cpython/unicodeobject.h +++ b/Include/cpython/unicodeobject.h @@ -236,11 +236,14 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency( #define _PyASCIIObject_CAST(op) \ - (assert(PyUnicode_Check(op)), (PyASCIIObject*)(op)) + (assert(PyUnicode_Check(op)), \ + _Py_reinterpret_cast(PyASCIIObject*, (op))) #define _PyCompactUnicodeObject_CAST(op) \ - (assert(PyUnicode_Check(op)), (PyCompactUnicodeObject*)(op)) + (assert(PyUnicode_Check(op)), \ + _Py_reinterpret_cast(PyCompactUnicodeObject*, (op))) #define _PyUnicodeObject_CAST(op) \ - (assert(PyUnicode_Check(op)), (PyUnicodeObject*)(op)) + (assert(PyUnicode_Check(op)), \ + _Py_reinterpret_cast(PyUnicodeObject*, (op))) /* --- Flexible String Representation Helper Macros (PEP 393) -------------- */ |