From c0a49011813ddf2b7f8b0364e3f622ffdec15d97 Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Sun, 13 Apr 2014 16:44:37 -0400 Subject: [PATCH] Fix support for Python 3 Closes #144. --- python/nrepl_fireplace.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/python/nrepl_fireplace.py b/python/nrepl_fireplace.py index 20b2a22..7736769 100644 --- a/python/nrepl_fireplace.py +++ b/python/nrepl_fireplace.py @@ -4,7 +4,10 @@ import select import socket import sys -from StringIO import StringIO +try: + from StringIO import StringIO +except ImportError: + from io import StringIO def noop(): pass @@ -84,13 +87,16 @@ class Connection: return self.socket.close() def send(self, payload): - self.socket.sendall(payload) + if sys.version_info[0] >= 3: + self.socket.sendall(bytes(payload, 'UTF-8')) + else: + self.socket.sendall(payload) return '' def receive(self, char=None): - while len(select.select([self.socket], [], [], 0.1)[0]) == 0: - self.poll() f = self.socket.makefile() + while len(select.select([f], [], [], 0.1)[0]) == 0: + self.poll() try: return bdecode(f) finally: @@ -118,8 +124,8 @@ def dispatch(host, port, poll, keepalive, command, *args): def main(host, port, keepalive, command, *args): try: sys.stdout.write(vim_encode(dispatch(host, port, noop, keepalive, command, *[bdecode(StringIO(arg)) for arg in args]))) - except Exception, e: - print(e) + except Exception: + print((sys.exc_info()[1])) exit(1) if __name__ == "__main__":