diff --git a/Include/pyport.h b/Include/pyport.h index 5a9adf162bfd9d..a78e290931fffe 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -24,9 +24,23 @@ // // The type argument must not be a constant type. #ifdef __cplusplus +#include # define _Py_STATIC_CAST(type, expr) static_cast(expr) extern "C++" { namespace { + template + inline type _Py_CAST_impl(long int ptr) { + return reinterpret_cast(ptr); + } + template + inline type _Py_CAST_impl(int ptr) { + return reinterpret_cast(ptr); + } + template + inline type _Py_CAST_impl(std::nullptr_t) { + return static_cast(nullptr); + } + template inline type _Py_CAST_impl(expr_type *expr) { return reinterpret_cast(expr); diff --git a/Lib/test/_testcppext.cpp b/Lib/test/_testcppext.cpp index eade7ccdaaff7a..70f434e6789ad7 100644 --- a/Lib/test/_testcppext.cpp +++ b/Lib/test/_testcppext.cpp @@ -74,6 +74,10 @@ test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) Py_INCREF(strong_ref); Py_DECREF(strong_ref); + // gh-93442: Pass 0 as NULL for PyObject* + Py_XINCREF(0); + Py_XDECREF(0); + Py_DECREF(obj); Py_RETURN_NONE; } diff --git a/Misc/NEWS.d/next/C API/2022-06-04-13-15-41.gh-issue-93442.4M4NDb.rst b/Misc/NEWS.d/next/C API/2022-06-04-13-15-41.gh-issue-93442.4M4NDb.rst new file mode 100644 index 00000000000000..f48ed37c814454 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2022-06-04-13-15-41.gh-issue-93442.4M4NDb.rst @@ -0,0 +1,3 @@ +Add C++ overloads for _Py_CAST_impl() to handle 0/NULL. This will allow C++ +extensions that pass 0 or NULL to macros using _Py_CAST() to continue to +compile.