mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
gh-94731: Revert to C-style casts for _Py_CAST (GH-94782) (#94849)
Co-authored-by: da-woods <dw-git@d-woods.co.uk>
(cherry picked from commit 6cbb57f62d)
This commit is contained in:
parent
a6c4ca9b7c
commit
d9107aa015
5 changed files with 85 additions and 59 deletions
|
|
@ -14,62 +14,14 @@
|
|||
#endif
|
||||
|
||||
|
||||
// Macro to use C++ static_cast<>, reinterpret_cast<> and const_cast<>
|
||||
// in the Python C API.
|
||||
//
|
||||
// In C++, _Py_CAST(type, expr) converts a constant expression to a
|
||||
// non constant type using const_cast<type>. For example,
|
||||
// _Py_CAST(PyObject*, op) can convert a "const PyObject*" to
|
||||
// "PyObject*".
|
||||
//
|
||||
// The type argument must not be a constant type.
|
||||
// Macro to use C++ static_cast<> in the Python C API.
|
||||
#ifdef __cplusplus
|
||||
#include <cstddef>
|
||||
# define _Py_STATIC_CAST(type, expr) static_cast<type>(expr)
|
||||
extern "C++" {
|
||||
namespace {
|
||||
template <typename type>
|
||||
inline type _Py_CAST_impl(long int ptr) {
|
||||
return reinterpret_cast<type>(ptr);
|
||||
}
|
||||
template <typename type>
|
||||
inline type _Py_CAST_impl(int ptr) {
|
||||
return reinterpret_cast<type>(ptr);
|
||||
}
|
||||
#if __cplusplus >= 201103
|
||||
template <typename type>
|
||||
inline type _Py_CAST_impl(std::nullptr_t) {
|
||||
return static_cast<type>(nullptr);
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename type, typename expr_type>
|
||||
inline type _Py_CAST_impl(expr_type *expr) {
|
||||
return reinterpret_cast<type>(expr);
|
||||
}
|
||||
|
||||
template <typename type, typename expr_type>
|
||||
inline type _Py_CAST_impl(expr_type const *expr) {
|
||||
return reinterpret_cast<type>(const_cast<expr_type *>(expr));
|
||||
}
|
||||
|
||||
template <typename type, typename expr_type>
|
||||
inline type _Py_CAST_impl(expr_type &expr) {
|
||||
return static_cast<type>(expr);
|
||||
}
|
||||
|
||||
template <typename type, typename expr_type>
|
||||
inline type _Py_CAST_impl(expr_type const &expr) {
|
||||
return static_cast<type>(const_cast<expr_type &>(expr));
|
||||
}
|
||||
}
|
||||
}
|
||||
# define _Py_CAST(type, expr) _Py_CAST_impl<type>(expr)
|
||||
|
||||
#else
|
||||
# define _Py_STATIC_CAST(type, expr) ((type)(expr))
|
||||
# define _Py_CAST(type, expr) ((type)(expr))
|
||||
#endif
|
||||
// Macro to use the more powerful/dangerous C-style cast even in C++.
|
||||
#define _Py_CAST(type, expr) ((type)(expr))
|
||||
|
||||
// Static inline functions should use _Py_NULL rather than using directly NULL
|
||||
// to prevent C++ compiler warnings. On C++11 and newer, _Py_NULL is defined as
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue