@@ -62,7 +62,7 @@ export interface Options extends SpawnOptions {
6262 args ?: string [ ]
6363}
6464
65- export class PythonShellError extends Error {
65+ export class PythonError extends Error {
6666 traceback : string | Buffer ;
6767 exitCode ?: number ;
6868}
@@ -90,7 +90,7 @@ export class PythonShell extends EventEmitter {
9090 private stderrHasEnded : boolean ;
9191 private stdoutHasEnded : boolean ;
9292 private _remaining : string
93- private _endCallback : ( err : PythonShellError , exitCode : number , exitSignal : string ) => any
93+ private _endCallback : ( err : PythonError , exitCode : number , exitSignal : string ) => any
9494
9595 // starting 2020 python2 is deprecated so we choose 3 as default
9696 static defaultPythonPath = process . platform != "win32" ? "python3" : "python" ;
@@ -178,6 +178,9 @@ export class PythonShell extends EventEmitter {
178178 self . stdoutHasEnded = true ;
179179 }
180180
181+ this . childProcess . on ( 'error' , function ( err : NodeJS . ErrnoException ) {
182+ self . emit ( 'error' , err ) ;
183+ } )
181184 this . childProcess . on ( 'exit' , function ( code , signal ) {
182185 self . exitCode = code ;
183186 self . exitSignal = signal ;
@@ -187,23 +190,23 @@ export class PythonShell extends EventEmitter {
187190 function terminateIfNeeded ( ) {
188191 if ( ! self . stderrHasEnded || ! self . stdoutHasEnded || ( self . exitCode == null && self . exitSignal == null ) ) return ;
189192
190- let err : PythonShellError ;
193+ let err : PythonError ;
191194 if ( self . exitCode && self . exitCode !== 0 ) {
192195 if ( errorData ) {
193196 err = self . parseError ( errorData ) ;
194197 } else {
195- err = new PythonShellError ( 'process exited with code ' + self . exitCode ) ;
198+ err = new PythonError ( 'process exited with code ' + self . exitCode ) ;
196199 }
197- err = < PythonShellError > extend ( err , {
200+ err = < PythonError > extend ( err , {
198201 executable : pythonPath ,
199202 options : pythonOptions . length ? pythonOptions : null ,
200203 script : self . scriptPath ,
201204 args : scriptArgs . length ? scriptArgs : null ,
202205 exitCode : self . exitCode
203206 } ) ;
204207 // do not emit error if only a callback is used
205- if ( self . listeners ( 'error ' ) . length || ! self . _endCallback ) {
206- self . emit ( 'error ' , err ) ;
208+ if ( self . listeners ( 'pythonError ' ) . length || ! self . _endCallback ) {
209+ self . emit ( 'pythonError ' , err ) ;
207210 }
208211 }
209212
@@ -270,7 +273,7 @@ export class PythonShell extends EventEmitter {
270273 * @param {Function } callback The callback function to invoke with the script results
271274 * @return {PythonShell } The PythonShell instance
272275 */
273- static run ( scriptPath : string , options ?: Options , callback ?: ( err ?: PythonShellError , output ?: any [ ] ) => any ) {
276+ static run ( scriptPath : string , options ?: Options , callback ?: ( err ?: PythonError , output ?: any [ ] ) => any ) {
274277 let pyshell = new PythonShell ( scriptPath , options ) ;
275278 let output = [ ] ;
276279
@@ -288,7 +291,7 @@ export class PythonShell extends EventEmitter {
288291 * @param {Function } callback The callback function to invoke with the script results
289292 * @return {PythonShell } The PythonShell instance
290293 */
291- static runString ( code : string , options ?: Options , callback ?: ( err : PythonShellError , output ?: any [ ] ) => any ) {
294+ static runString ( code : string , options ?: Options , callback ?: ( err : PythonError , output ?: any [ ] ) => any ) {
292295
293296 // put code in temp file
294297 const randomInt = getRandomInt ( ) ;
@@ -315,20 +318,20 @@ export class PythonShell extends EventEmitter {
315318 */
316319 private parseError ( data : string | Buffer ) {
317320 let text = '' + data ;
318- let error : PythonShellError ;
321+ let error : PythonError ;
319322
320323 if ( / ^ T r a c e b a c k / . test ( text ) ) {
321324 // traceback data is available
322325 let lines = text . trim ( ) . split ( newline ) ;
323326 let exception = lines . pop ( ) ;
324- error = new PythonShellError ( exception ) ;
327+ error = new PythonError ( exception ) ;
325328 error . traceback = data ;
326329 // extend stack trace
327330 error . stack += newline + ' ----- Python Traceback -----' + newline + ' ' ;
328331 error . stack += lines . slice ( 1 ) . join ( newline + ' ' ) ;
329332 } else {
330333 // otherwise, create a simpler error with stderr contents
331- error = new PythonShellError ( text ) ;
334+ error = new PythonError ( text ) ;
332335 }
333336
334337 return error ;
@@ -396,7 +399,7 @@ export class PythonShell extends EventEmitter {
396399 * this should cause the process to finish its work and close.
397400 * @returns {PythonShell } The same instance for chaining calls
398401 */
399- end ( callback : ( err : PythonShellError , exitCode : number , exitSignal : string ) => any ) {
402+ end ( callback : ( err : PythonError , exitCode : number , exitSignal : string ) => any ) {
400403 if ( this . childProcess . stdin ) {
401404 this . childProcess . stdin . end ( ) ;
402405 }
@@ -454,10 +457,17 @@ export interface PythonShell {
454457 prependListener ( event : "close" , listener : ( ) => void ) : this;
455458 prependOnceListener ( event : "close" , listener : ( ) => void ) : this;
456459
457- addListener ( event : "error" , listener : ( error : PythonShellError ) => void ) : this;
458- emit ( event : "error" , error : PythonShellError ) : boolean ;
459- on ( event : "error" , listener : ( error : PythonShellError ) => void ) : this;
460- once ( event : "error" , listener : ( error : PythonShellError ) => void ) : this;
461- prependListener ( event : "error" , listener : ( error : PythonShellError ) => void ) : this;
462- prependOnceListener ( event : "error" , listener : ( error : PythonShellError ) => void ) : this;
460+ addListener ( event : "error" , listener : ( error : NodeJS . ErrnoException ) => void ) : this;
461+ emit ( event : "error" , error : NodeJS . ErrnoException ) : boolean ;
462+ on ( event : "error" , listener : ( error : NodeJS . ErrnoException ) => void ) : this;
463+ once ( event : "error" , listener : ( error : NodeJS . ErrnoException ) => void ) : this;
464+ prependListener ( event : "error" , listener : ( error : NodeJS . ErrnoException ) => void ) : this;
465+ prependOnceListener ( event : "error" , listener : ( error : NodeJS . ErrnoException ) => void ) : this;
466+
467+ addListener ( event : "pythonError" , listener : ( error : PythonError ) => void ) : this;
468+ emit ( event : "pythonError" , error : PythonError ) : boolean ;
469+ on ( event : "pythonError" , listener : ( error : PythonError ) => void ) : this;
470+ once ( event : "pythonError" , listener : ( error : PythonError ) => void ) : this;
471+ prependListener ( event : "pythonError" , listener : ( error : PythonError ) => void ) : this;
472+ prependOnceListener ( event : "pythonError" , listener : ( error : PythonError ) => void ) : this;
463473}
0 commit comments