diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index ac6493892068e9..68cdcc75fe0a0a 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -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) from _ctypes import POINTER, pointer, _pointer_type_cache diff --git a/Lib/test/test_ctypes/test_byteswap.py b/Lib/test/test_ctypes/test_byteswap.py index 78eff0392c4548..0a8be309821f8b 100644 --- a/Lib/test/test_ctypes/test_byteswap.py +++ b/Lib/test/test_ctypes/test_byteswap.py @@ -10,7 +10,7 @@ c_byte, c_ubyte, c_char, c_wchar, c_void_p, c_short, c_ushort, c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong, - c_uint32, c_float, c_double) + c_uint32, c_float, c_double, c_bool) def bin(s): @@ -173,6 +173,9 @@ def test_endian_other(self): self.assertIs(c_char.__ctype_le__, c_char) self.assertIs(c_char.__ctype_be__, c_char) + self.assertIs(c_bool.__ctype_le__, c_bool) + self.assertIs(c_bool.__ctype_be__, c_bool) + def test_struct_fields_unsupported_byte_order(self): fields = [ @@ -373,13 +376,13 @@ def test_build_struct_union_opposite_system_byteorder(self): _Union = LittleEndianUnion class S1(_Structure): - _fields_ = [("a", c_byte), ("b", c_byte)] + _fields_ = [("a", c_byte), ("b", c_byte), ("c", c_bool)] class U1(_Union): - _fields_ = [("s1", S1), ("ab", c_short)] + _fields_ = [("s1", S1), ("ab", c_short), ("ac", c_bool)] class S2(_Structure): - _fields_ = [("u1", U1), ("c", c_byte)] + _fields_ = [("u1", U1), ("c", c_byte), ("d", c_bool)] if __name__ == "__main__": diff --git a/Misc/NEWS.d/next/C_API/2024-11-29-18-33-27.gh-issue-74695.xXInJz.rst b/Misc/NEWS.d/next/C_API/2024-11-29-18-33-27.gh-issue-74695.xXInJz.rst new file mode 100644 index 00000000000000..ba6f7eda19c836 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2024-11-29-18-33-27.gh-issue-74695.xXInJz.rst @@ -0,0 +1 @@ +Added support for :class:`ctypes.c_bool` for structs/unions that have an endianness opposite of the current system.