@@ -138,29 +138,37 @@ export class PythonShell extends EventEmitter{
138
138
139
139
[ 'stdout' , 'stdin' , 'stderr' ] . forEach ( function ( name ) {
140
140
self [ name ] = self . childProcess [ name ] ;
141
- self . parser && self [ name ] . setEncoding ( options . encoding || 'utf8' ) ;
141
+ self . parser && self [ name ] && self [ name ] . setEncoding ( options . encoding || 'utf8' ) ;
142
142
} ) ;
143
143
144
144
// parse incoming data on stdout
145
- if ( this . parser ) {
145
+ if ( this . parser && this . stdout ) {
146
146
this . stdout . on ( 'data' , this . receive . bind ( this ) ) ;
147
147
}
148
148
149
149
// listen to stderr and emit errors for incoming data
150
- this . stderr . on ( 'data' , function ( data ) {
151
- errorData += '' + data ;
152
- self . receiveStderr ( data ) ;
153
- } ) ;
150
+ if ( this . stderr ) {
151
+ this . stderr . on ( 'data' , function ( data ) {
152
+ errorData += '' + data ;
153
+ self . receiveStderr ( data ) ;
154
+ } ) ;
154
155
155
- this . stderr . on ( 'end' , function ( ) {
156
- self . stderrHasEnded = true
157
- terminateIfNeeded ( ) ;
158
- } )
156
+ this . stderr . on ( 'end' , function ( ) {
157
+ self . stderrHasEnded = true ;
158
+ terminateIfNeeded ( ) ;
159
+ } ) ;
160
+ } else {
161
+ self . stderrHasEnded = true ;
162
+ }
159
163
160
- this . stdout . on ( 'end' , function ( ) {
161
- self . stdoutHasEnded = true
162
- terminateIfNeeded ( ) ;
163
- } )
164
+ if ( this . stdout ) {
165
+ this . stdout . on ( 'end' , function ( ) {
166
+ self . stdoutHasEnded = true ;
167
+ terminateIfNeeded ( ) ;
168
+ } ) ;
169
+ } else {
170
+ self . stdoutHasEnded = true ;
171
+ }
164
172
165
173
this . childProcess . on ( 'exit' , function ( code , signal ) {
166
174
self . exitCode = code ;
@@ -339,6 +347,7 @@ export class PythonShell extends EventEmitter{
339
347
* @returns {PythonShell } The same instance for chaining calls
340
348
*/
341
349
send ( message :string | Object ) {
350
+ if ( ! this . stdin ) throw new Error ( "stdin not open for writting" ) ;
342
351
let data = this . formatter ? this . formatter ( message ) : message ;
343
352
if ( this . mode !== 'binary' ) data += newline ;
344
353
this . stdin . write ( data ) ;
@@ -352,7 +361,7 @@ export class PythonShell extends EventEmitter{
352
361
* @param {string|Buffer } data The data to parse into messages
353
362
*/
354
363
receive ( data :string | Buffer ) {
355
- return this . recieveInternal ( data , 'message' ) ;
364
+ return this . receiveInternal ( data , 'message' ) ;
356
365
} ;
357
366
358
367
/**
@@ -362,10 +371,10 @@ export class PythonShell extends EventEmitter{
362
371
* @param {string|Buffer } data The data to parse into messages
363
372
*/
364
373
receiveStderr ( data :string | Buffer ) {
365
- return this . recieveInternal ( data , 'stderr' ) ;
374
+ return this . receiveInternal ( data , 'stderr' ) ;
366
375
} ;
367
376
368
- private recieveInternal ( data :string | Buffer , emitType :'message' | 'stderr' ) {
377
+ private receiveInternal ( data :string | Buffer , emitType :'message' | 'stderr' ) {
369
378
let self = this ;
370
379
let parts = ( '' + data ) . split ( new RegExp ( newline , 'g' ) ) ;
371
380
@@ -394,7 +403,9 @@ export class PythonShell extends EventEmitter{
394
403
* @returns {PythonShell } The same instance for chaining calls
395
404
*/
396
405
end ( callback :( err :PythonShellError , exitCode :number , exitSignal :string ) => any ) {
397
- this . childProcess . stdin . end ( ) ;
406
+ if ( this . childProcess . stdin ) {
407
+ this . childProcess . stdin . end ( ) ;
408
+ }
398
409
this . _endCallback = callback ;
399
410
return this ;
400
411
} ;
0 commit comments