From 00114efc1ede202c856e767d015e4e0922ea9556 Mon Sep 17 00:00:00 2001 From: Johan Sommerfeld Date: Thu, 12 May 2016 12:20:51 +0200 Subject: [PATCH 1/2] pep8 fixes on __init__.py --- erlastic/__init__.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/erlastic/__init__.py b/erlastic/__init__.py index 4f3d1f1..ab78f89 100644 --- a/erlastic/__init__.py +++ b/erlastic/__init__.py @@ -1,4 +1,6 @@ """Erlang External Term Format serializer/deserializer""" +import struct +import sys from erlastic.codec import ErlangTermDecoder, ErlangTermEncoder from erlastic.types import * @@ -6,23 +8,24 @@ encode = ErlangTermEncoder().encode decode = ErlangTermDecoder().decode -import struct -import sys def mailbox_gen(): - while True: - len_bin = sys.stdin.buffer.read(4) - if len(len_bin) != 4: return - (length,) = struct.unpack('!I',len_bin) - yield decode(sys.stdin.buffer.read(length)) + while True: + len_bin = sys.stdin.buffer.read(4) + if len(len_bin) != 4: + return + (length,) = struct.unpack('!I', len_bin) + yield decode(sys.stdin.buffer.read(length)) + def port_gen(): - while True: - term = encode((yield)) - sys.stdout.buffer.write(struct.pack('!I',len(term))) - sys.stdout.buffer.write(term) + while True: + term = encode((yield)) + sys.stdout.buffer.write(struct.pack('!I', len(term))) + sys.stdout.buffer.write(term) + def port_connection(): - port = port_gen() - next(port) - return mailbox_gen(),port + port = port_gen() + next(port) + return mailbox_gen(), port From 853b3835b88380eb2c57990c154e89b8b87ae91a Mon Sep 17 00:00:00 2001 From: Johan Sommerfeld Date: Thu, 12 May 2016 13:18:03 +0200 Subject: [PATCH 2/2] py2/3 compatible stdin/out handling --- erlastic/__init__.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/erlastic/__init__.py b/erlastic/__init__.py index ab78f89..95f3a33 100644 --- a/erlastic/__init__.py +++ b/erlastic/__init__.py @@ -1,6 +1,7 @@ """Erlang External Term Format serializer/deserializer""" import struct import sys +import six from erlastic.codec import ErlangTermDecoder, ErlangTermEncoder from erlastic.types import * @@ -8,21 +9,28 @@ encode = ErlangTermEncoder().encode decode = ErlangTermDecoder().decode +if six.PY3: + stdread = sys.stdin.buffer.read + stdwrite = sys.stdout.buffer.write +else: + stdread = sys.stdin.read + stdwrite = sys.stdout.write + def mailbox_gen(): while True: - len_bin = sys.stdin.buffer.read(4) + len_bin = stdread(4) if len(len_bin) != 4: return (length,) = struct.unpack('!I', len_bin) - yield decode(sys.stdin.buffer.read(length)) + yield decode(stdread(length)) def port_gen(): while True: term = encode((yield)) - sys.stdout.buffer.write(struct.pack('!I', len(term))) - sys.stdout.buffer.write(term) + stdwrite(struct.pack('!I', len(term))) + stdwrite(term) def port_connection():