twister: fix utf-8 encoding for device handler

Fixed bug for when yaml contains utf-8 special character
Changed handler.log to be utf-8

Signed-off-by: Pavlo Havrylyuk <pavlo.havrylyuk@infineon.com>
This commit is contained in:
Pavlo Havrylyuk 2023-09-12 09:38:52 -07:00 committed by Fabio Baltieri
commit fa5636632e
3 changed files with 4 additions and 4 deletions

View file

@ -41,7 +41,7 @@ def yaml_load(filename):
:return: dictionary representing the YAML document
"""
try:
with open(filename, 'r') as f:
with open(filename, 'r', encoding='utf-8') as f:
return yaml.load(f, Loader=SafeLoader)
except yaml.scanner.ScannerError as e: # For errors parsing schema.yaml
mark = e.problem_mark

View file

@ -355,7 +355,7 @@ class DeviceHandler(Handler):
super().__init__(instance, type_str)
def monitor_serial(self, ser, halt_event, harness):
log_out_fp = open(self.log, "wt")
log_out_fp = open(self.log, "wb")
if self.options.coverage:
# Set capture_coverage to True to indicate that right after
@ -407,7 +407,7 @@ class DeviceHandler(Handler):
sl = serial_line.decode('utf-8', 'ignore').lstrip()
logger.debug("DEVICE: {0}".format(sl.rstrip()))
log_out_fp.write(sl)
log_out_fp.write(sl.encode('utf-8'))
log_out_fp.flush()
harness.handle(sl.rstrip())

View file

@ -162,7 +162,7 @@ def test_yaml_load(caplog, fail_parsing):
with pytest.raises(ScannerError) if fail_parsing else nullcontext():
result = scl.yaml_load(filename)
mock_file.assert_called_with('dummy/file.yaml', 'r')
mock_file.assert_called_with('dummy/file.yaml', 'r', encoding='utf-8')
if not fail_parsing:
assert result == result_mock