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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/python/mojo/bindings/descriptor.py
diff --git a/mojo/public/python/mojo/bindings/descriptor.py b/mojo/public/python/mojo/bindings/descriptor.py
index cecea0ed7887fe85edb7443e10f1abc625d7ea85..b04685c226da8f3f97bab6e1e3b627101cafa932 100644
--- a/mojo/public/python/mojo/bindings/descriptor.py
+++ b/mojo/public/python/mojo/bindings/descriptor.py
@@ -6,6 +6,8 @@
The descriptors used to define generated elements of the mojo python bindings.
"""
+import array
+
# pylint: disable=F0401
from mojo.system import Handle
@@ -59,11 +61,11 @@ class IntegerType(NumericType):
def Convert(self, value):
if value is None:
- raise ValueError('None is not an integer.')
+ raise TypeError('None is not an integer.')
if not isinstance(value, (int, long)):
- raise ValueError('%r is not an integer type' % value)
+ raise TypeError('%r is not an integer type' % value)
if value < self._min_value or value > self._max_value:
- raise ValueError('%r is not in the range [%d, %d]' %
+ raise OverflowError('%r is not in the range [%d, %d]' %
(value, self._min_value, self._max_value))
sdefresne 2014/09/10 08:40:00 nit: indent
qsr 2014/09/10 09:23:38 Done.
return value
@@ -77,9 +79,9 @@ class FloatType(NumericType):
def Convert(self, value):
if value is None:
- raise ValueError('None is not an floating point number.')
+ raise TypeError('None is not an floating point number.')
if not isinstance(value, (int, long, float)):
- raise ValueError('%r is not a numeric type' % value)
+ raise TypeError('%r is not a numeric type' % value)
return float(value)
@@ -100,7 +102,7 @@ class StringType(Type):
return value
if isinstance(value, str):
return unicode(value)
- raise ValueError('%r is not a string' % value)
+ raise TypeError('%r is not a string' % value)
class HandleType(Type):
@@ -114,25 +116,47 @@ class HandleType(Type):
if value is None:
return Handle()
if not isinstance(value, Handle):
- raise ValueError('%r is not a handle' % value)
+ raise TypeError('%r is not a handle' % value)
return value
-class ArrayType(Type):
- """Type object for arrays."""
+class GenericArrayType(Type):
+ """Abstract Type object for arrays."""
- def __init__(self, sub_type, nullable=False, length=0):
+ def __init__(self, nullable=False, length=0):
Type.__init__(self)
- self.sub_type = sub_type
self.nullable = nullable
self.length = length
+
+class PointerArrayType(GenericArrayType):
+ """Type object for arrays of pointers."""
+
+ def __init__(self, sub_type, nullable=False, length=0):
+ GenericArrayType.__init__(self, nullable, length)
+ self.sub_type = sub_type
+
def Convert(self, value):
if value is None:
return value
return [self.sub_type.Convert(x) for x in value]
+class NativeArrayType(GenericArrayType):
+ """Type object for arrays of native types."""
+
+ def __init__(self, typecode, nullable=False, length=0):
+ GenericArrayType.__init__(self, nullable, length)
+ self.typecode = typecode
+
+ def Convert(self, value):
+ if value is None:
+ return value
+ if isinstance(value, array.array) and value.typecode == self.typecode:
+ return value
+ return array.array(self.typecode, value)
+
+
class StructType(Type):
"""Type object for structs."""
@@ -144,7 +168,7 @@ class StructType(Type):
def Convert(self, value):
if value is None or isinstance(value, self.struct_type):
return value
- raise ValueError('%r is not an instance of %r' % (value, self.struct_type))
+ raise TypeError('%r is not an instance of %r' % (value, self.struct_type))
def GetDefaultValue(self, value):
if value:

Powered by Google App Engine
This is Rietveld 408576698