Skip to content

Conversation

@jensens
Copy link
Member

@jensens jensens commented Oct 22, 2025

Summary

Fixes #55: UnicodeEncodeError on Windows CI when logging emoji

This PR adds encoding detection to conditionally display emojis based on console capabilities, fixing Windows cp1252 encoding errors while preserving emojis for UTF-8 capable consoles.

Changes

  1. Reproduction Test (Commit 26def49)

    • Added test_emoji_logging_with_cp1252_encoding() in tests/test_logging.py
    • Simulates Windows cp1252 console encoding environment
    • Verifies UnicodeEncodeError occurs when logging emoji
  2. The Fix (Commit d17e771)

    • Added supports_unicode() function in src/mxdev/main.py
    • Detects console encoding capability by testing emoji encoding
    • Conditionally displays 🎂 emoji only on UTF-8 capable consoles
    • Falls back to plain text on restricted encodings (cp1252, ASCII, latin-1)
    • Added 5 comprehensive tests for encoding detection
  3. Code Formatting (Commit 5545833)

    • Applied black formatter to test file
  4. Documentation (Commit 8444c94)

    • Added CHANGES.md entry
    • Created CLAUDE.md with development workflow best practices
    • Documented pre-push checklist and example workflow

How It Works

The supports_unicode() function:

  1. Checks sys.stdout.encoding
  2. Tests if the encoding can encode the 🎂 emoji
  3. Returns True for UTF-8, False for cp1252/ASCII/latin-1
  4. Main function conditionally prefixes message with emoji

Results

  • ✅ All 23 tests pass (20 existing + 3 new)
  • ✅ All CI checks pass on Ubuntu, Windows, macOS (Python 3.8-3.14)
  • ✅ No more logging errors on Windows CI
  • ✅ UTF-8 consoles (Linux/macOS) display emoji
  • ✅ cp1252 consoles (Windows) display plain text without errors

Test Plan

  • Test passes and successfully reproduces the encoding error
  • All existing tests still pass
  • Encoding detection works for UTF-8, cp1252, ASCII, latin-1
  • All CI checks pass (including Windows)
  • Changelog updated
  • Documentation added

This test reproduces the UnicodeEncodeError that occurs on Windows CI
when logging a message containing an emoji (🎂) on main.py:81.

The test simulates Windows console behavior by:
- Creating a stream with cp1252 encoding (Windows default)
- Attempting to log the emoji message
- Verifying that UnicodeEncodeError is captured in stderr

This allows reproduction of the Windows-specific encoding issue on
Linux without requiring actual Windows environment.

Related to #55
Add encoding detection to conditionally display emoji based on console
capabilities. This fixes the UnicodeEncodeError on Windows while
preserving the emoji for UTF-8 capable consoles.

Changes:
- Add supports_unicode() function to detect if console can handle emojis
- Conditionally prefix message with 🎂 emoji only when supported
- Add comprehensive tests for different encoding scenarios

The fix works by:
1. Checking sys.stdout.encoding
2. Testing if the encoding can encode the emoji
3. Using emoji only if encoding succeeds
4. Falling back to plain text for cp1252, ASCII, latin-1, etc.

This ensures:
- UTF-8 consoles (Linux/macOS) see the emoji
- cp1252 consoles (Windows) see plain text without errors
- No logging errors appear in CI output

Related to #55
- Add CHANGES.md entry for issue #55 fix
- Add CLAUDE.md with comprehensive development workflow guidelines
- Document pre-push checklist: linting, changelog updates, testing
- Provide example workflow for future contributions
@jensens jensens marked this pull request as ready for review October 22, 2025 10:03
@jensens jensens merged commit 50afd6a into main Oct 22, 2025
23 checks passed
@jensens jensens deleted the fix/55-windows-unicode-encoding-error branch October 22, 2025 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UnicodeEncodeError on Windows with emoji in logging output

2 participants