]> jfr.im git - irc/weechat/qweechat.git/blobdiff - qweechat/weechat/protocol.py
More fixes.
[irc/weechat/qweechat.git] / qweechat / weechat / protocol.py
index 79b24c1dae23e8a747c739f2528ed6d5e152f0ba..4e1514da8814e1d5b1d64e6c40c2c053c9b84be5 100644 (file)
@@ -34,15 +34,11 @@ import collections
 import struct
 import zlib
 
-if hasattr(collections, 'OrderedDict'):
-    # python >= 2.7
-    class WeechatDict(collections.OrderedDict):
-        def __str__(self):
-            return '{%s}' % ', '.join(
-                ['%s: %s' % (repr(key), repr(self[key])) for key in self])
-else:
-    # python <= 2.6
-    WeechatDict = dict
+
+class WeechatDict(collections.OrderedDict):
+    def __str__(self):
+        return '{%s}' % ', '.join(
+            ['%s: %s' % (repr(key), repr(self[key])) for key in self])
 
 
 class WeechatObject:
@@ -151,7 +147,7 @@ class Protocol:
         if len(self.data) < 3:
             self.data = ''
             return ''
-        objtype = str(self.data[0:3])
+        objtype = self.data[0:3].decode()
         self.data = self.data[3:]
         return objtype
 
@@ -196,14 +192,14 @@ class Protocol:
         value = self._obj_len_data(1)
         if value is None:
             return None
-        return int(str(value))
+        return int(value)
 
     def _obj_str(self):
         """Read a string in data (length on 4 bytes + content)."""
         value = self._obj_len_data(4)
-        if value is None:
-            return None
-        return str(value)
+        if value in ("", None):
+            return ""
+        return value.decode()
 
     def _obj_buffer(self):
         """Read a buffer in data (length on 4 bytes + data)."""
@@ -214,14 +210,14 @@ class Protocol:
         value = self._obj_len_data(1)
         if value is None:
             return None
-        return '0x%s' % str(value)
+        return '0x%s' % value
 
     def _obj_time(self):
         """Read a time in data (length on 1 byte + value as string)."""
         value = self._obj_len_data(1)
         if value is None:
             return None
-        return int(str(value))
+        return int(value)
 
     def _obj_hashtable(self):
         """
@@ -314,8 +310,8 @@ class Protocol:
         if compression:
             uncompressed = zlib.decompress(self.data[5:])
             size_uncompressed = len(uncompressed) + 5
-            uncompressed = '%s%s%s' % (struct.pack('>i', size_uncompressed),
-                                       struct.pack('b', 0), uncompressed)
+            uncompressed = b'%s%s%s' % (struct.pack('>i', size_uncompressed),
+                                        struct.pack('b', 0), uncompressed)
             self.data = uncompressed
         else:
             uncompressed = self.data[:]
@@ -344,13 +340,20 @@ def hex_and_ascii(data, bytes_per_line=10):
     for i in range(num_lines):
         str_hex = []
         str_ascii = []
-        for char in data[i*bytes_per_line:(i*bytes_per_line)+bytes_per_line]:
+        for j in range(bytes_per_line):
+            # We can't easily iterate over individual bytes, so we are going to
+            # do it this way.
+            index = (i*bytes_per_line) + j
+            char = data[index:index+1]
+            if not char:
+                char = b'x'
             byte = struct.unpack('B', char)[0]
-            str_hex.append('%02X' % int(byte))
+            str_hex.append(b'%02X' % int(byte))
             if byte >= 32 and byte <= 127:
                 str_ascii.append(char)
             else:
-                str_ascii.append('.')
-        fmt = '%%-%ds %%s' % ((bytes_per_line * 3) - 1)
-        lines.append(fmt % (' '.join(str_hex), ''.join(str_ascii)))
-    return '\n'.join(lines)
+                str_ascii.append(b'.')
+        fmt = b'%%-%ds %%s' % ((bytes_per_line * 3) - 1)
+        lines.append(fmt % (b' '.join(str_hex),
+                            b''.join(str_ascii)))
+    return b'\n'.join(lines)