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: drop Python 2 support in find-python.js #2333

Merged
merged 15 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
36 changes: 25 additions & 11 deletions lib/find-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,30 @@ PythonFinder.prototype = {
log: logWithPrefix(log, 'find Python'),
argsExecutable: ['-c', 'import sys; print(sys.executable);'],
argsVersion: ['-c', 'import sys; print("%s.%s.%s" % sys.version_info[:3]);'],
semverRange: '2.7.x || >=3.5.0',
semverRange: '>=3.6.0',

// These can be overridden for testing:
execFile: cp.execFile,
env: process.env,
win: win,
pyLauncher: 'py.exe',
winDefaultLocations: [
path.join(process.env.SystemDrive || 'C:', 'Python37', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Python27', 'python.exe')
path.join(process.env.SystemDrive || 'C:', 'Users', process.env.USERNAME || process.env.USER, 'AppData', 'Local', 'Programs', 'Python', 'Python39', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Program Files', 'Python39', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Users', process.env.USERNAME || process.env.USER, 'AppData', 'Local', 'Programs', 'Python', 'Python39-32', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Program Files (x86)', 'Python39', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Users', process.env.USERNAME || process.env.USER, 'AppData', 'Local', 'Programs', 'Python', 'Python38', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Program Files', 'Python38', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Users', process.env.USERNAME || process.env.USER, 'AppData', 'Local', 'Programs', 'Python', 'Python38-32', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Program Files (x86)', 'Python38', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Users', process.env.USERNAME || process.env.USER, 'AppData', 'Local', 'Programs', 'Python', 'Python37', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Program Files', 'Python37', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Users', process.env.USERNAME || process.env.USER, 'AppData', 'Local', 'Programs', 'Python', 'Python37-32', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Program Files (x86)', 'Python37', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Users', process.env.USERNAME || process.env.USER, 'AppData', 'Local', 'Programs', 'Python', 'Python36', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Program Files', 'Python36', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Users', process.env.USERNAME || process.env.USER, 'AppData', 'Local', 'Programs', 'Python', 'Python36-32', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Program Files (x86)', 'Python36', 'python.exe')
cclauss marked this conversation as resolved.
Show resolved Hide resolved
],

// Logs a message at verbose level, but also saves it to be displayed later
Expand Down Expand Up @@ -96,11 +110,6 @@ PythonFinder.prototype = {
before: () => { this.addLog('checking if "python" can be used') },
check: this.checkCommand,
arg: 'python'
},
{
before: () => { this.addLog('checking if "python2" can be used') },
check: this.checkCommand,
arg: 'python2'
}
]

Expand All @@ -119,7 +128,7 @@ PythonFinder.prototype = {
checks.push({
before: () => {
this.addLog(
'checking if the py launcher can be used to find Python')
'checking if the py launcher can be used to find Python 3')
},
check: this.checkPyLauncher
})
Expand Down Expand Up @@ -188,10 +197,15 @@ PythonFinder.prototype = {
// Distributions of Python on Windows by default install with the "py.exe"
// Python launcher which is more likely to exist than the Python executable
// being in the $PATH.
// Because the Python launcher supports Python 2 and Python 3, we should
// explicitly request a Python 3 version. This is done by supplying "-3" as
// the first command line argument. Since "py.exe -3" would be an invalid
// executable for "execFile", we have to use the launcher to figure out
// where the actual "python.exe" executable is located.
checkPyLauncher: function checkPyLauncher (errorCallback) {
this.log.verbose(
`- executing "${this.pyLauncher}" to get Python executable path`)
this.run(this.pyLauncher, this.argsExecutable, false,
`- executing "${this.pyLauncher}" to get Python 3 executable path`)
this.run(this.pyLauncher, ['-3', ...this.argsExecutable], false,
function (err, execPath) {
// Possible outcomes: same as checkCommand
if (err) {
Expand Down
25 changes: 11 additions & 14 deletions test/test-find-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ const PythonFinder = findPython.test.PythonFinder
require('npmlog').level = 'warn'

test('find python', function (t) {
t.plan(4)
t.plan(5)

findPython.test.findPython(null, function (err, found) {
t.strictEqual(err, null)
var proc = execFile(found, ['-V'], function (err, stdout, stderr) {
t.strictEqual(err, null)
if (/Python 2/.test(stderr)) {
t.strictEqual(stdout, '')
t.ok(/Python 2/.test(stderr))
} else {
t.ok(/Python 3/.test(stdout))
t.strictEqual(stderr, '')
}
t.notOk(/Python 2/.test(stderr))
t.ok(/Python 3/.test(stdout))
t.strictEqual(stderr, '')
DeeDeeG marked this conversation as resolved.
Show resolved Hide resolved
})
proc.stdout.setEncoding('utf-8')
proc.stderr.setEncoding('utf-8')
Expand Down Expand Up @@ -66,7 +62,7 @@ test('find python - python', function (t) {
poison(f, 'execFile')
t.strictEqual(program, '/path/python')
t.ok(/sys\.version_info/.test(args[1]))
cb(null, '2.7.15')
cb(null, '3.9.1')
}
t.strictEqual(program,
process.platform === 'win32' ? '"python"' : 'python')
Expand Down Expand Up @@ -146,13 +142,14 @@ test('find python - no python2, no python, unix', function (t) {
})

test('find python - no python, use python launcher', function (t) {
t.plan(3)
t.plan(4)

var f = new TestPythonFinder(null, done)
f.win = true

f.execFile = function (program, args, opts, cb) {
if (program === 'py.exe') {
t.notEqual(args.indexOf('-3'), -1)
t.notEqual(args.indexOf('-c'), -1)
return cb(null, 'Z:\\snake.exe')
}
Expand All @@ -162,7 +159,7 @@ test('find python - no python, use python launcher', function (t) {
cb(new Error('not found'))
} else if (/sys\.version_info/.test(args[args.length - 1])) {
if (program === 'Z:\\snake.exe') {
cb(null, '2.7.14')
cb(null, '3.9.0')
} else {
t.fail()
}
Expand All @@ -181,17 +178,17 @@ test('find python - no python, use python launcher', function (t) {
test('find python - no python, no python launcher, good guess', function (t) {
t.plan(2)

var re = /C:[\\/]Python37[\\/]python[.]exe/
var f = new TestPythonFinder(null, done)
f.win = true
const expectedProgram = f.winDefaultLocations[0]

f.execFile = function (program, args, opts, cb) {
if (program === 'py.exe') {
return cb(new Error('not found'))
}
if (/sys\.executable/.test(args[args.length - 1])) {
cb(new Error('not found'))
} else if (re.test(program) &&
} else if (program === expectedProgram &&
/sys\.version_info/.test(args[args.length - 1])) {
cb(null, '3.7.3')
} else {
Expand All @@ -202,7 +199,7 @@ test('find python - no python, no python launcher, good guess', function (t) {

function done (err, python) {
t.strictEqual(err, null)
t.ok(re.test(python))
t.ok(python === expectedProgram)
}
})

Expand Down