From a7800e6ba29ff9dc52e09311948fad1e1bcc10ed Mon Sep 17 00:00:00 2001
From: Anders Blomdell <anders.blomdell@control.lth.se>
Date: Mon, 29 May 2017 19:11:56 +0200
Subject: [PATCH] Made python labrary python3 compatible

---
 lib/python/labcomm2014/LabComm.py  | 22 ++++++++++++++++------
 lib/python/labcomm2014/__init__.py |  6 +++---
 test/Makefile                      |  4 ++++
 test/python_generic_relay.py       | 21 +++++++++++++++++++++
 4 files changed, 44 insertions(+), 9 deletions(-)
 create mode 100644 test/python_generic_relay.py

diff --git a/lib/python/labcomm2014/LabComm.py b/lib/python/labcomm2014/LabComm.py
index 2ac3524..fd4b1fe 100644
--- a/lib/python/labcomm2014/LabComm.py
+++ b/lib/python/labcomm2014/LabComm.py
@@ -203,7 +203,7 @@ class length_encoder:
     def __init__(self, encoder):
         self.encoder = encoder
         self.version = encoder.version
-        self.data = ""
+        self.data = b''
 
     def write(self, data):
         self.data += data
@@ -509,7 +509,12 @@ class array(type_decl):
         encoder.encode_type_number(self.decl)
 
     def min_max_shape(self, l, depth, shape):
-        if isinstance(l, types.StringTypes):
+        def isstring(obj):
+            try:
+                return isinstance(obj, basestring)
+            except NameError:
+                return isinstance(obj, str)
+        if isstring(l):
             return shape
         try:
             length = len(l)
@@ -801,7 +806,12 @@ class Encoder(Codec):
         if not isinstance(ref, type_decl):
             # Trying to register a sample class
             ref = ref.signature
-        decl = sample_ref(name=ref.name, decl=ref.decl, sample=ref)
+        if not isinstance(ref, sample_ref):
+            sample = ref
+        else:
+            # Adding a sample_ref, keep sample type
+            sample = ref.sample
+        decl = sample_ref(name=ref.name, decl=ref.decl, sample=sample)
         if index == 0:
             self.writer.mark_begin(ref, None)
             if super(Encoder, self).add_ref(decl, index):
@@ -868,8 +878,8 @@ class Encoder(Codec):
 
     def encode_string(self, v):
         s = v.encode("utf8")
-	self.encode_packed32(len(s));
-	self.pack("%ds" % len(s),s)
+        self.encode_packed32(len(s));
+        self.pack("%ds" % len(s),s)
 
 class Decoder(Codec):
     def __init__(self, reader, version=DEFAULT_VERSION):
@@ -883,7 +893,7 @@ class Decoder(Codec):
 
     def unpack(self, format):
         size = packer.calcsize(format)
-        data = ""
+        data = b''
         while len(data) < size:
             data += self.reader.read(size - len(data))
         result = packer.unpack(format, data)
diff --git a/lib/python/labcomm2014/__init__.py b/lib/python/labcomm2014/__init__.py
index 2e36c21..905cd31 100644
--- a/lib/python/labcomm2014/__init__.py
+++ b/lib/python/labcomm2014/__init__.py
@@ -1,9 +1,9 @@
 __all__ = [ 'LabComm' ]
 
-import LabComm
+from . import LabComm
 
-from StreamReader import StreamReader
-from StreamWriter import StreamWriter
+from .StreamReader import StreamReader
+from .StreamWriter import StreamWriter
 
 Decoder = LabComm.Decoder
 Encoder = LabComm.Encoder
diff --git a/test/Makefile b/test/Makefile
index 3694ac5..1d9a7c9 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -26,6 +26,8 @@ test_%: gen/%/signatures.py \
 		./test_encoder_decoder.py \
 		--signatures=gen/$*/signatures.py \
 		--test tee gen/$*/testdata \
+	        --test python2 python_generic_relay.py /dev/stdin /dev/stdout \
+	        --test python3 python_generic_relay.py /dev/stdin /dev/stdout \
 		--test $(shell echo $(VALGRIND) | sed -e 's/[-][-]/\\\\--/g') \
 		       gen/$*/c_relay /dev/stdin /dev/stdout \
 		--test mono gen/$*/cs_relay.exe /dev/stdin /dev/stdout \
@@ -42,6 +44,8 @@ test_renaming_%: gen/%/signatures.py \
 	    ./test_renaming_encoder_decoder.py \
 		--signatures=gen/$*/signatures.py \
 		--test tee gen/$*/testdata.renamed \
+	        --test python2 python_generic_relay.py /dev/stdin /dev/stdout \
+	        --test python3 python_generic_relay.py /dev/stdin /dev/stdout \
 		--test $(shell echo $(VALGRIND) | sed -e 's/[-][-]/\\\\--/g') \
 		       gen/$*/c_renaming_relay /dev/stdin /dev/stdout \
 		--test mono gen/$*/cs_renaming_relay.exe \
diff --git a/test/python_generic_relay.py b/test/python_generic_relay.py
new file mode 100644
index 0000000..453b9cf
--- /dev/null
+++ b/test/python_generic_relay.py
@@ -0,0 +1,21 @@
+from __future__ import print_function
+import sys
+import labcomm2014
+
+if __name__ == '__main__':
+    reader = labcomm2014.StreamReader(open(sys.argv[1], 'rb'))
+    writer = labcomm2014.StreamWriter(open(sys.argv[2], 'wb'))
+    decoder = labcomm2014.Decoder(reader)
+    encoder = labcomm2014.Encoder(writer)
+    while True:
+        try:
+            value, decl = decoder.decode()
+        except EOFError:
+            break
+        if value == None:
+            if isinstance(decl, labcomm2014.sample_ref):
+                encoder.add_ref(decl)
+            else:
+                encoder.add_decl(decl)
+        else:
+            encoder.encode(value, decl)
-- 
GitLab