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:
MarcoFalke committed 2025-11-26 11:31:16 +01:00
1 parent fa7d72bd1b
commit fae612424b
54 files changed
+127 -128

No files matched your search

+2 -2
View File
@@ -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)
+2 -2
View File
@@ -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)