Skip to content

Commit

Permalink
test: Explicitly set encoding to utf8 when opening text files
Browse files Browse the repository at this point in the history
These are text files but their encoding does not depend on the locale.
Not all of them require utf8 but it is better to fix it at something
to remove potential unpredictability.

This is necessary on FreeBSD where no locale is set by default,
and apparently Python defaults not only the terminal encoding to the locale
but that of every text file. So without LOCALE environment it defaults text
file encoding to ASCII. This causes problems with e.g. `bitcoin.conf`.

Luckily the locale doesn't affect the default encoding for str.encode() and
bytes.decode() on Python 3, so this is the only change necessary.

Github-Pull: #8840
Rebased-From: 30930e847e2483c7c8163cc581b392bc288250e9
  • Loading branch information
laanwj authored and MarcoFalke committed Oct 3, 2016
1 parent 0bee740 commit cbc3fe5
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions qa/rpc-tests/forknotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self):
def setup_network(self):
self.nodes = []
self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt")
with open(self.alert_filename, 'w') as f:
with open(self.alert_filename, 'w', encoding='utf8') as f:
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 All @@ -44,7 +44,7 @@ def run_test(self):
self.nodes[1].generate(1)
self.sync_all()

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 All @@ -56,7 +56,7 @@ def run_test(self):
self.nodes[1].generate(1)
self.sync_all()

with open(self.alert_filename, 'r') as f:
with open(self.alert_filename, 'r', encoding='utf8') as f:
alert_text2 = f.read()

if alert_text != alert_text2:
Expand Down
2 changes: 1 addition & 1 deletion qa/rpc-tests/multi_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def setup_chain(self):
#Append rpcauth to bitcoin.conf before initialization
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
with open(os.path.join(self.options.tmpdir+"/node0", "bitcoin.conf"), 'a') as f:
with open(os.path.join(self.options.tmpdir+"/node0", "bitcoin.conf"), 'a', encoding='utf8') as f:
f.write(rpcauth+"\n")
f.write(rpcauth2+"\n")

Expand Down
6 changes: 3 additions & 3 deletions qa/rpc-tests/p2p-versionbits-warning.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(self):
def setup_network(self):
self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt")
# Open and close to create zero-length file
with open(self.alert_filename, 'w') as _:
with open(self.alert_filename, 'w', encoding='utf8') as _:
pass
self.extra_args = [["-debug", "-logtimemicros=1", "-alertnotify=echo %s >> \"" + self.alert_filename + "\""]]
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, self.extra_args)
Expand All @@ -95,7 +95,7 @@ def send_blocks_with_version(self, peer, numblocks, nVersionToUse):
peer.sync_with_ping()

def test_versionbits_in_alert_file(self):
with open(self.alert_filename, 'r') as f:
with open(self.alert_filename, 'r', encoding='utf8') as f:
alert_text = f.read()
assert(VB_PATTERN.match(alert_text))

Expand Down Expand Up @@ -146,7 +146,7 @@ def run_test(self):
self.nodes[0].generate(VB_PERIOD)
stop_nodes(self.nodes)
# Empty out the alert file
with open(self.alert_filename, 'w') as _:
with open(self.alert_filename, 'w', encoding='utf8') as _:
pass
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, self.extra_args)

Expand Down
4 changes: 2 additions & 2 deletions qa/rpc-tests/test_framework/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __call__(self, *args, **kwargs):
rpc_method = self.auth_service_proxy_instance._service_name

if self.coverage_logfile:
with open(self.coverage_logfile, 'a+') as f:
with open(self.coverage_logfile, 'a+', encoding='utf8') as f:
f.write("%s\n" % rpc_method)

return return_val
Expand Down Expand Up @@ -100,7 +100,7 @@ def write_all_rpc_commands(dirname, node):
if line and not line.startswith('='):
commands.add("%s\n" % line.split()[0])

with open(filename, 'w') as f:
with open(filename, 'w', encoding='utf8') as f:
f.writelines(list(commands))

return True
2 changes: 1 addition & 1 deletion qa/rpc-tests/test_framework/netutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def netstat(typ='tcp'):
To get pid of all network process running on system, you must run this script
as superuser
'''
with open('/proc/net/'+typ,'r') as f:
with open('/proc/net/'+typ,'r',encoding='utf8') as f:
content = f.readlines()
content.pop(0)
result = []
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 @@ -157,7 +157,7 @@ def initialize_datadir(dirname, n):
if not os.path.isdir(datadir):
os.makedirs(datadir)
rpc_u, rpc_p = rpc_auth_pair(n)
with open(os.path.join(datadir, "bitcoin.conf"), 'w') as f:
with open(os.path.join(datadir, "bitcoin.conf"), 'w', encoding='utf8') as f:
f.write("regtest=1\n")
f.write("rpcuser=" + rpc_u + "\n")
f.write("rpcpassword=" + rpc_p + "\n")
Expand Down
2 changes: 1 addition & 1 deletion qa/rpc-tests/wallet-dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def read_dump(file_name, addrs, hd_master_addr_old):
Read the given dump, count the addrs that match, count change and reserve.
Also check that the old hd_master is inactive
"""
with open(file_name) as inputfile:
with open(file_name, encoding='utf8') as inputfile:
found_addr = 0
found_addr_chg = 0
found_addr_rsv = 0
Expand Down

0 comments on commit cbc3fe5

Please sign in to comment.