annotate Lib/ctypes/_endian.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 7cdbf627f958
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38780
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 36905
diff changeset
1 ######################################################################
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 36905
diff changeset
2 # This file should be kept compatible with Python 2.3, see PEP 291. #
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 36905
diff changeset
3 ######################################################################
36899
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
4 import sys
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
5 from ctypes import *
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
6
74775
7cdbf627f958 Issue #12142: Fixed reference cycle when importing ctypes
Meador Inge <meadori@gmail.com>
parents: 71313
diff changeset
7 _array_type = type(Array)
36899
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
8
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
9 def _other_endian(typ):
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
10 """Return the type with the 'other' byte order. Simple types like
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
11 c_int and so on already have __ctype_be__ and __ctype_le__
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
12 attributes which contain the types, for more complicated types
71313
a9d0fab19d5e Close #4376: ctypes now supports nested structures in a endian different than
Victor Stinner <victor.stinner@haypocalc.com>
parents: 38780
diff changeset
13 arrays and structures are supported.
36899
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
14 """
71313
a9d0fab19d5e Close #4376: ctypes now supports nested structures in a endian different than
Victor Stinner <victor.stinner@haypocalc.com>
parents: 38780
diff changeset
15 # check _OTHER_ENDIAN attribute (present if typ is primitive type)
a9d0fab19d5e Close #4376: ctypes now supports nested structures in a endian different than
Victor Stinner <victor.stinner@haypocalc.com>
parents: 38780
diff changeset
16 if hasattr(typ, _OTHER_ENDIAN):
36899
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
17 return getattr(typ, _OTHER_ENDIAN)
71313
a9d0fab19d5e Close #4376: ctypes now supports nested structures in a endian different than
Victor Stinner <victor.stinner@haypocalc.com>
parents: 38780
diff changeset
18 # if typ is array
a9d0fab19d5e Close #4376: ctypes now supports nested structures in a endian different than
Victor Stinner <victor.stinner@haypocalc.com>
parents: 38780
diff changeset
19 if isinstance(typ, _array_type):
a9d0fab19d5e Close #4376: ctypes now supports nested structures in a endian different than
Victor Stinner <victor.stinner@haypocalc.com>
parents: 38780
diff changeset
20 return _other_endian(typ._type_) * typ._length_
a9d0fab19d5e Close #4376: ctypes now supports nested structures in a endian different than
Victor Stinner <victor.stinner@haypocalc.com>
parents: 38780
diff changeset
21 # if typ is structure
a9d0fab19d5e Close #4376: ctypes now supports nested structures in a endian different than
Victor Stinner <victor.stinner@haypocalc.com>
parents: 38780
diff changeset
22 if issubclass(typ, Structure):
a9d0fab19d5e Close #4376: ctypes now supports nested structures in a endian different than
Victor Stinner <victor.stinner@haypocalc.com>
parents: 38780
diff changeset
23 return typ
a9d0fab19d5e Close #4376: ctypes now supports nested structures in a endian different than
Victor Stinner <victor.stinner@haypocalc.com>
parents: 38780
diff changeset
24 raise TypeError("This type does not support other endian: %s" % typ)
36899
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
25
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
26 class _swapped_meta(type(Structure)):
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
27 def __setattr__(self, attrname, value):
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
28 if attrname == "_fields_":
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
29 fields = []
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
30 for desc in value:
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
31 name = desc[0]
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
32 typ = desc[1]
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
33 rest = desc[2:]
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
34 fields.append((name, _other_endian(typ)) + rest)
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
35 value = fields
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
36 super(_swapped_meta, self).__setattr__(attrname, value)
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
37
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
38 ################################################################
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
39
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
40 # Note: The Structure metaclass checks for the *presence* (not the
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
41 # value!) of a _swapped_bytes_ attribute to determine the bit order in
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
42 # structures containing bit fields.
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
43
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
44 if sys.byteorder == "little":
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
45 _OTHER_ENDIAN = "__ctype_be__"
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
46
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
47 LittleEndianStructure = Structure
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
48
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
49 class BigEndianStructure(Structure):
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
50 """Structure with big endian byte order"""
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
51 __metaclass__ = _swapped_meta
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
52 _swappedbytes_ = None
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
53
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
54 elif sys.byteorder == "big":
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
55 _OTHER_ENDIAN = "__ctype_le__"
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
56
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
57 BigEndianStructure = Structure
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
58 class LittleEndianStructure(Structure):
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
59 """Structure with little endian byte order"""
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
60 __metaclass__ = _swapped_meta
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
61 _swappedbytes_ = None
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
62
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
63 else:
0023c0cf8feb Copy ctypes-0.9.9.4 Python modules from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
64 raise RuntimeError("Invalid byteorder")