Skip to content
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-105059: Remove anonymous union from PyObject #105275

Closed
wants to merge 1 commit into from

Conversation

eduardo-elizondo
Copy link
Contributor

@eduardo-elizondo eduardo-elizondo commented Jun 4, 2023

Currently, the use of an anonymous union causes warnings in C++ extensions. This updates the implementation of PyObject to not rely on anonymous unions and remove these warnings.

Current Issue that goes away after this patch:

#include <Python.h>
int main (void) { return 0; }

-> gcc -I/path/to/include/python3.13 -std=c99 -pedantic repro.c
In file included from repro.c:1:
In file included from /Users/eelizondo/dev/cpython_no_union/build_install/include/python3.13/Python.h:44:
/Users/eelizondo/dev/cpython_no_union/build_install/include/python3.13/object.h:168:5: warning: anonymous unions are a C11 extension [-Wc11-extensions]
    union {

Note: Currently, this is being fixed through the use of a memcpy but a couple of more options will be tried as well. I will also follow-up with benchmark numbers to verify we are not regressing.

PY_UINT32_T new_refcnt = cur_refcnt + 1;
if (new_refcnt == 0) {
return;
}
op->ob_refcnt_split[PY_BIG_ENDIAN] = new_refcnt;
memcpy(&op->ob_refcnt, &new_refcnt, sizeof(new_refcnt));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this require the little-endian?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move the union here, use memcpy() and only use the bits that you need in the union?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem of the current implementation (without this change) is the usage of anonymous union in PyObject: it is not compatible with (strict) C99. A named union is fine with C89 and C99. It's more about finding the right syntax to get the most efficient code.

@rhettinger rhettinger removed their request for review June 4, 2023 04:59
@@ -34,6 +33,7 @@

#include <assert.h> // assert()
#include <wchar.h> // wchar_t
#include <string.h> // memcpy()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes me sad to use memcpy() in the limited C API which should attempt to hide as many implementation details as possible. I don't get how the immortal object change interacts with the stable ABI. I would prefer to convert Py_INCREF/Py_DECREF to opaque function calls. It would avoid any C and C++ compatibility issue. At least, it would make the ABI a little bit more "forward compatible".

But that can be done later, since the priority is to unblock the next Python 3.12 beta release.

By the way, the nogil fork has also a completely different implementation which is also ABI incompatible unless these functions are implemented as opaque function calls.

@gpshead gpshead marked this pull request as draft June 4, 2023 19:21
@gpshead gpshead added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jun 4, 2023
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @gpshead for commit 79851a5 🤖

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@eduardo-elizondo
Copy link
Contributor Author

I'll be closing since there's better discussions on this in other places

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants