Skip to content

Commit 30930e8

Browse files
committed
test: Explicitly set encoding to utf8 when opening text files
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.
1 parent f560d95 commit 30930e8

File tree

7 files changed

+12
-12
lines changed

7 files changed

+12
-12
lines changed

qa/rpc-tests/forknotify.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self):
2222
def setup_network(self):
2323
self.nodes = []
2424
self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt")
25-
with open(self.alert_filename, 'w') as f:
25+
with open(self.alert_filename, 'w', encoding='utf8') as f:
2626
pass # Just open then close to create zero-length file
2727
self.nodes.append(start_node(0, self.options.tmpdir,
2828
["-blockversion=2", "-alertnotify=echo %s >> \"" + self.alert_filename + "\""]))
@@ -44,7 +44,7 @@ def run_test(self):
4444
self.nodes[1].generate(1)
4545
self.sync_all()
4646

47-
with open(self.alert_filename, 'r') as f:
47+
with open(self.alert_filename, 'r', encoding='utf8') as f:
4848
alert_text = f.read()
4949

5050
if len(alert_text) == 0:
@@ -56,7 +56,7 @@ def run_test(self):
5656
self.nodes[1].generate(1)
5757
self.sync_all()
5858

59-
with open(self.alert_filename, 'r') as f:
59+
with open(self.alert_filename, 'r', encoding='utf8') as f:
6060
alert_text2 = f.read()
6161

6262
if alert_text != alert_text2:

qa/rpc-tests/multi_rpc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def setup_chain(self):
2626
#Append rpcauth to bitcoin.conf before initialization
2727
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
2828
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
29-
with open(os.path.join(self.options.tmpdir+"/node0", "bitcoin.conf"), 'a') as f:
29+
with open(os.path.join(self.options.tmpdir+"/node0", "bitcoin.conf"), 'a', encoding='utf8') as f:
3030
f.write(rpcauth+"\n")
3131
f.write(rpcauth2+"\n")
3232

qa/rpc-tests/p2p-versionbits-warning.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(self):
7272
def setup_network(self):
7373
self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt")
7474
# Open and close to create zero-length file
75-
with open(self.alert_filename, 'w') as _:
75+
with open(self.alert_filename, 'w', encoding='utf8') as _:
7676
pass
7777
self.extra_args = [["-debug", "-logtimemicros=1", "-alertnotify=echo %s >> \"" + self.alert_filename + "\""]]
7878
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, self.extra_args)
@@ -95,7 +95,7 @@ def send_blocks_with_version(self, peer, numblocks, nVersionToUse):
9595
peer.sync_with_ping()
9696

9797
def test_versionbits_in_alert_file(self):
98-
with open(self.alert_filename, 'r') as f:
98+
with open(self.alert_filename, 'r', encoding='utf8') as f:
9999
alert_text = f.read()
100100
assert(VB_PATTERN.match(alert_text))
101101

@@ -147,7 +147,7 @@ def run_test(self):
147147
stop_node(self.nodes[0], 0)
148148
wait_bitcoinds()
149149
# Empty out the alert file
150-
with open(self.alert_filename, 'w') as _:
150+
with open(self.alert_filename, 'w', encoding='utf8') as _:
151151
pass
152152
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, self.extra_args)
153153

qa/rpc-tests/test_framework/coverage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __call__(self, *args, **kwargs):
5050
rpc_method = self.auth_service_proxy_instance._service_name
5151

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

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

103-
with open(filename, 'w') as f:
103+
with open(filename, 'w', encoding='utf8') as f:
104104
f.writelines(list(commands))
105105

106106
return True

qa/rpc-tests/test_framework/netutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def netstat(typ='tcp'):
5858
To get pid of all network process running on system, you must run this script
5959
as superuser
6060
'''
61-
with open('/proc/net/'+typ,'r') as f:
61+
with open('/proc/net/'+typ,'r',encoding='utf8') as f:
6262
content = f.readlines()
6363
content.pop(0)
6464
result = []

qa/rpc-tests/test_framework/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def initialize_datadir(dirname, n):
161161
if not os.path.isdir(datadir):
162162
os.makedirs(datadir)
163163
rpc_u, rpc_p = rpc_auth_pair(n)
164-
with open(os.path.join(datadir, "bitcoin.conf"), 'w') as f:
164+
with open(os.path.join(datadir, "bitcoin.conf"), 'w', encoding='utf8') as f:
165165
f.write("regtest=1\n")
166166
f.write("rpcuser=" + rpc_u + "\n")
167167
f.write("rpcpassword=" + rpc_p + "\n")

qa/rpc-tests/wallet-dump.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def read_dump(file_name, addrs, hd_master_addr_old):
1212
Read the given dump, count the addrs that match, count change and reserve.
1313
Also check that the old hd_master is inactive
1414
"""
15-
with open(file_name) as inputfile:
15+
with open(file_name, encoding='utf8') as inputfile:
1616
found_addr = 0
1717
found_addr_chg = 0
1818
found_addr_rsv = 0

0 commit comments

Comments
 (0)