Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-89188: replace bitfield with struct fields in PyASCIIObject#102589

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

@davidhewitt
Copy link
Contributor

@davidhewittdavidhewitt commentedMar 10, 2023
edited by bedevere-bot
Loading

Closes#89188

I noticed thatPyASCIIObject.state is a 32-bit bitfield with 4 members, so I replaced it with 4 separateuint8_t fields. The advantage of doing this is that C bitfield layout is implementation-defined, which makes it hard to interact with them from non-C languages (e.g. Rust).

cc@encukou@vstinner as you participated in the discussion on the original issue, I'd be interested to hear if you think this looks like a suitable solution.

mejrs reacted with heart emoji
Comment on lines -136 to -137
/* Padding to ensure that PyUnicode_DATA() is always aligned to
4 bytes (see issue #19537 on m68k). */
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

This comment on alignment seemed important, so I created a test in_testinternalcapi.c which I believe codifies this.

@davidhewittdavidhewittforce-pushed theasciiobject-state-fields branch from14c79d4 to4dd82a4CompareMarch 10, 2023 19:15
@davidhewitt
Copy link
ContributorAuthor

Looks like I may not have got the gdb lib changes exactly right, I can take another look at those on a Linux system once I've had some input on whether this direction is feasible, i.e. I know it's worth me spending the time to debug.

@encukou
Copy link
Member

I don't find the Rust argument convincing -- we should expose good API functions, rather than make internal structs easy to use. Or is there a performance-sensitive use case? (If there is we might want to add some higher-level API for it, rather than making you play with bits.)

I do think this should change. Exposing compiler-implementation-defined behavior in the API is a ticking bomb.

IMO, if it changes it should change to bit masks. With this PR, there's no way to add an additional unrelated flag. (And one might be needed soon for static/immortal objects).

Both a version with bit masks and the PR as is are an API change. Per PEP 387, it needs a deprecation warning period or a SC exception.
(If you take the current PR and wrap the fields instructstate, it might not be an API change -- but as above, fouru8s are painting ourselves in a corner.)
IMO, since this isn't time-sensitive, we should deprecate thestate struct (for use outside CPython), and after a deprecation period, make it an 32-bit int and use it with use bit masks. And make the bit masks themselves private, or at leastunstable, so that users preferably go through the public accessor functions.

Meanwhile, for Rust, you can expose the accessors as actual API functions, while keeping thestatic inline versions. There's a bit of a dance to do that, see e.g. (1,2,3) forPyVectorcall_NARGS.

@davidhewitt
Copy link
ContributorAuthor

Thanks@encukou, I wasn't aware that this struct would need the deprecation cycle applied.

I'll replace this PR with one proposing API functions. To check I understand, as per thePyVectorcall_NARGS dance, I'll need to add these accessors to the stable API?

@encukou
Copy link
Member

I'll need to add these accessors to the stable API?

No, unlike vectorcall, they don't need to be in thelimited API/stable ABI. (They can be added later if needed.)
The dance is to avoid having onlyinline functions (which can be faster but are only usable from C/C++).

@davidhewitt
Copy link
ContributorAuthor

Superseded by#115211 (which may itself yet be superseded by something else, who knows!)

@davidhewittdavidhewitt deleted the asciiobject-state-fields branchFebruary 9, 2024 14:00
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@iritkatrieliritkatrielAwaiting requested review from iritkatriel

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Reliance on C bit fields in C API is undefined behavior

4 participants

@davidhewitt@encukou@arhadthedev@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp