Commit b5b736fc authored by Shane Kerr's avatar Shane Kerr
Browse files

Python 2 to 3 conversions



git-svn-id: svn://bind10.isc.org/svn/bind10/branches/f2f200910@166 e5f2f494-b856-4b98-b285-d166d9295462
parent ca8f4c43
......@@ -13,6 +13,9 @@
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# XXX
from ISC.Util import hexdump
import sys
import struct
......@@ -35,16 +38,13 @@ def to_wire(items):
"""Encode a dict into wire format.
>>> wire_format = Message.to_wire({"a": "b"})
"""
ret = []
ret.append(struct.pack(">I", PROTOCOL_VERSION))
ret.append(_encode_hash(items))
return (''.join(ret))
return struct.pack(">I", PROTOCOL_VERSION) + _encode_hash(items)
def _encode_tag(tag):
"""Encode a single UTF-8 tag.
... wire_partial = Message._encode_tag('this')
"""
return(struct.pack(">B", len(str(tag))) + str(tag))
return(struct.pack(">B", len(tag)) + bytearray(tag, 'utf-8'))
def _encode_length_and_type(data, datatype):
"""Helper method to handle the length encoding in one place."""
......@@ -52,22 +52,19 @@ def _encode_length_and_type(data, datatype):
return(struct.pack(">B", _ITEM_NULL))
length = len(data)
if length < 0x0000100:
return(struct.pack(">B B", datatype | _ITEM_LENGTH_8, length)
+ data)
return(struct.pack(">B B", datatype | _ITEM_LENGTH_8, length) + data)
elif length < 0x00010000:
return(struct.pack(">B H", datatype | _ITEM_LENGTH_16, length)
+ data)
return(struct.pack(">B H", datatype | _ITEM_LENGTH_16, length) + data)
else:
return(struct.pack(">B I", datatype, length) + data)
def _pack_string(item):
"""Pack a string (data) and its type/length prefix."""
return (_encode_length_and_type(item, _ITEM_DATA))
return (_encode_length_and_type(bytearray(item, 'utf-8'), _ITEM_DATA))
def _pack_array(item):
"""Pack a list (array) and its type/length prefix."""
return (_encode_length_and_type(_encode_array(item),
_ITEM_LIST))
return (_encode_length_and_type(_encode_array(item), _ITEM_LIST))
def _pack_hash(item):
"""Pack a dict (hash) and its type/length prefix."""
......@@ -90,24 +87,26 @@ def _encode_item(item):
return (_pack_hash(item))
elif type(item) == list:
return (_pack_array(item))
elif type(item) in (bytearray, bytes):
return (_pack_string(item.decode()))
else:
return (_pack_string(str(item)))
def _encode_array(item):
"""Encode an array, where each value is encoded recursively"""
ret = []
ret = bytearray()
for i in item:
ret.append(_encode_item(i))
return (''.join(ret))
ret += _encode_item(i)
return ret
def _encode_hash(item):
"""Encode a hash, where each value is encoded recursively"""
ret = []
ret = bytearray()
for key, value in item.items():
ret.append(_encode_tag(key))
ret.append(_encode_item(value))
return (''.join(ret))
ret += _encode_tag(key)
ret += _encode_item(value)
return ret
#
# decode methods
......@@ -125,15 +124,15 @@ def from_wire(data):
def _decode_tag(data):
if len(data) < 1:
raise DecodeError("Data underrun while decoding")
length = struct.unpack(">B", data[0])[0]
length = data[0]
if len(data) - 1 < length:
raise DecodeError("Data underrun while decoding")
return [data[1:length + 1], data[length + 1:]]
return [data[1:length + 1].decode(), data[length + 1:]]
def _decode_item(data):
if len(data) < 1:
raise DecodeError("Data underrun while decoding")
type_and_length_format = struct.unpack(">B", data[0])[0]
type_and_length_format = data[0]
item_type = type_and_length_format & _ITEM_MASK
length_format = type_and_length_format & _ITEM_LENGTH_MASK
......@@ -143,7 +142,7 @@ def _decode_item(data):
if length_format == _ITEM_LENGTH_8:
if len(data) - 1 < 1:
raise DecodeError("Data underrun while decoding")
length = struct.unpack(">B", data[1])[0]
length = data[1]
data = data[2:]
elif length_format == _ITEM_LENGTH_16:
if len(data) - 1 < 2:
......
......@@ -13,6 +13,10 @@
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# XXX
import pprint
from ISC.Util import hexdump
import sys
import socket
import struct
......@@ -27,9 +31,9 @@ class Session:
def __init__(self):
self._socket = None
self._lname = None
self._recvbuffer = ""
self._recvbuffer = bytearray()
self._recvlength = None
self._sendbuffer = ""
self._sendbuffer = bytearray()
self._sequence = 1
try:
......@@ -81,7 +85,7 @@ class Session:
if len(self._recvbuffer) < 4:
return None
self._recvlength = struct.unpack('>I', self._recvbuffer)[0]
self._recvbuffer = ""
self._recvbuffer = bytearray()
length = self._recvlength - len(self._recvbuffer)
while (length > 0):
......@@ -94,7 +98,7 @@ class Session:
self._recvbuffer += data
length -= len(data)
data = self._recvbuffer
self._recvbuffer = ""
self._recvbuffer = bytearray()
self._recvlength = None
return (data)
......
......@@ -4,10 +4,10 @@ _FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
def hexdump(src, length=16):
result=[]
for i in xrange(0, len(src), length):
for i in range(0, len(src), length):
s = src[i:i+length]
hexa = ' '.join(["%02X" % ord(x) for x in s])
printable = s.translate(_FILTER)
hexa = ' '.join(["%02X" % x for x in s])
printable = s.decode().translate(_FILTER)
item = "%08X %-*s %s\n" % (i, length * 3, hexa, printable)
result.append(item[0:34] + ' ' + item[34:])
print(''.join(result))
......@@ -8,10 +8,10 @@ ss = { "list": [ 1, 2, 3 ],
s = ISC.CC.Message.to_wire(ss)
ISC.Util.hexdump(s)
print ISC.CC.Message.from_wire(s)
print(ISC.CC.Message.from_wire(s))
tcp = ISC.CC.Session()
print tcp.lname
print(tcp.lname)
tcp.group_subscribe("test")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment