From d48883ca17b3d00493e9adbd9165ce6a723f1ab5 Mon Sep 17 00:00:00 2001 From: cameel Date: Mon, 20 Jan 2020 11:20:18 +0100 Subject: [PATCH] Fix crashes in prepare_report.py caused by using str where bytes is expected and vice-versa 1) `Popen.communicate()` expects `bytes` (a raw, binary string) if `stdout`/`stderr` are open in binary mode but is given output from `json.loads()` which is str (an abstract unicode string). Encoding the `str` object into `bytes` using UTF-8 encoding fixes that. 2) `REPORT_FILE` gets opened in binary mode which means that functions like `write()` expect `bytes`. We're giving them `str` which results in an error. Changed mode to text solves the problem. --- scripts/bytecodecompare/prepare_report.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/bytecodecompare/prepare_report.py b/scripts/bytecodecompare/prepare_report.py index 9a3d86c8d..dad4dbbd4 100755 --- a/scripts/bytecodecompare/prepare_report.py +++ b/scripts/bytecodecompare/prepare_report.py @@ -6,7 +6,7 @@ import subprocess import json SOLC_BIN = sys.argv[1] -REPORT_FILE = open("report.txt", "wb") +REPORT_FILE = open("report.txt", "w") for optimize in [False, True]: for f in sorted(glob.glob("*.sol")): @@ -26,9 +26,9 @@ for optimize in [False, True]: if optimize: args += ['--optimize'] proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (out, err) = proc.communicate(json.dumps(input_json)) + (out, err) = proc.communicate(json.dumps(input_json).encode('utf-8')) try: - result = json.loads(out.strip()) + result = json.loads(out.decode('utf-8').strip()) for filename in sorted(result['contracts'].keys()): for contractName in sorted(result['contracts'][filename].keys()): contractData = result['contracts'][filename][contractName]