From abeafbea08c57a69eb8550aa88fb7a9f81bd5e08 Mon Sep 17 00:00:00 2001 From: owl-from-hogvarts Date: Fri, 6 Nov 2020 21:23:23 +0300 Subject: [PATCH] lib: improved if statments for Unicode string support for Windows Better checking for python version (fix bug with python 4.0.0 which could break unicode strings support) Added checking for OS (unicode support doesn't necessary for other systems) --- gyp/pylib/gyp/easy_xml.py | 10 +++++++--- gyp/pylib/gyp/input.py | 10 +++++++--- lib/find-python-script.py | 14 ++++++++------ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/gyp/pylib/gyp/easy_xml.py b/gyp/pylib/gyp/easy_xml.py index ed67618742..43a9ef7b7b 100644 --- a/gyp/pylib/gyp/easy_xml.py +++ b/gyp/pylib/gyp/easy_xml.py @@ -122,10 +122,14 @@ def WriteXmlIfChanged(content, path, encoding="utf-8", pretty=False, win32=False default_encoding = locale.getdefaultlocale()[1] if default_encoding and default_encoding.upper() != encoding.upper(): - if (sys.version_info[0] >= 3) and (sys.version_info[1] >= 7): - xml_string = xml_string.encode(encoding) + if sys.platform == "win32": + if (sys.version_info[0] + sys.version_info[1] * 0.1) > 3.7: + xml_string = xml_string.encode(encoding) + else: + xml_string = xml_string.decode("cp1251").encode(encoding) + # for non windows systems else: - xml_string = xml_string.decode("cp1251").encode(encoding) + xml_string = xml_string.encode(encoding) # Get the old content try: diff --git a/gyp/pylib/gyp/input.py b/gyp/pylib/gyp/input.py index 4fd7565ca5..5f457fdcd1 100644 --- a/gyp/pylib/gyp/input.py +++ b/gyp/pylib/gyp/input.py @@ -236,10 +236,14 @@ def LoadOneBuildFile(build_file_path, data, aux_data, includes, is_target, check # But since node-gyp produces ebcdic files, do not use that mode. build_file_contents = open(build_file_path, "r").read() else: - if (sys.version_info[0] >= 3) and (sys.version_info[1] >= 7): - build_file_contents = open(build_file_path, 'rU', encoding="utf8").read() + if sys.platform == "win32": + if (sys.version_info[0] + sys.version_info[1] * 0.1) > 3.7: + build_file_contents = open(build_file_path, 'r', newline=None , encoding="utf8").read() + else: + # "U" flag is used becouse of backward compatibility + build_file_contents = open(build_file_path, 'rU').read() else: - build_file_contents = open(build_file_path, 'rU').read() + build_file_contents = open(build_file_path, 'r', newline=None).read() else: raise GypError("%s not found (cwd: %s)" % (build_file_path, os.getcwd())) diff --git a/lib/find-python-script.py b/lib/find-python-script.py index 96e468d436..f563770159 100644 --- a/lib/find-python-script.py +++ b/lib/find-python-script.py @@ -1,13 +1,15 @@ import sys, codecs; -if (sys.stdout.encoding != "utf-8"): - if (sys.version_info[0] >= 3) and (sys.version_info[1] >= 7): +if (sys.stdout.encoding != "utf-8" and sys.platform == "win32"): + if (sys.version_info[0] + sys.version_info[1] * 0.1) > 3.7: sys.stdout.reconfigure(encoding='utf-8') else: sys.stdout = codecs.getwriter("utf8")(sys.stdout) - -if (sys.version_info[0] >= 3) and (sys.version_info[1] >= 7): - print(sys.executable) +if sys.platform == "win32": + if (sys.version_info[0] + sys.version_info[1] * 0.1) > 3.7: + print(sys.executable) + else: + print(sys.executable.decode("cp1251")); else: - print(sys.executable.decode("cp1251")); \ No newline at end of file + print(sys.executable) \ No newline at end of file