From 337e528d337812bd1f067e480dd9102fe2b98692 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 28 Oct 2020 12:51:11 +0000 Subject: [PATCH] python: Explicitly set encoding to utf8 when opening text files --- qa/rpc-tests/hardforkdetection.py | 4 ++-- qa/rpc-tests/sapling_rewind_check.py | 2 +- qa/rpc-tests/test_framework/util.py | 2 +- qa/rpc-tests/wallet_import_export.py | 2 +- qa/zcash/smoke_tests.py | 2 +- qa/zcash/updatecheck.py | 8 ++++---- zcutil/make-release.py | 4 ++-- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/qa/rpc-tests/hardforkdetection.py b/qa/rpc-tests/hardforkdetection.py index 78735beb4..01374573f 100755 --- a/qa/rpc-tests/hardforkdetection.py +++ b/qa/rpc-tests/hardforkdetection.py @@ -17,7 +17,7 @@ class HardForkDetectionTest(BitcoinTestFramework): def setup_network(self): self.nodes = [] self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt") - with open(self.alert_filename, 'w'): + with open(self.alert_filename, 'w', encoding='utf8'): pass # Just open then close to create zero-length file self.nodes.append(start_node(0, self.options.tmpdir, ["-blockversion=2", "-alertnotify=echo %s >> \"" + self.alert_filename + "\""])) @@ -48,7 +48,7 @@ class HardForkDetectionTest(BitcoinTestFramework): self.assert_safemode_on("We do not appear to fully agree with our peers!") # Check that an -alertnotify was triggered. - with open(self.alert_filename, 'r') as f: + with open(self.alert_filename, 'r', encoding='utf8') as f: alert_text = f.read() if len(alert_text) == 0: diff --git a/qa/rpc-tests/sapling_rewind_check.py b/qa/rpc-tests/sapling_rewind_check.py index 206a28541..05c0bae72 100755 --- a/qa/rpc-tests/sapling_rewind_check.py +++ b/qa/rpc-tests/sapling_rewind_check.py @@ -98,7 +98,7 @@ class SaplingRewindTest(BitcoinTestFramework): except: logpath = self.options.tmpdir + "/node2/regtest/debug.log" found = False - with open(logpath, 'r') as f: + with open(logpath, 'r', encoding='utf8') as f: for line in f: # Search for the rollback message in the debug log, and ensure that it has the # correct expected rollback length. diff --git a/qa/rpc-tests/test_framework/util.py b/qa/rpc-tests/test_framework/util.py index 5d06ed623..eeacfdba5 100644 --- a/qa/rpc-tests/test_framework/util.py +++ b/qa/rpc-tests/test_framework/util.py @@ -600,7 +600,7 @@ def check_node_log(self, node_number, line_to_check, stop_node = True): self.nodes[node_number].stop() bitcoind_processes[node_number].wait() logpath = self.options.tmpdir + "/node" + str(node_number) + "/regtest/debug.log" - with open(logpath, "r") as myfile: + with open(logpath, "r", encoding="utf8") as myfile: logdata = myfile.readlines() for (n, logline) in enumerate(logdata): if line_to_check in logline: diff --git a/qa/rpc-tests/wallet_import_export.py b/qa/rpc-tests/wallet_import_export.py index fbf4f3c89..67e43842c 100755 --- a/qa/rpc-tests/wallet_import_export.py +++ b/qa/rpc-tests/wallet_import_export.py @@ -57,7 +57,7 @@ class WalletImportExportTest (BitcoinTestFramework): # Helper functions def parse_wallet_file(dump_path): - file_lines = open(dump_path, "r").readlines() + file_lines = open(dump_path, "r", encoding="utf8").readlines() # We expect information about the HDSeed and fingerpring in the header assert_true("HDSeed" in file_lines[4], "Expected HDSeed") assert_true("fingerprint" in file_lines[4], "Expected fingerprint") diff --git a/qa/zcash/smoke_tests.py b/qa/zcash/smoke_tests.py index 0333a01b1..574151184 100755 --- a/qa/zcash/smoke_tests.py +++ b/qa/zcash/smoke_tests.py @@ -747,7 +747,7 @@ class ZcashNode(object): cli_args.append('-testnet=1') cli_args.append('getblockcount') - devnull = open('/dev/null', 'w+') + devnull = open('/dev/null', 'w+', encoding='utf8') if os.getenv('PYTHON_DEBUG', ''): print('start_node: zcashd started, calling zcash-cli -rpcwait getblockcount') subprocess.check_call(cli_args, stdout=devnull) diff --git a/qa/zcash/updatecheck.py b/qa/zcash/updatecheck.py index 38ae8761f..4d929eda7 100755 --- a/qa/zcash/updatecheck.py +++ b/qa/zcash/updatecheck.py @@ -303,7 +303,7 @@ class DependsVersionGetter: def current_version(self): mk_file_path = os.path.join(SOURCE_ROOT, "depends", "packages", safe_depends(self.name) + ".mk") - mk_file = open(mk_file_path, 'r').read() + mk_file = open(mk_file_path, 'r', encoding='utf8').read() regexp_whitelist = [ "package\)_version=(\d+)\.(\d+)\.(\d+)$", @@ -329,7 +329,7 @@ class DependsVersionGetter: class LevelDbVersionGetter: def current_version(self): header_path = os.path.join(SOURCE_ROOT, "src", "leveldb", "include", "leveldb", "db.h") - header_contents = open(header_path, 'r').read() + header_contents = open(header_path, 'r', encoding='utf8').read() match = re.search("kMajorVersion\s*=\s*(\d+);\s*.*kMinorVersion\s*=\s*(\d+);\s*$", header_contents, re.MULTILINE) if match: @@ -340,7 +340,7 @@ class LevelDbVersionGetter: class UnivalueVersionGetter: def current_version(self): configure_path = os.path.join(SOURCE_ROOT, "src", "univalue", "configure.ac") - configure_contents = open(configure_path, 'r').read() + configure_contents = open(configure_path, 'r', encoding='utf8').read() match = re.search("AC_INIT.*univalue.*\[(\d+)\.(\d+)\.(\d+)\]", configure_contents) if match: @@ -357,7 +357,7 @@ class PostponedUpdates(): "postponed-updates.txt" ) - file = open(postponedlist_path, 'r') + file = open(postponedlist_path, 'r', encoding='utf8') for line in file.readlines(): stripped = re.sub('#.*$', '', line).strip() if stripped != "": diff --git a/zcutil/make-release.py b/zcutil/make-release.py index e795dd4a0..67f10e2c9 100755 --- a/zcutil/make-release.py +++ b/zcutil/make-release.py @@ -595,14 +595,14 @@ class PathPatcher (object): def __enter__(self): logging.debug('Patching %r', self._path) - self._inf = open(self._path, 'r') + self._inf = open(self._path, 'r', encoding='utf8') self._outf = StringIO() return (self._inf, self._outf) def __exit__(self, et, ev, tb): if (et, ev, tb) == (None, None, None): self._inf.close() - with open(self._path, 'w') as f: + with open(self._path, 'w', encoding='utf8') as f: f.write(self._outf.getvalue())