Skip to content

Commit 2c2b7e4

Browse files
committed
Fixed bugs and updated tests. All tests should now pass.
1 parent 0ed5f54 commit 2c2b7e4

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

index.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,38 @@ var PythonShell = function (script, options) {
7373
errorData += ''+data;
7474
});
7575

76-
this.childProcess.on('exit', function (code, signal) {
76+
this.stderr.on('end', function(){
77+
self.stderrHasEnded = true
78+
terminateIfNeeded();
79+
})
80+
81+
this.stdout.on('end', function(){
82+
self.stdoutHasEnded = true
83+
terminateIfNeeded();
84+
})
85+
86+
this.childProcess.on('exit', function (code,signal) {
87+
self.exitCode = code;
88+
self.exitSignal = signal;
89+
terminateIfNeeded();
90+
});
91+
92+
function terminateIfNeeded() {
93+
if(!self.stderrHasEnded || !self.stdoutHasEnded || (self.exitCode == null && self.exitSignal == null)) return;
94+
7795
var err;
78-
if (errorData || (code && code !== 0)) {
96+
if (errorData || (self.exitCode && self.exitCode !== 0)) {
7997
if (errorData) {
8098
err = self.parseError(errorData);
8199
} else {
82-
err = new Error('process exited with code ' + code);
100+
err = new Error('process exited with code ' + self.exitCode);
83101
}
84102
err = extend(err, {
85103
executable: pythonPath,
86104
options: pythonOptions.length ? pythonOptions : null,
87105
script: self.script,
88106
args: scriptArgs.length ? scriptArgs : null,
89-
exitCode: code
107+
exitCode: self.exitCode
90108
});
91109
// do not emit error if only a callback is used
92110
if (self.listeners('error').length || !self._endCallback) {
@@ -96,8 +114,8 @@ var PythonShell = function (script, options) {
96114

97115
self.terminated = true;
98116
self.emit('close');
99-
self._endCallback && self._endCallback(err,self.exitCode,signal);
100-
});
117+
self._endCallback && self._endCallback(err,self.exitCode,self.exitSignal);
118+
};
101119
};
102120
util.inherits(PythonShell, EventEmitter);
103121

test/test-python-shell.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ describe('PythonShell', function () {
238238
describe('.end(callback)', function () {
239239
it('should end normally when exit code is zero', function (done) {
240240
var pyshell = new PythonShell('exit-code.py');
241-
pyshell.end(function (err) {
241+
pyshell.end(function (err,code,signal) {
242242
if (err) return done(err);
243-
pyshell.exitCode.should.be.exactly(0);
243+
code.should.be.exactly(0);
244244
done();
245245
});
246246
});
@@ -292,7 +292,7 @@ describe('PythonShell', function () {
292292
it('set terminated to true', function (done) {
293293
var pyshell = new PythonShell('infinite_loop.py');
294294
pyshell.terminate();
295-
pyshell.terminated.should.be.true;
295+
pyshell.terminated.should.be.true
296296
done();
297297
});
298298
it('run the end callback if specified', function (done) {
@@ -302,7 +302,18 @@ describe('PythonShell', function () {
302302
endCalled = true;
303303
})
304304
pyshell.terminate();
305-
endCalled.should.be.true;
305+
pyshell.terminated.should.be.true
306+
done();
307+
});
308+
it('terminate with correct kill signal', function (done) {
309+
var pyshell = new PythonShell('infinite_loop.py');
310+
var endCalled = false;
311+
pyshell.end(()=>{
312+
endCalled = true;
313+
})
314+
pyshell.terminate('SIGKILL');
315+
pyshell.terminated.should.be.true;
316+
setTimeout(()=>{pyshell.exitSignal.should.be.exactly('SIGKILL');},500);
306317
done();
307318
});
308319
});

0 commit comments

Comments
 (0)