8000 gh-91320: Add _Py_reinterpret_cast() macro (#91959) · python/cpython@29e2245 · GitHub
[go: up one dir, main page]

Skip to content

Commit 29e2245

Browse files
authored
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.
1 parent f882d33 commit 29e2245

10 files changed

+42
-17
lines changed

Include/cpython/abstract.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ PyAPI_FUNC(PyObject *) _PyObject_MakeTpCall(
5050
PyObject *const *args, Py_ssize_t nargs,
5151
PyObject *keywords);
5252

53-
#define PY_VECTORCALL_ARGUMENTS_OFFSET ((size_t)1 << (8 * sizeof(size_t) - 1))
53+
#define PY_VECTORCALL_ARGUMENTS_OFFSET \
54+
(_Py_static_cast(size_t, 1) << (8 * sizeof(size_t) - 1))
5455

5556
static inline Py_ssize_t
5657
PyVectorcall_NARGS(size_t n)

Include/cpython/listobject.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *);
2525
PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
2626

2727
/* Cast argument to PyListObject* type. */
28-
#define _PyList_CAST(op) (assert(PyList_Check(op)), (PyListObject *)(op))
28+
#define _PyList_CAST(op) \
29+
(assert(PyList_Check(op)), _Py_reinterpret_cast(PyListObject*, (op)))
2930

3031
// Macros and static inline functions, trading safety for speed
3132

Include/cpython/methodobject.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ PyAPI_DATA(PyTypeObject) PyCMethod_Type;
88
#define PyCMethod_Check(op) PyObject_TypeCheck(op, &PyCMethod_Type)
99

1010
#define _PyCFunctionObject_CAST(func) \
11-
(assert(PyCFunction_Check(func)), (PyCFunctionObject *)(func))
11+
(assert(PyCFunction_Check(func)), \
12+
_Py_reinterpret_cast(PyCFunctionObject*, (func)))
1213
#define _PyCMethodObject_CAST(func) \
13-
(assert(PyCMethod_Check(func)), (PyCMethodObject *)(func))
14+
(assert(PyCMethod_Check(func)), \
15+
_Py_reinterpret_cast(PyCMethodObject*, (func)))
1416

1517
/* Macros for direct access to these values. Type checks are *not*
1618
done, so use with care. */

Include/cpython/tupleobject.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ PyAPI_FUNC(int) _PyTuple_Resize(PyObject **, Py_ssize_t);
1414
PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *);
1515

1616
/* Cast argument to PyTupleObject* type. */
17-
#define _PyTuple_CAST(op) (assert(PyTuple_Check(op)), (PyTupleObject *)(op))
17+
#define _PyTuple_CAST(op) \
18+
(assert(PyTuple_Check(op)), _Py_reinterpret_cast(PyTupleObject*, (op)))
1819

1920
// Macros and static inline functions, trading safety for speed
2021

Include/cpython/unicodeobject.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,14 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
236236

237237

238238
#define _PyASCIIObject_CAST(op) \
239-
(assert(PyUnicode_Check(op)), (PyASCIIObject*)(op))
239+
(assert(PyUnicode_Check(op)), \
240+
_Py_reinterpret_cast(PyASCIIObject*, (op)))
240241
#define _PyCompactUnicodeObject_CAST(op) \
241-
(assert(PyUnicode_Check(op)), (PyCompactUnicodeObject*)(op))
242+
(assert(PyUnicode_Check(op)), \
243+
_Py_reinterpret_cast(PyCompactUnicodeObject*, (op)))
242244
#define _PyUnicodeObject_CAST(op) \
243-
(assert(PyUnicode_Check(op)), (PyUnicodeObject*)(op))
245+
(assert(PyUnicode_Check(op)), \
246+
_Py_reinterpret_cast(PyUnicodeObject*, (op)))
244247

245248

246249
/* --- Flexible String Representation Helper Macros (PEP 393) -------------- */

