| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5f526b7 commit 96dfae8
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -323,10 +323,19 @@ shell.on('stderr', function (stderr) { | |||
| 323 | 323 | ||
| 324 | 324 | Fires when the process has been terminated, with an error or not. | |
| 325 | 325 | ||
| 326 | - #### event: `error` | ||
| 326 | + #### event: `pythonError` | ||
| 327 | 327 | ||
| 328 | 328 | Fires when the process terminates with a non-zero exit code. | |
| 329 | 329 | ||
| 330 | + #### event: `error` | ||
| 331 | + | ||
| 332 | + Fires when: | ||
| 333 | + * The process could not be spawned, or | ||
| 334 | + * The process could not be killed, or | ||
| 335 | + * Sending a message to the child process failed. | ||
| 336 | + | ||
| 337 | + If the process could not be spawned please double-check that python can be launched from the terminal. | ||
| 338 | + | ||
| 330 | 339 | ## Used By: | |
| 331 | 340 | ||
| 332 | 341 | Python-Shell is used by [arepl-vscode](https://github.com/almenon/arepl-vscode), [gitinspector](https://github.com/ejwa/gitinspector), [pyspreadsheet](https://github.com/extrabacon/pyspreadsheet), [AtlantOS Ocean Data QC](https://github.com/ocean-data-qc/ocean-data-qc) and more! | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,7 +62,7 @@ export interface Options extends SpawnOptions { | |||
| 62 | 62 | args?: string[] | |
| 63 | 63 | } | |
| 64 | 64 | ||
| 65 | - export class PythonShellError extends Error { | ||
| 65 | + export class PythonError extends Error { | ||
| 66 | 66 | traceback: string | Buffer; | |
| 67 | 67 | exitCode?: number; | |
| 68 | 68 | } | |
@@ -90,7 +90,7 @@ export class PythonShell extends EventEmitter { | |||
| 90 | 90 | private stderrHasEnded: boolean; | |
| 91 | 91 | private stdoutHasEnded: boolean; | |
| 92 | 92 | private _remaining: string | |
| 93 | - private _endCallback: (err: PythonShellError, exitCode: number, exitSignal: string) => any | ||
| 93 | + private _endCallback: (err: PythonError, exitCode: number, exitSignal: string) => any | ||
| 94 | 94 | ||
| 95 | 95 | // starting 2020 python2 is deprecated so we choose 3 as default | |
| 96 | 96 | static defaultPythonPath = process.platform != "win32" ? "python3" : "python"; | |
@@ -178,6 +178,9 @@ export class PythonShell extends EventEmitter { | |||
| 178 | 178 | self.stdoutHasEnded = true; | |
| 179 | 179 | } | |
| 180 | 180 | ||
| 181 | + this.childProcess.on('error', function(err: NodeJS.ErrnoException){ | ||
| 182 | + self.emit('error', err); | ||
| 183 | + }) | ||
| 181 | 184 | this.childProcess.on('exit', function (code, signal) { | |
| 182 | 185 | self.exitCode = code; | |
| 183 | 186 | self.exitSignal = signal; | |
@@ -187,23 +190,23 @@ export class PythonShell extends EventEmitter { | |||
| 187 | 190 | function terminateIfNeeded() { | |
| 188 | 191 | if (!self.stderrHasEnded || !self.stdoutHasEnded || (self.exitCode == null && self.exitSignal == null)) return; | |
| 189 | 192 | ||
| 190 | - let err: PythonShellError; | ||
| 193 | + let err: PythonError; | ||
| 191 | 194 | if (self.exitCode && self.exitCode !== 0) { | |
| 192 | 195 | if (errorData) { | |
| 193 | 196 | err = self.parseError(errorData); | |
| 194 | 197 | } else { | |
| 195 | - err = new PythonShellError('process exited with code ' + self.exitCode); | ||
| 198 | + err = new PythonError('process exited with code ' + self.exitCode); | ||
| 196 | 199 | } | |
| 197 | - err = <PythonShellError>extend(err, { | ||
| 200 | + err = <PythonError>extend(err, { | ||
| 198 | 201 | executable: pythonPath, | |
| 199 | 202 | options: pythonOptions.length ? pythonOptions : null, | |
| 200 | 203 | script: self.scriptPath, | |
| 201 | 204 | args: scriptArgs.length ? scriptArgs : null, | |
| 202 | 205 | exitCode: self.exitCode | |
| 203 | 206 | }); | |
| 204 | 207 | // 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); | ||
| 207 | 210 | } | |
| 208 | 211 | } | |
| 209 | 212 | ||
@@ -270,7 +273,7 @@ export class PythonShell extends EventEmitter { | |||
| 270 | 273 | * @param {Function} callback The callback function to invoke with the script results | |
| 271 | 274 | * @return {PythonShell} The PythonShell instance | |
| 272 | 275 | */ | |
| 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) { | ||
| 274 | 277 | let pyshell = new PythonShell(scriptPath, options); | |
| 275 | 278 | let output = []; | |
| 276 | 279 | ||
@@ -288,7 +291,7 @@ export class PythonShell extends EventEmitter { | |||
| 288 | 291 | * @param {Function} callback The callback function to invoke with the script results | |
| 289 | 292 | * @return {PythonShell} The PythonShell instance | |
| 290 | 293 | */ | |
| 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) { | ||
| 292 | 295 | ||
| 293 | 296 | // put code in temp file | |
| 294 | 297 | const randomInt = getRandomInt(); | |
@@ -315,20 +318,20 @@ export class PythonShell extends EventEmitter { | |||
| 315 | 318 | */ | |
| 316 | 319 | private parseError(data: string | Buffer) { | |
| 317 | 320 | let text = '' + data; | |
| 318 | - let error: PythonShellError; | ||
| 321 | + let error: PythonError; | ||
| 319 | 322 | ||
| 320 | 323 | if (/^Traceback/.test(text)) { | |
| 321 | 324 | // traceback data is available | |
| 322 | 325 | let lines = text.trim().split(newline); | |
| 323 | 326 | let exception = lines.pop(); | |
| 324 | - error = new PythonShellError(exception); | ||
| 327 | + error = new PythonError(exception); | ||
| 325 | 328 | error.traceback = data; | |
| 326 | 329 | // extend stack trace | |
| 327 | 330 | error.stack += newline + ' ----- Python Traceback -----' + newline + ' '; | |
| 328 | 331 | error.stack += lines.slice(1).join(newline + ' '); | |
| 329 | 332 | } else { | |
| 330 | 333 | // otherwise, create a simpler error with stderr contents | |
| 331 | - error = new PythonShellError(text); | ||
| 334 | + error = new PythonError(text); | ||
| 332 | 335 | } | |
| 333 | 336 | ||
| 334 | 337 | return error; | |
@@ -396,7 +399,7 @@ export class PythonShell extends EventEmitter { | |||
| 396 | 399 | * this should cause the process to finish its work and close. | |
| 397 | 400 | * @returns {PythonShell} The same instance for chaining calls | |
| 398 | 401 | */ | |
| 399 | - end(callback: (err: PythonShellError, exitCode: number, exitSignal: string) => any) { | ||
| 402 | + end(callback: (err: PythonError, exitCode: number, exitSignal: string) => any) { | ||
| 400 | 403 | if (this.childProcess.stdin) { | |
| 401 | 404 | this.childProcess.stdin.end(); | |
| 402 | 405 | } | |
@@ -454,10 +457,17 @@ export interface PythonShell { | |||
| 454 | 457 | prependListener(event: "close", listener: () => void): this; | |
| 455 | 458 | prependOnceListener(event: "close", listener: () => void): this; | |
| 456 | 459 | ||
| 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; | ||
| 463 | 473 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,7 +59,7 @@ describe('PythonShell', function () { | |||
| 59 | 59 | let pyshell = new PythonShell('exit-code.py', { | |
| 60 | 60 | pythonPath: 'foeisjofseij' | |
| 61 | 61 | }, ); | |
| 62 | - pyshell.on('error', (err: NodeJS.ErrnoException)=>{ | ||
| 62 | + pyshell.on('error', (err)=>{ | ||
| 63 | 63 | err.code.should.eql('ENOENT') | |
| 64 | 64 | done() | |
| 65 | 65 | }) | |
@@ -450,7 +450,7 @@ describe('PythonShell', function () { | |||
| 450 | 450 | let pyshell = new PythonShell('exit-code.py', { | |
| 451 | 451 | args: ['3'] | |
| 452 | 452 | }); | |
| 453 | - pyshell.on('error', function (err) { | ||
| 453 | + pyshell.on('pythonError', function (err) { | ||
| 454 | 454 | err.should.have.properties({ | |
| 455 | 455 | message: 'process exited with code 3', | |
| 456 | 456 | exitCode: 3 | |
@@ -460,7 +460,7 @@ describe('PythonShell', function () { | |||
| 460 | 460 | }); | |
| 461 | 461 | it('should emit error when the program exits because of an unhandled exception', function (done) { | |
| 462 | 462 | let pyshell = new PythonShell('error.py'); | |
| 463 | - pyshell.on('error', function (err) { | ||
| 463 | + pyshell.on('pythonError', function (err) { | ||
| 464 | 464 | err.message.should.be.equalOneOf('ZeroDivisionError: integer division or modulo by zero','ZeroDivisionError: division by zero'); | |
| 465 | 465 | err.should.have.property('traceback'); | |
| 466 | 466 | err.traceback.should.containEql('Traceback (most recent call last)'); | |
@@ -469,7 +469,7 @@ describe('PythonShell', function () { | |||
| 469 | 469 | }); | |
| 470 | 470 | it('should NOT emit error when logging is written to stderr', function (done) { | |
| 471 | 471 | let pyshell = new PythonShell('stderrLogging.py'); | |
| 472 | - pyshell.on('error', function (err) { | ||
| 472 | + pyshell.on('pythonError', function (err) { | ||
| 473 | 473 | done(new Error("an error should not have been raised")); | |
| 474 | 474 | }); | |
| 475 | 475 | pyshell.on('close', function(){ | |
@@ -483,14 +483,14 @@ describe('PythonShell', function () { | |||
| 483 | 483 | let pyshell = new PythonShell('exit-code.py', { | |
| 484 | 484 | args: ['1'] | |
| 485 | 485 | }); | |
| 486 | - pyshell.on('error', function (err) { | ||
| 486 | + pyshell.on('pythonError', function (err) { | ||
| 487 | 487 | err.should.have.properties(['exitCode', 'script', 'options', 'args']); | |
| 488 | 488 | done(); | |
| 489 | 489 | }); | |
| 490 | 490 | }); | |
| 491 | 491 | it('should extend err.stack with traceback', function (done) { | |
| 492 | 492 | let pyshell = new PythonShell('error.py'); | |
| 493 | - pyshell.on('error', function (err) { | ||
| 493 | + pyshell.on('pythonError', function (err) { | ||
| 494 | 494 | err.stack.should.containEql('----- Python Traceback -----'); | |
| 495 | 495 | err.stack.should.containEql('File "test' + sep + 'python' + sep + 'error.py", line 4'); | |
| 496 | 496 | err.stack.should.containEql('File "test' + sep + 'python' + sep + 'error.py", line 6'); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments