Browse Source

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>
pull/62824/head
Pavlo Havrylyuk 2 years ago committed by Fabio Baltieri
parent
commit
fa5636632e
  1. 2
      scripts/pylib/twister/scl.py
  2. 4
      scripts/pylib/twister/twisterlib/handlers.py
  3. 2
      scripts/tests/twister/test_scl.py

2
scripts/pylib/twister/scl.py

@ -41,7 +41,7 @@ def yaml_load(filename): @@ -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

4
scripts/pylib/twister/twisterlib/handlers.py

@ -355,7 +355,7 @@ class DeviceHandler(Handler): @@ -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): @@ -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())

2
scripts/tests/twister/test_scl.py

@ -162,7 +162,7 @@ def test_yaml_load(caplog, fail_parsing): @@ -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

Loading…
Cancel
Save