From 8a2c7bea8e66a73679a5ebd3287f8eaeec1063ce Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Thu, 21 Jul 2016 19:43:56 -0700 Subject: [PATCH] py3: fix copy to clipboard --- mitmproxy/console/common.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mitmproxy/console/common.py b/mitmproxy/console/common.py index 6f0b46ddc..c7f87a8f7 100644 --- a/mitmproxy/console/common.py +++ b/mitmproxy/console/common.py @@ -134,7 +134,11 @@ def save_data(path, data): if not path: return try: - with open(path, "wb") as f: + if isinstance(data, bytes): + mode = "wb" + else: + mode = "w" + with open(path, mode) as f: f.write(data) except IOError as v: signals.status_message.send(message=v.strerror) @@ -193,10 +197,9 @@ def ask_scope_and_callback(flow, cb, *args): def copy_to_clipboard_or_prompt(data): # pyperclip calls encode('utf-8') on data to be copied without checking. # if data are already encoded that way UnicodeDecodeError is thrown. - toclip = "" - try: - toclip = data.decode('utf-8') - except (UnicodeDecodeError): + if isinstance(data, bytes): + toclip = data.decode("utf8", "replace") + else: toclip = data try: