From 729ca482507066cd1a7cb50d1de75e2369b497cf Mon Sep 17 00:00:00 2001 From: Julianne Swinoga Date: Fri, 1 Mar 2024 14:47:48 -0500 Subject: [PATCH] Fix text byte decoding to not use strict error handling --- OATFWGUI/anon_usage_data.py | 5 +++-- OATFWGUI/external_processes.py | 6 ++++-- OATFWGUI/misc_utils.py | 5 +++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/OATFWGUI/anon_usage_data.py b/OATFWGUI/anon_usage_data.py index eb2363d..494bb57 100644 --- a/OATFWGUI/anon_usage_data.py +++ b/OATFWGUI/anon_usage_data.py @@ -15,6 +15,7 @@ from platform_check import get_platform, PlatformEnum from gui_state import LogicState +from misc_utils import decode_bytes log = logging.getLogger('') @@ -163,7 +164,7 @@ def get_uuid_windows() -> str: capture_output=True) if sub_proc.returncode != 0: return 'unknown-windows' - windows_uuid = sub_proc.stdout.decode('UTF-8') + windows_uuid = decode_bytes(sub_proc.stdout) return windows_uuid @@ -185,7 +186,7 @@ def to_nearest_half(num: float) -> float: def get_approx_location() -> Tuple[float, float]: geo_ip_url = 'https://ipinfo.io/loc' response = requests.get(geo_ip_url, timeout=2.0) - resp_str = response.content.decode().strip() + resp_str = decode_bytes(response.content).strip() lat_str, lon_str = resp_str.split(',') lat_approx = to_nearest_half(float(lat_str)) lon_approx = to_nearest_half(float(lon_str)) diff --git a/OATFWGUI/external_processes.py b/OATFWGUI/external_processes.py index 7ce11fd..e27c902 100644 --- a/OATFWGUI/external_processes.py +++ b/OATFWGUI/external_processes.py @@ -5,6 +5,8 @@ from PySide6.QtCore import Slot, QProcess, QStandardPaths, QProcessEnvironment +from misc_utils import decode_bytes + log = logging.getLogger('') @@ -62,14 +64,14 @@ def cleanup(self): @Slot() def handle_stderr(self): data = self.qproc.readAllStandardError() - stderr = bytes(data).decode("utf8") + stderr = decode_bytes(bytes(data)) self.stderr_text += stderr log.error(stderr) @Slot() def handle_stdout(self): data = self.qproc.readAllStandardOutput() - stdout = bytes(data).decode("utf8") + stdout = decode_bytes(bytes(data)) self.stdout_text += stdout log.info(stdout) diff --git a/OATFWGUI/misc_utils.py b/OATFWGUI/misc_utils.py index 1e682ce..843174b 100644 --- a/OATFWGUI/misc_utils.py +++ b/OATFWGUI/misc_utils.py @@ -16,3 +16,8 @@ def remove_readonly(func: Callable, path, excinfo): func(path) shutil.rmtree(dir_to_delete, onerror=remove_readonly) + + +def decode_bytes(byte_string: bytes) -> str: + # Just to consolidate all text decoding and make sure they're all the same + return byte_string.decode('utf-8', errors='backslashreplace')