[svn] commit: r297 - in /branches/jelte-datadef/src/lib/cc/python/ISC/CC: Message.py session.py

BIND 10 source code commits bind10-changes at lists.isc.org
Thu Nov 19 09:52:07 UTC 2009


Author: jelte
Date: Thu Nov 19 09:52:07 2009
New Revision: 297

Log:
(temporary!) workaround for decode problem; this needs a more substantial change (i.e. make the api encoding-agnostic), and will be reverted later, but is needed right now for the other changes in this branch

Modified:
    branches/jelte-datadef/src/lib/cc/python/ISC/CC/Message.py
    branches/jelte-datadef/src/lib/cc/python/ISC/CC/session.py

Modified: branches/jelte-datadef/src/lib/cc/python/ISC/CC/Message.py
==============================================================================
--- branches/jelte-datadef/src/lib/cc/python/ISC/CC/Message.py (original)
+++ branches/jelte-datadef/src/lib/cc/python/ISC/CC/Message.py Thu Nov 19 09:52:07 2009
@@ -51,6 +51,9 @@
     if data == None:
         return(struct.pack(">B", _ITEM_NULL))
     length = len(data)
+    if type(data) == str:
+        #print("[XX] data is still string, converting to bytearray...");
+        data = bytearray(data, 'utf-8')
     if length < 0x0000100:
         return(struct.pack(">B B", datatype | _ITEM_LENGTH_8, length) + data)
     elif length < 0x00010000:
@@ -60,7 +63,7 @@
 
 def _pack_string(item):
     """Pack a string (data) and its type/length prefix."""
-    return (_encode_length_and_type(bytearray(item, 'utf-8'), _ITEM_DATA))
+    return (_encode_length_and_type(item, _ITEM_DATA))
 
 def _pack_array(item):
     """Pack a list (array) and its type/length prefix."""
@@ -88,7 +91,7 @@
     elif type(item) == list:
         return (_pack_array(item))
     elif type(item) in (bytearray, bytes):
-        return (_pack_string(item.decode()))
+        return (_pack_string(item))
     else:
         return (_pack_string(str(item)))
 
@@ -116,6 +119,9 @@
     if len(data) < 5:
         raise DecodeError("Data is too short to decode")
     wire_version, data = data[0:4], data[4:]
+    if (type(wire_version) == str):
+        wire_version = bytearray(wire_version, 'utf-8')
+    #wire_version = struct.unpack(">I", wire_version)[0]
     wire_version = struct.unpack(">I", wire_version)[0]
     if wire_version != PROTOCOL_VERSION:
         raise DecodeError("Incorrect protocol version")
@@ -124,10 +130,12 @@
 def _decode_tag(data):
     if len(data) < 1:
         raise DecodeError("Data underrun while decoding")
+    if (type(data) == str):
+        data = bytearray(data, 'utf-8')
     length = data[0]
     if len(data) - 1 < length:
         raise DecodeError("Data underrun while decoding")
-    return [data[1:length + 1].decode(), data[length + 1:]]
+    return [data[1:length + 1], data[length + 1:]]
 
 def _decode_item(data):
     if len(data) < 1:
@@ -160,7 +168,7 @@
         data = data[length:]
 
     if item_type == _ITEM_DATA:
-        value = item.decode()
+        value = item
     elif item_type == _ITEM_HASH:
         value = _decode_hash(item)
     elif item_type == _ITEM_LIST:
@@ -177,6 +185,15 @@
     while len(data) > 0:
         tag, data = _decode_tag(data)
         value, data = _decode_item(data)
+        if type(value) == bytearray:
+            # hack! just try it in case it is really a string
+            try:
+                value = value.decode('utf-8')
+            except UnicodeDecodeError as ude:
+                # apparently not a final item, leave it a bytearray
+                pass
+        if type(tag) == bytearray:
+            tag = tag.decode('utf-8')
         ret[tag] = value
     return ret
 
@@ -184,6 +201,8 @@
     ret = []
     while len(data) > 0:
         value, data = _decode_item(data)
+        if (type(value) == bytearray):
+            value = value.decode('utf-8')
         ret.append(value)
     return ret
 

Modified: branches/jelte-datadef/src/lib/cc/python/ISC/CC/session.py
==============================================================================
--- branches/jelte-datadef/src/lib/cc/python/ISC/CC/session.py (original)
+++ branches/jelte-datadef/src/lib/cc/python/ISC/CC/session.py Thu Nov 19 09:52:07 2009
@@ -137,7 +137,7 @@
             # return none twice to match normal return value
             # (so caller won't get a type error on no data)
             return (None, None)
-        msg = Message.from_wire(env["msg"].encode('ascii'))
+        msg = Message.from_wire(env["msg"])
         return (msg, env)
 
     def group_reply(self, routing, msg):




More information about the bind10-changes mailing list