From 39cb5565bf5c8c9ad45e279e93a974ee1ed07251 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Thu, 2 Jun 2016 21:39:14 +0300 Subject: [PATCH] HACK: better line iteration --- trezor_agent/gpg/agent.py | 16 ++++++++++------ trezor_agent/gpg/keyring.py | 3 ++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/trezor_agent/gpg/agent.py b/trezor_agent/gpg/agent.py index 4d2841e..56c3897 100644 --- a/trezor_agent/gpg/agent.py +++ b/trezor_agent/gpg/agent.py @@ -49,6 +49,14 @@ def pksign(keygrip, digest, algo): return result +def iterlines(conn): + while True: + line = keyring.recvline(conn) + if line is None: + break + yield line + + def handle_connection(conn): """Handle connection from GPG binary using the ASSUAN protocol.""" keygrip = None @@ -56,8 +64,7 @@ def handle_connection(conn): algo = None keyring.sendline(conn, b'OK') - while True: - line = keyring.recvline(conn) + for line in iterlines(conn): parts = line.split(' ') command = parts[0] args = parts[1:] @@ -90,10 +97,7 @@ def main(): with server.unix_domain_socket_server(sock_path) as sock: for conn in yield_connections(sock): with contextlib.closing(conn): - try: - handle_connection(conn) - except EOFError: - break + handle_connection(conn) if __name__ == '__main__': diff --git a/trezor_agent/gpg/keyring.py b/trezor_agent/gpg/keyring.py index 22d3deb..5b0a513 100644 --- a/trezor_agent/gpg/keyring.py +++ b/trezor_agent/gpg/keyring.py @@ -42,7 +42,8 @@ def recvline(sock): while True: c = sock.recv(1) if not c: - raise EOFError + return None # socket is closed + if c == b'\n': break reply.write(c)