code_puppy icon indicating copy to clipboard operation
code_puppy copied to clipboard

Fix UnicodeEncodeError on Windows when printing emojis

Open gs131016 opened this issue 2 months ago • 6 comments

gs131016 avatar Nov 07 '25 14:11 gs131016

Tested this branch on windows and I'm still getting the unicode error

mpfaffenberger avatar Nov 09 '25 03:11 mpfaffenberger

Intersting. It works fine for me after this fix.

gs131016 avatar Nov 09 '25 03:11 gs131016

Maybe we're talking about two different things. Please provide steps to repro!

mpfaffenberger avatar Nov 11 '25 14:11 mpfaffenberger

So, bascially I updated the puppy version (0.0.270) today and tried to execute code-puppy -i, which worked fine. When I tried code-puppy -w, I received the same error again. image UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f436' in position 0: character maps to .

The fix suggested in this PR didn't work for me either for this update. It was working fine for that last 2 updates. On further analyzing this, the issue was resolved after I executed the below powershell command to update the registry settings and without making any changes to main.py [Environment]::SetEnvironmentVariable("PYTHONUTF8", "1", "User")

gs131016 avatar Nov 17 '25 08:11 gs131016

So, bascially I updated the puppy version (0.0.270) today and tried to execute code-puppy -i, which worked fine. When I tried code-puppy -w, I received the same error again. image UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f436' in position 0: character maps to .

The fix suggested in this PR didn't work for me either for this update. It was working fine for that last 2 updates. On further analyzing this, the issue was resolved after I executed the below powershell command to update the registry settings and without making any changes to main.py [Environment]::SetEnvironmentVariable("PYTHONUTF8", "1", "User")

If this Environment var fixes the emoji bug I will be sooooo happy. Going to test.

mpfaffenberger avatar Nov 17 '25 13:11 mpfaffenberger

So, bascially I updated the puppy version (0.0.270) today and tried to execute code-puppy -i, which worked fine. When I tried code-puppy -w, I received the same error again. image UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f436' in position 0: character maps to .

The fix suggested in this PR didn't work for me either for this update. It was working fine for that last 2 updates. On further analyzing this, the issue was resolved after I executed the below powershell command to update the registry settings and without making any changes to main.py [Environment]::SetEnvironmentVariable("PYTHONUTF8", "1", "User")

The environment var change had no effect for me when I tested it.

This bug is so frustrating. :(

mpfaffenberger avatar Nov 21 '25 14:11 mpfaffenberger

import sys
import os
import io

# Windows Unicode/Emoji fix
if sys.platform == "win32":
    os.environ["PYTHONUTF8"] = "1"
    try:
        import ctypes
        kernel32 = ctypes.windll.kernel32
        kernel32.SetConsoleOutputCP(65001)
        kernel32.SetConsoleCP(65001)
    except Exception:
        pass
    if hasattr(sys.stdout, 'buffer') and not isinstance(sys.stdout, io.TextIOWrapper):
        sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace', line_buffering=True)
    if hasattr(sys.stderr, 'buffer') and not isinstance(sys.stderr, io.TextIOWrapper):
        sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace', line_buffering=True)
        

should be doing this ... the prob the env var alone doesnt change the CP on the the thing ... this isnt a good way and hacky :D but i had to do similar because windows is STUPIDLY setting the shell type depending on CMD / Powershell / OS version ... and initializing the ConsoleCP

The above provided shell var works fine in WSL but not clean windwos

janfeddersen-wq avatar Dec 07 '25 10:12 janfeddersen-wq

This should be fixed - I forgot to update the issue. Lol, my solution was similar. @janfeddersen-wq

mpfaffenberger avatar Dec 07 '25 12:12 mpfaffenberger