and static_cast<> casts when the Python C API is used ...">

gh-91320: Add _Py_reinterpret_cast() macro by vstinner · Pull Request #91959 · python/cpython (original) (raw)

Expand Up

@@ -14,6 +14,16 @@

#endif

// Macro to use C++ static_cast<> and reinterpret_cast<> in the Python C API

#ifdef __cplusplus

# define _Py_static_cast(type, expr) static_cast(expr)

# define _Py_reinterpret_cast(type, expr) reinterpret_cast(expr)

#else

# define _Py_static_cast(type, expr) ((type)(expr))

# define _Py_reinterpret_cast(type, expr) ((type)(expr))

#endif

/* Defines to build Python and its standard library:

*

* - Py_BUILD_CORE: Build Python core. Give access to Python internals, but

Expand Down Expand Up

@@ -295,10 +305,11 @@ extern "C" {

* VALUE may be evaluated more than once.

*/

#ifdef Py_DEBUG

#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \

(assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))

define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \

(assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))

#else

#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)

# define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \

_Py_reinterpret_cast(NARROW, (VALUE))

#endif

Expand Down