Skip to content

Commit 634bd97

Browse files
Explicitly specify encoding when opening text files in Python code
1 parent fa4b906 commit 634bd97

19 files changed

+38
-38
lines changed

contrib/devtools/circular-dependencies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def module_name(path):
3737
# TODO: implement support for multiple include directories
3838
for arg in sorted(files.keys()):
3939
module = files[arg]
40-
with open(arg, 'r') as f:
40+
with open(arg, 'r', encoding="utf8") as f:
4141
for line in f:
4242
match = RE.match(line)
4343
if match:

contrib/devtools/clang-format-diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def main():
152152
sys.exit(p.returncode)
153153

154154
if not args.i:
155-
with open(filename) as f:
155+
with open(filename, encoding="utf8") as f:
156156
code = f.readlines()
157157
formatted_code = io.StringIO(stdout).readlines()
158158
diff = difflib.unified_diff(code, formatted_code,

contrib/devtools/copyright_header.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def file_has_without_c_style_copyright_for_holder(contents, holder_name):
146146
################################################################################
147147

148148
def read_file(filename):
149-
return open(os.path.abspath(filename), 'r').read()
149+
return open(os.path.abspath(filename), 'r', encoding="utf8").read()
150150

151151
def gather_file_info(filename):
152152
info = {}
@@ -325,13 +325,13 @@ def get_most_recent_git_change_year(filename):
325325
################################################################################
326326

327327
def read_file_lines(filename):
328-
f = open(os.path.abspath(filename), 'r')
328+
f = open(os.path.abspath(filename), 'r', encoding="utf8")
329329
file_lines = f.readlines()
330330
f.close()
331331
return file_lines
332332

333333
def write_file_lines(filename, file_lines):
334-
f = open(os.path.abspath(filename), 'w')
334+
f = open(os.path.abspath(filename), 'w', encoding="utf8")
335335
f.write(''.join(file_lines))
336336
f.close()
337337

contrib/devtools/github-merge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def main():
191191
merge_branch = 'pull/'+pull+'/merge'
192192
local_merge_branch = 'pull/'+pull+'/local-merge'
193193

194-
devnull = open(os.devnull,'w')
194+
devnull = open(os.devnull, 'w', encoding="utf8")
195195
try:
196196
subprocess.check_call([GIT,'checkout','-q',branch])
197197
except subprocess.CalledProcessError:

contrib/devtools/test-security-check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import unittest
1010

1111
def write_testcode(filename):
12-
with open(filename, 'w') as f:
12+
with open(filename, 'w', encoding="utf8") as f:
1313
f.write('''
1414
#include <stdio.h>
1515
int main()

contrib/filter-lcov.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
outfile = args.outfile
1414

1515
in_remove = False
16-
with open(tracefile, 'r') as f:
17-
with open(outfile, 'w') as wf:
16+
with open(tracefile, 'r', encoding="utf8") as f:
17+
with open(outfile, 'w', encoding="utf8") as wf:
1818
for line in f:
1919
for p in pattern:
2020
if line.startswith("SF:") and p in line:

contrib/linearize/linearize-data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def get_blk_dt(blk_hdr):
7575
# When getting the list of block hashes, undo any byte reversals.
7676
def get_block_hashes(settings):
7777
blkindex = []
78-
f = open(settings['hashlist'], "r")
78+
f = open(settings['hashlist'], "r", encoding="utf8")
7979
for line in f:
8080
line = line.rstrip()
8181
if settings['rev_hash_bytes'] == 'true':
@@ -261,7 +261,7 @@ def run(self):
261261
print("Usage: linearize-data.py CONFIG-FILE")
262262
sys.exit(1)
263263

264-
f = open(sys.argv[1])
264+
f = open(sys.argv[1], encoding="utf8")
265265
for line in f:
266266
# skip comment lines
267267
m = re.search('^\s*#', line)

contrib/linearize/linearize-hashes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def get_block_hashes(settings, max_blocks_per_call=10000):
9696

9797
def get_rpc_cookie():
9898
# Open the cookie file
99-
with open(os.path.join(os.path.expanduser(settings['datadir']), '.cookie'), 'r') as f:
99+
with open(os.path.join(os.path.expanduser(settings['datadir']), '.cookie'), 'r', encoding="ascii") as f:
100100
combined = f.readline()
101101
combined_split = combined.split(":")
102102
settings['rpcuser'] = combined_split[0]
@@ -107,7 +107,7 @@ def get_rpc_cookie():
107107
print("Usage: linearize-hashes.py CONFIG-FILE")
108108
sys.exit(1)
109109

110-
f = open(sys.argv[1])
110+
f = open(sys.argv[1], encoding="utf8")
111111
for line in f:
112112
# skip comment lines
113113
m = re.search('^\s*#', line)

contrib/seeds/generate-seeds.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ def main():
127127
g.write(' * Each line contains a 16-byte IPv6 address and a port.\n')
128128
g.write(' * IPv4 as well as onion addresses are wrapped inside an IPv6 address accordingly.\n')
129129
g.write(' */\n')
130-
with open(os.path.join(indir,'nodes_main.txt'),'r') as f:
130+
with open(os.path.join(indir,'nodes_main.txt'), 'r', encoding="utf8") as f:
131131
process_nodes(g, f, 'pnSeed6_main', 8333)
132132
g.write('\n')
133-
with open(os.path.join(indir,'nodes_test.txt'),'r') as f:
133+
with open(os.path.join(indir,'nodes_test.txt'), 'r', encoding="utf8") as f:
134134
process_nodes(g, f, 'pnSeed6_test', 18333)
135135
g.write('#endif // BITCOIN_CHAINPARAMSSEEDS_H\n')
136136

contrib/verify-commits/verify-commits.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ def main():
7676
# get directory of this program and read data files
7777
dirname = os.path.dirname(os.path.abspath(__file__))
7878
print("Using verify-commits data from " + dirname)
79-
verified_root = open(dirname + "/trusted-git-root", "r").read().splitlines()[0]
80-
verified_sha512_root = open(dirname + "/trusted-sha512-root-commit", "r").read().splitlines()[0]
81-
revsig_allowed = open(dirname + "/allow-revsig-commits", "r").read().splitlines()
82-
unclean_merge_allowed = open(dirname + "/allow-unclean-merge-commits", "r").read().splitlines()
83-
incorrect_sha512_allowed = open(dirname + "/allow-incorrect-sha512-commits", "r").read().splitlines()
79+
verified_root = open(dirname + "/trusted-git-root", "r", encoding="utf8").read().splitlines()[0]
80+
verified_sha512_root = open(dirname + "/trusted-sha512-root-commit", "r", encoding="utf8").read().splitlines()[0]
81+
revsig_allowed = open(dirname + "/allow-revsig-commits", "r", encoding="utf-8").read().splitlines()
82+
unclean_merge_allowed = open(dirname + "/allow-unclean-merge-commits", "r", encoding="utf-8").read().splitlines()
83+
incorrect_sha512_allowed = open(dirname + "/allow-incorrect-sha512-commits", "r", encoding="utf-8").read().splitlines()
8484

8585
# Set commit and branch and set variables
8686
current_commit = args.commit

0 commit comments

Comments
 (0)