From 2ecfbd6ac3bc2ecc041f941e0792e37834c913a6 Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Wed, 13 Apr 2022 09:36:36 -0400 Subject: [PATCH 1/3] xcresult_logs.py: Fix potential UnicodeEncodeError when writing xcresult logs to stdout --- scripts/xcresult_logs.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/scripts/xcresult_logs.py b/scripts/xcresult_logs.py index 5e94f667e1e..679eda3217b 100755 --- a/scripts/xcresult_logs.py +++ b/scripts/xcresult_logs.py @@ -64,7 +64,19 @@ def main(): # but also makes it harder to deal with. log_id = find_log_id(xcresult_path) log = export_log(xcresult_path, log_id) - sys.stdout.write(log) + + # Avoid UnicodeEncodeError by adapting the code example from + # https://docs.python.org/3/library/sys.html#sys.displayhook + try: + sys.stdout.write(log) + except UnicodeEncodeError: + log_encoded = log.encode(sys.stdout.encoding, errors='backslashreplace') + if hasattr(sys.stdout, 'buffer'): + sys.stdout.flush() + sys.stdout.buffer.write(log_encoded) + else: + log_decoded = log_encoded.decode(sys.stdout.encoding, errors='strict') + sys.stdout.write(log_decoded) # Most flags on the xcodebuild command-line are uninteresting, so only pull From ae9bdf542e13e340e7c1fab8bf18495353c4bab7 Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Wed, 13 Apr 2022 12:37:44 -0400 Subject: [PATCH 2/3] xcresult_logs.py: change the UnicodeEncodeError avoidance strategy to instead just encode to UTF-8 in a relaxed manner and write the resulting bytes directly to stdout --- scripts/xcresult_logs.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/scripts/xcresult_logs.py b/scripts/xcresult_logs.py index 679eda3217b..f21d0b62687 100755 --- a/scripts/xcresult_logs.py +++ b/scripts/xcresult_logs.py @@ -65,18 +65,11 @@ def main(): log_id = find_log_id(xcresult_path) log = export_log(xcresult_path, log_id) - # Avoid UnicodeEncodeError by adapting the code example from - # https://docs.python.org/3/library/sys.html#sys.displayhook - try: - sys.stdout.write(log) - except UnicodeEncodeError: - log_encoded = log.encode(sys.stdout.encoding, errors='backslashreplace') - if hasattr(sys.stdout, 'buffer'): - sys.stdout.flush() - sys.stdout.buffer.write(log_encoded) - else: - log_decoded = log_encoded.decode(sys.stdout.encoding, errors='strict') - sys.stdout.write(log_decoded) + # Avoid a potential UnicodeEncodeError raised by sys.stdout.write() by + # doing a relaxed encoding ourselves and writing the resulting bytes. + log_encoded = log.encode('utf8', errors='backslashreplace') + sys.stdout.flush() + sys.stdout.buffer.write(log_encoded) # Most flags on the xcodebuild command-line are uninteresting, so only pull From 1b5d29636d7d68dacd8ae0835a1b6e4e2e899cf8 Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Wed, 13 Apr 2022 14:12:30 -0400 Subject: [PATCH 3/3] xcresult_logs.py: Another fix for when sys.stdout does not have a 'buffer' attribute --- scripts/xcresult_logs.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/scripts/xcresult_logs.py b/scripts/xcresult_logs.py index f21d0b62687..744734ebfa7 100755 --- a/scripts/xcresult_logs.py +++ b/scripts/xcresult_logs.py @@ -66,10 +66,15 @@ def main(): log = export_log(xcresult_path, log_id) # Avoid a potential UnicodeEncodeError raised by sys.stdout.write() by - # doing a relaxed encoding ourselves and writing the resulting bytes. - log_encoded = log.encode('utf8', errors='backslashreplace') - sys.stdout.flush() - sys.stdout.buffer.write(log_encoded) + # doing a relaxed encoding ourselves. + if hasattr(sys.stdout, 'buffer'): + log_encoded = log.encode('utf8', errors='backslashreplace') + sys.stdout.flush() + sys.stdout.buffer.write(log_encoded) + else: + log_encoded = log.encode('ascii', errors='backslashreplace') + log_decoded = log_encoded.decode('ascii', errors='strict') + sys.stdout.write(log_decoded) # Most flags on the xcodebuild command-line are uninteresting, so only pull