Skip to content

Commit

Permalink
Make capture of task output use PYTHONIOENCODING
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-n committed Dec 26, 2022
1 parent 71cf194 commit 75d8b97
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion poethepoet/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...]):
"""
Expand Down
3 changes: 3 additions & 0 deletions poethepoet/env/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]],
Expand Down
4 changes: 4 additions & 0 deletions poethepoet/executor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 75d8b97

Please sign in to comment.