Skip to content

Commit 37ae7be

Browse files
committed
Try python launcher when stock python is python 3.
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 <[email protected]>
1 parent e3778d9 commit 37ae7be

File tree

2 files changed

+83
-6
lines changed

2 files changed

+83
-6
lines changed

lib/configure.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ function PythonFinder(python, callback) {
343343
}
344344

345345
PythonFinder.prototype = {
346+
checkPythonLauncherDepth: 0,
346347
env: process.env,
347348
execFile: cp.execFile,
348349
log: log,
@@ -386,6 +387,8 @@ PythonFinder.prototype = {
386387
// executable for "execFile", we have to use the launcher to figure out
387388
// where the actual "python.exe" executable is located.
388389
checkPythonLauncher: function checkPythonLauncher () {
390+
this.checkPythonLauncherDepth += 1
391+
389392
this.log.verbose(
390393
'could not find "' + this.python + '". checking python launcher')
391394
var env = extend({}, this.env)
@@ -395,13 +398,14 @@ PythonFinder.prototype = {
395398
this.execFile('py.exe', launcherArgs, { env: env }, function (err, stdout) {
396399
if (err) {
397400
this.guessPython()
398-
return
401+
} else {
402+
this.python = stdout.trim()
403+
this.log.verbose('check python launcher',
404+
'python executable found: %j',
405+
this.python)
406+
this.checkPythonVersion()
399407
}
400-
this.python = stdout.trim()
401-
this.log.verbose('check python launcher',
402-
'python executable found: %j',
403-
this.python)
404-
this.checkPythonVersion()
408+
this.checkPythonLauncherDepth -= 1
405409
}.bind(this))
406410
},
407411

@@ -435,6 +439,8 @@ PythonFinder.prototype = {
435439
}
436440
if (valid) {
437441
this.callback(null, this.python)
442+
} else if (this.win && this.checkPythonLauncherDepth === 0) {
443+
this.checkPythonLauncher()
438444
} else {
439445
this.failPythonVersion(version)
440446
}

test/test-find-python.js

+71
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,77 @@ test('find python - no python, use python launcher', function (t) {
195195
}
196196
})
197197

198+
test('find python - python 3, use python launcher', function (t) {
199+
t.plan(10)
200+
201+
var f = new TestPythonFinder('python', done)
202+
f.env = {}
203+
f.win = true
204+
205+
f.which = function(program, cb) {
206+
t.strictEqual(program, 'python')
207+
cb(null, program)
208+
}
209+
f.execFile = function(program, args, opts, cb) {
210+
f.execFile = function(program, args, opts, cb) {
211+
f.execFile = function(program, args, opts, cb) {
212+
t.strictEqual(program, 'Z:\\snake.exe')
213+
t.ok(/import platform/.test(args[1]))
214+
cb(null, '2.7.0')
215+
}
216+
t.strictEqual(program, 'py.exe')
217+
t.notEqual(args.indexOf('-2'), -1)
218+
t.notEqual(args.indexOf('-c'), -1)
219+
cb(null, 'Z:\\snake.exe')
220+
}
221+
t.strictEqual(program, 'python')
222+
t.ok(/import platform/.test(args[1]))
223+
cb(null, '3.0.0')
224+
}
225+
f.checkPython()
226+
227+
function done(err, python) {
228+
t.strictEqual(err, null)
229+
t.strictEqual(python, 'Z:\\snake.exe')
230+
}
231+
})
232+
233+
test('find python - python 3, use python launcher, python 2 too old',
234+
function (t) {
235+
t.plan(9)
236+
237+
var f = new TestPythonFinder('python', done)
238+
f.checkedPythonLauncher = false
239+
f.env = {}
240+
f.win = true
241+
242+
f.which = function(program, cb) {
243+
t.strictEqual(program, 'python')
244+
cb(null, program)
245+
}
246+
f.execFile = function(program, args, opts, cb) {
247+
f.execFile = function(program, args, opts, cb) {
248+
f.execFile = function(program, args, opts, cb) {
249+
t.strictEqual(program, 'Z:\\snake.exe')
250+
t.ok(/import platform/.test(args[1]))
251+
cb(null, '2.3.4')
252+
}
253+
t.strictEqual(program, 'py.exe')
254+
t.notEqual(args.indexOf('-2'), -1)
255+
t.notEqual(args.indexOf('-c'), -1)
256+
cb(null, 'Z:\\snake.exe')
257+
}
258+
t.strictEqual(program, 'python')
259+
t.ok(/import platform/.test(args[1]))
260+
cb(null, '3.0.0')
261+
}
262+
f.checkPython()
263+
264+
function done(err, python) {
265+
t.ok(/is not supported by gyp/.test(err))
266+
}
267+
})
268+
198269
test('find python - no python, no python launcher, good guess', function (t) {
199270
t.plan(6)
200271

0 commit comments

Comments
 (0)