From fae612424b3e70acd6011a4459518174463b3424 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Sat, 25 Oct 2025 10:50:32 +0200 Subject: [PATCH] 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. --- ci/test/02_run_container.py | 3 +- contrib/devtools/circular-dependencies.py | 2 +- contrib/devtools/clang-format-diff.py | 2 +- contrib/devtools/copyright_header.py | 6 +-- contrib/filter-lcov.py | 4 +- contrib/linearize/linearize-data.py | 4 +- contrib/linearize/linearize-hashes.py | 4 +- .../message-capture/message-capture-parser.py | 2 +- contrib/seeds/generate-seeds.py | 8 ++-- contrib/seeds/makeseeds.py | 2 +- contrib/verify-binaries/verify.py | 6 +-- contrib/verify-commits/verify-commits.py | 12 +++--- test/functional/combine_logs.py | 2 +- test/functional/feature_asmap.py | 2 +- test/functional/feature_config_args.py | 42 +++++++++---------- test/functional/feature_includeconf.py | 14 +++---- test/functional/feature_loadblock.py | 4 +- test/functional/feature_notifications.py | 4 +- test/functional/feature_startupnotify.py | 2 +- test/functional/feature_taproot.py | 2 +- .../functional/feature_versionbits_warning.py | 4 +- test/functional/mining_mainnet.py | 2 +- test/functional/mocks/invalid_signer.py | 2 +- test/functional/mocks/signer.py | 4 +- test/functional/rpc_createmultisig.py | 2 +- test/functional/rpc_decodescript.py | 2 +- test/functional/rpc_getblockstats.py | 4 +- test/functional/rpc_help.py | 4 +- test/functional/rpc_psbt.py | 2 +- test/functional/rpc_signer.py | 2 +- test/functional/rpc_users.py | 4 +- test/functional/rpc_whitelist.py | 4 +- test/functional/test_framework/coverage.py | 4 +- .../test_framework/crypto/ellswift.py | 4 +- test/functional/test_framework/key.py | 2 +- test/functional/test_framework/netutil.py | 2 +- .../test_framework/test_framework.py | 2 +- test/functional/test_framework/test_node.py | 4 +- test/functional/test_framework/util.py | 8 ++-- test/functional/test_runner.py | 8 ++-- test/functional/tool_utils.py | 6 +-- test/functional/tool_wallet.py | 4 +- test/functional/wallet_backup.py | 2 +- .../wallet_backwards_compatibility.py | 4 +- test/functional/wallet_encryption.py | 6 +-- test/functional/wallet_multiwallet.py | 2 +- test/functional/wallet_signer.py | 6 +-- test/fuzz/test_runner.py | 2 +- test/lint/lint-files.py | 2 +- test/lint/lint-include-guards.py | 2 +- test/lint/lint-includes.py | 16 +++---- test/lint/lint-locale-dependence.py | 2 +- test/lint/lint-shell-locale.py | 2 +- test/lint/lint-tests.py | 2 +- 54 files changed, 127 insertions(+), 128 deletions(-) diff --git a/ci/test/02_run_container.py b/ci/test/02_run_container.py index 4d0bed2ad41..311fe923954 100755 --- a/ci/test/02_run_container.py +++ b/ci/test/02_run_container.py @@ -26,7 +26,6 @@ def main(): ["bash", "-c", "grep export ./ci/test/00_setup_env*.sh"], stdout=subprocess.PIPE, text=True, - encoding="utf8", ).stdout.splitlines() settings = set(l.split("=")[0].split("export ")[1] for l in settings) # Add "hidden" settings, which are never exported, manually. Otherwise, @@ -42,7 +41,7 @@ def main(): u=os.environ["USER"], c=os.environ["CONTAINER_NAME"], ) - with open(env_file, "w", encoding="utf8") as file: + with open(env_file, "w") as file: for k, v in os.environ.items(): if k in settings: file.write(f"{k}={v}\n") diff --git a/contrib/devtools/circular-dependencies.py b/contrib/devtools/circular-dependencies.py index b742a8cea67..09362c9f41e 100755 --- a/contrib/devtools/circular-dependencies.py +++ b/contrib/devtools/circular-dependencies.py @@ -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: diff --git a/contrib/devtools/clang-format-diff.py b/contrib/devtools/clang-format-diff.py index 30e804dbe27..f3515631738 100755 --- a/contrib/devtools/clang-format-diff.py +++ b/contrib/devtools/clang-format-diff.py @@ -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( diff --git a/contrib/devtools/copyright_header.py b/contrib/devtools/copyright_header.py index 12d7276455c..39b67ec4929 100755 --- a/contrib/devtools/copyright_header.py +++ b/contrib/devtools/copyright_header.py @@ -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)) ################################################################################ diff --git a/contrib/filter-lcov.py b/contrib/filter-lcov.py index db780ad53bd..373dfe3ee67 100755 --- a/contrib/filter-lcov.py +++ b/contrib/filter-lcov.py @@ -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: diff --git a/contrib/linearize/linearize-data.py b/contrib/linearize/linearize-data.py index 74d98307056..1613f5cae23 100755 --- a/contrib/linearize/linearize-data.py +++ b/contrib/linearize/linearize-data.py @@ -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) diff --git a/contrib/linearize/linearize-hashes.py b/contrib/linearize/linearize-hashes.py index 695bafad342..ddd9a7ba23b 100755 --- a/contrib/linearize/linearize-hashes.py +++ b/contrib/linearize/linearize-hashes.py @@ -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) diff --git a/contrib/message-capture/message-capture-parser.py b/contrib/message-capture/message-capture-parser.py index 0f409717d4c..19ab8d02c7a 100755 --- a/contrib/message-capture/message-capture-parser.py +++ b/contrib/message-capture/message-capture-parser.py @@ -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) diff --git a/contrib/seeds/generate-seeds.py b/contrib/seeds/generate-seeds.py index a3ce0122519..41261091c4d 100755 --- a/contrib/seeds/generate-seeds.py +++ b/contrib/seeds/generate-seeds.py @@ -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') diff --git a/contrib/seeds/makeseeds.py b/contrib/seeds/makeseeds.py index 0e5826ef875..1cb9603998a 100755 --- a/contrib/seeds/makeseeds.py +++ b/contrib/seeds/makeseeds.py @@ -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) diff --git a/contrib/verify-binaries/verify.py b/contrib/verify-binaries/verify.py index 6c07b36c9d8..6f5a933ffff 100755 --- a/contrib/verify-binaries/verify.py +++ b/contrib/verify-binaries/verify.py @@ -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: " " - 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)] diff --git a/contrib/verify-commits/verify-commits.py b/contrib/verify-commits/verify-commits.py index a1fe78a6436..808f26ed185 100755 --- a/contrib/verify-commits/verify-commits.py +++ b/contrib/verify-commits/verify-commits.py @@ -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 diff --git a/test/functional/combine_logs.py b/test/functional/combine_logs.py index 998cb208310..c4e4673228b 100755 --- a/test/functional/combine_logs.py +++ b/test/functional/combine_logs.py @@ -148,7 +148,7 @@ def get_log_events(source, logfile): Log events may be split over multiple lines. We use the timestamp regex match as the marker for a new log event.""" try: - with open(logfile, 'r', encoding='utf-8') as infile: + with open(logfile, 'r') as infile: event = '' timestamp = '' for line in infile: diff --git a/test/functional/feature_asmap.py b/test/functional/feature_asmap.py index eef599f1456..139725bbba4 100755 --- a/test/functional/feature_asmap.py +++ b/test/functional/feature_asmap.py @@ -97,7 +97,7 @@ class AsmapTest(BitcoinTestFramework): self.log.info('Test bitcoind -asmap with empty map file') self.stop_node(0) empty_asmap = os.path.join(self.datadir, "ip_asn.map") - with open(empty_asmap, "w", encoding="utf-8") as f: + with open(empty_asmap, "w") as f: f.write("") msg = f"Error: Could not parse asmap file \"{empty_asmap}\"" self.node.assert_start_raises_init_error(extra_args=[f'-asmap={empty_asmap}'], expected_msg=msg) diff --git a/test/functional/feature_config_args.py b/test/functional/feature_config_args.py index 441c21f03a3..14a5115ae77 100755 --- a/test/functional/feature_config_args.py +++ b/test/functional/feature_config_args.py @@ -55,7 +55,7 @@ class ConfArgsTest(BitcoinTestFramework): os.rename(conf_path.with_suffix('.confbkp'), conf_path) self.log.debug('Verifying includeconf directive pointing to directory is caught') - with open(conf_path, 'a', encoding='utf-8') as conf: + with open(conf_path, 'a') as conf: conf.write(f'includeconf={self.nodes[0].datadir_path}\n') self.nodes[0].assert_start_raises_init_error( extra_args=['-regtest'], @@ -68,12 +68,12 @@ class ConfArgsTest(BitcoinTestFramework): self.log.info('Disabling configuration via -noconf') conf_path = self.nodes[0].datadir_path / 'bitcoin.conf' - with open(conf_path, encoding='utf-8') as conf: + with open(conf_path) as conf: settings = [f'-{line.rstrip()}' for line in conf if len(line) > 1 and line[0] != '['] os.rename(conf_path, conf_path.with_suffix('.confbkp')) self.log.debug('Verifying garbage in config can be detected') - with open(conf_path, 'a', encoding='utf-8') as conf: + with open(conf_path, 'a') as conf: conf.write('garbage\n') self.nodes[0].assert_start_raises_init_error( extra_args=['-regtest'], @@ -107,9 +107,9 @@ class ConfArgsTest(BitcoinTestFramework): expected_msg=conf_in_config_file_err, ) inc_conf_file_path = self.nodes[0].datadir_path / 'include.conf' - with open(self.nodes[0].datadir_path / 'bitcoin.conf', 'a', encoding='utf-8') as conf: + with open(self.nodes[0].datadir_path / 'bitcoin.conf', 'a') as conf: conf.write(f'includeconf={inc_conf_file_path}\n') - with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: + with open(inc_conf_file_path, 'w') as conf: conf.write('conf=some.conf\n') self.nodes[0].assert_start_raises_init_error( expected_msg=conf_in_config_file_err, @@ -119,65 +119,65 @@ class ConfArgsTest(BitcoinTestFramework): expected_msg='Error: Error parsing command line arguments: Invalid parameter -dash_cli=1', extra_args=['-dash_cli=1'], ) - with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: + with open(inc_conf_file_path, 'w') as conf: conf.write('dash_conf=1\n') with self.nodes[0].assert_debug_log(expected_msgs=['Ignoring unknown configuration value dash_conf']): self.start_node(0) self.stop_node(0) - with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: + with open(inc_conf_file_path, 'w') as conf: conf.write('reindex=1\n') with self.nodes[0].assert_debug_log(expected_msgs=['Warning: reindex=1 is set in the configuration file, which will significantly slow down startup. Consider removing or commenting out this option for better performance, unless there is currently a condition which makes rebuilding the indexes necessary']): self.start_node(0) self.stop_node(0) - with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: + with open(inc_conf_file_path, 'w') as conf: conf.write('-dash=1\n') self.nodes[0].assert_start_raises_init_error(expected_msg='Error: Error reading configuration file: parse error on line 1: -dash=1, options in configuration file must be specified without leading -') if self.is_wallet_compiled(): - with open(inc_conf_file_path, 'w', encoding='utf8') as conf: + with open(inc_conf_file_path, 'w') as conf: conf.write("wallet=foo\n") self.nodes[0].assert_start_raises_init_error(expected_msg=f'Error: Config setting for -wallet only applied on {self.chain} network when in [{self.chain}] section.') main_conf_file_path = self.nodes[0].datadir_path / "bitcoin_main.conf" util.write_config(main_conf_file_path, n=0, chain='', extra_config=f'includeconf={inc_conf_file_path}\n') - with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: + with open(inc_conf_file_path, 'w') as conf: conf.write('acceptnonstdtxn=1\n') self.nodes[0].assert_start_raises_init_error(extra_args=[f"-conf={main_conf_file_path}", "-allowignoredconf"], expected_msg='Error: acceptnonstdtxn is not currently supported for main chain') - with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: + with open(inc_conf_file_path, 'w') as conf: conf.write('nono\n') self.nodes[0].assert_start_raises_init_error(expected_msg='Error: Error reading configuration file: parse error on line 1: nono, if you intended to specify a negated option, use nono=1 instead') - with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: + with open(inc_conf_file_path, 'w') as conf: conf.write('server=1\nrpcuser=someuser\nrpcpassword=some#pass') self.nodes[0].assert_start_raises_init_error(expected_msg='Error: Error reading configuration file: parse error on line 3, using # in rpcpassword can be ambiguous and should be avoided') - with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: + with open(inc_conf_file_path, 'w') as conf: conf.write('server=1\nrpcuser=someuser\nmain.rpcpassword=some#pass') self.nodes[0].assert_start_raises_init_error(expected_msg='Error: Error reading configuration file: parse error on line 3, using # in rpcpassword can be ambiguous and should be avoided') - with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: + with open(inc_conf_file_path, 'w') as conf: conf.write('server=1\nrpcuser=someuser\n[main]\nrpcpassword=some#pass') self.nodes[0].assert_start_raises_init_error(expected_msg='Error: Error reading configuration file: parse error on line 4, using # in rpcpassword can be ambiguous and should be avoided') inc_conf_file2_path = self.nodes[0].datadir_path / 'include2.conf' - with open(self.nodes[0].datadir_path / 'bitcoin.conf', 'a', encoding='utf-8') as conf: + with open(self.nodes[0].datadir_path / 'bitcoin.conf', 'a') as conf: conf.write(f'includeconf={inc_conf_file2_path}\n') - with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: + with open(inc_conf_file_path, 'w') as conf: conf.write('testnot.datadir=1\n') - with open(inc_conf_file2_path, 'w', encoding='utf-8') as conf: + with open(inc_conf_file2_path, 'w') as conf: conf.write('[testnet]\n') self.restart_node(0) self.nodes[0].stop_node(expected_stderr=f'Warning: {inc_conf_file_path}:1 Section [testnot] is not recognized.{os.linesep}{inc_conf_file2_path}:1 Section [testnet] is not recognized.') - with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: + with open(inc_conf_file_path, 'w') as conf: conf.write('') # clear - with open(inc_conf_file2_path, 'w', encoding='utf-8') as conf: + with open(inc_conf_file2_path, 'w') as conf: conf.write('') # clear def test_config_file_log(self): @@ -522,9 +522,9 @@ class ConfArgsTest(BitcoinTestFramework): conf_file = default_data_dir / "bitcoin.conf" # datadir needs to be set before [chain] section - with open(conf_file, encoding='utf8') as f: + with open(conf_file) as f: conf_file_contents = f.read() - with open(conf_file, 'w', encoding='utf8') as f: + with open(conf_file, 'w') as f: f.write(f"datadir={new_data_dir}\n") f.write(conf_file_contents) diff --git a/test/functional/feature_includeconf.py b/test/functional/feature_includeconf.py index ee484e7ec50..a416b2293dd 100755 --- a/test/functional/feature_includeconf.py +++ b/test/functional/feature_includeconf.py @@ -24,12 +24,12 @@ class IncludeConfTest(BitcoinTestFramework): def run_test(self): # Create additional config files # - tmpdir/node0/relative.conf - with open(self.nodes[0].datadir_path / "relative.conf", "w", encoding="utf8") as f: + with open(self.nodes[0].datadir_path / "relative.conf", "w") as f: f.write("uacomment=relative\n") # - tmpdir/node0/relative2.conf - with open(self.nodes[0].datadir_path / "relative2.conf", "w", encoding="utf8") as f: + with open(self.nodes[0].datadir_path / "relative2.conf", "w") as f: f.write("uacomment=relative2\n") - with open(self.nodes[0].datadir_path / "bitcoin.conf", "a", encoding="utf8") as f: + with open(self.nodes[0].datadir_path / "bitcoin.conf", "a") as f: f.write("uacomment=main\nincludeconf=relative.conf\n") self.restart_node(0) @@ -50,7 +50,7 @@ class IncludeConfTest(BitcoinTestFramework): ) self.log.info("-includeconf cannot be used recursively. subversion should end with 'main; relative)/'") - with open(self.nodes[0].datadir_path / "relative.conf", "a", encoding="utf8") as f: + with open(self.nodes[0].datadir_path / "relative.conf", "a") as f: f.write("includeconf=relative2.conf\n") self.start_node(0) @@ -61,7 +61,7 @@ class IncludeConfTest(BitcoinTestFramework): self.log.info("-includeconf cannot contain invalid arg") # Commented out as long as we ignore invalid arguments in configuration files - #with open(self.nodes[0].datadir_path / "relative.conf", "w", encoding="utf8") as f: + #with open(self.nodes[0].datadir_path / "relative.conf", "w") as f: # f.write("foo=bar\n") #self.nodes[0].assert_start_raises_init_error(expected_msg="Error: Error reading configuration file: Invalid configuration value foo") @@ -70,11 +70,11 @@ class IncludeConfTest(BitcoinTestFramework): self.nodes[0].assert_start_raises_init_error(expected_msg="Error: Error reading configuration file: Failed to include configuration file relative.conf") self.log.info("multiple -includeconf args can be used from the base config file. subversion should end with 'main; relative; relative2)/'") - with open(self.nodes[0].datadir_path / "relative.conf", "w", encoding="utf8") as f: + with open(self.nodes[0].datadir_path / "relative.conf", "w") as f: # Restore initial file contents f.write("uacomment=relative\n") - with open(self.nodes[0].datadir_path / "bitcoin.conf", "a", encoding="utf8") as f: + with open(self.nodes[0].datadir_path / "bitcoin.conf", "a") as f: f.write("includeconf=relative2.conf\n") self.start_node(0) diff --git a/test/functional/feature_loadblock.py b/test/functional/feature_loadblock.py index 0e6ec14c773..2bf700d16c5 100755 --- a/test/functional/feature_loadblock.py +++ b/test/functional/feature_loadblock.py @@ -41,10 +41,10 @@ class LoadblockTest(BitcoinTestFramework): hash_list = tempfile.NamedTemporaryFile(dir=data_dir, mode='w', delete=False, - encoding="utf-8") + ) self.log.info("Create linearization config file") - with open(cfg_file, "a", encoding="utf-8") as cfg: + with open(cfg_file, "a") as cfg: cfg.write(f"datadir={data_dir}\n") cfg.write(f"rpcuser={node_url.username}\n") cfg.write(f"rpcpassword={node_url.password}\n") diff --git a/test/functional/feature_notifications.py b/test/functional/feature_notifications.py index 519d80b9abf..12bc6b1804e 100755 --- a/test/functional/feature_notifications.py +++ b/test/functional/feature_notifications.py @@ -200,7 +200,7 @@ class NotificationsTest(BitcoinTestFramework): self.wait_until(lambda: os.path.isfile(self.shutdownnotify_file), timeout=10) def large_work_invalid_chain_warning_in_alert_file(self): - with open(self.alertnotify_file, 'r', encoding='utf8') as f: + with open(self.alertnotify_file, 'r') as f: alert_text = f.read() return LARGE_WORK_INVALID_CHAIN_WARNING in alert_text @@ -213,7 +213,7 @@ class NotificationsTest(BitcoinTestFramework): fname = os.path.join(self.walletnotify_dir, notify_outputname(self.wallet, tx_id)) # Wait for the cached writes to hit storage self.wait_until(lambda: os.path.getsize(fname) > 0, timeout=10) - with open(fname, 'rt', encoding='utf-8') as f: + with open(fname, 'rt') as f: text = f.read() # Universal newline ensures '\n' on 'nt' assert_equal(text[-1], '\n') diff --git a/test/functional/feature_startupnotify.py b/test/functional/feature_startupnotify.py index 1e071037254..d9c2f864c7d 100755 --- a/test/functional/feature_startupnotify.py +++ b/test/functional/feature_startupnotify.py @@ -27,7 +27,7 @@ class StartupNotifyTest(BitcoinTestFramework): self.log.info("Test -startupnotify is executed once") def get_count(): - with open(tmpdir_file, "r", encoding="utf8") as f: + with open(tmpdir_file, "r") as f: file_content = f.read() return file_content.count(FILE_NAME) diff --git a/test/functional/feature_taproot.py b/test/functional/feature_taproot.py index 24cb26a3440..d3c1ac03791 100755 --- a/test/functional/feature_taproot.py +++ b/test/functional/feature_taproot.py @@ -1396,7 +1396,7 @@ def dump_json_test(tx, input_utxos, idx, success, failure): sha1 = hashlib.sha1(dump.encode("utf-8")).hexdigest() dirname = os.environ.get("TEST_DUMP_DIR", ".") + ("/%s" % sha1[0]) os.makedirs(dirname, exist_ok=True) - with open(dirname + ("/%s" % sha1), 'w', encoding="utf8") as f: + with open(dirname + ("/%s" % sha1), 'w') as f: f.write(dump) # Data type to keep track of UTXOs, where they were created, and how to spend them. diff --git a/test/functional/feature_versionbits_warning.py b/test/functional/feature_versionbits_warning.py index 96ec7c570fa..bc4b67915b7 100755 --- a/test/functional/feature_versionbits_warning.py +++ b/test/functional/feature_versionbits_warning.py @@ -32,7 +32,7 @@ class VersionBitsWarningTest(BitcoinTestFramework): 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', encoding='utf8'): + with open(self.alert_filename, 'w'): pass self.extra_args = [[f"-alertnotify=echo %s >> \"{self.alert_filename}\""]] self.setup_nodes() @@ -55,7 +55,7 @@ class VersionBitsWarningTest(BitcoinTestFramework): def versionbits_in_alert_file(self): """Test that the versionbits warning has been written to the alert file.""" - with open(self.alert_filename, 'r', encoding='utf8') as f: + with open(self.alert_filename, 'r') as f: alert_text = f.read() return VB_PATTERN.search(alert_text) is not None diff --git a/test/functional/mining_mainnet.py b/test/functional/mining_mainnet.py index 672191006d2..122ea481735 100755 --- a/test/functional/mining_mainnet.py +++ b/test/functional/mining_mainnet.py @@ -83,7 +83,7 @@ class MiningMainnetTest(BitcoinTestFramework): path = os.path.join(os.path.dirname(os.path.realpath(__file__)), self.options.datafile) prev_hash = node.getbestblockhash() blocks = None - with open(path, encoding='utf-8') as f: + with open(path) as f: blocks = json.load(f) n_blocks = len(blocks['timestamps']) assert_equal(n_blocks, 2016) diff --git a/test/functional/mocks/invalid_signer.py b/test/functional/mocks/invalid_signer.py index 7bfa9051acc..b9a5c2a108e 100755 --- a/test/functional/mocks/invalid_signer.py +++ b/test/functional/mocks/invalid_signer.py @@ -11,7 +11,7 @@ import json def perform_pre_checks(): mock_result_path = os.path.join(os.getcwd(), "mock_result") if os.path.isfile(mock_result_path): - with open(mock_result_path, "r", encoding="utf8") as f: + with open(mock_result_path, "r") as f: mock_result = f.read() if mock_result[0]: sys.stdout.write(mock_result[2:]) diff --git a/test/functional/mocks/signer.py b/test/functional/mocks/signer.py index 23d163aac33..da5d9d9f752 100755 --- a/test/functional/mocks/signer.py +++ b/test/functional/mocks/signer.py @@ -11,7 +11,7 @@ import json def perform_pre_checks(): mock_result_path = os.path.join(os.getcwd(), "mock_result") if os.path.isfile(mock_result_path): - with open(mock_result_path, "r", encoding="utf8") as f: + with open(mock_result_path, "r") as f: mock_result = f.read() if mock_result[0]: sys.stdout.write(mock_result[2:]) @@ -60,7 +60,7 @@ def signtx(args): if args.fingerprint != "00000001": return sys.stdout.write(json.dumps({"error": "Unexpected fingerprint", "fingerprint": args.fingerprint})) - with open(os.path.join(os.getcwd(), "mock_psbt"), "r", encoding="utf8") as f: + with open(os.path.join(os.getcwd(), "mock_psbt"), "r") as f: mock_psbt = f.read() if args.fingerprint == "00000001" : diff --git a/test/functional/rpc_createmultisig.py b/test/functional/rpc_createmultisig.py index 1675aa0c527..5ae6c39e251 100755 --- a/test/functional/rpc_createmultisig.py +++ b/test/functional/rpc_createmultisig.py @@ -184,7 +184,7 @@ class RpcCreateMultiSigTest(BitcoinTestFramework): def test_sortedmulti_descriptors_bip67(self): self.log.info('Testing sortedmulti descriptors with BIP 67 test vectors') - with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/rpc_bip67.json'), encoding='utf-8') as f: + with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/rpc_bip67.json')) as f: vectors = json.load(f) for t in vectors: diff --git a/test/functional/rpc_decodescript.py b/test/functional/rpc_decodescript.py index 4f127fa8b7f..40a53ce24de 100755 --- a/test/functional/rpc_decodescript.py +++ b/test/functional/rpc_decodescript.py @@ -266,7 +266,7 @@ class DecodeScriptTest(BitcoinTestFramework): assert_equal('OP_RETURN 3011020701010101010101020601010101010101', rpc_result['vin'][0]['scriptSig']['asm']) def decodescript_datadriven_tests(self): - with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/rpc_decodescript.json'), encoding='utf-8') as f: + with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/rpc_decodescript.json')) as f: dd_tests = json.load(f) for script, result in dd_tests: diff --git a/test/functional/rpc_getblockstats.py b/test/functional/rpc_getblockstats.py index 7def4f8fa50..fee23d20b93 100755 --- a/test/functional/rpc_getblockstats.py +++ b/test/functional/rpc_getblockstats.py @@ -78,11 +78,11 @@ class GetblockstatsTest(BitcoinTestFramework): 'mocktime': int(mocktime), 'stats': self.expected_stats, } - with open(filename, 'w', encoding="utf8") as f: + with open(filename, 'w') as f: json.dump(to_dump, f, sort_keys=True, indent=2) def load_test_data(self, filename): - with open(filename, 'r', encoding="utf8") as f: + with open(filename, 'r') as f: d = json.load(f) blocks = d['blocks'] mocktime = d['mocktime'] diff --git a/test/functional/rpc_help.py b/test/functional/rpc_help.py index 74d34ddca1a..92ee5d15966 100755 --- a/test/functional/rpc_help.py +++ b/test/functional/rpc_help.py @@ -22,7 +22,7 @@ def process_mapping(fname): cmds = [] string_params = [] in_rpcs = False - with open(fname, "r", encoding="utf8") as f: + with open(fname, "r") as f: for line in f: line = line.rstrip() if not in_rpcs: @@ -153,7 +153,7 @@ class HelpRpcTest(BitcoinTestFramework): os.mkdir(dump_dir) calls = [line.split(' ', 1)[0] for line in self.nodes[0].help().splitlines() if line and not line.startswith('==')] for call in calls: - with open(os.path.join(dump_dir, call), 'w', encoding='utf-8') as f: + with open(os.path.join(dump_dir, call), 'w') as f: # Make sure the node can generate the help at runtime without crashing f.write(self.nodes[0].help(call)) diff --git a/test/functional/rpc_psbt.py b/test/functional/rpc_psbt.py index 3943abcec40..1cd2a1da772 100755 --- a/test/functional/rpc_psbt.py +++ b/test/functional/rpc_psbt.py @@ -799,7 +799,7 @@ class PSBTTest(BitcoinTestFramework): assert_equal(unknown_psbt, unknown_out) # Open the data file - with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/rpc_psbt.json'), encoding='utf-8') as f: + with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/rpc_psbt.json')) as f: d = json.load(f) invalids = d['invalid'] invalid_with_msgs = d["invalid_with_msg"] diff --git a/test/functional/rpc_signer.py b/test/functional/rpc_signer.py index 715520b1507..0cfc2324429 100755 --- a/test/functional/rpc_signer.py +++ b/test/functional/rpc_signer.py @@ -37,7 +37,7 @@ class RPCSignerTest(BitcoinTestFramework): self.skip_if_no_external_signer() def set_mock_result(self, node, res): - with open(os.path.join(node.cwd, "mock_result"), "w", encoding="utf8") as f: + with open(os.path.join(node.cwd, "mock_result"), "w") as f: f.write(res) def clear_mock_result(self, node): diff --git a/test/functional/rpc_users.py b/test/functional/rpc_users.py index 9e475bbe853..a12adc80e34 100755 --- a/test/functional/rpc_users.py +++ b/test/functional/rpc_users.py @@ -61,11 +61,11 @@ class HTTPBasicsTest(BitcoinTestFramework): rpcauth3 = lines[1] self.password = lines[3] - with open(self.nodes[0].datadir_path / "bitcoin.conf", "a", encoding="utf8") as f: + with open(self.nodes[0].datadir_path / "bitcoin.conf", "a") as f: f.write(rpcauth + "\n") f.write(rpcauth2 + "\n") f.write(rpcauth3 + "\n") - with open(self.nodes[1].datadir_path / "bitcoin.conf", "a", encoding="utf8") as f: + with open(self.nodes[1].datadir_path / "bitcoin.conf", "a") as f: f.write("rpcuser={}\n".format(self.rpcuser)) f.write("rpcpassword={}\n".format(self.rpcpassword)) self.restart_node(0) diff --git a/test/functional/rpc_whitelist.py b/test/functional/rpc_whitelist.py index b103289fab3..5acae0cbcbc 100755 --- a/test/functional/rpc_whitelist.py +++ b/test/functional/rpc_whitelist.py @@ -61,7 +61,7 @@ class RPCWhitelistTest(BitcoinTestFramework): ] # These commands shouldn't be allowed for any user to test failures self.never_allowed = ["getnetworkinfo"] - with open(self.nodes[0].datadir_path / "bitcoin.conf", "a", encoding="utf8") as f: + with open(self.nodes[0].datadir_path / "bitcoin.conf", "a") as f: f.write("\nrpcwhitelistdefault=0\n") for user in self.users: f.write("rpcauth=" + user[0] + ":" + user[1] + "\n") @@ -96,7 +96,7 @@ class RPCWhitelistTest(BitcoinTestFramework): # Replace file configurations self.nodes[0].replace_in_config([("rpcwhitelistdefault=0", "rpcwhitelistdefault=1")]) - with open(self.nodes[0].datadir_path / "bitcoin.conf", 'a', encoding='utf8') as f: + with open(self.nodes[0].datadir_path / "bitcoin.conf", 'a') as f: f.write("rpcwhitelist=__cookie__:getblockcount,getblockchaininfo,getmempoolinfo,stop\n") self.restart_node(0) diff --git a/test/functional/test_framework/coverage.py b/test/functional/test_framework/coverage.py index 912a945d958..29e918d426d 100644 --- a/test/functional/test_framework/coverage.py +++ b/test/functional/test_framework/coverage.py @@ -55,7 +55,7 @@ class AuthServiceProxyWrapper(): rpc_method = self.auth_service_proxy_instance._service_name if self.coverage_logfile: - with open(self.coverage_logfile, 'a+', encoding='utf8') as f: + with open(self.coverage_logfile, 'a+') as f: f.write("%s\n" % rpc_method) def __truediv__(self, relative_uri): @@ -107,7 +107,7 @@ def write_all_rpc_commands(dirname: str, node: AuthServiceProxy) -> bool: if line and not line.startswith('='): commands.add("%s\n" % line.split()[0]) - with open(filename, 'w', encoding='utf8') as f: + with open(filename, 'w') as f: f.writelines(list(commands)) return True diff --git a/test/functional/test_framework/crypto/ellswift.py b/test/functional/test_framework/crypto/ellswift.py index 429b7b9f4d3..4880ef5b3b6 100644 --- a/test/functional/test_framework/crypto/ellswift.py +++ b/test/functional/test_framework/crypto/ellswift.py @@ -134,7 +134,7 @@ class TestFrameworkEllSwift(unittest.TestCase): def test_elligator_encode_testvectors(self): """Implement the BIP324 test vectors for ellswift encoding (read from xswiftec_inv_test_vectors.csv).""" vectors_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'xswiftec_inv_test_vectors.csv') - with open(vectors_file, newline='', encoding='utf8') as csvfile: + with open(vectors_file, newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: u = FE.from_bytes(bytes.fromhex(row['u'])) @@ -150,7 +150,7 @@ class TestFrameworkEllSwift(unittest.TestCase): def test_elligator_decode_testvectors(self): """Implement the BIP324 test vectors for ellswift decoding (read from ellswift_decode_test_vectors.csv).""" vectors_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ellswift_decode_test_vectors.csv') - with open(vectors_file, newline='', encoding='utf8') as csvfile: + with open(vectors_file, newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: encoding = bytes.fromhex(row['ellswift']) diff --git a/test/functional/test_framework/key.py b/test/functional/test_framework/key.py index 558dcbf23ed..6caafe8bcdb 100644 --- a/test/functional/test_framework/key.py +++ b/test/functional/test_framework/key.py @@ -316,7 +316,7 @@ class TestFrameworkKey(unittest.TestCase): """Implement the BIP340 test vectors (read from bip340_test_vectors.csv).""" num_tests = 0 vectors_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'bip340_test_vectors.csv') - with open(vectors_file, newline='', encoding='utf8') as csvfile: + with open(vectors_file, newline='') as csvfile: reader = csv.reader(csvfile) next(reader) for row in reader: diff --git a/test/functional/test_framework/netutil.py b/test/functional/test_framework/netutil.py index f79a8c361a8..6cb34eb8156 100644 --- a/test/functional/test_framework/netutil.py +++ b/test/functional/test_framework/netutil.py @@ -69,7 +69,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',encoding='utf8') as f: + with open('/proc/net/'+typ,'r') as f: content = f.readlines() content.pop(0) result = [] diff --git a/test/functional/test_framework/test_framework.py b/test/functional/test_framework/test_framework.py index 503120213a6..a74ab5aed92 100755 --- a/test/functional/test_framework/test_framework.py +++ b/test/functional/test_framework/test_framework.py @@ -751,7 +751,7 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass): self.log = logging.getLogger('TestFramework') self.log.setLevel(logging.DEBUG) # Create file handler to log all messages - fh = logging.FileHandler(self.options.tmpdir + '/test_framework.log', encoding='utf-8') + fh = logging.FileHandler(self.options.tmpdir + '/test_framework.log') fh.setLevel(logging.DEBUG) # Create console handler to log messages to stderr. By default this logs only error messages, but can be configured with --loglevel. ch = logging.StreamHandler(sys.stdout) diff --git a/test/functional/test_framework/test_node.py b/test/functional/test_framework/test_node.py index 4186cec5b75..060a932d1c8 100755 --- a/test/functional/test_framework/test_node.py +++ b/test/functional/test_framework/test_node.py @@ -506,13 +506,13 @@ class TestNode(): The substitutions are passed as a list of search-replace-tuples, e.g. [("old", "new"), ("foo", "bar"), ...] """ - with open(self.bitcoinconf, 'r', encoding='utf8') as conf: + with open(self.bitcoinconf, 'r') as conf: conf_data = conf.read() for replacement in replacements: assert_equal(len(replacement), 2) old, new = replacement[0], replacement[1] conf_data = conf_data.replace(old, new) - with open(self.bitcoinconf, 'w', encoding='utf8') as conf: + with open(self.bitcoinconf, 'w') as conf: conf.write(conf_data) @property diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py index bbad6ea7710..f08b36d5e9a 100644 --- a/test/functional/test_framework/util.py +++ b/test/functional/test_framework/util.py @@ -528,7 +528,7 @@ def write_config(config_path, *, n, chain, extra_config="", disable_autoconnect= else: chain_name_conf_arg = chain chain_name_conf_section = chain - with open(config_path, 'w', encoding='utf8') as f: + with open(config_path, 'w') as f: if chain_name_conf_arg: f.write("{}=1\n".format(chain_name_conf_arg)) if chain_name_conf_section: @@ -594,7 +594,7 @@ def get_temp_default_datadir(temp_dir: pathlib.Path) -> tuple[dict, pathlib.Path def append_config(datadir, options): - with open(os.path.join(datadir, "bitcoin.conf"), 'a', encoding='utf8') as f: + with open(os.path.join(datadir, "bitcoin.conf"), 'a') as f: for option in options: f.write(option + "\n") @@ -603,7 +603,7 @@ def get_auth_cookie(datadir, chain): user = None password = None if os.path.isfile(os.path.join(datadir, "bitcoin.conf")): - with open(os.path.join(datadir, "bitcoin.conf"), 'r', encoding='utf8') as f: + with open(os.path.join(datadir, "bitcoin.conf"), 'r') as f: for line in f: if line.startswith("rpcuser="): assert user is None # Ensure that there is only one rpcuser line @@ -612,7 +612,7 @@ def get_auth_cookie(datadir, chain): assert password is None # Ensure that there is only one rpcpassword line password = line.split("=")[1].strip("\n") try: - with open(os.path.join(datadir, chain, ".cookie"), 'r', encoding="ascii") as f: + with open(os.path.join(datadir, chain, ".cookie"), 'r') as f: userpass = f.read() split_userpass = userpass.split(':') user = split_userpass[0] diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index 8c26ab952ac..d63dbadefd3 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -428,7 +428,7 @@ def main(): # Read config generated by configure. config = configparser.ConfigParser() configfile = os.path.abspath(os.path.dirname(__file__)) + "/../config.ini" - config.read_file(open(configfile, encoding="utf8")) + config.read_file(open(configfile)) passon_args.append("--configfile=%s" % configfile) @@ -699,7 +699,7 @@ def print_results(test_results, max_len_name, runtime): def write_results(test_results, filepath, total_runtime): - with open(filepath, mode="w", encoding="utf8") as results_file: + with open(filepath, mode="w") as results_file: results_writer = csv.writer(results_file) results_writer.writerow(['test', 'status', 'duration(seconds)']) all_passed = True @@ -911,7 +911,7 @@ class RPCCoverage(): if not os.path.isfile(coverage_ref_filename): raise RuntimeError("No coverage reference found") - with open(coverage_ref_filename, 'r', encoding="utf8") as coverage_ref_file: + with open(coverage_ref_filename, 'r') as coverage_ref_file: all_cmds.update([line.strip() for line in coverage_ref_file.readlines()]) for root, _, files in os.walk(self.dir): @@ -920,7 +920,7 @@ class RPCCoverage(): coverage_filenames.add(os.path.join(root, filename)) for filename in coverage_filenames: - with open(filename, 'r', encoding="utf8") as coverage_file: + with open(filename, 'r') as coverage_file: covered_cmds.update([line.strip() for line in coverage_file.readlines()]) return all_cmds - covered_cmds diff --git a/test/functional/tool_utils.py b/test/functional/tool_utils.py index 6dfe27c3466..525285deb0c 100755 --- a/test/functional/tool_utils.py +++ b/test/functional/tool_utils.py @@ -28,7 +28,7 @@ class ToolUtils(BitcoinTestFramework): def run_test(self): self.testcase_dir = Path(self.config["environment"]["SRCDIR"]) / "test" / "functional" / "data" / "util" self.bins = self.get_binaries() - with open(self.testcase_dir / "bitcoin-util-test.json", encoding="utf8") as f: + with open(self.testcase_dir / "bitcoin-util-test.json") as f: input_data = json.loads(f.read()) for i, test_obj in enumerate(input_data): @@ -50,7 +50,7 @@ class ToolUtils(BitcoinTestFramework): # Read the input data (if there is any) inputData = None if "input" in testObj: - with open(self.testcase_dir / testObj["input"], encoding="utf8") as f: + with open(self.testcase_dir / testObj["input"]) as f: inputData = f.read() # Read the expected output data (if there is any) @@ -60,7 +60,7 @@ class ToolUtils(BitcoinTestFramework): if "output_cmp" in testObj: outputFn = testObj['output_cmp'] outputType = os.path.splitext(outputFn)[1][1:] # output type from file extension (determines how to compare) - with open(self.testcase_dir / outputFn, encoding="utf8") as f: + with open(self.testcase_dir / outputFn) as f: outputData = f.read() if not outputData: raise Exception(f"Output data missing for {outputFn}") diff --git a/test/functional/tool_wallet.py b/test/functional/tool_wallet.py index bbf84d7a017..d5bc4ad67b4 100755 --- a/test/functional/tool_wallet.py +++ b/test/functional/tool_wallet.py @@ -83,7 +83,7 @@ class ToolWalletTest(BitcoinTestFramework): def read_dump(self, filename): dump = OrderedDict() - with open(filename, "r", encoding="utf8") as f: + with open(filename, "r") as f: for row in f: row = row.strip() key, value = row.split(',') @@ -98,7 +98,7 @@ class ToolWalletTest(BitcoinTestFramework): def write_dump(self, dump, filename, magic=None, skip_checksum=False): if magic is None: magic = "BITCOIN_CORE_WALLET_DUMP" - with open(filename, "w", encoding="utf8") as f: + with open(filename, "w") as f: row = ",".join([magic, dump[magic]]) + "\n" f.write(row) for k, v in dump.items(): diff --git a/test/functional/wallet_backup.py b/test/functional/wallet_backup.py index 6f3637a0dda..4d08ace424c 100755 --- a/test/functional/wallet_backup.py +++ b/test/functional/wallet_backup.py @@ -112,7 +112,7 @@ class WalletBackupTest(BitcoinTestFramework): def restore_invalid_wallet(self): node = self.nodes[3] invalid_wallet_file = self.nodes[0].datadir_path / 'invalid_wallet_file.bak' - open(invalid_wallet_file, 'a', encoding="utf8").write('invald wallet') + open(invalid_wallet_file, 'a').write('invald wallet') wallet_name = "res0" not_created_wallet_file = node.wallets_path / wallet_name error_message = "Wallet file verification failed. Failed to load database path '{}'. Data is not in recognized format.".format(not_created_wallet_file) diff --git a/test/functional/wallet_backwards_compatibility.py b/test/functional/wallet_backwards_compatibility.py index 1f335398738..a67b4676a86 100755 --- a/test/functional/wallet_backwards_compatibility.py +++ b/test/functional/wallet_backwards_compatibility.py @@ -100,7 +100,7 @@ class BackwardsCompatibilityTest(BitcoinTestFramework): bad_deriv_wallet.dumpwallet(dump_path) addr = None seed = None - with open(dump_path, encoding="utf8") as f: + with open(dump_path) as f: for line in f: if f"hdkeypath=m/0'/0'/{LAST_KEYPOOL_INDEX}'" in line: addr = line.split(" ")[4].split("=")[1] @@ -123,7 +123,7 @@ class BackwardsCompatibilityTest(BitcoinTestFramework): os.unlink(dump_path) bad_deriv_wallet.dumpwallet(dump_path) bad_path_addr = None - with open(dump_path, encoding="utf8") as f: + with open(dump_path) as f: for line in f: if f"hdkeypath={bad_deriv_path}" in line: bad_path_addr = line.split(" ")[4].split("=")[1] diff --git a/test/functional/wallet_encryption.py b/test/functional/wallet_encryption.py index b54852b7c33..f8182e10c18 100755 --- a/test/functional/wallet_encryption.py +++ b/test/functional/wallet_encryption.py @@ -134,13 +134,13 @@ class WalletEncryptionTest(BitcoinTestFramework): do_wallet_tool("-wallet=noprivs", f"-dumpfile={dumpfile_path}", "dump") # Modify the dump - with open(dumpfile_path, "r", encoding="utf-8") as f: + with open(dumpfile_path, "r") as f: dump_content = f.readlines() # Drop the checksum line dump_content = dump_content[:-1] # Insert a valid mkey line. This corresponds to a passphrase of "pass". dump_content.append("046d6b657901000000,300dc926f3b3887aad3d5d5f5a0fc1b1a4a1722f9284bd5c6ff93b64a83902765953939c58fe144013c8b819f42cf698b208e9911e5f0c544fa300000000cc52050000\n") - with open(dumpfile_path, "w", encoding="utf-8") as f: + with open(dumpfile_path, "w") as f: contents = "".join(dump_content) f.write(contents) checksum = hash256(contents.encode()) @@ -158,7 +158,7 @@ class WalletEncryptionTest(BitcoinTestFramework): # Make a new dump and check that there are no mkeys dumpfile_path = self.nodes[0].datadir_path / "noprivs_enc.dump" do_wallet_tool("-wallet=noprivs_enc", f"-dumpfile={dumpfile_path}", "dump") - with open(dumpfile_path, "r", encoding="utf-8") as f: + with open(dumpfile_path, "r") as f: # Check there's nothing with an 'mkey' prefix assert_equal(all([not line.startswith("046d6b6579") for line in f]), True) diff --git a/test/functional/wallet_multiwallet.py b/test/functional/wallet_multiwallet.py index 64f57cecf66..f15c1506875 100755 --- a/test/functional/wallet_multiwallet.py +++ b/test/functional/wallet_multiwallet.py @@ -180,7 +180,7 @@ class MultiWalletTest(BitcoinTestFramework): self.nodes[0].assert_start_raises_init_error(['-walletdir=bad'], 'Error: Specified -walletdir "bad" does not exist') # should not initialize if the specified walletdir is not a directory not_a_dir = wallet_dir('notadir') - open(not_a_dir, 'a', encoding="utf8").close() + open(not_a_dir, 'a').close() self.nodes[0].assert_start_raises_init_error(['-walletdir=' + not_a_dir], 'Error: Specified -walletdir "' + not_a_dir + '" is not a directory') # if wallets/ doesn't exist, datadir should be the default wallet dir diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index c239bb61694..52780e6fb88 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -48,7 +48,7 @@ class WalletSignerTest(BitcoinTestFramework): self.skip_if_no_wallet() def set_mock_result(self, node, res): - with open(os.path.join(node.cwd, "mock_result"), "w", encoding="utf8") as f: + with open(os.path.join(node.cwd, "mock_result"), "w") as f: f.write(res) def clear_mock_result(self, node): @@ -170,7 +170,7 @@ class WalletSignerTest(BitcoinTestFramework): assert hww.testmempoolaccept([mock_tx])[0]["allowed"] - with open(os.path.join(self.nodes[1].cwd, "mock_psbt"), "w", encoding="utf8") as f: + with open(os.path.join(self.nodes[1].cwd, "mock_psbt"), "w") as f: f.write(mock_psbt_signed["psbt"]) self.log.info('Test send using hww1') @@ -195,7 +195,7 @@ class WalletSignerTest(BitcoinTestFramework): mock_psbt_bumped = mock_wallet.psbtbumpfee(orig_tx_id)["psbt"] mock_psbt_bumped_signed = mock_wallet.walletprocesspsbt(psbt=mock_psbt_bumped, sign=True, sighashtype="ALL", bip32derivs=True) - with open(os.path.join(self.nodes[1].cwd, "mock_psbt"), "w", encoding="utf8") as f: + with open(os.path.join(self.nodes[1].cwd, "mock_psbt"), "w") as f: f.write(mock_psbt_bumped_signed["psbt"]) self.log.info('Test bumpfee using hww1') diff --git a/test/fuzz/test_runner.py b/test/fuzz/test_runner.py index 5b2c847bbbb..d31d95edfba 100755 --- a/test/fuzz/test_runner.py +++ b/test/fuzz/test_runner.py @@ -99,7 +99,7 @@ def main(): # Read config generated by configure. config = configparser.ConfigParser() configfile = os.path.abspath(os.path.dirname(__file__)) + "/../config.ini" - config.read_file(open(configfile, encoding="utf8")) + config.read_file(open(configfile)) if not config["components"].getboolean("ENABLE_FUZZ_BINARY"): logging.error("Must have fuzz executable built") diff --git a/test/lint/lint-files.py b/test/lint/lint-files.py index c5af959e0a8..cddaed98db3 100755 --- a/test/lint/lint-files.py +++ b/test/lint/lint-files.py @@ -185,7 +185,7 @@ def check_shebang_file_permissions(files_meta) -> int: # *.py files which don't contain an `if __name__ == '__main__'` are not expected to be executed directly if file_meta.extension == "py": - with open(filename, "r", encoding="utf8") as f: + with open(filename, "r") as f: file_data = f.read() if not re.search("""if __name__ == ['"]__main__['"]:""", file_data): continue diff --git a/test/lint/lint-include-guards.py b/test/lint/lint-include-guards.py index e16bb94a770..e0d4eb95fd5 100755 --- a/test/lint/lint-include-guards.py +++ b/test/lint/lint-include-guards.py @@ -71,7 +71,7 @@ def main(): regex_pattern = f'^#(ifndef|define|endif //) {header_id}' - with open(header_file, 'r', encoding='utf-8') as f: + with open(header_file, 'r') as f: header_file_contents = f.readlines() count = 0 diff --git a/test/lint/lint-includes.py b/test/lint/lint-includes.py index b0c91b90114..0f108904b84 100755 --- a/test/lint/lint-includes.py +++ b/test/lint/lint-includes.py @@ -40,13 +40,13 @@ EXPECTED_BOOST_INCLUDES = [ def get_toplevel(): - return check_output(["git", "rev-parse", "--show-toplevel"], text=True, encoding="utf8").rstrip("\n") + return check_output(["git", "rev-parse", "--show-toplevel"], text=True).rstrip("\n") def list_files_by_suffix(suffixes): exclude_args = [":(exclude)" + dir for dir in EXCLUDED_DIRS] - files_list = check_output(["git", "ls-files", "src"] + exclude_args, text=True, encoding="utf8").splitlines() + files_list = check_output(["git", "ls-files", "src"] + exclude_args, text=True).splitlines() return [file for file in files_list if file.endswith(suffixes)] @@ -68,7 +68,7 @@ def find_included_cpps(): included_cpps = list() try: - included_cpps = check_output(["git", "grep", "-E", r"^#include [<\"][^>\"]+\.cpp[>\"]", "--", "*.cpp", "*.h"], text=True, encoding="utf8").splitlines() + included_cpps = check_output(["git", "grep", "-E", r"^#include [<\"][^>\"]+\.cpp[>\"]", "--", "*.cpp", "*.h"], text=True).splitlines() except CalledProcessError as e: if e.returncode > 1: raise e @@ -82,7 +82,7 @@ def find_extra_boosts(): exclusion_set = set() try: - included_boosts = check_output(["git", "grep", "-E", r"^#include 1: raise e @@ -105,7 +105,7 @@ def find_quote_syntax_inclusions(): quote_syntax_inclusions = list() try: - quote_syntax_inclusions = check_output(["git", "grep", r"^#include \"", "--", "*.cpp", "*.h"] + exclude_args, text=True, encoding="utf8").splitlines() + quote_syntax_inclusions = check_output(["git", "grep", r"^#include \"", "--", "*.cpp", "*.h"] + exclude_args, text=True).splitlines() except CalledProcessError as e: if e.returncode > 1: raise e @@ -120,7 +120,7 @@ def main(): # Check for duplicate includes for filename in list_files_by_suffix((".cpp", ".h")): - with open(filename, "r", encoding="utf8") as file: + with open(filename, "r") as file: include_list = [line.rstrip("\n") for line in file if re.match(r"^#include", line)] duplicates = find_duplicate_includes(include_list) @@ -148,13 +148,13 @@ def main(): if extra_boosts: for boost in extra_boosts: print(f"A new Boost dependency in the form of \"{boost}\" appears to have been introduced:") - print(check_output(["git", "grep", boost, "--", "*.cpp", "*.h"], text=True, encoding="utf8")) + print(check_output(["git", "grep", boost, "--", "*.cpp", "*.h"], text=True)) exit_code = 1 # Check if Boost dependencies are no longer used for expected_boost in EXPECTED_BOOST_INCLUDES: try: - check_output(["git", "grep", "-q", r"^#include <%s>" % expected_boost, "--", "*.cpp", "*.h"], text=True, encoding="utf8") + check_output(["git", "grep", "-q", r"^#include <%s>" % expected_boost, "--", "*.cpp", "*.h"], text=True) except CalledProcessError as e: if e.returncode > 1: raise e diff --git a/test/lint/lint-locale-dependence.py b/test/lint/lint-locale-dependence.py index e728123e694..c6a64587091 100755 --- a/test/lint/lint-locale-dependence.py +++ b/test/lint/lint-locale-dependence.py @@ -219,7 +219,7 @@ def find_locale_dependent_function_uses(): git_grep_output = list() try: - git_grep_output = check_output(git_grep_command, text=True, encoding="utf8").splitlines() + git_grep_output = check_output(git_grep_command, text=True).splitlines() except CalledProcessError as e: if e.returncode > 1: raise e diff --git a/test/lint/lint-shell-locale.py b/test/lint/lint-shell-locale.py index 59515e7888d..3602289d18a 100755 --- a/test/lint/lint-shell-locale.py +++ b/test/lint/lint-shell-locale.py @@ -41,7 +41,7 @@ def main(): if re.search('src/(secp256k1|minisketch)/', file_path): continue - with open(file_path, 'r', encoding='utf-8') as file_obj: + with open(file_path, 'r') as file_obj: contents = file_obj.read() non_comment_pattern = re.compile(r'^\s*((?!#).+)$', re.MULTILINE) diff --git a/test/lint/lint-tests.py b/test/lint/lint-tests.py index 100339d217b..f4072d691c9 100755 --- a/test/lint/lint-tests.py +++ b/test/lint/lint-tests.py @@ -23,7 +23,7 @@ def grep_boost_fixture_test_suite(): "src/test/**.cpp", "src/wallet/test/**.cpp", ] - return subprocess.check_output(command, text=True, encoding="utf8") + return subprocess.check_output(command, text=True) def check_matching_test_names(test_suite_list):