Skip to content

๐Ÿ› [Bug]: Emoji output crashes on Windows (cp1252) โ€” Number Guessing Game + 3 failing testsย #1323

Description

@Ashvin-KS

๐Ÿ“ 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:

python -m pytest -q

๐ŸŽฏ 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.


  • I am a registered GSSoC 2026 contributor.
  • I want to be assigned to fix this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions