summaryrefslogtreecommitdiffstats
path: root/Include
diff options
context:
space:
mode:
authorPetr Viktorin <encukou@gmail.com>2022-07-14 09:57:18 (GMT)
committerGitHub <noreply@github.com>2022-07-14 09:57:18 (GMT)
commit6cbb57f62d345d7a5d6aeb1b3b5d37a845344d5e (patch)
tree4a36bb68bad648cac32fe0e49b9888dd9616826f /Include
parent81dca70d704d0834d8c30580e648a973250b2973 (diff)
downloadcpython-6cbb57f62d345d7a5d6aeb1b3b5d37a845344d5e.zip
cpython-6cbb57f62d345d7a5d6aeb1b3b5d37a845344d5e.tar.gz
cpython-6cbb57f62d345d7a5d6aeb1b3b5d37a845344d5e.tar.bz2
gh-94731: Revert to C-style casts for _Py_CAST (GH-94782)
Co-authored-by: da-woods <dw-git@d-woods.co.uk>
Diffstat (limited to 'Include')
-rw-r--r--Include/pyport.h54
1 files changed, 3 insertions, 51 deletions
diff --git a/Include/pyport.h b/Include/pyport.h
index 313bc8d..b3ff2f4 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -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