Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: mojo/public/python/mojo_bindings/descriptor.py

Issue 1413033011: Fix bug whereby booleans in unions could not be serialized in python. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | mojo/public/tools/bindings/generators/mojom_python_generator.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 corresponds to the first element in the tuple returned by 95 corresponds to the first element in the tuple returned by
96 Serialize. 96 Serialize.
97 data: the bytearray to retrieve additional data from. 97 data: the bytearray to retrieve additional data from.
98 handles: the array of handles contained in the message to deserialize. 98 handles: the array of handles contained in the message to deserialize.
99 99
100 Returns the deserialized value. 100 Returns the deserialized value.
101 """ 101 """
102 raise NotImplementedError() 102 raise NotImplementedError()
103 103
104 104
105 class BooleanType(Type): 105 class BooleanType(SerializableType):
106 """Type object for booleans""" 106 """Type object for booleans"""
107 107
108 def Convert(self, value): 108 def Convert(self, value):
109 return bool(value) 109 return bool(value)
110 110
111 def Serialize(self, value, data_offset, data, handle_offset):
112 return (_ConvertBooleansToByte([value]), [])
113
114 def Deserialize(self, value, context):
115 return _ConvertByteToBooleans(value)[0]
116
111 117
112 class NumericType(SerializableType): 118 class NumericType(SerializableType):
113 """Base Type object for all numeric types""" 119 """Base Type object for all numeric types"""
114 120
115 def GetDefaultValue(self, value): 121 def GetDefaultValue(self, value):
116 if value is None: 122 if value is None:
117 return self.Convert(0) 123 return self.Convert(0)
118 return self.Convert(value) 124 return self.Convert(value)
119 125
120 def Serialize(self, value, data_offset, data, handle_offset): 126 def Serialize(self, value, data_offset, data, handle_offset):
(...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 return None 635 return None
630 636
631 @staticmethod 637 @staticmethod
632 def _GetArrayType(t): 638 def _GetArrayType(t):
633 if t == TYPE_BOOL: 639 if t == TYPE_BOOL:
634 return BooleanArrayType() 640 return BooleanArrayType()
635 else: 641 else:
636 return GenericArrayType(t) 642 return GenericArrayType(t)
637 643
638 644
639 TYPE_BOOL = BooleanType() 645 TYPE_BOOL = BooleanType('B')
640 646
641 TYPE_INT8 = IntegerType('b') 647 TYPE_INT8 = IntegerType('b')
642 TYPE_INT16 = IntegerType('h') 648 TYPE_INT16 = IntegerType('h')
643 TYPE_INT32 = IntegerType('i') 649 TYPE_INT32 = IntegerType('i')
644 TYPE_INT64 = IntegerType('q') 650 TYPE_INT64 = IntegerType('q')
645 651
646 TYPE_UINT8 = IntegerType('B') 652 TYPE_UINT8 = IntegerType('B')
647 TYPE_UINT16 = IntegerType('H') 653 TYPE_UINT16 = IntegerType('H')
648 TYPE_UINT32 = IntegerType('I') 654 TYPE_UINT32 = IntegerType('I')
649 TYPE_UINT64 = IntegerType('Q') 655 TYPE_UINT64 = IntegerType('Q')
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
800 806
801 807
802 def _ConvertByteToBooleans(value, min_size=0): 808 def _ConvertByteToBooleans(value, min_size=0):
803 """Unpack an integer into a list of booleans.""" 809 """Unpack an integer into a list of booleans."""
804 res = [] 810 res = []
805 while value: 811 while value:
806 res.append(bool(value&1)) 812 res.append(bool(value&1))
807 value = value / 2 813 value = value / 2
808 res.extend([False] * (min_size - len(res))) 814 res.extend([False] * (min_size - len(res)))
809 return res 815 return res
OLDNEW
« no previous file with comments | « no previous file | mojo/public/tools/bindings/generators/mojom_python_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698