New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-93442: Make C++ version of _Py_CAST work with 0/NULL. #93500
Conversation
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
// gh-93442: Pass 0 as NULL for PyObject* | ||
Py_XINCREF(0); | ||
Py_XDECREF(0); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit
Py_XINCREF(nullptr); | |
Py_XDECREF(nullptr); | |
Py_XINCREF(NULL); | |
Py_XDECREF(NULL); |
Thanks @nascheme for the PR |
GH-93507 is a backport of this pull request to the 3.11 branch. |
…nGH-93500) 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 8bcc3fa) Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
…h-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 8bcc3fa) Co-authored-by: Neil Schemenauer <nas-github@arctrix.com> Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
Since there is a _Py_CAST_impl() implementation for nullptr, it would be nice to add a test on nullptr. What is the purpose of |
I added a test for I'm a bit scared about how complicated |
It's more about generating "correct" code for a very tricky operation: convert any operation to But you're right that my initial concern was fixing new C++ compiler warnings: to fix a regression. See #91320 (comment) for the rationale and why it's a regression. |
Ah right, commit 713eb18: thanks! |
test_cppext is new in Python 3.11! Previously, there was simply zero test on the C++ compatibility of the Python C API ;-) |
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:
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