| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,12 @@ | |||
| 1 | + ## [5.0.0] - 2023-02-10 | ||
| 2 | + ### BREAKING CHANGES | ||
| 3 | + - run and runString now return a promise instead of a using a callback. | ||
| 4 | + - You will need to 1) check the return value, 2) remove the callback argument, and 3) change to a promise | ||
| 5 | + - see readme for usage examples | ||
| 6 | + | ||
| 7 | + ### Other notes | ||
| 8 | + - I confirmed that python-shell works with python 3.11 and node v18. | ||
| 9 | + | ||
| 1 | 10 | ## [4.0.0] - 2023-02-10 | |
| 2 | 11 | ### Changed | |
| 3 | 12 | - run and runString now return a promise instead of a using a callback. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,8 +27,7 @@ npm install python-shell | |||
| 27 | 27 | ```typescript | |
| 28 | 28 | import {PythonShell} from 'python-shell'; | |
| 29 | 29 | ||
| 30 | - PythonShell.runString('x=1+1;print(x)', null, function (err) { | ||
| 31 | - if (err) throw err; | ||
| 30 | + PythonShell.runString('x=1+1;print(x)', null).then(messages=>{ | ||
| 32 | 31 | console.log('finished'); | |
| 33 | 32 | }); | |
| 34 | 33 | ``` | |
@@ -47,8 +46,7 @@ let {PythonShell} = require('python-shell') | |||
| 47 | 46 | ```typescript | |
| 48 | 47 | import {PythonShell} from 'python-shell'; | |
| 49 | 48 | ||
| 50 | - PythonShell.run('my_script.py', null, function (err) { | ||
| 51 | - if (err) throw err; | ||
| 49 | + PythonShell.run('my_script.py', null).then(messages=>{ | ||
| 52 | 50 | console.log('finished'); | |
| 53 | 51 | }); | |
| 54 | 52 | ``` | |
@@ -68,8 +66,7 @@ let options = { | |||
| 68 | 66 | args: ['value1', 'value2', 'value3'] | |
| 69 | 67 | }; | |
| 70 | 68 | ||
| 71 | - PythonShell.run('my_script.py', options, function (err, results) { | ||
| 72 | - if (err) throw err; | ||
| 69 | + PythonShell.run('my_script.py', options).then(messages=>{ | ||
| 73 | 70 | // results is an array consisting of messages collected during execution | |
| 74 | 71 | console.log('results: %j', results); | |
| 75 | 72 | }); | |
@@ -91,7 +88,6 @@ pyshell.on('message', function (message) { | |||
| 91 | 88 | ||
| 92 | 89 | // end the input stream and allow the process to exit | |
| 93 | 90 | pyshell.end(function (err,code,signal) { | |
| 94 | - if (err) throw err; | ||
| 95 | 91 | console.log('The exit code was: ' + code); | |
| 96 | 92 | console.log('The exit signal was: ' + signal); | |
| 97 | 93 | console.log('finished'); | |
@@ -205,32 +201,28 @@ Example: | |||
| 205 | 201 | PythonShell.defaultOptions = { scriptPath: '../scripts' }; | |
| 206 | 202 | ``` | |
| 207 | 203 | ||
| 208 | - #### `#run(script, options, callback)` | ||
| 204 | + #### `#run(script, options)` | ||
| 209 | 205 | ||
| 210 | - Runs the Python script and invokes `callback` with the results. The callback contains the execution error (if any) as well as an array of messages emitted from the Python script. | ||
| 211 | - | ||
| 212 | - This method is also returning the `PythonShell` instance. | ||
| 206 | + Runs the Python script and returns a promise. When you handle the promise the argument will be an array of messages emitted from the Python script. | ||
| 213 | 207 | ||
| 214 | 208 | Example: | |
| 215 | 209 | ||
| 216 | 210 | ```typescript | |
| 217 | 211 | // run a simple script | |
| 218 | - PythonShell.run('script.py', null, function (err, results) { | ||
| 212 | + PythonShell.run('script.py', null).then(results => { | ||
| 219 | 213 | // script finished | |
| 220 | 214 | }); | |
| 221 | 215 | ``` | |
| 222 | 216 | ||
| 223 | 217 | #### `#runString(code, options, callback)` | |
| 224 | 218 | ||
| 225 | - Runs the Python code and invokes `callback` with the results. The callback contains the execution error (if any) as well as an array of messages emitted from the Python script. | ||
| 226 | - | ||
| 227 | - This method is also returning the `PythonShell` instance. | ||
| 219 | + Runs the Python script and returns a promise. When you handle the promise the argument will be an array of messages emitted from the Python script. | ||
| 228 | 220 | ||
| 229 | 221 | Example: | |
| 230 | 222 | ||
| 231 | 223 | ```typescript | |
| 232 | - // run a simple script | ||
| 233 | - PythonShell.runString('x=1;print(x)', null, function (err, results) { | ||
| 224 | + // run some simple code | ||
| 225 | + PythonShell.runString('x=1;print(x)', null).then(messages=>{ | ||
| 234 | 226 | // script finished | |
| 235 | 227 | }); | |
| 236 | 228 | ``` | |
@@ -247,7 +239,7 @@ Promise is rejected if there is a syntax error. | |||
| 247 | 239 | ||
| 248 | 240 | #### `#getVersion(pythonPath?:string)` | |
| 249 | 241 | ||
| 250 | - Returns the python version. Optional pythonPath param to get the version | ||
| 242 | + Returns the python version as a promise. Optional pythonPath param to get the version | ||
| 251 | 243 | of a specific python interpreter. | |
| 252 | 244 | ||
| 253 | 245 | #### `#getVersionSync(pythonPath?:string)` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -67,6 +67,10 @@ export class PythonShellError extends Error { | |||
| 67 | 67 | exitCode?: number; | |
| 68 | 68 | } | |
| 69 | 69 | ||
| 70 | + export class PythonShellErrorWithLogs extends PythonShellError { | ||
| 71 | + logs: any[] | ||
| 72 | + } | ||
| 73 | + | ||
| 70 | 74 | /** | |
| 71 | 75 | * Takes in a string stream and emits batches seperated by newlines | |
| 72 | 76 | */ | |
@@ -306,62 +310,44 @@ export class PythonShell extends EventEmitter { | |||
| 306 | 310 | } | |
| 307 | 311 | ||
| 308 | 312 | /** | |
| 309 | - * Runs a Python script and returns collected messages | ||
| 310 | - * @param {string} scriptPath The path to the script to execute | ||
| 311 | - * @param {Options} options The execution options | ||
| 312 | - * @param {Function} (deprecated argument) callback The callback function to invoke with the script results | ||
| 313 | - * @return {Promise<string[]> | PythonShell} the output from the python script | ||
| 313 | + * Runs a Python script and returns collected messages as a promise | ||
| 314 | + * @param scriptPath The path to the script to execute | ||
| 315 | + * @param options The execution options | ||
| 316 | + * @return a promise with the output from the python script | ||
| 314 | 317 | */ | |
| 315 | - static run(scriptPath: string, options?: Options, callback?: (err?: PythonShellError, output?: any[]) => any) { | ||
| 316 | - | ||
| 317 | - if(callback) { | ||
| 318 | - console.warn('PythonShell.run() callback is deprecated. Use PythonShell.run() promise instead.') | ||
| 319 | - | ||
| 320 | - return this.runLegacy(scriptPath, options, callback); | ||
| 321 | - } | ||
| 322 | - else { | ||
| 323 | - return new Promise((resolve, reject) => { | ||
| 324 | - let pyshell = new PythonShell(scriptPath, options); | ||
| 325 | - let output = []; | ||
| 326 | - | ||
| 327 | - pyshell.on('message', function (message) { | ||
| 328 | - output.push(message); | ||
| 329 | - }).end(function (err) { | ||
| 330 | - if(err) reject(err); | ||
| 331 | - else resolve(output); | ||
| 332 | - }); | ||
| 318 | + static run(scriptPath: string, options?: Options) { | ||
| 319 | + return new Promise((resolve, reject) => { | ||
| 320 | + let pyshell = new PythonShell(scriptPath, options); | ||
| 321 | + let output = []; | ||
| 322 | + | ||
| 323 | + pyshell.on('message', function (message) { | ||
| 324 | + output.push(message); | ||
| 325 | + }).end(function (err) { | ||
| 326 | + if(err){ | ||
| 327 | + (err as PythonShellErrorWithLogs).logs = output | ||
| 328 | + reject(err); | ||
| 329 | + } | ||
| 330 | + else resolve(output); | ||
| 333 | 331 | }); | |
| 334 | - } | ||
| 335 | - }; | ||
| 336 | - | ||
| 337 | - private static runLegacy(scriptPath: string, options?: Options, callback?: (err?: PythonShellError, output?: any[]) => any) { | ||
| 338 | - let pyshell = new PythonShell(scriptPath, options); | ||
| 339 | - let output = []; | ||
| 340 | - | ||
| 341 | - return pyshell.on('message', function (message) { | ||
| 342 | - output.push(message); | ||
| 343 | - }).end(function (err) { | ||
| 344 | - return callback(err ? err : null, output.length ? output : null); | ||
| 345 | 332 | }); | |
| 346 | 333 | }; | |
| 347 | 334 | ||
| 348 | 335 | ||
| 349 | 336 | ||
| 350 | 337 | /** | |
| 351 | - * Runs the inputted string of python code and returns collected messages. DO NOT ALLOW UNTRUSTED USER INPUT HERE! | ||
| 352 | - * @param {string} code The python code to execute | ||
| 353 | - * @param {Options} options The execution options | ||
| 354 | - * @param {Function} callback The callback function to invoke with the script results | ||
| 355 | - * @return {PythonShell} The PythonShell instance | ||
| 338 | + * Runs the inputted string of python code and returns collected messages as a promise. DO NOT ALLOW UNTRUSTED USER INPUT HERE! | ||
| 339 | + * @param code The python code to execute | ||
| 340 | + * @param options The execution options | ||
| 341 | + * @return a promise with the output from the python script | ||
| 356 | 342 | */ | |
| 357 | - static runString(code: string, options?: Options, callback?: (err: PythonShellError, output?: any[]) => any) { | ||
| 343 | + static runString(code: string, options?: Options) { | ||
| 358 | 344 | ||
| 359 | 345 | // put code in temp file | |
| 360 | 346 | const randomInt = getRandomInt(); | |
| 361 | 347 | const filePath = tmpdir + sep + `pythonShellFile${randomInt}.py` | |
| 362 | 348 | writeFileSync(filePath, code); | |
| 363 | 349 | ||
| 364 | - return PythonShell.run(filePath, options, callback); | ||
| 350 | + return PythonShell.run(filePath, options); | ||
| 365 | 351 | }; | |
| 366 | 352 | ||
| 367 | 353 | static getVersion(pythonPath?: string) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -117,9 +117,8 @@ describe('PythonShell', function () { | |||
| 117 | 117 | before(() => { | |
| 118 | 118 | PythonShell.defaultOptions = {}; | |
| 119 | 119 | }) | |
| 120 | - it('should be able to execute a string of python code using callbacks', function (done) { | ||
| 121 | - let pythonshell = PythonShell.runString('print("hello");print("world")', null, function (err, results) { | ||
| 122 | - if (err) return done(err); | ||
| 120 | + it('should be able to execute a string of python code', function (done) { | ||
| 121 | + PythonShell.runString('print("hello");print("world")', null).then((results) => { | ||
| 123 | 122 | results.should.be.an.Array().and.have.lengthOf(2); | |
| 124 | 123 | results.should.eql(['hello', 'world']); | |
| 125 | 124 | done(); | |
@@ -132,6 +131,11 @@ describe('PythonShell', function () { | |||
| 132 | 131 | results.should.be.an.Array().and.have.lengthOf(2); | |
| 133 | 132 | results.should.eql(['hello', 'world']); | |
| 134 | 133 | }); | |
| 134 | + it('should be able to execute a string of python code async', async function () { | ||
| 135 | + let results = await PythonShell.runString('print("hello");print("world")'); | ||
| 136 | + results.should.be.an.Array().and.have.lengthOf(2); | ||
| 137 | + results.should.eql(['hello', 'world']); | ||
| 138 | + }); | ||
| 135 | 139 | after(() => { | |
| 136 | 140 | PythonShell.defaultOptions = { | |
| 137 | 141 | // reset to match initial value | |
@@ -144,28 +148,27 @@ describe('PythonShell', function () { | |||
| 144 | 148 | it('should run the script and return output data using callbacks', function (done) { | |
| 145 | 149 | PythonShell.run('echo_args.py', { | |
| 146 | 150 | args: ['hello', 'world'] | |
| 147 | - }, function (err, results) { | ||
| 148 | - if (err) return done(err); | ||
| 151 | + }).then((results) => { | ||
| 149 | 152 | results.should.be.an.Array().and.have.lengthOf(2); | |
| 150 | 153 | results.should.eql(['hello', 'world']); | |
| 151 | 154 | done(); | |
| 152 | 155 | }); | |
| 153 | 156 | }); | |
| 154 | - it('should run the script and return output data using promise', async function () { | ||
| 157 | + it('should run the script and return output data async', async function () { | ||
| 155 | 158 | let results = await PythonShell.run('echo_args.py', { | |
| 156 | 159 | args: ['hello', 'world'] | |
| 157 | 160 | }); | |
| 158 | 161 | results.should.be.an.Array().and.have.lengthOf(2); | |
| 159 | 162 | results.should.eql(['hello', 'world']); | |
| 160 | 163 | }); | |
| 161 | 164 | it('should try to run the script and fail appropriately', function (done) { | |
| 162 | - PythonShell.run('unknown_script.py', null, function (err, results) { | ||
| 165 | + PythonShell.run('unknown_script.py', null).catch((err) => { | ||
| 163 | 166 | err.should.be.an.Error; | |
| 164 | 167 | err.exitCode.should.be.exactly(2); | |
| 165 | 168 | done(); | |
| 166 | 169 | }); | |
| 167 | 170 | }); | |
| 168 | - it('should try to run the script and fail appropriately', async function () { | ||
| 171 | + it('should try to run the script and fail appropriately - async', async function () { | ||
| 169 | 172 | try { | |
| 170 | 173 | let results = await PythonShell.run('unknown_script.py'); | |
| 171 | 174 | throw new Error(`should not get here because the script should fail` + results); | |
@@ -175,22 +178,24 @@ describe('PythonShell', function () { | |||
| 175 | 178 | } | |
| 176 | 179 | }); | |
| 177 | 180 | it('should include both output and error', function (done) { | |
| 178 | - PythonShell.run('echo_hi_then_error.py', null, function (err, results) { | ||
| 179 | - err.should.be.an.Error; | ||
| 180 | - results.should.eql(['hi']) | ||
| 181 | - done(); | ||
| 181 | + PythonShell.run('echo_hi_then_error.py', null).then((results) => { | ||
| 182 | + done("Error: This promise should never successfully resolve"); | ||
| 183 | + }).catch((err)=>{ | ||
| 184 | + err.logs.should.eql(['hi']) | ||
| 185 | + err.should.be.an.Error | ||
| 186 | + done() | ||
| 182 | 187 | }); | |
| 183 | 188 | }); | |
| 184 | 189 | it('should run the script and fail with an extended stack trace', function (done) { | |
| 185 | - PythonShell.run('error.py', null, function (err, results) { | ||
| 190 | + PythonShell.run('error.py', null).catch((err) => { | ||
| 186 | 191 | err.should.be.an.Error; | |
| 187 | 192 | err.exitCode.should.be.exactly(1); | |
| 188 | 193 | err.stack.should.containEql('----- Python Traceback -----'); | |
| 189 | 194 | done(); | |
| 190 | 195 | }); | |
| 191 | 196 | }); | |
| 192 | 197 | it('should run the script and fail with an extended stack trace even when mode is binary', function (done) { | |
| 193 | - PythonShell.run('error.py', { mode: "binary" }, function (err, results) { | ||
| 198 | + PythonShell.run('error.py', { mode: "binary" }).catch((err) => { | ||
| 194 | 199 | err.should.be.an.Error; | |
| 195 | 200 | err.exitCode.should.be.exactly(1); | |
| 196 | 201 | err.stack.should.containEql('----- Python Traceback -----'); | |
@@ -210,7 +215,7 @@ describe('PythonShell', function () { | |||
| 210 | 215 | } | |
| 211 | 216 | } | |
| 212 | 217 | function runSingleErrorScript(callback) { | |
| 213 | - PythonShell.run('error.py', null, function (err, results) { | ||
| 218 | + PythonShell.run('error.py', null).catch((err) => { | ||
| 214 | 219 | err.should.be.an.Error; | |
| 215 | 220 | err.exitCode.should.be.exactly(1); | |
| 216 | 221 | err.stack.should.containEql('----- Python Traceback -----'); | |
@@ -234,8 +239,7 @@ describe('PythonShell', function () { | |||
| 234 | 239 | function runSingleScript(callback) { | |
| 235 | 240 | PythonShell.run('echo_args.py', { | |
| 236 | 241 | args: ['hello', 'world'] | |
| 237 | - }, function (err, results) { | ||
| 238 | - if (err) return done(err); | ||
| 242 | + }).then((results)=> { | ||
| 239 | 243 | results.should.be.an.Array().and.have.lengthOf(2); | |
| 240 | 244 | results.should.eql(['hello', 'world']); | |
| 241 | 245 | callback(); | |
@@ -249,14 +253,11 @@ describe('PythonShell', function () { | |||
| 249 | 253 | ||
| 250 | 254 | PythonShell.run('-m', { | |
| 251 | 255 | args: ['timeit', '-n 1', `'x=5'`] | |
| 252 | - }, function (err, results) { | ||
| 253 | - | ||
| 256 | + }).then((results)=> { | ||
| 254 | 257 | PythonShell.defaultOptions = { | |
| 255 | 258 | // reset to match initial value | |
| 256 | 259 | scriptPath: pythonFolder | |
| 257 | 260 | }; | |
| 258 | - | ||
| 259 | - if (err) return done(err); | ||
| 260 | 261 | results.should.be.an.Array(); | |
| 261 | 262 | results[0].should.be.an.String(); | |
| 262 | 263 | results[0].slice(0, 6).should.eql('1 loop'); | |
@@ -522,17 +523,17 @@ describe('PythonShell', function () { | |||
| 522 | 523 | let pyshell = new PythonShell('error.py'); | |
| 523 | 524 | pyshell.on('pythonError', function (err) { | |
| 524 | 525 | err.stack.should.containEql('----- Python Traceback -----'); | |
| 525 | - err.stack.should.containEql('File "test' + sep + 'python' + sep + 'error.py", line 4'); | ||
| 526 | - err.stack.should.containEql('File "test' + sep + 'python' + sep + 'error.py", line 6'); | ||
| 526 | + err.stack.should.containEql('test' + sep + 'python' + sep + 'error.py", line 4'); | ||
| 527 | + err.stack.should.containEql('test' + sep + 'python' + sep + 'error.py", line 6'); | ||
| 527 | 528 | done(); | |
| 528 | 529 | }); | |
| 529 | 530 | }); | |
| 530 | 531 | it('should work in json mode', function (done) { | |
| 531 | 532 | let pyshell = new PythonShell('error.py', { mode: 'json' }); | |
| 532 | 533 | pyshell.on('pythonError', function (err) { | |
| 533 | 534 | err.stack.should.containEql('----- Python Traceback -----'); | |
| 534 | - err.stack.should.containEql('File "test' + sep + 'python' + sep + 'error.py", line 4'); | ||
| 535 | - err.stack.should.containEql('File "test' + sep + 'python' + sep + 'error.py", line 6'); | ||
| 535 | + err.stack.should.containEql('test' + sep + 'python' + sep + 'error.py", line 4'); | ||
| 536 | + err.stack.should.containEql('test' + sep + 'python' + sep + 'error.py", line 6'); | ||
| 536 | 537 | done(); | |
| 537 | 538 | }); | |
| 538 | 539 | }); | |
@@ -563,4 +564,4 @@ describe('PythonShell', function () { | |||
| 563 | 564 | }, 500); | |
| 564 | 565 | }); | |
| 565 | 566 | }); | |
| 566 | - }); | ||
| 567 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments