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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 """Type object for floating point number types.""" | 146 """Type object for floating point number types.""" |
147 | 147 |
148 def Convert(self, value): | 148 def Convert(self, value): |
149 if value is None: | 149 if value is None: |
150 raise TypeError('None is not an floating point number.') | 150 raise TypeError('None is not an floating point number.') |
151 if not isinstance(value, (int, long, float)): | 151 if not isinstance(value, (int, long, float)): |
152 raise TypeError('%r is not a numeric type' % value) | 152 raise TypeError('%r is not a numeric type' % value) |
153 return float(value) | 153 return float(value) |
154 | 154 |
155 | 155 |
| 156 class UnionType(SerializableType): |
| 157 """Base Type object for union.""" |
| 158 |
| 159 def __init__(self, union_type_getter, nullable=False): |
| 160 SerializableType.__init__(self, 'IIQ') |
| 161 self.nullable = nullable |
| 162 self._union_type_getter = union_type_getter |
| 163 self._union_type = None |
| 164 |
| 165 @property |
| 166 def union_type(self): |
| 167 if not self._union_type: |
| 168 self._union_type = self._union_type_getter() |
| 169 return self._union_type |
| 170 |
| 171 def Serialize(self, value, data_offset, data, handle_offset): |
| 172 if not value: |
| 173 if not self.nullable: |
| 174 raise serialization.SerializationException( |
| 175 'Trying to serialize null for non nullable type.') |
| 176 return ((0, 0, 0), []) |
| 177 |
| 178 ((size, tag, entry, new_data), new_handles) = ( |
| 179 value.SerializeInline(handle_offset)) |
| 180 if len(new_data) > 0: |
| 181 data.extend(new_data) |
| 182 entry = data_offset - 8 |
| 183 |
| 184 return ((size, tag, entry), new_handles) |
| 185 |
| 186 def Deserialize(self, value, context): |
| 187 result = self.union_type.Deserialize(context) |
| 188 if not result and not self.nullable: |
| 189 raise serialization.DeserializationException( |
| 190 'Trying to deserialize null for non nullable type.') |
| 191 return result |
| 192 |
| 193 |
156 class PointerType(SerializableType): | 194 class PointerType(SerializableType): |
157 """Base Type object for pointers.""" | 195 """Base Type object for pointers.""" |
158 | 196 |
159 def __init__(self, nullable=False): | 197 def __init__(self, nullable=False): |
160 SerializableType.__init__(self, 'Q') | 198 SerializableType.__init__(self, 'Q') |
161 self.nullable = nullable | 199 self.nullable = nullable |
162 | 200 |
163 def Serialize(self, value, data_offset, data, handle_offset): | 201 def Serialize(self, value, data_offset, data, handle_offset): |
164 if value is None and not self.nullable: | 202 if value is None and not self.nullable: |
165 raise serialization.SerializationException( | 203 raise serialization.SerializationException( |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 to_pack = [] | 465 to_pack = [] |
428 for item in value: | 466 for item in value: |
429 (new_data, new_handles) = self.sub_type.Serialize( | 467 (new_data, new_handles) = self.sub_type.Serialize( |
430 item, | 468 item, |
431 len(data) - position, | 469 len(data) - position, |
432 data, | 470 data, |
433 handle_offset + len(returned_handles)) | 471 handle_offset + len(returned_handles)) |
434 to_pack.extend(serialization.Flatten(new_data)) | 472 to_pack.extend(serialization.Flatten(new_data)) |
435 returned_handles.extend(new_handles) | 473 returned_handles.extend(new_handles) |
436 position = position + self.sub_type.GetByteSize() | 474 position = position + self.sub_type.GetByteSize() |
| 475 |
437 serialization.HEADER_STRUCT.pack_into(data, data_end, size, len(value)) | 476 serialization.HEADER_STRUCT.pack_into(data, data_end, size, len(value)) |
438 struct.pack_into('%d%s' % (len(value), self.sub_type.GetTypeCode()), | 477 # TODO(azani): Refactor so we don't have to create big formatting strings. |
| 478 struct.pack_into(('%s' % self.sub_type.GetTypeCode()) * len(value), |
439 data, | 479 data, |
440 data_end + serialization.HEADER_STRUCT.size, | 480 data_end + serialization.HEADER_STRUCT.size, |
441 *to_pack) | 481 *to_pack) |
442 return (data_offset, returned_handles) | 482 return (data_offset, returned_handles) |
443 | 483 |
444 def DeserializeArray(self, size, nb_elements, context): | 484 def DeserializeArray(self, size, nb_elements, context): |
| 485 # TODO(azani): Refactor so the format string isn't so big. |
445 values = struct.unpack_from( | 486 values = struct.unpack_from( |
446 '%d%s' % (nb_elements, self.sub_type.GetTypeCode()), | 487 nb_elements * self.sub_type.GetTypeCode(), |
447 buffer(context.data, serialization.HEADER_STRUCT.size)) | 488 buffer(context.data, serialization.HEADER_STRUCT.size)) |
448 values_per_element = len(self.sub_type.GetTypeCode()) | 489 values_per_element = len(self.sub_type.GetTypeCode()) |
449 assert nb_elements * values_per_element == len(values) | 490 assert nb_elements * values_per_element == len(values) |
450 | 491 |
451 result = [] | 492 result = [] |
452 sub_context = context.GetSubContext(serialization.HEADER_STRUCT.size) | 493 sub_context = context.GetSubContext(serialization.HEADER_STRUCT.size) |
453 for index in xrange(nb_elements): | 494 for index in xrange(nb_elements): |
454 if values_per_element == 1: | 495 if values_per_element == 1: |
455 value = values[index] | 496 value = values[index] |
456 else: | 497 else: |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
749 | 790 |
750 | 791 |
751 def _ConvertByteToBooleans(value, min_size=0): | 792 def _ConvertByteToBooleans(value, min_size=0): |
752 """Unpack an integer into a list of booleans.""" | 793 """Unpack an integer into a list of booleans.""" |
753 res = [] | 794 res = [] |
754 while value: | 795 while value: |
755 res.append(bool(value&1)) | 796 res.append(bool(value&1)) |
756 value = value / 2 | 797 value = value / 2 |
757 res.extend([False] * (min_size - len(res))) | 798 res.extend([False] * (min_size - len(res))) |
758 return res | 799 return res |
OLD | NEW |