๐ Description
Several scripts print emoji (e.g. ๐ฏ, ๐ฎ). On Windows, when standard output is not an
interactive UTF-8 console โ i.e. when output is piped, captured, or run under CI โ Python uses
the legacy cp1252 code page for stdout, which cannot encode emoji. The script then dies with
UnicodeEncodeError on its very first emoji print(), before producing any usable output.
This surfaces most visibly as 3 failing tests in tests/test_number_guessing.py, which run
the game via subprocess and capture its output (so stdout is a pipe, not a TTY). The captured
stdout is empty because the child process crashed, so every assertIn(...) fails. The same
latent crash affects any emoji-printing script when its output is redirected.
๐ Steps to Reproduce
On Windows:
printf "1\n82\nn\n" | python games/Number-Guessing-Game/Number-Guessing-Game.py 42
or simply run the suite:
๐ฏ Expected Behavior
Scripts run to completion (and tests pass) regardless of whether stdout is a UTF-8 console, a
pipe, or a CI log.
โ Actual Behavior / Error Logs
Direct run:
File ".../games/Number-Guessing-Game/Number-Guessing-Game.py", line 27, in main
print("\U0001f3af Welcome to the Number Guessing Game!\n")
File ".../lib/encodings/cp1252.py", line 19, in encode
return codecs.charmap_encode(input, self.errors, encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f3af' in position 0: character maps to <undefined>
Test suite:
FAILED tests/test_number_guessing.py::TestNumberGuessing::test_game_invalid_inputs
FAILED tests/test_number_guessing.py::TestNumberGuessing::test_game_lose
FAILED tests/test_number_guessing.py::TestNumberGuessing::test_game_win
======================== 3 failed, 228 passed ========================
๐ป Environment
- OS: Windows 11 (Linux/macOS default to UTF-8, so they are unaffected โ which is why CI on
Ubuntu may not catch this)
- Python Version: 3.10.x
- File Name: games/Number-Guessing-Game/Number-Guessing-Game.py (and other emoji-printing scripts)
๐ก Possible Fix
Force UTF-8 stdout at program start, e.g. near the top of the affected script(s):
import sys
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8")
(Or set PYTHONUTF8=1 / PYTHONIOENCODING=utf-8 in the test runner environment.) Happy to
discuss the preferred approach before sending a PR.
๐ Description
Several scripts print emoji (e.g.
๐ฏ,๐ฎ). On Windows, when standard output is not aninteractive UTF-8 console โ i.e. when output is piped, captured, or run under CI โ Python uses
the legacy
cp1252code page for stdout, which cannot encode emoji. The script then dies withUnicodeEncodeErroron its very first emojiprint(), before producing any usable output.This surfaces most visibly as 3 failing tests in
tests/test_number_guessing.py, which runthe game via
subprocessand capture its output (so stdout is a pipe, not a TTY). The capturedstdout is empty because the child process crashed, so every
assertIn(...)fails. The samelatent crash affects any emoji-printing script when its output is redirected.
๐ Steps to Reproduce
On Windows:
or simply run the suite:
๐ฏ Expected Behavior
Scripts run to completion (and tests pass) regardless of whether stdout is a UTF-8 console, a
pipe, or a CI log.
โ Actual Behavior / Error Logs
Direct run:
Test suite:
๐ป Environment
Ubuntu may not catch this)
๐ก Possible Fix
Force UTF-8 stdout at program start, e.g. near the top of the affected script(s):
(Or set
PYTHONUTF8=1/PYTHONIOENCODING=utf-8in the test runner environment.) Happy todiscuss the preferred approach before sending a PR.