Skip to content
Snippets Groups Projects
Commit a7800e6b authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Made python labrary python3 compatible

parent f90d1486
Branches
No related tags found
No related merge requests found
...@@ -203,7 +203,7 @@ class length_encoder: ...@@ -203,7 +203,7 @@ class length_encoder:
def __init__(self, encoder): def __init__(self, encoder):
self.encoder = encoder self.encoder = encoder
self.version = encoder.version self.version = encoder.version
self.data = "" self.data = b''
def write(self, data): def write(self, data):
self.data += data self.data += data
...@@ -509,7 +509,12 @@ class array(type_decl): ...@@ -509,7 +509,12 @@ class array(type_decl):
encoder.encode_type_number(self.decl) encoder.encode_type_number(self.decl)
def min_max_shape(self, l, depth, shape): 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 return shape
try: try:
length = len(l) length = len(l)
...@@ -801,7 +806,12 @@ class Encoder(Codec): ...@@ -801,7 +806,12 @@ class Encoder(Codec):
if not isinstance(ref, type_decl): if not isinstance(ref, type_decl):
# Trying to register a sample class # Trying to register a sample class
ref = ref.signature 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: if index == 0:
self.writer.mark_begin(ref, None) self.writer.mark_begin(ref, None)
if super(Encoder, self).add_ref(decl, index): if super(Encoder, self).add_ref(decl, index):
...@@ -883,7 +893,7 @@ class Decoder(Codec): ...@@ -883,7 +893,7 @@ class Decoder(Codec):
def unpack(self, format): def unpack(self, format):
size = packer.calcsize(format) size = packer.calcsize(format)
data = "" data = b''
while len(data) < size: while len(data) < size:
data += self.reader.read(size - len(data)) data += self.reader.read(size - len(data))
result = packer.unpack(format, data) result = packer.unpack(format, data)
......
__all__ = [ 'LabComm' ] __all__ = [ 'LabComm' ]
import LabComm from . import LabComm
from StreamReader import StreamReader from .StreamReader import StreamReader
from StreamWriter import StreamWriter from .StreamWriter import StreamWriter
Decoder = LabComm.Decoder Decoder = LabComm.Decoder
Encoder = LabComm.Encoder Encoder = LabComm.Encoder
......
...@@ -26,6 +26,8 @@ test_%: gen/%/signatures.py \ ...@@ -26,6 +26,8 @@ test_%: gen/%/signatures.py \
./test_encoder_decoder.py \ ./test_encoder_decoder.py \
--signatures=gen/$*/signatures.py \ --signatures=gen/$*/signatures.py \
--test tee gen/$*/testdata \ --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') \ --test $(shell echo $(VALGRIND) | sed -e 's/[-][-]/\\\\--/g') \
gen/$*/c_relay /dev/stdin /dev/stdout \ gen/$*/c_relay /dev/stdin /dev/stdout \
--test mono gen/$*/cs_relay.exe /dev/stdin /dev/stdout \ --test mono gen/$*/cs_relay.exe /dev/stdin /dev/stdout \
...@@ -42,6 +44,8 @@ test_renaming_%: gen/%/signatures.py \ ...@@ -42,6 +44,8 @@ test_renaming_%: gen/%/signatures.py \
./test_renaming_encoder_decoder.py \ ./test_renaming_encoder_decoder.py \
--signatures=gen/$*/signatures.py \ --signatures=gen/$*/signatures.py \
--test tee gen/$*/testdata.renamed \ --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') \ --test $(shell echo $(VALGRIND) | sed -e 's/[-][-]/\\\\--/g') \
gen/$*/c_renaming_relay /dev/stdin /dev/stdout \ gen/$*/c_renaming_relay /dev/stdin /dev/stdout \
--test mono gen/$*/cs_renaming_relay.exe \ --test mono gen/$*/cs_renaming_relay.exe \
......
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment