Skip to content

Commit

Permalink
Catch UnicodeEncodeErrors (dotnet#64251)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunalspathak authored Jan 25, 2022
1 parent a5cf724 commit 341c394
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/coreclr/scripts/jitutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,31 @@ def set_pipeline_variable(name, value):
print(define_variable_format.format(name, value)) # set variable



################################################################################
##
## Helper functions
##
################################################################################

def decode_string(str_to_decode):
"""Decode a UTF-8 encoded bytes to string.
Args:
str_to_decode (byte stream): Byte stream to decode
Returns:
String output. If there any encoding/decoding errors, it will replace it with
UnicodeEncodeError.
"""
try:
output = str_to_decode.decode("utf-8", errors='replace')
except UnicodeEncodeError:
output = "UnicodeEncodeError"
except UnicodeDecodeError:
output = "UnicodeDecodeError"
return output

def run_command(command_to_run, _cwd=None, _exit_on_fail=False, _output_file=None):
""" Runs the command.
Expand Down Expand Up @@ -119,15 +138,15 @@ def run_command(command_to_run, _cwd=None, _exit_on_fail=False, _output_file=Non
if proc.poll() is not None:
break
if output:
output_str = output.strip().decode("utf-8", errors='replace')
output_str = decode_string(output.strip())
print(output_str)
of.write(output_str + "\n")
else:
command_stdout, command_stderr = proc.communicate()
if len(command_stdout) > 0:
print(command_stdout.decode("utf-8", errors='replace'))
print(decode_string(command_stdout))
if len(command_stderr) > 0:
print(command_stderr.decode("utf-8", errors='replace'))
print(decode_string(command_stderr))

return_code = proc.returncode
if _exit_on_fail and return_code != 0:
Expand Down

0 comments on commit 341c394

Please sign in to comment.