Skip to content

Commit

Permalink
Try python launcher when stock python is python 3.
Browse files Browse the repository at this point in the history
Consult the python launcher when the python on the path is python 3.
If a python 2 exists on the system, it will tell us.

Fixes: #987
PR-URL: #992
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
  • Loading branch information
bnoordhuis committed Oct 8, 2016
1 parent e3778d9 commit 37ae7be
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ function PythonFinder(python, callback) {
}

PythonFinder.prototype = {
checkPythonLauncherDepth: 0,
env: process.env,
execFile: cp.execFile,
log: log,
Expand Down Expand Up @@ -386,6 +387,8 @@ PythonFinder.prototype = {
// executable for "execFile", we have to use the launcher to figure out
// where the actual "python.exe" executable is located.
checkPythonLauncher: function checkPythonLauncher () {
this.checkPythonLauncherDepth += 1

this.log.verbose(
'could not find "' + this.python + '". checking python launcher')
var env = extend({}, this.env)
Expand All @@ -395,13 +398,14 @@ PythonFinder.prototype = {
this.execFile('py.exe', launcherArgs, { env: env }, function (err, stdout) {
if (err) {
this.guessPython()
return
} else {
this.python = stdout.trim()
this.log.verbose('check python launcher',
'python executable found: %j',
this.python)
this.checkPythonVersion()
}
this.python = stdout.trim()
this.log.verbose('check python launcher',
'python executable found: %j',
this.python)
this.checkPythonVersion()
this.checkPythonLauncherDepth -= 1
}.bind(this))
},

Expand Down Expand Up @@ -435,6 +439,8 @@ PythonFinder.prototype = {
}
if (valid) {
this.callback(null, this.python)
} else if (this.win && this.checkPythonLauncherDepth === 0) {
this.checkPythonLauncher()
} else {
this.failPythonVersion(version)
}
Expand Down
71 changes: 71 additions & 0 deletions test/test-find-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,77 @@ test('find python - no python, use python launcher', function (t) {
}
})

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

var f = new TestPythonFinder('python', done)
f.env = {}
f.win = true

f.which = function(program, cb) {
t.strictEqual(program, 'python')
cb(null, program)
}
f.execFile = function(program, args, opts, cb) {
f.execFile = function(program, args, opts, cb) {
f.execFile = function(program, args, opts, cb) {
t.strictEqual(program, 'Z:\\snake.exe')
t.ok(/import platform/.test(args[1]))
cb(null, '2.7.0')
}
t.strictEqual(program, 'py.exe')
t.notEqual(args.indexOf('-2'), -1)
t.notEqual(args.indexOf('-c'), -1)
cb(null, 'Z:\\snake.exe')
}
t.strictEqual(program, 'python')
t.ok(/import platform/.test(args[1]))
cb(null, '3.0.0')
}
f.checkPython()

function done(err, python) {
t.strictEqual(err, null)
t.strictEqual(python, 'Z:\\snake.exe')
}
})

test('find python - python 3, use python launcher, python 2 too old',
function (t) {
t.plan(9)

var f = new TestPythonFinder('python', done)
f.checkedPythonLauncher = false
f.env = {}
f.win = true

f.which = function(program, cb) {
t.strictEqual(program, 'python')
cb(null, program)
}
f.execFile = function(program, args, opts, cb) {
f.execFile = function(program, args, opts, cb) {
f.execFile = function(program, args, opts, cb) {
t.strictEqual(program, 'Z:\\snake.exe')
t.ok(/import platform/.test(args[1]))
cb(null, '2.3.4')
}
t.strictEqual(program, 'py.exe')
t.notEqual(args.indexOf('-2'), -1)
t.notEqual(args.indexOf('-c'), -1)
cb(null, 'Z:\\snake.exe')
}
t.strictEqual(program, 'python')
t.ok(/import platform/.test(args[1]))
cb(null, '3.0.0')
}
f.checkPython()

function done(err, python) {
t.ok(/is not supported by gyp/.test(err))
}
})

test('find python - no python, no python launcher, good guess', function (t) {
t.plan(6)

Expand Down

0 comments on commit 37ae7be

Please sign in to comment.