Skip to content

Commit dad7050

Browse files
committed
fix: force to use UTF-8 encoding on stdout/stderr
Windows may use other encodings / code pages that have many characters missing.
1 parent a7a362c commit dad7050

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

python/pythonmonkey/require.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# @date May 2023
2424
#
2525

26-
import sys, os
26+
import sys, os, io
2727
from typing import Union, Dict, Literal, List
2828
import importlib
2929
import importlib.util
@@ -42,6 +42,13 @@
4242
)
4343
evalOpts = { 'filename': __file__, 'fromPythonFrame': True }
4444

45+
# Force to use UTF-8 encoding
46+
# Windows may use other encodings / code pages that have many characters missing/unrepresentable
47+
# Error: Python UnicodeEncodeError: 'charmap' codec can't encode characters in position ##-##: character maps to <undefined>
48+
sys.stdin = io.TextIOWrapper(sys.stdin .buffer, encoding='utf-8')
49+
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
50+
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
51+
4552
# Add some python functions to the global python object for code in this file to use.
4653
globalThis = pm.eval("globalThis;", evalOpts)
4754
pm.eval("globalThis.python = { pythonMonkey: {}, stdout: {}, stderr: {} }", evalOpts);

0 commit comments

Comments
 (0)