Skip to content

Commit 89c9b46

Browse files
committed
Merge pull request #29 from iamdoron/master
fix #25
2 parents d5fc8e6 + 20946c7 commit 89c9b46

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

index.js

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

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+
7686
this.childProcess.on('exit', function (code) {
87+
self.exitCode = code;
88+
terminateIfNeeded();
89+
});
90+
91+
function terminateIfNeeded() {
92+
if (!self.stderrHasEnded || !self.stdoutHasEnded || self.exitCode == null) {
93+
return;
94+
}
7795
var err;
78-
if (errorData || code !== 0) {
96+
if (errorData || 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) {
93111
self.emit('error', err);
94112
}
95113
}
96-
self.exitCode = code;
97114
self.terminated = true;
98115
self.emit('close');
99116
self._endCallback && self._endCallback(err);
100-
});
117+
}
101118
};
102119
util.inherits(PythonShell, EventEmitter);
103120

test/test-python-shell.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,52 @@ describe('PythonShell', function () {
6060
done();
6161
});
6262
});
63+
it('should run multiple scripts and fail with an extended stack trace for each of them', function (done) {
64+
var numberOfTimesToRun = 20;
65+
for (var i = 0; i < numberOfTimesToRun; i++) {
66+
runSingleErrorScript(end);
67+
}
68+
var count = 0;
69+
function end() {
70+
count++;
71+
if (count === numberOfTimesToRun) {
72+
done();
73+
}
74+
}
75+
function runSingleErrorScript(callback) {
76+
PythonShell.run('error.py', function (err, results) {
77+
err.should.be.an.Error;
78+
err.exitCode.should.be.exactly(1);
79+
err.stack.should.containEql('----- Python Traceback -----');
80+
callback();
81+
});
82+
}
83+
});
84+
85+
it('should run multiple scripts and return output data for each of them', function (done) {
86+
var numberOfTimesToRun = 20;
87+
for (var i = 0; i < numberOfTimesToRun; i++) {
88+
runSingleScript(end);
89+
}
90+
var count = 0;
91+
function end() {
92+
count++;
93+
if (count === numberOfTimesToRun) {
94+
done();
95+
}
96+
}
97+
function runSingleScript(callback) {
98+
PythonShell.run('echo_args.py', {
99+
args: ['hello', 'world']
100+
}, function (err, results) {
101+
if (err) return done(err);
102+
results.should.be.an.Array.and.have.lengthOf(2);
103+
results.should.eql(['hello', 'world']);
104+
callback();
105+
});
106+
}
107+
108+
});
63109
});
64110

65111
describe('.send(message)', function () {

0 commit comments

Comments
 (0)