GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-105059: Remove anonymous union from PyObject by eduardo-elizondo · Pull Request #105275 · python/cpython

Conversation

@eduardo-elizondo

@eduardo-elizondo eduardo-elizondo commented Jun 4, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

  • Issue: object.h uses an anonymous union in a struct (older C incompatible) #105059

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@eduardo-elizondo
Remove anonymous union from PyObject
79851a5
@eduardo-elizondo
eduardo-elizondo requested a review from rhettinger as a code owner June 4, 2023 02:35
@bedevere-bot bedevere-bot added the awaiting review label Jun 4, 2023
@bedevere-bot bedevere-bot mentioned this pull request Jun 4, 2023
object.h uses an anonymous union in a struct (older C incompatible) #105059
Closed
sunmy2019
sunmy2019 reviewed Jun 4, 2023
View reviewed changes
Comment thread Include/object.h
return;
}
op->ob_refcnt_split[PY_BIG_ENDIAN] = new_refcnt;
memcpy(&op->ob_refcnt, &new_refcnt, sizeof(new_refcnt));

@sunmy2019 sunmy2019 Jun 4, 2023

Copy link
Copy Markdown
Member

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.

Isn't this require the little-endian?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Jun 4, 2023

Copy link
Copy Markdown
Member

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.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Jun 14, 2023

Copy link
Copy Markdown
Member

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.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@rhettinger
rhettinger removed their request for review June 4, 2023 04:59
vstinner
vstinner reviewed Jun 4, 2023
View reviewed changes
Comment thread Include/Python.h

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

@vstinner vstinner Jun 4, 2023

Copy link
Copy Markdown
Member

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.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

bedevere-bot commented Jun 4, 2023

Copy link
Copy Markdown

🤖 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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jun 4, 2023
@vstinner vstinner mentioned this pull request Jun 6, 2023
PEP 683 (Immortal Objects): Implement Py_INCREF() as function call in limited C API 3.12 #105387
Closed
@vstinner vstinner mentioned this pull request Jun 14, 2023
[DRAFT] gh-105059: In C++, Py_INCREF() uses a function call #105764
Closed
@eduardo-elizondo

eduardo-elizondo commented Jun 14, 2023

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eduardo-elizondo eduardo-elizondo closed this Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner left review comments
@sunmy2019 sunmy2019 sunmy2019 left review comments
@rhettinger rhettinger Awaiting requested review from rhettinger rhettinger will be requested when the pull request is marked ready for review rhettinger is a code owner
@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity ZeroIntensity will be requested when the pull request is marked ready for review ZeroIntensity is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@eduardo-elizondo @bedevere-bot @vstinner @sunmy2019 @gpshead
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.