From fa63326fbc1634ef2816f79c54db9266e56f4b3b Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Thu, 20 Jul 2023 09:17:59 +0200 Subject: [PATCH 1/3] test: Fix debug_log_size helper debug_log_bytes returned "an opaque number when in text mode" (https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects), not the number of bytes. Fix this by using binary mode or text mode (with the same encoding) consistently when opening the file for ftell() and read(). --- test/functional/test_framework/test_node.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/functional/test_framework/test_node.py b/test/functional/test_framework/test_node.py index 56087013fb6..f2ef17ccb72 100755 --- a/test/functional/test_framework/test_node.py +++ b/test/functional/test_framework/test_node.py @@ -424,8 +424,8 @@ class TestNode(): def wallets_path(self) -> Path: return self.chain_path / "wallets" - def debug_log_bytes(self) -> int: - with open(self.debug_log_path, encoding='utf-8') as dl: + def debug_log_size(self, **kwargs) -> int: + with open(self.debug_log_path, **kwargs) as dl: dl.seek(0, 2) return dl.tell() @@ -434,7 +434,7 @@ class TestNode(): if unexpected_msgs is None: unexpected_msgs = [] time_end = time.time() + timeout * self.timeout_factor - prev_size = self.debug_log_bytes() + prev_size = self.debug_log_size(encoding="utf-8") # Must use same encoding that is used to read() below yield @@ -465,7 +465,7 @@ class TestNode(): the number of log lines we encountered when matching """ time_end = time.time() + timeout * self.timeout_factor - prev_size = self.debug_log_bytes() + prev_size = self.debug_log_size(mode="rb") # Must use same mode that is used to read() below yield From fa6bb85cd264adf2e1f0c71fa97b650f9a62745f Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Thu, 6 Jul 2023 12:47:42 +0200 Subject: [PATCH 2/3] test: Ignore UTF-8 errors in assert_debug_log read() fails in text mode when the unicode hasn't been fully written yet. Fixes: "wallet_importdescriptors.py: can't decode bytes in position 228861-228863: unexpected end of data" (https://github.com/bitcoin/bitcoin/issues/28030) --- test/functional/test_framework/test_node.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/test_framework/test_node.py b/test/functional/test_framework/test_node.py index f2ef17ccb72..7ef3280ffcf 100755 --- a/test/functional/test_framework/test_node.py +++ b/test/functional/test_framework/test_node.py @@ -440,7 +440,7 @@ class TestNode(): while True: found = True - with open(self.debug_log_path, encoding='utf-8') as dl: + with open(self.debug_log_path, encoding="utf-8", errors="replace") as dl: dl.seek(prev_size) log = dl.read() print_log = " - " + "\n - ".join(log.splitlines()) From fa3d72960bc86319aa8b838e3df4e833f845c71f Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Thu, 6 Jul 2023 16:07:20 +0200 Subject: [PATCH 3/3] lint: Ignore check_fileopens failure on **kwargs This fixes a bug in the linter: """ Python's open(...) seems to be used to open text files without explicitly specifying encoding='utf8': test/functional/test_framework/test_node.py: with open(self.debug_log_path, **kwargs) as dl: """ --- test/lint/lint-python-utf8-encoding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lint/lint-python-utf8-encoding.py b/test/lint/lint-python-utf8-encoding.py index 64d04bff57c..8c9266470f1 100755 --- a/test/lint/lint-python-utf8-encoding.py +++ b/test/lint/lint-python-utf8-encoding.py @@ -28,7 +28,7 @@ def check_fileopens(): if e.returncode > 1: raise e - filtered_fileopens = [fileopen for fileopen in fileopens if not re.search(r"encoding=.(ascii|utf8|utf-8).|open\([^,]*, ['\"][^'\"]*b[^'\"]*['\"]", fileopen)] + filtered_fileopens = [fileopen for fileopen in fileopens if not re.search(r"encoding=.(ascii|utf8|utf-8).|open\([^,]*, (\*\*kwargs|['\"][^'\"]*b[^'\"]*['\"])", fileopen)] return filtered_fileopens