diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-06-05 05:15:59 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-05 05:15:59 (GMT) |
commit | d97e2c52d3c7d0f3800cbed3371005295be78921 (patch) | |
tree | b34ff9dc033f243698e143f068fb1b9d0d512ecc | |
parent | 4443c285a61f2dab4de6c383abad3580bf2aa06d (diff) | |
download | cpython-d97e2c52d3c7d0f3800cbed3371005295be78921.zip cpython-d97e2c52d3c7d0f3800cbed3371005295be78921.tar.gz cpython-d97e2c52d3c7d0f3800cbed3371005295be78921.tar.bz2 |
gh-93442: Make C++ version of _Py_CAST work with 0/NULL. (GH-93500) (gh-93507)
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. Without this, you get an error like:
invalid ‘static_cast’ from type ‘int’ to type ‘_object*’
The modern way to use a NULL value in C++ is to use nullptr. However,
we want to not break extensions that do things the old way.
Co-authored-by: serge-sans-paille
(cherry picked from commit 8bcc3fa3453e28511d04eaa0aa7d8e1a3495d518)
Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
-rw-r--r-- | Include/pyport.h | 14 | ||||
-rw-r--r-- | Lib/test/_testcppext.cpp | 4 | ||||
-rw-r--r-- | Misc/NEWS.d/next/C API/2022-06-04-13-15-41.gh-issue-93442.4M4NDb.rst | 3 |
3 files changed, 21 insertions, 0 deletions
diff --git a/Include/pyport.h b/Include/pyport.h index 5a9adf1..a78e290 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 <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); + } + template <typename type> + inline type _Py_CAST_impl(std::nullptr_t) { + return static_cast<type>(nullptr); + } + template <typename type, typename expr_type> inline type _Py_CAST_impl(expr_type *expr) { return reinterpret_cast<type>(expr); diff --git a/Lib/test/_testcppext.cpp b/Lib/test/_testcppext.cpp index eade7cc..70f434e 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 0000000..f48ed37 --- /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. |