File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ( ) {
Original file line number Diff line number Diff 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 ( ) {
You can’t perform that action at this time.
0 commit comments