@@ -57,7 +57,7 @@ def CalcNumElements(fmt):
57
57
return len (elements )
58
58
59
59
60
- def Struct (name , fmt , fields , substructs = {}):
60
+ def Struct (name , fmt , fieldnames , substructs = {}):
61
61
"""Function that returns struct classes."""
62
62
63
63
class Meta (type ):
@@ -77,12 +77,12 @@ class CStruct(object):
77
77
# Name of the struct.
78
78
_name = name
79
79
# List of field names.
80
- _fields = fields
80
+ _fieldnames = fieldnames
81
81
# Dict mapping field indices to nested struct classes.
82
82
_nested = {}
83
83
84
- if isinstance (_fields , str ):
85
- _fields = _fields .split (" " )
84
+ if isinstance (_fieldnames , str ):
85
+ _fieldnames = _fieldnames .split (" " )
86
86
87
87
# Parse fmt into _format, converting any S format characters to "XXs",
88
88
# where XX is the length of the struct type's packed representation.
@@ -121,14 +121,14 @@ def __init__(self, values):
121
121
self ._Parse (values )
122
122
else :
123
123
# Initializing from a tuple.
124
- if len (values ) != len (self ._fields ):
125
- raise TypeError ("%s has exactly %d fields (%d given)" %
126
- (self ._name , len (self ._fields ), len (values )))
124
+ if len (values ) != len (self ._fieldnames ):
125
+ raise TypeError ("%s has exactly %d fieldnames (%d given)" %
126
+ (self ._name , len (self ._fieldnames ), len (values )))
127
127
self ._SetValues (values )
128
128
129
129
def _FieldIndex (self , attr ):
130
130
try :
131
- return self ._fields .index (attr )
131
+ return self ._fieldnames .index (attr )
132
132
except ValueError :
133
133
raise AttributeError ("'%s' has no attribute '%s'" %
134
134
(self ._name , attr ))
@@ -143,6 +143,15 @@ def __setattr__(self, name, value):
143
143
def __len__ (cls ):
144
144
return cls ._length
145
145
146
+ def __ne__ (self , other ):
147
+ return not self .__eq__ (other )
148
+
149
+ def __eq__ (self , other ):
150
+ return (isinstance (other , self .__class__ ) and
151
+ self ._name == other ._name and
152
+ self ._fieldnames == other ._fieldnames and
153
+ self ._values == other ._values )
154
+
146
155
@staticmethod
147
156
def _MaybePackStruct (value ):
148
157
if hasattr (value , "__metaclass__" ):# and value.__metaclass__ == Meta:
@@ -156,12 +165,14 @@ def Pack(self):
156
165
157
166
def __str__ (self ):
158
167
def FieldDesc (index , name , value ):
159
- if isinstance (value , str ) and any (c not in string .printable for c in value ):
168
+ if isinstance (value , str ) and any (
169
+ c not in string .printable for c in value ):
160
170
value = value .encode ("hex" )
161
171
return "%s=%s" % (name , value )
162
172
163
173
descriptions = [
164
- FieldDesc (i , n , v ) for i , (n , v ) in enumerate (zip (self ._fields , self ._values ))]
174
+ FieldDesc (i , n , v ) for i , (n , v ) in
175
+ enumerate (zip (self ._fieldnames , self ._values ))]
165
176
166
177
return "%s(%s)" % (self ._name , ", " .join (descriptions ))
167
178
0 commit comments