diff --git a/src/poetry/utils/env/base_env.py b/src/poetry/utils/env/base_env.py index eb8dac6cf97..40ecfbe3895 100644 --- a/src/poetry/utils/env/base_env.py +++ b/src/poetry/utils/env/base_env.py @@ -327,6 +327,8 @@ def run_python_script(self, content: str, **kwargs: Any) -> str: "-I", "-W", "ignore", + "-X", + "utf8", "-", input_=content, stderr=subprocess.PIPE, @@ -352,6 +354,7 @@ def _run(self, cmd: list[str], **kwargs: Any) -> str: check=True, env=env, text=True, + encoding="utf-8", **kwargs, ).stdout elif call: diff --git a/tests/utils/test_env.py b/tests/utils/test_env.py index dafd9af4c08..728d9dd8483 100644 --- a/tests/utils/test_env.py +++ b/tests/utils/test_env.py @@ -1124,6 +1124,16 @@ def test_run_python_script_only_stdout(tmp_path: Path, tmp_venv: VirtualEnv) -> assert "some warning" not in output +def test_run_python_script_non_ascii_input( + tmp_path: Path, tmp_venv: VirtualEnv +) -> None: + output = tmp_venv.run_python_script( + "import sys; print('👎', file=sys.stderr); print('👍')" + ) + assert "👍" in output + assert "👎" not in output + + def test_create_venv_tries_to_find_a_compatible_python_executable_using_generic_ones_first( # noqa: E501 manager: EnvManager, poetry: Poetry,