From d01ebc7690bd6d0e6d543a5dc1d6c85cead939fa Mon Sep 17 00:00:00 2001 From: Stefan Grushko Date: Fri, 5 May 2023 12:24:01 +0200 Subject: [PATCH 1/6] Fix missing encoding for Windows with non-utf code page Code page number is obtained once at ctor of the ConsoleCohesionEventHandler. The change applies only to Windows OS. For other OS the encoding remains the same "utf-8". --- colcon_output/event_handler/console_cohesion.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/colcon_output/event_handler/console_cohesion.py b/colcon_output/event_handler/console_cohesion.py index 5960d84..2d4b21b 100644 --- a/colcon_output/event_handler/console_cohesion.py +++ b/colcon_output/event_handler/console_cohesion.py @@ -9,6 +9,7 @@ from colcon_core.event_handler import EventHandlerExtensionPoint from colcon_core.plugin_system import satisfies_version from colcon_core.subprocess import SIGINT_RESULT +import subprocess, sys class ConsoleCohesionEventHandler(EventHandlerExtensionPoint): @@ -36,6 +37,12 @@ def __init__(self): # noqa: D107 EventHandlerExtensionPoint.EXTENSION_POINT_VERSION, '^1.0') self.enabled = ConsoleCohesionEventHandler.ENABLED_BY_DEFAULT self._lines = defaultdict(list) + self.encoding = self.get_encoding() + + def get_encoding(self): + if sys.platform == "win32": + return subprocess.getoutput("chcp").replace("Active code page: ", "") + return "utf-8" def __call__(self, event): # noqa: D102 data = event[0] @@ -50,7 +57,7 @@ def __call__(self, event): # noqa: D102 msg = '--- output: {data.identifier}\n' \ .format_map(locals()) + \ b''.join( - self._lines[job]).decode() + \ + self._lines[job]).decode(encoding=self.encoding) + \ '---' print(msg, flush=True) del self._lines[job] From 134cbfafe7d875511b2ab61ccfaa8880807ab534 Mon Sep 17 00:00:00 2001 From: Stefan Grushko Date: Fri, 5 May 2023 13:48:11 +0200 Subject: [PATCH 2/6] Change string literals to single-quotes --- colcon_output/event_handler/console_cohesion.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/colcon_output/event_handler/console_cohesion.py b/colcon_output/event_handler/console_cohesion.py index 2d4b21b..ac4c0c9 100644 --- a/colcon_output/event_handler/console_cohesion.py +++ b/colcon_output/event_handler/console_cohesion.py @@ -40,9 +40,9 @@ def __init__(self): # noqa: D107 self.encoding = self.get_encoding() def get_encoding(self): - if sys.platform == "win32": - return subprocess.getoutput("chcp").replace("Active code page: ", "") - return "utf-8" + if sys.platform == 'win32': + return subprocess.getoutput('chcp').replace('Active code page: ', '') + return 'utf-8' def __call__(self, event): # noqa: D102 data = event[0] From ddae91d53c27dbf35da3a84a32c7e5e2751be109 Mon Sep 17 00:00:00 2001 From: Stefan Grushko Date: Tue, 23 May 2023 10:13:47 +0200 Subject: [PATCH 3/6] add universal handling for languages --- colcon_output/event_handler/console_cohesion.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/colcon_output/event_handler/console_cohesion.py b/colcon_output/event_handler/console_cohesion.py index ac4c0c9..6bedce5 100644 --- a/colcon_output/event_handler/console_cohesion.py +++ b/colcon_output/event_handler/console_cohesion.py @@ -9,7 +9,7 @@ from colcon_core.event_handler import EventHandlerExtensionPoint from colcon_core.plugin_system import satisfies_version from colcon_core.subprocess import SIGINT_RESULT -import subprocess, sys +import subprocess, sys, re class ConsoleCohesionEventHandler(EventHandlerExtensionPoint): @@ -40,9 +40,15 @@ def __init__(self): # noqa: D107 self.encoding = self.get_encoding() def get_encoding(self): - if sys.platform == 'win32': - return subprocess.getoutput('chcp').replace('Active code page: ', '') - return 'utf-8' + default_encoding = 'utf-8' + if sys.platform == 'win32': # handling non-utf-8 code page + chcp_out = subprocess.getoutput('chcp') + regex = re.search(r'.+: (.+)', chcp_out) + if regex is not None and len(regex.groups()) == 1: + return regex.group(1) + print("Unknown Windows code page, fallback to utf-8!") + return default_encoding + return default_encoding def __call__(self, event): # noqa: D102 data = event[0] From 7dda71f3adb65b231f3c8573a8483f26a3336fce Mon Sep 17 00:00:00 2001 From: Stefan Grushko Date: Tue, 23 May 2023 10:16:10 +0200 Subject: [PATCH 4/6] single quotes --- colcon_output/event_handler/console_cohesion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/colcon_output/event_handler/console_cohesion.py b/colcon_output/event_handler/console_cohesion.py index 6bedce5..2124daf 100644 --- a/colcon_output/event_handler/console_cohesion.py +++ b/colcon_output/event_handler/console_cohesion.py @@ -46,7 +46,7 @@ def get_encoding(self): regex = re.search(r'.+: (.+)', chcp_out) if regex is not None and len(regex.groups()) == 1: return regex.group(1) - print("Unknown Windows code page, fallback to utf-8!") + print('Unknown Windows code page, fallback to utf-8!') return default_encoding return default_encoding From a44e62d57fc913b40a80ab47c470555ad34d02c8 Mon Sep 17 00:00:00 2001 From: Stefan Grushko Date: Tue, 23 May 2023 10:37:52 +0200 Subject: [PATCH 5/6] Reuse default_encoding for user output --- colcon_output/event_handler/console_cohesion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/colcon_output/event_handler/console_cohesion.py b/colcon_output/event_handler/console_cohesion.py index 2124daf..f288932 100644 --- a/colcon_output/event_handler/console_cohesion.py +++ b/colcon_output/event_handler/console_cohesion.py @@ -46,7 +46,7 @@ def get_encoding(self): regex = re.search(r'.+: (.+)', chcp_out) if regex is not None and len(regex.groups()) == 1: return regex.group(1) - print('Unknown Windows code page, fallback to utf-8!') + print(f'Unknown Windows code page, fallback to {default_encoding}!') return default_encoding return default_encoding From 3986238c44937e830df960d07e63dd6dc6a0b93e Mon Sep 17 00:00:00 2001 From: Stefan Grushko Date: Fri, 26 May 2023 08:33:50 +0200 Subject: [PATCH 6/6] use GetConsoleOutputCP instead of chcp --- colcon_output/event_handler/console_cohesion.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/colcon_output/event_handler/console_cohesion.py b/colcon_output/event_handler/console_cohesion.py index f288932..a1c6f6d 100644 --- a/colcon_output/event_handler/console_cohesion.py +++ b/colcon_output/event_handler/console_cohesion.py @@ -1,6 +1,7 @@ # Copyright 2016-2018 Dirk Thomas # Licensed under the Apache License, Version 2.0 +import sys from collections import defaultdict from colcon_core.event.job import JobEnded @@ -9,7 +10,6 @@ from colcon_core.event_handler import EventHandlerExtensionPoint from colcon_core.plugin_system import satisfies_version from colcon_core.subprocess import SIGINT_RESULT -import subprocess, sys, re class ConsoleCohesionEventHandler(EventHandlerExtensionPoint): @@ -40,15 +40,10 @@ def __init__(self): # noqa: D107 self.encoding = self.get_encoding() def get_encoding(self): - default_encoding = 'utf-8' - if sys.platform == 'win32': # handling non-utf-8 code page - chcp_out = subprocess.getoutput('chcp') - regex = re.search(r'.+: (.+)', chcp_out) - if regex is not None and len(regex.groups()) == 1: - return regex.group(1) - print(f'Unknown Windows code page, fallback to {default_encoding}!') - return default_encoding - return default_encoding + if sys.platform == 'win32': + from ctypes import windll + return str(windll.kernel32.GetConsoleOutputCP()) + return 'utf-8' def __call__(self, event): # noqa: D102 data = event[0]