Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: print Python executable path using UTF-8 #2995

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/find-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class PythonFinder {
static findPython = (...args) => new PythonFinder(...args).findPython()

log = log.withPrefix('find Python')
argsExecutable = ['-c', 'import sys; print(sys.executable);']
argsExecutable = ['-c', 'import sys; sys.stdout.buffer.write(sys.executable.encode(\'utf-8\'));']
cclauss marked this conversation as resolved.
Show resolved Hide resolved
argsVersion = ['-c', 'import sys; print("%s.%s.%s" % sys.version_info[:3]);']
semverRange = '>=3.6.0'

Expand Down
31 changes: 30 additions & 1 deletion test/test-find-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

delete process.env.PYTHON

const { describe, it } = require('mocha')
const { describe, it, after } = require('mocha')
const assert = require('assert')
const PythonFinder = require('../lib/find-python')
const { execFile } = require('../lib/util')
const { poison } = require('./common')
const fs = require('fs')
const path = require('path')
const os = require('os')

class TestPythonFinder extends PythonFinder {
constructor (...args) {
Expand All @@ -32,6 +35,32 @@ describe('find-python', function () {
assert.strictEqual(stderr, '')
})

it('find python - encoding', async function () {
const found = await PythonFinder.findPython(null)
const testFolderPath = fs.mkdtempSync(path.join(os.tmpdir(), 'test-ü-'))
const testFilePath = path.join(testFolderPath, 'python.exe')
after(function () {
try {
fs.unlinkSync(testFilePath)
fs.rmdirSync(testFolderPath)
} catch {}
})

try {
fs.symlinkSync(found, testFilePath)
} catch (err) {
switch (err.code) {
case 'EPERM':
return assert.fail(err, null, 'Please try to run console as an administrator')
default:
return assert.fail(err)
}
}

const finder = new PythonFinder(testFilePath)
await assert.doesNotReject(finder.checkCommand(testFilePath))
})

it('find python - python', async function () {
const f = new TestPythonFinder('python')
f.execFile = async function (program, args, opts) {
Expand Down