Skip to content

Commit

Permalink
Improve encoding handling on Window
Browse files Browse the repository at this point in the history
  • Loading branch information
houtianze committed Feb 1, 2025
1 parent 3576a92 commit 0b3ebef
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
11 changes: 6 additions & 5 deletions bypy/chkreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class CheckResult:

def check_requirements():
result = CheckResult.Pass
if iswindows():
if False and iswindows():
bannerwarn("You are running Python on Windows, which doesn't support Unicode so well.\n"
"Files with non-ASCII names may not be handled correctly.")
result = max(result, CheckResult.Warning)

if sys.version_info[0] < 2 \
or (sys.version_info[0] == 2 and sys.version_info[1] < 7) \
or (sys.version_info[0] == 3 and sys.version_info[1] < 3):
Expand All @@ -32,7 +32,8 @@ def check_requirements():
# we have warned Windows users, so the following is for *nix users only
if gvar.SystemEncoding:
sysencu = gvar.SystemEncoding.upper()
if sysencu != 'UTF-8' and sysencu != 'UTF8':
if sysencu != 'UTF-8' and sysencu != 'UTF8' and sysencu != 'UTF_8' \
and sysencu != 'CP65001' and sysencu != '65001':
msg = "WARNING: System locale is not 'UTF-8'.\n" \
"Files with non-ASCII names may not be handled correctly.\n" \
"You should set your System Locale to 'UTF-8'.\n" \
Expand All @@ -46,7 +47,7 @@ def check_requirements():
bannerwarn("WARNING: Can't detect the system encoding, assume it's 'UTF-8'.\n"
"Files with non-ASCII names may not be handled correctly." )
result = max(result, CheckResult.Warning)

stdenc = sys.stdout.encoding
if stdenc:
stdencu = stdenc.upper()
Expand All @@ -58,7 +59,7 @@ def check_requirements():
fixenc(stdenc)
else:
fixenc(stdenc)

return result

if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions bypy/gvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

## global variables
try:
SystemLanguageCode, SystemEncoding = locale.getdefaultlocale()
SystemLanguageCode, SystemEncoding = locale.getlocale()
except ValueError as e:
# https://coderwall.com/p/-k_93g/mac-os-x-valueerror-unknown-locale-utf-8-in-python
# Mac OS X: ValueError: unknown locale: UTF-8 in Python
# Proper fix:
# export LC_ALL=en_US.UTF-8
# export LANG=en_US.UTF-8
if e.args and e.args[0] and e.args[0] == "unknown locale: UTF-8":
SystemLanguageCode, SystemEncoding = '', 'UTF-8'
SystemLanguageCode, SystemEncoding = '', 'utf-8'
else:
raise
# the previous time stdout was flushed, maybe we just flush every time, or maybe this way performs better
Expand Down
4 changes: 2 additions & 2 deletions bypy/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def pwarn(msg, showtime = True, showdate = False, prefix = '', suffix = ''):
return plog('<W> ', msg, showtime, showdate, prefix, suffix, TermColor.Yellow)

def bannerwarn(msg):
pwarn('!' * 160, showtime = False)
pwarn('!' * 80, showtime = False)
pwarn(msg, showtime = False)
pwarn('!' * 160, showtime = False)
pwarn('!' * 80, showtime = False)

def pinfo(msg, showtime = True, showdate = False, prefix = '', suffix = ''):
return plog('<I> ', msg, showtime, showdate, prefix, suffix, TermColor.Green)
Expand Down

0 comments on commit 0b3ebef

Please sign in to comment.