Skip to content

Commit

Permalink
python: Explicitly set encoding to utf8 when opening text files
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Nov 9, 2020
1 parent 1944eb1 commit 337e528
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions qa/rpc-tests/hardforkdetection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "\""]))
Expand Down Expand Up @@ -48,7 +48,7 @@ def run_test(self):
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:
Expand Down
2 changes: 1 addition & 1 deletion qa/rpc-tests/sapling_rewind_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def run_test(self):
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.
Expand Down
2 changes: 1 addition & 1 deletion qa/rpc-tests/test_framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion qa/rpc-tests/wallet_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def run_test(self):

# 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")
Expand Down
2 changes: 1 addition & 1 deletion qa/zcash/smoke_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ def start(self, extra_args=None, timewait=None):
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)
Expand Down
8 changes: 4 additions & 4 deletions qa/zcash/updatecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def __init__(self, name):

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+)$",
Expand All @@ -329,7 +329,7 @@ def current_version(self):
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:
Expand All @@ -340,7 +340,7 @@ def current_version(self):
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:
Expand All @@ -357,7 +357,7 @@ def __init__(self):
"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 != "":
Expand Down
4 changes: 2 additions & 2 deletions zcutil/make-release.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,14 +595,14 @@ def __init__(self, path):

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())


Expand Down

0 comments on commit 337e528

Please sign in to comment.