contrib: Remove confusing and redundant encoding from IO
The encoding arg is confusing, because it is not applied consistently for all IO. Also, it is useless, as the majority of files are ASCII encoded, which are fine to encode and decode with any mode. Moreover, UTF-8 is already required for most scripts to work properly, so setting the encoding twice is redundant. So remove the encoding from most IO. It would be fine to remove from all IO, however I kept it for two files: * contrib/asmap/asmap-tool.py: This specifically looks for utf-8 encoding errors, so it makes sense to sepecify the utf-8 encoding explicitly. * test/functional/test_framework/test_node.py: Reading the debug log in text mode specifically counts the utf-8 characters (not bytes), so it makes sense to specify the utf-8 encoding explicitly.
This commit is contained in:
54 files changed
+127
-128
No files matched your search
@@ -49,7 +49,7 @@ for arg in sys.argv[1:]:
|
||||
# TODO: implement support for multiple include directories
|
||||
for arg in sorted(files.keys()):
|
||||
module = files[arg]
|
||||
with open(arg, 'r', encoding="utf8") as f:
|
||||
with open(arg, 'r') as f:
|
||||
for line in f:
|
||||
match = RE.match(line)
|
||||
if match:
|
||||
|
||||
@@ -169,7 +169,7 @@ def main():
|
||||
sys.exit(p.returncode)
|
||||
|
||||
if not args.i:
|
||||
with open(filename, encoding="utf8") as f:
|
||||
with open(filename) as f:
|
||||
code = f.readlines()
|
||||
formatted_code = StringIO(stdout).readlines()
|
||||
diff = difflib.unified_diff(
|
||||
|
||||
@@ -140,7 +140,7 @@ def file_has_without_c_style_copyright_for_holder(contents, holder_name):
|
||||
################################################################################
|
||||
|
||||
def read_file(filename):
|
||||
return open(filename, 'r', encoding="utf8").read()
|
||||
return open(filename, 'r').read()
|
||||
|
||||
def gather_file_info(filename):
|
||||
info = {}
|
||||
@@ -316,12 +316,12 @@ def get_most_recent_git_change_year(filename):
|
||||
################################################################################
|
||||
|
||||
def read_file_lines(filename):
|
||||
with open(filename, 'r', encoding="utf8") as f:
|
||||
with open(filename, 'r') as f:
|
||||
file_lines = f.readlines()
|
||||
return file_lines
|
||||
|
||||
def write_file_lines(filename, file_lines):
|
||||
with open(filename, 'w', encoding="utf8") as f:
|
||||
with open(filename, 'w') as f:
|
||||
f.write(''.join(file_lines))
|
||||
|
||||
################################################################################
|
||||
|
||||
@@ -16,8 +16,8 @@ pattern = args.pattern
|
||||
outfile = args.outfile
|
||||
|
||||
in_remove = False
|
||||
with open(tracefile, 'r', encoding="utf8") as f:
|
||||
with open(outfile, 'w', encoding="utf8") as wf:
|
||||
with open(tracefile, 'r') as f:
|
||||
with open(outfile, 'w') as wf:
|
||||
for line in f:
|
||||
for p in pattern:
|
||||
if line.startswith("SF:") and p in line:
|
||||
|
||||
@@ -34,7 +34,7 @@ def get_blk_dt(blk_hdr):
|
||||
# When getting the list of block hashes, undo any byte reversals.
|
||||
def get_block_hashes(settings):
|
||||
blkindex = []
|
||||
with open(settings['hashlist'], "r", encoding="utf8") as f:
|
||||
with open(settings['hashlist'], "r") as f:
|
||||
for line in f:
|
||||
line = line.rstrip()
|
||||
if settings['rev_hash_bytes'] == 'true':
|
||||
@@ -267,7 +267,7 @@ if __name__ == '__main__':
|
||||
print("Usage: linearize-data.py CONFIG-FILE")
|
||||
sys.exit(1)
|
||||
|
||||
with open(sys.argv[1], encoding="utf8") as f:
|
||||
with open(sys.argv[1]) as f:
|
||||
for line in f:
|
||||
# skip comment lines
|
||||
m = re.search(r'^\s*#', line)
|
||||
|
||||
@@ -87,7 +87,7 @@ def get_block_hashes(settings, max_blocks_per_call=10000):
|
||||
|
||||
def get_rpc_cookie():
|
||||
# Open the cookie file
|
||||
with open(os.path.join(os.path.expanduser(settings['datadir']), '.cookie'), 'r', encoding="ascii") as f:
|
||||
with open(os.path.join(os.path.expanduser(settings['datadir']), '.cookie'), 'r') as f:
|
||||
combined = f.readline()
|
||||
combined_split = combined.split(":")
|
||||
settings['rpcuser'] = combined_split[0]
|
||||
@@ -98,7 +98,7 @@ if __name__ == '__main__':
|
||||
print("Usage: linearize-hashes.py CONFIG-FILE")
|
||||
sys.exit(1)
|
||||
|
||||
with open(sys.argv[1], encoding="utf8") as f:
|
||||
with open(sys.argv[1]) as f:
|
||||
for line in f:
|
||||
# skip comment lines
|
||||
m = re.search(r'^\s*#', line)
|
||||
|
||||
@@ -205,7 +205,7 @@ def main():
|
||||
|
||||
jsonrep = json.dumps(messages)
|
||||
if output:
|
||||
with open(str(output), 'w+', encoding="utf8") as f_out:
|
||||
with open(str(output), 'w+') as f_out:
|
||||
f_out.write(jsonrep)
|
||||
else:
|
||||
print(jsonrep)
|
||||
|
||||
@@ -168,16 +168,16 @@ def main():
|
||||
g.write(' *\n')
|
||||
g.write(' * Each line contains a BIP155 serialized (networkID, addr, port) tuple.\n')
|
||||
g.write(' */\n')
|
||||
with open(os.path.join(indir,'nodes_main.txt'), 'r', encoding="utf8") as f:
|
||||
with open(os.path.join(indir,'nodes_main.txt'), 'r') as f:
|
||||
process_nodes(g, f, 'chainparams_seed_main')
|
||||
g.write('\n')
|
||||
with open(os.path.join(indir,'nodes_signet.txt'), 'r', encoding="utf8") as f:
|
||||
with open(os.path.join(indir,'nodes_signet.txt'), 'r') as f:
|
||||
process_nodes(g, f, 'chainparams_seed_signet')
|
||||
g.write('\n')
|
||||
with open(os.path.join(indir,'nodes_test.txt'), 'r', encoding="utf8") as f:
|
||||
with open(os.path.join(indir,'nodes_test.txt'), 'r') as f:
|
||||
process_nodes(g, f, 'chainparams_seed_test')
|
||||
g.write('\n')
|
||||
with open(os.path.join(indir,'nodes_testnet4.txt'), 'r', encoding="utf8") as f:
|
||||
with open(os.path.join(indir,'nodes_testnet4.txt'), 'r') as f:
|
||||
process_nodes(g, f, 'chainparams_seed_testnet4')
|
||||
g.write('#endif // BITCOIN_CHAINPARAMSSEEDS_H\n')
|
||||
|
||||
|
||||
@@ -211,7 +211,7 @@ def main():
|
||||
print('Done.', file=sys.stderr)
|
||||
|
||||
print('Loading and parsing DNS seeds…', end='', file=sys.stderr, flush=True)
|
||||
with open(args.seeds, 'r', encoding='utf8') as f:
|
||||
with open(args.seeds, 'r') as f:
|
||||
lines = f.readlines()
|
||||
ips = [parseline(line) for line in lines]
|
||||
random.shuffle(ips)
|
||||
|
||||
@@ -246,8 +246,8 @@ def files_are_equal(filename1, filename2):
|
||||
eq = contents1 == contents2
|
||||
|
||||
if not eq:
|
||||
with open(filename1, 'r', encoding='utf-8') as f1, \
|
||||
open(filename2, 'r', encoding='utf-8') as f2:
|
||||
with open(filename1, 'r') as f1, \
|
||||
open(filename2, 'r') as f2:
|
||||
f1lines = f1.readlines()
|
||||
f2lines = f2.readlines()
|
||||
|
||||
@@ -426,7 +426,7 @@ def verify_shasums_signature(
|
||||
def parse_sums_file(sums_file_path: str, filename_filter: list[str]) -> list[list[str]]:
|
||||
# extract hashes/filenames of binaries to verify from hash file;
|
||||
# each line has the following format: "<hash> <binary_filename>"
|
||||
with open(sums_file_path, 'r', encoding='utf8') as hash_file:
|
||||
with open(sums_file_path, 'r') as hash_file:
|
||||
return [line.split()[:2] for line in hash_file if len(filename_filter) == 0 or any(f in line for f in filename_filter)]
|
||||
|
||||
|
||||
|
||||
@@ -82,17 +82,17 @@ def main():
|
||||
# get directory of this program and read data files
|
||||
dirname = os.path.dirname(os.path.abspath(__file__))
|
||||
print("Using verify-commits data from " + dirname)
|
||||
with open(dirname + "/trusted-git-root", "r", encoding="utf8") as f:
|
||||
with open(dirname + "/trusted-git-root", "r") as f:
|
||||
verified_root = f.read().splitlines()[0]
|
||||
with open(dirname + "/trusted-sha512-root-commit", "r", encoding="utf8") as f:
|
||||
with open(dirname + "/trusted-sha512-root-commit", "r") as f:
|
||||
verified_sha512_root = f.read().splitlines()[0]
|
||||
with open(dirname + "/allow-revsig-commits", "r", encoding="utf8") as f:
|
||||
with open(dirname + "/allow-revsig-commits", "r") as f:
|
||||
revsig_allowed = f.read().splitlines()
|
||||
with open(dirname + "/allow-unclean-merge-commits", "r", encoding="utf8") as f:
|
||||
with open(dirname + "/allow-unclean-merge-commits", "r") as f:
|
||||
unclean_merge_allowed = f.read().splitlines()
|
||||
with open(dirname + "/allow-incorrect-sha512-commits", "r", encoding="utf8") as f:
|
||||
with open(dirname + "/allow-incorrect-sha512-commits", "r") as f:
|
||||
incorrect_sha512_allowed = f.read().splitlines()
|
||||
with open(dirname + "/trusted-keys", "r", encoding="utf8") as f:
|
||||
with open(dirname + "/trusted-keys", "r") as f:
|
||||
trusted_keys = f.read().splitlines()
|
||||
|
||||
# Set commit and variables
|
||||
|
||||
Reference in new issue
Block a user