diff --git a/gyp/pylib/gyp/easy_xml.py b/gyp/pylib/gyp/easy_xml.py index e0628ef4d8..ed67618742 100644 --- a/gyp/pylib/gyp/easy_xml.py +++ b/gyp/pylib/gyp/easy_xml.py @@ -2,6 +2,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import sys import re import os import locale @@ -121,7 +122,10 @@ 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(): - xml_string = xml_string.encode(encoding) + if (sys.version_info[0] >= 3) and (sys.version_info[1] >= 7): + xml_string = xml_string.encode(encoding) + else: + xml_string = xml_string.decode("cp1251").encode(encoding) # Get the old content try: diff --git a/gyp/pylib/gyp/input.py b/gyp/pylib/gyp/input.py index 5504390c0b..4fd7565ca5 100644 --- a/gyp/pylib/gyp/input.py +++ b/gyp/pylib/gyp/input.py @@ -236,7 +236,10 @@ 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: - build_file_contents = open(build_file_path, "rU").read() + if (sys.version_info[0] >= 3) and (sys.version_info[1] >= 7): + build_file_contents = open(build_file_path, 'rU', encoding="utf8").read() + else: + build_file_contents = open(build_file_path, 'rU').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 new file mode 100644 index 0000000000..96e468d436 --- /dev/null +++ b/lib/find-python-script.py @@ -0,0 +1,13 @@ +import sys, codecs; + +if (sys.stdout.encoding != "utf-8"): + if (sys.version_info[0] >= 3) and (sys.version_info[1] >= 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) +else: + print(sys.executable.decode("cp1251")); \ No newline at end of file diff --git a/lib/find-python.js b/lib/find-python.js index af269de2fc..811ccd4019 100644 --- a/lib/find-python.js +++ b/lib/find-python.js @@ -16,7 +16,7 @@ function PythonFinder (configPython, callback) { PythonFinder.prototype = { log: logWithPrefix(log, 'find Python'), - argsExecutable: ['-c', 'import sys; print(sys.executable);'], + argsExecutable: [path.resolve(__dirname, 'find-python-script.py')], argsVersion: ['-c', 'import sys; print("%s.%s.%s" % sys.version_info[:3]);'], semverRange: '2.7.x || >=3.5.0', @@ -246,7 +246,7 @@ PythonFinder.prototype = { run: function run (exec, args, shell, callback) { var env = extend({}, this.env) env.TERM = 'dumb' - const opts = { env: env, shell: shell } + const opts = { env: env, shell: shell, encoding: 'utf8' } this.log.silly('execFile: exec = %j', exec) this.log.silly('execFile: args = %j', args)