-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-74695: Add support for ctypes.c_bool on opposite endian systems #127280
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
base: main
Are you sure you want to change the base?
Conversation
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
e02338d
to
99d064a
Compare
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
I'm thinking this probably won't need a news entry since it's just fixing a small bug that's been outstanding for quite a while. But I'm sure it will affect someone's workflow. |
If it's a user-visible change - it worth a news entry. |
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 implementation looks pretty good, apart from my nitpicks. This needs a test though, could you add one somewhere in test_ctypes? (You should be able to just retrofit the reproducer from the issue as a test.)
|
||
Previously, on a little endian system, attempting to use a ``c_bool`` field in a ``BigEndianStructure`` would result in an exception (TypeError: This type does not support other endian: <class 'ctypes.c_bool'>). Now, it works as expected. |
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.
Love the effort on the blurb entry, but it doesn't need to be nearly this long. I think just the first sentence is fine.
Previously, on a little endian system, attempting to use a ``c_bool`` field in a ``BigEndianStructure`` would result in an exception (TypeError: This type does not support other endian: <class 'ctypes.c_bool'>). Now, it works as expected. |
@@ -0,0 +1,3 @@ | ||
Added support for ``ctypes.c_bool`` for structs/unions that have an endianness opposite of the current system. |
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.
Let's use a reference:
Added support for ``ctypes.c_bool`` for structs/unions that have an endianness opposite of the current system. | |
Added support for :class:`ctypes.c_bool` for structs/unions that have an endianness opposite of the current system. |
@@ -263,6 +263,8 @@ class c_void_p(_SimpleCData): | ||
|
||
class c_bool(_SimpleCData): | ||
_type_ = "?" | ||
c_bool.__ctype_le__ = c_bool.__ctype_be__ = c_bool | ||
_check_size(c_bool) |
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.
Not disagreeing that this should be here, I'd prefer to keep unrelated changes out of a PR. Could you open a seperate PR that adds this? (No need for an issue or blurb entry on it, just the one line change should be fine; tag me and I'll add skip issue
and skip news
.)
_check_size(c_bool) |
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.
I appreciate the input but it's not really worth my time to split this single line out into a separate PR. Any real reason it can't ride along with this one?
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.
We don't really like to accept changes that are unrelated to the actual fix. You can leave it in, but expect pushback from other reviewers.
Hey @ZeroIntensity, thanks for the review! This is my first time contributing to Python so I'm still getting a feel for how this project works. I just added some tests and cleaned up the news blurb. |
0507454
to
356d934
Compare
As I noted on the issue, |
As far as I know, basically all systems interpret a bool as 1 byte, but I guess it's not always true. Why not just apply the |
Then, using If you know your bools are single bytes, you should use |
It would break, but I don't think |
What would crash? You can't really use the struct in C, where working with it would crash. Non-native endianness is mostly useful for data sent from another system. (Arguably it would fit better in For data interchange, you really should be using fixed-width types, e.g. |
Using a I can't say much about use cases: I haven't ever used ctypes for data exchange, so I'd be mostly speculating. I'd like to hear from @zbarnett before closing this. |
Sorry for dropping the ball here.
Generally yes, but here, “somewhere” is inside a struct with a non-native byte order. That is, a struct that's not meant for the current architecture. You can't quite use it from C.
Same here. @zbarnett, do you have a use case for this? |
I appreciate the time you both have taken to think through this. I'm using ctypes to read/write data across the network to other systems that have opposite endianness. I am currently using Regarding portability, aren't all the builtin types in ctypes provided assuming some things are constant (like bit endianness) that aren't actually 100% constant across all systems? I had, probably wrongly, assumed that a bool would almost always be represented by 8 bits. But I do see the value in explicitly defining the size in bits of all the types when interfacing with other systems. In that case, would we want something like Either way, I would assume that if |
Thanks for the input! Yes, I think
You also want I'm sorry for complicating your simple use case, but if we add something that's hard to generalize, we'll need to deal with the edge cases piling up later :(
Unfortunately, |
Uh oh!
There was an error while loading. Please reload this page.