Skip to content

Commit 8a7f97b

Browse files
authored
Merge pull request #1 from cmaddalozzo/feature/stderr-buffer-size-config
Truncate the size of stderr buffer according to config
2 parents 0daee5f + c2d8cda commit 8a7f97b

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ var PythonShell = function (script, options) {
5353
this.script = path.join(options.scriptPath || './', script);
5454
this.command = pythonOptions.concat(this.script, scriptArgs);
5555
this.mode = options.mode || 'text';
56+
this.stdErrMaxBufferSize = options.stdErrMaxBufferSize || Infinity;
5657
this.formatter = resolve('format', options.formatter || this.mode);
5758
this.parser = resolve('parse', options.parser || this.mode);
5859
this.terminated = false;
@@ -70,7 +71,11 @@ var PythonShell = function (script, options) {
7071

7172
// listen to stderr and emit errors for incoming data
7273
this.stderr.on('data', function (data) {
73-
errorData += ''+data;
74+
var tmp = errorData + data;
75+
if (tmp.length >= self.stdErrMaxBufferSize) {
76+
tmp = tmp.substring(tmp.length - self.stdErrMaxBufferSize, tmp.length);
77+
}
78+
errorData = tmp;
7479
});
7580

7681
this.stderr.on('end', function(){

test/test-python-shell.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ describe('PythonShell', function () {
265265
done();
266266
});
267267
});
268+
it('should truncate stderr when it\'s longer than stdErrMaxBufferSize', function (done) {
269+
var pyshell = new PythonShell('error.py', { stdErrMaxBufferSize: 128 });
270+
pyshell.on('error', function (err) {
271+
err.message.should.have.lengthOf(128);
272+
done();
273+
});
274+
});
268275
});
269276

270277
describe('.parseError(data)', function () {

0 commit comments

Comments
 (0)