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

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

Issue 548343005: mojo: Specialize native type arrays. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months 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
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
10
9 # pylint: disable=F0401 11 # pylint: disable=F0401
10 from mojo.system import Handle 12 from mojo.system import Handle
11 13
12 class Type(object): 14 class Type(object):
13 """Describes the type of a struct field or a method parameter,""" 15 """Describes the type of a struct field or a method parameter,"""
14 16
15 def Convert(self, value): # pylint: disable=R0201 17 def Convert(self, value): # pylint: disable=R0201
16 """ 18 """
17 Convert the given value into its canonical representation, raising an 19 Convert the given value into its canonical representation, raising an
18 exception if the value cannot be converted. 20 exception if the value cannot be converted.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 self.signed = signed 54 self.signed = signed
53 if self.signed: 55 if self.signed:
54 self._min_value = -(1 << (size - 1)) 56 self._min_value = -(1 << (size - 1))
55 self._max_value = (1 << (size - 1)) - 1 57 self._max_value = (1 << (size - 1)) - 1
56 else: 58 else:
57 self._min_value = 0 59 self._min_value = 0
58 self._max_value = (1 << size) - 1 60 self._max_value = (1 << size) - 1
59 61
60 def Convert(self, value): 62 def Convert(self, value):
61 if value is None: 63 if value is None:
62 raise ValueError('None is not an integer.') 64 raise TypeError('None is not an integer.')
63 if not isinstance(value, (int, long)): 65 if not isinstance(value, (int, long)):
64 raise ValueError('%r is not an integer type' % value) 66 raise TypeError('%r is not an integer type' % value)
65 if value < self._min_value or value > self._max_value: 67 if value < self._min_value or value > self._max_value:
66 raise ValueError('%r is not in the range [%d, %d]' % 68 raise OverflowError('%r is not in the range [%d, %d]' %
67 (value, self._min_value, self._max_value)) 69 (value, self._min_value, self._max_value))
sdefresne 2014/09/10 08:40:00 nit: indent
qsr 2014/09/10 09:23:38 Done.
68 return value 70 return value
69 71
70 72
71 class FloatType(NumericType): 73 class FloatType(NumericType):
72 """Type object for floating point number types.""" 74 """Type object for floating point number types."""
73 75
74 def __init__(self, size): 76 def __init__(self, size):
75 NumericType.__init__(self) 77 NumericType.__init__(self)
76 self.size = size 78 self.size = size
77 79
78 def Convert(self, value): 80 def Convert(self, value):
79 if value is None: 81 if value is None:
80 raise ValueError('None is not an floating point number.') 82 raise TypeError('None is not an floating point number.')
81 if not isinstance(value, (int, long, float)): 83 if not isinstance(value, (int, long, float)):
82 raise ValueError('%r is not a numeric type' % value) 84 raise TypeError('%r is not a numeric type' % value)
83 return float(value) 85 return float(value)
84 86
85 87
86 class StringType(Type): 88 class StringType(Type):
87 """ 89 """
88 Type object for strings. 90 Type object for strings.
89 91
90 Strings are represented as unicode, and the conversion is done using the 92 Strings are represented as unicode, and the conversion is done using the
91 default encoding if a string instance is used. 93 default encoding if a string instance is used.
92 """ 94 """
93 95
94 def __init__(self, nullable=False): 96 def __init__(self, nullable=False):
95 Type.__init__(self) 97 Type.__init__(self)
96 self.nullable = nullable 98 self.nullable = nullable
97 99
98 def Convert(self, value): 100 def Convert(self, value):
99 if value is None or isinstance(value, unicode): 101 if value is None or isinstance(value, unicode):
100 return value 102 return value
101 if isinstance(value, str): 103 if isinstance(value, str):
102 return unicode(value) 104 return unicode(value)
103 raise ValueError('%r is not a string' % value) 105 raise TypeError('%r is not a string' % value)
104 106
105 107
106 class HandleType(Type): 108 class HandleType(Type):
107 """Type object for handles.""" 109 """Type object for handles."""
108 110
109 def __init__(self, nullable=False): 111 def __init__(self, nullable=False):
110 Type.__init__(self) 112 Type.__init__(self)
111 self.nullable = nullable 113 self.nullable = nullable
112 114
113 def Convert(self, value): 115 def Convert(self, value):
114 if value is None: 116 if value is None:
115 return Handle() 117 return Handle()
116 if not isinstance(value, Handle): 118 if not isinstance(value, Handle):
117 raise ValueError('%r is not a handle' % value) 119 raise TypeError('%r is not a handle' % value)
118 return value 120 return value
119 121
120 122
121 class ArrayType(Type): 123 class GenericArrayType(Type):
122 """Type object for arrays.""" 124 """Abstract Type object for arrays."""
125
126 def __init__(self, nullable=False, length=0):
127 Type.__init__(self)
128 self.nullable = nullable
129 self.length = length
130
131
132 class PointerArrayType(GenericArrayType):
133 """Type object for arrays of pointers."""
123 134
124 def __init__(self, sub_type, nullable=False, length=0): 135 def __init__(self, sub_type, nullable=False, length=0):
125 Type.__init__(self) 136 GenericArrayType.__init__(self, nullable, length)
126 self.sub_type = sub_type 137 self.sub_type = sub_type
127 self.nullable = nullable
128 self.length = length
129 138
130 def Convert(self, value): 139 def Convert(self, value):
131 if value is None: 140 if value is None:
132 return value 141 return value
133 return [self.sub_type.Convert(x) for x in value] 142 return [self.sub_type.Convert(x) for x in value]
134 143
135 144
145 class NativeArrayType(GenericArrayType):
146 """Type object for arrays of native types."""
147
148 def __init__(self, typecode, nullable=False, length=0):
149 GenericArrayType.__init__(self, nullable, length)
150 self.typecode = typecode
151
152 def Convert(self, value):
153 if value is None:
154 return value
155 if isinstance(value, array.array) and value.typecode == self.typecode:
156 return value
157 return array.array(self.typecode, value)
158
159
136 class StructType(Type): 160 class StructType(Type):
137 """Type object for structs.""" 161 """Type object for structs."""
138 162
139 def __init__(self, struct_type, nullable=False): 163 def __init__(self, struct_type, nullable=False):
140 Type.__init__(self) 164 Type.__init__(self)
141 self.struct_type = struct_type 165 self.struct_type = struct_type
142 self.nullable = nullable 166 self.nullable = nullable
143 167
144 def Convert(self, value): 168 def Convert(self, value):
145 if value is None or isinstance(value, self.struct_type): 169 if value is None or isinstance(value, self.struct_type):
146 return value 170 return value
147 raise ValueError('%r is not an instance of %r' % (value, self.struct_type)) 171 raise TypeError('%r is not an instance of %r' % (value, self.struct_type))
148 172
149 def GetDefaultValue(self, value): 173 def GetDefaultValue(self, value):
150 if value: 174 if value:
151 return self.struct_type() 175 return self.struct_type()
152 return None 176 return None
153 177
154 178
155 class NoneType(Type): 179 class NoneType(Type):
156 """Placeholder type, used temporarily until all mojo types are handled.""" 180 """Placeholder type, used temporarily until all mojo types are handled."""
157 181
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 def __init__(self, name, field_type, offset, 213 def __init__(self, name, field_type, offset,
190 bit_offset=None, default_value=None): 214 bit_offset=None, default_value=None):
191 self.name = name 215 self.name = name
192 self.field_type = field_type 216 self.field_type = field_type
193 self.offset = offset 217 self.offset = offset
194 self.bit_offset = bit_offset 218 self.bit_offset = bit_offset
195 self._default_value = default_value 219 self._default_value = default_value
196 220
197 def GetDefaultValue(self): 221 def GetDefaultValue(self):
198 return self.field_type.GetDefaultValue(self._default_value) 222 return self.field_type.GetDefaultValue(self._default_value)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698