From 8fe41e135d565d9f8922e6b4c97aada4e9837403 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Sun, 27 Sep 2015 23:21:12 -0700 Subject: [PATCH] make blocking/send-text work on py3, add dependency on 'six' * use modern/portable "next(iter)" instead of "iter.next()" * use six.moves.input() instead of raw_input() * tell requests' Response.iter_lines that we want str, not bytes --- setup.py | 3 ++- src/wormhole/blocking/eventsource.py | 14 ++++++++++---- src/wormhole/codes.py | 4 ++-- src/wormhole/scripts/cmd_send_text.py | 4 ++-- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/setup.py b/setup.py index 7488beb..216da9c 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,8 @@ setup(name="magic-wormhole", package_data={"wormhole": ["db-schemas/*.sql"]}, entry_points={"console_scripts": ["wormhole = wormhole.scripts.runner:entry"]}, - install_requires=["spake2==0.3", "pynacl", "requests", "argparse"], + install_requires=["spake2==0.3", "pynacl", "requests", "argparse", + "six"], test_suite="wormhole.test", cmdclass=commands, ) diff --git a/src/wormhole/blocking/eventsource.py b/src/wormhole/blocking/eventsource.py index 28bc70f..e02667b 100644 --- a/src/wormhole/blocking/eventsource.py +++ b/src/wormhole/blocking/eventsource.py @@ -1,3 +1,4 @@ +import six import requests class EventSourceFollower: @@ -13,11 +14,12 @@ class EventSourceFollower: def _get_fields(self, lines): while True: - first_line = lines.next() # raises StopIteration when closed + first_line = next(lines) # raises StopIteration when closed + assert isinstance(first_line, type(six.u(""))), type(first_line) fieldname, data = first_line.split(": ", 1) data_lines = [data] while True: - next_line = lines.next() + next_line = next(lines) if not next_line: # empty string, original was "\n" yield (fieldname, "\n".join(data_lines)) break @@ -30,12 +32,16 @@ class EventSourceFollower: # for a long time. I'd prefer that chunk_size behaved like # read(size), and gave you 1<=x<=size bytes in response. eventtype = "message" - lines_iter = self.resp.iter_lines(chunk_size=1) + lines_iter = self.resp.iter_lines(chunk_size=1, decode_unicode=True) for (fieldname, data) in self._get_fields(lines_iter): + # fieldname/data are unicode on both py2 and py3. On py2, where + # ("abc"==u"abc" is True), this compares unicode against str, + # which matches. On py3, where (b"abc"=="abc" is False), this + # compares unicode against unicode, which matches. if fieldname == "data": yield (eventtype, data) eventtype = "message" elif fieldname == "event": eventtype = data else: - print("weird fieldname", fieldname, data) + print("weird fieldname", fieldname, type(fieldname), data) diff --git a/src/wormhole/codes.py b/src/wormhole/codes.py index 966b262..c26a819 100644 --- a/src/wormhole/codes.py +++ b/src/wormhole/codes.py @@ -1,5 +1,5 @@ from __future__ import print_function -import os +import os, six from .wordlist import (byte_to_even_word, byte_to_odd_word, even_words_lowercase, odd_words_lowercase) @@ -81,7 +81,7 @@ def input_code_with_completion(prompt, get_channel_ids, code_length): readline.parse_and_bind("tab: complete") readline.set_completer(c.wrap_completer) readline.set_completer_delims("") - code = raw_input(prompt) + code = six.moves.input(prompt) return code if __name__ == "__main__": diff --git a/src/wormhole/scripts/cmd_send_text.py b/src/wormhole/scripts/cmd_send_text.py index 2041e6f..2a0ce4f 100644 --- a/src/wormhole/scripts/cmd_send_text.py +++ b/src/wormhole/scripts/cmd_send_text.py @@ -1,5 +1,5 @@ from __future__ import print_function -import sys, json, binascii +import sys, json, binascii, six from ..errors import handle_server_error APPID = b"lothar.com/wormhole/text-xfer" @@ -31,7 +31,7 @@ def send_text(args): if args.verify: verifier = binascii.hexlify(w.get_verifier()) while True: - ok = raw_input("Verifier %s. ok? (yes/no): " % verifier) + ok = six.moves.input("Verifier %s. ok? (yes/no): " % verifier) if ok.lower() == "yes": break if ok.lower() == "no":