From 0b3ebefbfa681d081a75e8ce7c0d9d81cc3b869c Mon Sep 17 00:00:00 2001 From: HOU Tianze Date: Sat, 1 Feb 2025 14:39:18 +0800 Subject: [PATCH] Improve encoding handling on Window --- bypy/chkreq.py | 11 ++++++----- bypy/gvar.py | 4 ++-- bypy/printer.py | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/bypy/chkreq.py b/bypy/chkreq.py index 3781c87..cc22fc8 100755 --- a/bypy/chkreq.py +++ b/bypy/chkreq.py @@ -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): @@ -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" \ @@ -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() @@ -58,7 +59,7 @@ def check_requirements(): fixenc(stdenc) else: fixenc(stdenc) - + return result if __name__ == "__main__": diff --git a/bypy/gvar.py b/bypy/gvar.py index 74c2b3b..9cf4e27 100644 --- a/bypy/gvar.py +++ b/bypy/gvar.py @@ -13,7 +13,7 @@ ## 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 @@ -21,7 +21,7 @@ # 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 diff --git a/bypy/printer.py b/bypy/printer.py index 1a254df..285433e 100644 --- a/bypy/printer.py +++ b/bypy/printer.py @@ -38,9 +38,9 @@ def pwarn(msg, showtime = True, showdate = False, prefix = '', suffix = ''): return plog(' ', 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(' ', msg, showtime, showdate, prefix, suffix, TermColor.Green)