summaryrefslogtreecommitdiffstats
path: root/src/vpp-api/python/vpp_papi/vpp_serializer.py
diff options
context:
space:
mode:
authorPaul Vinciguerra <pvinci@vinciconsulting.com>2018-11-27 08:15:22 -0800
committerOle Trøan <otroan@employees.org>2018-11-29 19:18:07 +0000
commit6ccc6e91648312cc210e9b6a18e469e64f4a523f (patch)
treec80d92e7d8c1c28f90cb92d4455c39c52f3b2617 /src/vpp-api/python/vpp_papi/vpp_serializer.py
parent039a1c2f1401ebf3b38fa9bd55dffff0713b8b98 (diff)
vpp_papi: Add custom exceptions.
This patchset adds and raises the following custom exception classes: * class VPPApiError(Exception): * class VPPNotImplementedError(NotImplementedError): * class VPPIOError(IOError): * class VPPRuntimeError(RuntimeError): * class VPPValueError(ValueError): * class VPPSerializerValueError(ValueError): * class VPPStatsIOError(IOError): * class VPPStatsClientLoadError(RuntimeError): * class VppTransportShmemIOError(IOError): * class VppTransportSocketIOError(IOError) Change-Id: Ia40900fd2dcef148d01125d6c691329fc666901e Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
Diffstat (limited to 'src/vpp-api/python/vpp_papi/vpp_serializer.py')
-rw-r--r--src/vpp-api/python/vpp_papi/vpp_serializer.py54
1 files changed, 31 insertions, 23 deletions
diff --git a/src/vpp-api/python/vpp_papi/vpp_serializer.py b/src/vpp-api/python/vpp_papi/vpp_serializer.py
index a001cca565a..f78972979cd 100644
--- a/src/vpp-api/python/vpp_papi/vpp_serializer.py
+++ b/src/vpp-api/python/vpp_papi/vpp_serializer.py
@@ -55,14 +55,10 @@ class BaseTypes(object):
return self.packer.unpack_from(data, offset)[0], self.packer.size
-types = {}
-types['u8'] = BaseTypes('u8')
-types['u16'] = BaseTypes('u16')
-types['u32'] = BaseTypes('u32')
-types['i32'] = BaseTypes('i32')
-types['u64'] = BaseTypes('u64')
-types['f64'] = BaseTypes('f64')
-types['bool'] = BaseTypes('bool')
+types = {'u8': BaseTypes('u8'), 'u16': BaseTypes('u16'),
+ 'u32': BaseTypes('u32'), 'i32': BaseTypes('i32'),
+ 'u64': BaseTypes('u64'), 'f64': BaseTypes('f64'),
+ 'bool': BaseTypes('bool')}
def vpp_get_type(name):
@@ -72,6 +68,10 @@ def vpp_get_type(name):
return None
+class VPPSerializerValueError(ValueError):
+ pass
+
+
class FixedList_u8(object):
def __init__(self, name, field_type, num):
self.name = name
@@ -85,16 +85,18 @@ class FixedList_u8(object):
if not list:
return b'\x00' * self.size
if len(list) > self.num:
- raise ValueError('Fixed list length error for "{}", got: {}'
- ' expected: {}'
- .format(self.name, len(list), self.num))
+ raise VPPSerializerValueError(
+ 'Fixed list length error for "{}", got: {}'
+ ' expected: {}'
+ .format(self.name, len(list), self.num))
return self.packer.pack(list)
def unpack(self, data, offset=0, result=None):
if len(data[offset:]) < self.num:
- raise ValueError('Invalid array length for "{}" got {}'
- ' expected {}'
- .format(self.name, len(data[offset:]), self.num))
+ raise VPPSerializerValueError(
+ 'Invalid array length for "{}" got {}'
+ ' expected {}'
+ .format(self.name, len(data[offset:]), self.num))
return self.packer.unpack(data, offset)
@@ -106,8 +108,9 @@ class FixedList(object):
def pack(self, list, kwargs):
if len(list) != self.num:
- raise ValueError('Fixed list length error, got: {} expected: {}'
- .format(len(list), self.num))
+ raise VPPSerializerValueError(
+ 'Fixed list length error, got: {} expected: {}'
+ .format(len(list), self.num))
b = bytes()
for e in list:
b += self.packer.pack(e)
@@ -137,8 +140,9 @@ class VLAList(object):
if not list:
return b""
if len(list) != kwargs[self.length_field]:
- raise ValueError('Variable length error, got: {} expected: {}'
- .format(len(list), kwargs[self.length_field]))
+ raise VPPSerializerValueError(
+ 'Variable length error, got: {} expected: {}'
+ .format(len(list), kwargs[self.length_field]))
b = bytes()
# u8 array
@@ -187,7 +191,8 @@ class VLAList_legacy():
total = 0
# Return a list of arguments
if (len(data) - offset) % self.packer.size:
- raise ValueError('Legacy Variable Length Array length mismatch.')
+ raise VPPSerializerValueError(
+ 'Legacy Variable Length Array length mismatch.')
elements = int((len(data) - offset) / self.packer.size)
r = []
for e in range(elements):
@@ -241,7 +246,8 @@ class VPPUnionType(object):
f_type, f_name = f
if f_type not in types:
logger.debug('Unknown union type {}'.format(f_type))
- raise ValueError('Unknown message type {}'.format(f_type))
+ raise VPPSerializerValueError(
+ 'Unknown message type {}'.format(f_type))
fields.append(f_name)
size = types[f_type].size
self.packers[f_name] = types[f_type]
@@ -309,7 +315,8 @@ class VPPType(object):
self.fieldtypes.append(f_type)
if f_type not in types:
logger.debug('Unknown type {}'.format(f_type))
- raise ValueError('Unknown message type {}'.format(f_type))
+ raise VPPSerializerValueError(
+ 'Unknown message type {}'.format(f_type))
if len(f) == 3: # list
list_elements = f[2]
if list_elements == 0:
@@ -345,8 +352,9 @@ class VPPType(object):
# Try one of the format functions
if data and type(data) is not dict and a not in data:
- raise ValueError("Invalid argument: {} expected {}.{}".
- format(data, self.name, a))
+ raise VPPSerializerValueError(
+ "Invalid argument: {} expected {}.{}".
+ format(data, self.name, a))
# Defaulting to zero.
if not data or a not in data: # Default to 0