Include/methodobject.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ typedef PyObject *(*PyCMethod)(PyObject *, PyTypeObject *, PyObject *const *,
4242
// used to prevent a compiler warning. If the function has a single parameter,
4343
// it triggers an undefined behavior when Python calls it with 2 parameters
4444
// (bpo-33012).
45-
#define _PyCFunction_CAST(func) ((PyCFunction)(void(*)(void))(func))
45+
#define _PyCFunction_CAST(func) \
46+
_Py_reinterpret_cast(PyCFunction, \
47+
_Py_reinterpret_cast(void(*)(void), (func)))
4648

4749
PyAPI_FUNC(PyCFunction) PyCFunction_GetFunction(PyObject *);
4850
PyAPI_FUNC(PyObject *) PyCFunction_GetSelf(PyObject *);

Include/object.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ struct _object {
104104
};
105105

106106
/* Cast argument to PyObject* type. */
107-
#define _PyObject_CAST(op) ((PyObject*)(op))
107+
#define _PyObject_CAST(op) _Py_reinterpret_cast(PyObject*, (op))
108108

109109
typedef struct {
110110
PyObject ob_base;
111111
Py_ssize_t ob_size; /* Number of items in variable part */
112112
} PyVarObject;
113113

114114
/* Cast argument to PyVarObject* type. */
115-
#define _PyVarObject_CAST(op) ((PyVarObject*)(op))
115+
#define _PyVarObject_CAST(op) _Py_reinterpret_cast(PyVarObject*, (op))
116116

117117

118118
// 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) {
780780
# define PyType_Check(op) PyType_Check(_PyObject_CAST(op))
781781
#endif
782782

783-
#define _PyType_CAST(op) (assert(PyType_Check(op)), (PyTypeObject*)(op))
783+
#define _PyType_CAST(op) \
784+
(assert(PyType_Check(op)), _Py_reinterpret_cast(PyTypeObject*, (op)))
784785

785786
static inline int PyType_CheckExact(PyObject *op) {
786787
return Py_IS_TYPE(op, &PyType_Type);

Include/objimpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
182182
PyAPI_FUNC(void) PyObject_GC_Del(void *);
183183

184184
#define PyObject_GC_New(type, typeobj) \
185-
( (type *) _PyObject_GC_New(typeobj) )
185+
_Py_reinterpret_cast(type*, _PyObject_GC_New(typeobj))
186186
#define PyObject_GC_NewVar(type, typeobj, n) \
187-
( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
187+
_Py_reinterpret_cast(type*, _PyObject_GC_NewVar((typeobj), (n)))
188188

189189
PyAPI_FUNC(int) PyObject_GC_IsTracked(PyObject *);
190190
PyAPI_FUNC(int) PyObject_GC_IsFinalized(PyObject *);

Include/pyport.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
#endif
1515

1616

17+
// Macro to use C++ static_cast<> and reinterpret_cast<> in the Python C API
18+
#ifdef __cplusplus
19+
# define _Py_static_cast(type, expr) static_cast<type>(expr)
20+
# define _Py_reinterpret_cast(type, expr) reinterpret_cast<type>(expr)
21+
#else
22+
# define _Py_static_cast(type, expr) ((type)(expr))
23+
# define _Py_reinterpret_cast(type, expr) ((type)(expr))
24+
#endif
25+
26+
1727
/* Defines to build Python and its standard library:
1828
*
1929
* - Py_BUILD_CORE: Build Python core. Give access to Python internals, but
@@ -295,10 +305,11 @@ extern "C" {
295305
* VALUE may be evaluated more than once.
296306
*/
297307
#ifdef Py_DEBUG
298-
#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
299-
(assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
308+
# define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
309+
(assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
300310
#else
301-
#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
311+
# define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
312+
_Py_reinterpret_cast(NARROW, (VALUE))
302313
#endif
303314

304315

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix C++ compiler warnings about "old-style cast" (``g++ -Wold-style-cast``) in
2+
the Python C API. Use C++ ``reinterpret_cast<>`` and ``static_cast<>`` casts
3+
when the Python C API is used in C++. Patch by Victor Stinner.

0 commit comments

Comments
 (0)
0