vici: Add support for python 3
authorBjörn Schuberg <bjorn.schuberg@gmail.com>
Mon, 9 Mar 2015 11:28:02 +0000 (12:28 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 18 Mar 2015 12:59:15 +0000 (13:59 +0100)
src/libcharon/plugins/vici/python/Makefile.am
src/libcharon/plugins/vici/python/setup.py.in
src/libcharon/plugins/vici/python/vici/compat.py [new file with mode: 0644]
src/libcharon/plugins/vici/python/vici/protocol.py
src/libcharon/plugins/vici/python/vici/session.py

index 7be733e..f517378 100644 (file)
@@ -3,6 +3,7 @@ EXTRA_DIST = LICENSE MANIFEST.in \
        vici/test/__init__.py \
        vici/test/test_protocol.py \
        vici/__init__.py \
+       vici/compat.py \
        vici/exception.py \
        vici/protocol.py \
        vici/session.py
index 9b85565..0e4ad82 100644 (file)
@@ -25,6 +25,9 @@ setup(
         "License :: OSI Approved :: MIT License",
         "Natural Language :: English",
         "Programming Language :: Python :: 2.7",
+        "Programming Language :: Python :: 3.2",
+        "Programming Language :: Python :: 3.3",
+        "Programming Language :: Python :: 3.4",
         "Topic :: Security",
         "Topic :: Software Development :: Libraries",
     )
diff --git a/src/libcharon/plugins/vici/python/vici/compat.py b/src/libcharon/plugins/vici/python/vici/compat.py
new file mode 100644 (file)
index 0000000..b5f4699
--- /dev/null
@@ -0,0 +1,14 @@
+# Help functions for compatibility between python version 2 and 3
+
+
+# From http://legacy.python.org/dev/peps/pep-0469
+try:
+    dict.iteritems
+except AttributeError:
+    # python 3
+    def iteritems(d):
+        return iter(d.items())
+else:
+    # python 2
+    def iteritems(d):
+        return d.iteritems()
index 88e1c34..855a7b2 100644 (file)
@@ -5,6 +5,7 @@ import struct
 from collections import namedtuple
 from collections import OrderedDict
 
+from .compat import iteritems
 from .exception import DeserializationException
 
 
@@ -51,6 +52,7 @@ class Packet(object):
 
     @classmethod
     def _named_request(cls, request_type, request, message=None):
+        request = request.encode()
         payload = struct.pack("!BB", request_type, len(request)) + request
         if message is not None:
             return payload + message
@@ -93,22 +95,23 @@ class Message(object):
     @classmethod
     def serialize(cls, message):
         def encode_named_type(marker, name):
-            name = str(name)
+            name = name.encode()
             return struct.pack("!BB", marker, len(name)) + name
 
         def encode_blob(value):
-            value = str(value)
+            if not isinstance(value, bytes):
+                value = str(value).encode()
             return struct.pack("!H", len(value)) + value
 
         def serialize_list(lst):
-            segment = str()
+            segment = bytes()
             for item in lst:
                 segment += struct.pack("!B", cls.LIST_ITEM) + encode_blob(item)
             return segment
 
         def serialize_dict(d):
-            segment = str()
-            for key, value in d.iteritems():
+            segment = bytes()
+            for key, value in iteritems(d):
                 if isinstance(value, dict):
                     segment += (
                         encode_named_type(cls.SECTION_START, key)
@@ -134,7 +137,7 @@ class Message(object):
     def deserialize(cls, stream):
         def decode_named_type(stream):
             length, = struct.unpack("!B", stream.read(1))
-            return stream.read(length)
+            return stream.read(length).decode()
 
         def decode_blob(stream):
             length, = struct.unpack("!H", stream.read(2))
index 9f4dc5f..dee5869 100644 (file)
@@ -241,7 +241,7 @@ class SessionHandler(object):
 
         command_response = Message.deserialize(response.payload)
         if "success" in command_response:
-            if command_response["success"] != "yes":
+            if command_response["success"] != b"yes":
                 raise CommandException(
                     "Command failed: {errmsg}".format(
                         errmsg=command_response["errmsg"]
@@ -319,7 +319,7 @@ class SessionHandler(object):
 
         # evaluate command result, if any
         if "success" in command_response:
-            if command_response["success"] != "yes":
+            if command_response["success"] != b"yes":
                 raise CommandException(
                     "Command failed: {errmsg}".format(
                         errmsg=command_response["errmsg"]