[svn] commit: r311 - in /branches/parkinglot/src/lib/cc/python: ISC/CC/Message.py test.py

BIND 10 source code commits bind10-changes at lists.isc.org
Fri Nov 20 13:31:38 UTC 2009


Author: jelte
Date: Fri Nov 20 13:31:38 2009
New Revision: 311

Log:
bool type on wire in python messaging part

Modified:
    branches/parkinglot/src/lib/cc/python/ISC/CC/Message.py
    branches/parkinglot/src/lib/cc/python/test.py

Modified: branches/parkinglot/src/lib/cc/python/ISC/CC/Message.py
==============================================================================
--- branches/parkinglot/src/lib/cc/python/ISC/CC/Message.py (original)
+++ branches/parkinglot/src/lib/cc/python/ISC/CC/Message.py Fri Nov 20 13:31:38 2009
@@ -27,6 +27,7 @@
 _ITEM_HASH = 0x02
 _ITEM_LIST = 0x03
 _ITEM_NULL = 0x04
+_ITEM_BOOL = 0x05
 _ITEM_UTF8 = 0x08
 _ITEM_MASK = 0x0f
 
@@ -68,6 +69,10 @@
     """Pack a blob (binary data) and its type/length prefix."""
     return (_encode_length_and_type(item, _ITEM_BLOB))
 
+def _pack_bool(item):
+    """Pack a bool and its type/length prefix."""
+    return (_encode_length_and_type(_encode_bool(item), _ITEM_BOOL))
+
 def _pack_array(item):
     """Pack a list (array) and its type/length prefix."""
     return (_encode_length_and_type(_encode_array(item), _ITEM_LIST))
@@ -93,6 +98,8 @@
     """Encode each item depending on its type"""
     if item == None:
         return (_pack_nil())
+    elif type(item) == bool:
+        return (_pack_bool(item))
     elif type(item) == dict:
         return (_pack_hash(item))
     elif type(item) == list:
@@ -101,6 +108,13 @@
         return (_pack_blob(item))
     else:
         return (_pack_utf8(str(item)))
+
+def _encode_bool(item):
+    """Encode a boolean value into a bytearray of one byte (0=false)"""
+    if item:
+        return b'\x01'
+    else:
+        return b'\x00'
 
 def _encode_array(item):
     """Encode an array, where each value is encoded recursively"""
@@ -170,6 +184,8 @@
 
     if item_type == _ITEM_BLOB:
         value = item
+    elif item_type == _ITEM_BOOL:
+        value = _decode_bool(item)
     elif item_type == _ITEM_UTF8:
         value = str(item, 'utf-8')
     elif item_type == _ITEM_HASH:
@@ -183,6 +199,9 @@
 
     return (value, data)
 
+def _decode_bool(data):
+    return data == b'0x01'
+
 def _decode_hash(data):
     ret = {}
     while len(data) > 0:

Modified: branches/parkinglot/src/lib/cc/python/test.py
==============================================================================
--- branches/parkinglot/src/lib/cc/python/test.py (original)
+++ branches/parkinglot/src/lib/cc/python/test.py Fri Nov 20 13:31:38 2009
@@ -76,5 +76,23 @@
         decoded = ISC.CC.Message.from_wire(wire)
         self.assertEqual(decoded["せんせい"], "string")
 
+    def test_to_wire_of_bool_true(self):
+        wire = ISC.CC.Message.to_wire({ "bool": True })
+        self.assertEqual(wire, b'Skan\x04bool%\x01\x01')
+
+    def test_to_wire_of_bool_false(self):
+        wire = ISC.CC.Message.to_wire({ "bool": False })
+        self.assertEqual(wire, b'Skan\x04bool%\x01\x00')
+
+    def test_from_wire_of_bool_true(self):
+        wire = b'Skan\x04bool%\x01\x01'
+        decoded = ISC.CC.Message.from_wire(wire)
+        self.assertEqual(decoded["bool"], True)
+
+    def test_from_wire_of_bool_true(self):
+        wire = b'Skan\x04bool%\x01\x00'
+        decoded = ISC.CC.Message.from_wire(wire)
+        self.assertEqual(decoded["bool"], False)
+
 if __name__ == '__main__':
     unittest.main()




More information about the bind10-changes mailing list