From ddf7f53fdfbe08460d31f8b395bc9b76d5d079a4 Mon Sep 17 00:00:00 2001 From: Fuming Zhang Date: Fri, 20 Jun 2025 10:46:58 +1000 Subject: [PATCH 1/2] update --- src/azure-cli-testsdk/azure/cli/testsdk/utilities.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/azure-cli-testsdk/azure/cli/testsdk/utilities.py b/src/azure-cli-testsdk/azure/cli/testsdk/utilities.py index c96ff2a4bb6..a5c893fc714 100644 --- a/src/azure-cli-testsdk/azure/cli/testsdk/utilities.py +++ b/src/azure-cli-testsdk/azure/cli/testsdk/utilities.py @@ -50,7 +50,12 @@ def force_progress_logging(): def _byte_to_str(byte_or_str): - return str(byte_or_str, 'utf-8') if isinstance(byte_or_str, bytes) else byte_or_str + if isinstance(byte_or_str, bytes): + try: + return byte_or_str.decode('utf-8') + except UnicodeDecodeError: + pass + return byte_or_str class StorageAccountKeyReplacer(RecordingProcessor): @@ -230,7 +235,8 @@ def process_request(self, request): def process_response(self, response): if response['body']['string']: body = _byte_to_str(response['body']['string']) - response['body']['string'] = self._replace_email_address(body) + if isinstance(body, str): + response['body']['string'] = self._replace_email_address(body) return response From facab742a0335ad9ef14a67e73bbbd7764ecf1d9 Mon Sep 17 00:00:00 2001 From: Fuming Zhang Date: Fri, 20 Jun 2025 10:57:24 +1000 Subject: [PATCH 2/2] update --- .../azure/cli/testsdk/utilities.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/azure-cli-testsdk/azure/cli/testsdk/utilities.py b/src/azure-cli-testsdk/azure/cli/testsdk/utilities.py index a5c893fc714..fc51aa77dcd 100644 --- a/src/azure-cli-testsdk/azure/cli/testsdk/utilities.py +++ b/src/azure-cli-testsdk/azure/cli/testsdk/utilities.py @@ -50,12 +50,7 @@ def force_progress_logging(): def _byte_to_str(byte_or_str): - if isinstance(byte_or_str, bytes): - try: - return byte_or_str.decode('utf-8') - except UnicodeDecodeError: - pass - return byte_or_str + return str(byte_or_str, 'utf-8') if isinstance(byte_or_str, bytes) else byte_or_str class StorageAccountKeyReplacer(RecordingProcessor): @@ -234,9 +229,12 @@ def process_request(self, request): def process_response(self, response): if response['body']['string']: - body = _byte_to_str(response['body']['string']) - if isinstance(body, str): + try: + body = _byte_to_str(response['body']['string']) response['body']['string'] = self._replace_email_address(body) + except UnicodeDecodeError: + # If the body is not a string, we cannot decode it, so we skip the replacement + pass return response