[issue4376] Nested ctypes 'BigEndianStructure' fails

Thomas Heller report at bugs.python.org
Thu Nov 27 20:20:46 CET 2008


Thomas Heller <theller at ctypes.org> added the comment:

Currently, the _fields_ of ctypes Structures with non-native byte order
can only contain simple types (like int, char, but not pointers), and
arrays of those.

Since this is all Python code (in Lib/ctypes/endian.py) it should be
possible to extend the code to handle other types as well.

If we do this, it must be decided if a Structure (call it 'part' for
this discussion) of some byte order is contained in a _field_ of a
non-native Structure type:
- Should 'part' be inserted as is, leading to a total structure of
fields with mixed byte order?

- Should a new type be created from the 'part' _fields_, with also
non-native byte-order?

Other approaches would be possible as well...

Here is a simple patch that implements the first approach; I have not
tested if it works correctly:

Index: _endian.py
===================================================================
--- _endian.py	(revision 67045)
+++ _endian.py	(working copy)
@@ -17,6 +17,8 @@
     except AttributeError:
         if type(typ) == _array_type:
             return _other_endian(typ._type_) * typ._length_
+        if issubclass(typ, Structure):
+            return typ
         raise TypeError("This type does not support other endian: %s" %
typ)
 
 class _swapped_meta(type(Structure)):

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4376>
_______________________________________


More information about the Python-bugs-list mailing list