Skip to content

Commit c472912

Browse files
lib: print Python executable path using UTF-8 (#2995)
* lib: print Python executable path using UTF-8 The Python executable path may have non-ASCII characters, which can make the print function fail if the environment encoding is different. This fixes this issue by using stdout.buffer, which can be used with UTF-8 encoding for the output, regardless of the environment encoding. Fixes: #2829 * fixup! lib: print Python executable path using UTF-8
1 parent 1205fb0 commit c472912

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/find-python.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class PythonFinder {
4141
static findPython = (...args) => new PythonFinder(...args).findPython()
4242

4343
log = log.withPrefix('find Python')
44-
argsExecutable = ['-c', 'import sys; print(sys.executable);']
44+
argsExecutable = ['-c', 'import sys; sys.stdout.buffer.write(sys.executable.encode(\'utf-8\'));']
4545
argsVersion = ['-c', 'import sys; print("%s.%s.%s" % sys.version_info[:3]);']
4646
semverRange = '>=3.6.0'
4747

test/test-find-python.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
delete process.env.PYTHON
44

5-
const { describe, it } = require('mocha')
5+
const { describe, it, after } = require('mocha')
66
const assert = require('assert')
77
const PythonFinder = require('../lib/find-python')
88
const { execFile } = require('../lib/util')
99
const { poison } = require('./common')
10+
const fs = require('fs')
11+
const path = require('path')
12+
const os = require('os')
1013

1114
class TestPythonFinder extends PythonFinder {
1215
constructor (...args) {
@@ -32,6 +35,32 @@ describe('find-python', function () {
3235
assert.strictEqual(stderr, '')
3336
})
3437

38+
it('find python - encoding', async function () {
39+
const found = await PythonFinder.findPython(null)
40+
const testFolderPath = fs.mkdtempSync(path.join(os.tmpdir(), 'test-ü-'))
41+
const testFilePath = path.join(testFolderPath, 'python.exe')
42+
after(function () {
43+
try {
44+
fs.unlinkSync(testFilePath)
45+
fs.rmdirSync(testFolderPath)
46+
} catch {}
47+
})
48+
49+
try {
50+
fs.symlinkSync(found, testFilePath)
51+
} catch (err) {
52+
switch (err.code) {
53+
case 'EPERM':
54+
return assert.fail(err, null, 'Please try to run console as an administrator')
55+
default:
56+
return assert.fail(err)
57+
}
58+
}
59+
60+
const finder = new PythonFinder(testFilePath)
61+
await assert.doesNotReject(finder.checkCommand(testFilePath))
62+
})
63+
3564
it('find python - python', async function () {
3665
const f = new TestPythonFinder('python')
3766
f.execFile = async function (program, args, opts) {

0 commit comments

Comments
 (0)