diff --git a/poethepoet/context.py b/poethepoet/context.py index 1ae766d43..5a6d3b3ac 100644 --- a/poethepoet/context.py +++ b/poethepoet/context.py @@ -77,7 +77,15 @@ def save_task_output(self, invocation: Tuple[str, ...], captured_stdout: bytes): """ Store the stdout data from a task so that it can be reused by other tasks """ - self.captured_stdout[invocation] = captured_stdout.decode() + try: + self.captured_stdout[invocation] = captured_stdout.decode() + except UnicodeDecodeError: + # Attempt to recover in case a specific encoding is configured + io_encoding = self.env.get("PYTHONIOENCODING") + if io_encoding: + self.captured_stdout[invocation] = captured_stdout.decode(io_encoding) + else: + raise def get_task_output(self, invocation: Tuple[str, ...]): """ diff --git a/poethepoet/env/manager.py b/poethepoet/env/manager.py index 3ccfe7fec..380e798c7 100644 --- a/poethepoet/env/manager.py +++ b/poethepoet/env/manager.py @@ -49,6 +49,9 @@ def __init__( self._vars["POE_ROOT"] = str(self._config.project_dir) + def get(self, key: str, default: Optional[str] = None) -> Optional[str]: + return self._vars.get(key, default) + def _apply_env_config( self, config_env: Mapping[str, Union[str, Mapping[str, str]]], diff --git a/poethepoet/executor/base.py b/poethepoet/executor/base.py index 05a1fa450..80bcb66fd 100644 --- a/poethepoet/executor/base.py +++ b/poethepoet/executor/base.py @@ -199,6 +199,10 @@ def _exec_via_subproc( popen_kwargs["stdout"] = open(self.capture_stdout, "wb") else: popen_kwargs["stdout"] = PIPE + + if "PYTHONIOENCODING" not in popen_kwargs["env"]: + popen_kwargs["env"]["PYTHONIOENCODING"] = "utf-8" + if self.working_dir is not None: popen_kwargs["cwd"] = self.working_dir