OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """ | 5 """ |
6 The descriptors used to define generated elements of the mojo python bindings. | 6 The descriptors used to define generated elements of the mojo python bindings. |
7 """ | 7 """ |
8 | 8 |
9 import array | 9 import array |
10 import itertools | 10 import itertools |
(...skipping 16 matching lines...) Expand all Loading... |
27 """ | 27 """ |
28 return value | 28 return value |
29 | 29 |
30 def GetDefaultValue(self, value): | 30 def GetDefaultValue(self, value): |
31 """ | 31 """ |
32 Returns the default value for this type associated with the given value. | 32 Returns the default value for this type associated with the given value. |
33 This method must be able to correcly handle value being None. | 33 This method must be able to correcly handle value being None. |
34 """ | 34 """ |
35 return self.Convert(value) | 35 return self.Convert(value) |
36 | 36 |
| 37 def IsUnion(self): |
| 38 """ |
| 39 Returns true if the type is a union. This is necessary to be able to |
| 40 identify a union when descriptor.py cannot be imported. |
| 41 """ |
| 42 return False |
| 43 |
37 | 44 |
38 class SerializableType(Type): | 45 class SerializableType(Type): |
39 """Describe a type that can be serialized by itself.""" | 46 """Describe a type that can be serialized by itself.""" |
40 | 47 |
41 def __init__(self, typecode): | 48 def __init__(self, typecode): |
42 Type.__init__(self) | 49 Type.__init__(self) |
43 self.typecode = typecode | 50 self.typecode = typecode |
44 self.byte_size = struct.calcsize('<%s' % self.GetTypeCode()) | 51 self.byte_size = struct.calcsize('<%s' % self.GetTypeCode()) |
45 | 52 |
46 def GetTypeCode(self): | 53 def GetTypeCode(self): |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 """Type object for floating point number types.""" | 153 """Type object for floating point number types.""" |
147 | 154 |
148 def Convert(self, value): | 155 def Convert(self, value): |
149 if value is None: | 156 if value is None: |
150 raise TypeError('None is not an floating point number.') | 157 raise TypeError('None is not an floating point number.') |
151 if not isinstance(value, (int, long, float)): | 158 if not isinstance(value, (int, long, float)): |
152 raise TypeError('%r is not a numeric type' % value) | 159 raise TypeError('%r is not a numeric type' % value) |
153 return float(value) | 160 return float(value) |
154 | 161 |
155 | 162 |
| 163 class UnionType(SerializableType): |
| 164 """Base Type object for union.""" |
| 165 |
| 166 def __init__(self, union_type_getter, nullable=False): |
| 167 SerializableType.__init__(self, 'IIQ') |
| 168 self.nullable = nullable |
| 169 self._union_type_getter = union_type_getter |
| 170 self._union_type = None |
| 171 |
| 172 def IsUnion(self): |
| 173 return True |
| 174 |
| 175 @property |
| 176 def union_type(self): |
| 177 if not self._union_type: |
| 178 self._union_type = self._union_type_getter() |
| 179 return self._union_type |
| 180 |
| 181 def Serialize(self, value, data_offset, data, handle_offset): |
| 182 if not value: |
| 183 if not self.nullable: |
| 184 raise serialization.SerializationException( |
| 185 'Trying to serialize null for non nullable type.') |
| 186 return ((0, 0, 0), []) |
| 187 |
| 188 ((size, tag, entry, new_data), new_handles) = ( |
| 189 value.SerializeInline(handle_offset)) |
| 190 if len(new_data) > 0: |
| 191 data.extend(new_data) |
| 192 entry = data_offset - 8 |
| 193 |
| 194 return ((size, tag, entry), new_handles) |
| 195 |
| 196 def Deserialize(self, value, context): |
| 197 result = self.union_type.Deserialize(context) |
| 198 if not result and not self.nullable: |
| 199 raise serialization.DeserializationException( |
| 200 'Trying to deserialize null for non nullable type.') |
| 201 return result |
| 202 |
| 203 |
156 class PointerType(SerializableType): | 204 class PointerType(SerializableType): |
157 """Base Type object for pointers.""" | 205 """Base Type object for pointers.""" |
158 | 206 |
159 def __init__(self, nullable=False): | 207 def __init__(self, nullable=False): |
160 SerializableType.__init__(self, 'Q') | 208 SerializableType.__init__(self, 'Q') |
161 self.nullable = nullable | 209 self.nullable = nullable |
162 | 210 |
163 def Serialize(self, value, data_offset, data, handle_offset): | 211 def Serialize(self, value, data_offset, data, handle_offset): |
164 if value is None and not self.nullable: | 212 if value is None and not self.nullable: |
165 raise serialization.SerializationException( | 213 raise serialization.SerializationException( |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 to_pack = [] | 475 to_pack = [] |
428 for item in value: | 476 for item in value: |
429 (new_data, new_handles) = self.sub_type.Serialize( | 477 (new_data, new_handles) = self.sub_type.Serialize( |
430 item, | 478 item, |
431 len(data) - position, | 479 len(data) - position, |
432 data, | 480 data, |
433 handle_offset + len(returned_handles)) | 481 handle_offset + len(returned_handles)) |
434 to_pack.extend(serialization.Flatten(new_data)) | 482 to_pack.extend(serialization.Flatten(new_data)) |
435 returned_handles.extend(new_handles) | 483 returned_handles.extend(new_handles) |
436 position = position + self.sub_type.GetByteSize() | 484 position = position + self.sub_type.GetByteSize() |
| 485 |
437 serialization.HEADER_STRUCT.pack_into(data, data_end, size, len(value)) | 486 serialization.HEADER_STRUCT.pack_into(data, data_end, size, len(value)) |
438 struct.pack_into('%d%s' % (len(value), self.sub_type.GetTypeCode()), | 487 # TODO(azani): Refactor so we don't have to create big formatting strings. |
| 488 struct.pack_into(('%s' % self.sub_type.GetTypeCode()) * len(value), |
439 data, | 489 data, |
440 data_end + serialization.HEADER_STRUCT.size, | 490 data_end + serialization.HEADER_STRUCT.size, |
441 *to_pack) | 491 *to_pack) |
442 return (data_offset, returned_handles) | 492 return (data_offset, returned_handles) |
443 | 493 |
444 def DeserializeArray(self, size, nb_elements, context): | 494 def DeserializeArray(self, size, nb_elements, context): |
| 495 # TODO(azani): Refactor so the format string isn't so big. |
445 values = struct.unpack_from( | 496 values = struct.unpack_from( |
446 '%d%s' % (nb_elements, self.sub_type.GetTypeCode()), | 497 nb_elements * self.sub_type.GetTypeCode(), |
447 buffer(context.data, serialization.HEADER_STRUCT.size)) | 498 buffer(context.data, serialization.HEADER_STRUCT.size)) |
448 values_per_element = len(self.sub_type.GetTypeCode()) | 499 values_per_element = len(self.sub_type.GetTypeCode()) |
449 assert nb_elements * values_per_element == len(values) | 500 assert nb_elements * values_per_element == len(values) |
450 | 501 |
451 result = [] | 502 result = [] |
452 sub_context = context.GetSubContext(serialization.HEADER_STRUCT.size) | 503 sub_context = context.GetSubContext(serialization.HEADER_STRUCT.size) |
453 for index in xrange(nb_elements): | 504 for index in xrange(nb_elements): |
454 if values_per_element == 1: | 505 if values_per_element == 1: |
455 value = values[index] | 506 value = values[index] |
456 else: | 507 else: |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
749 | 800 |
750 | 801 |
751 def _ConvertByteToBooleans(value, min_size=0): | 802 def _ConvertByteToBooleans(value, min_size=0): |
752 """Unpack an integer into a list of booleans.""" | 803 """Unpack an integer into a list of booleans.""" |
753 res = [] | 804 res = [] |
754 while value: | 805 while value: |
755 res.append(bool(value&1)) | 806 res.append(bool(value&1)) |
756 value = value / 2 | 807 value = value / 2 |
757 res.extend([False] * (min_size - len(res))) | 808 res.extend([False] * (min_size - len(res))) |
758 return res | 809 return res |
OLD | NEW |