-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Adding a ctypes.Union to a ctypes.BigEndianStructure results in an error #87239
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
Comments
Placing a ctypes.Union inside of a ctypes.BigEndianStructure results in "TypeError: This type does not support other endian". I believe this is a similar problem to issue bpo-4376 (https://bugs.python.org/issue4376) Minimum repro test case: import ctypes as ct
class U(ct.Union):
_pack_=True
_fields_=[
('a', ct.c_int),
('b', ct.c_int),
]
class S(ct.BigEndianStructure):
_pack_=True
_fields_=[
('x', ct.c_int),
('y', U),
] I believe the fix is similar to that issue, though I admit I don't know enough about this code to be sure. diff --git a/Lib/ctypes/_endian.py b/Lib/ctypes/_endian.py
index 37444bd6a7..525c5e58c9 100644
--- a/Lib/ctypes/_endian.py
+++ b/Lib/ctypes/_endian.py
@@ -18,6 +18,9 @@ def _other_endian(typ):
# if typ is structure
if issubclass(typ, Structure):
return typ
+ # if typ is union:
+ if issubclass(typ, Union):
+ return typ
raise TypeError("This type does not support other endian: %s" % typ)
class _swapped_meta(type(Structure)): |
I have created a workaround, since it might take years to fix this in master. Hope it'll come in useful. For the example in https://bugs.python.org/issue43073#msg385970, but probably any combination of Unions and BigEndianStructures can be constructed this way.
|
This works in Python 3.11; presumably due to #25480. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: