Skip to content

Commit e001d41

Browse files
committed
#135 added ability to run a python string
1 parent 1cea78f commit e001d41

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

index.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ChildProcess,spawn, SpawnOptions, exec } from 'child_process';
33
import {EOL as newline, tmpdir} from 'os';
44
import {join, sep} from 'path'
55
import {Readable,Writable} from 'stream'
6-
import { writeFile } from 'fs';
6+
import { writeFile, writeFileSync } from 'fs';
77

88
function toArray<T>(source?:T|T[]):T[] {
99
if (typeof source === 'undefined' || source === null) {
@@ -198,7 +198,7 @@ export class PythonShell extends EventEmitter{
198198
* @returns {Promise} rejects w/ stderr if syntax failure
199199
*/
200200
static async checkSyntax(code:string){
201-
let randomInt = Math.floor(Math.random()*10000000000);
201+
let randomInt = PythonShell.getRandomInt();
202202
let filePath = tmpdir + sep + `pythonShellSyntaxCheck${randomInt}.py`
203203

204204
// todo: replace this with util.promisify (once we no longer support node v7)
@@ -246,6 +246,23 @@ export class PythonShell extends EventEmitter{
246246
});
247247
};
248248

249+
/**
250+
* Runs the inputted string of python code and returns collected messages. DO NOT ALLOW UNTRUSTED USER INPUT HERE!
251+
* @param {string} code The python code to execute
252+
* @param {Options} options The execution options
253+
* @param {Function} callback The callback function to invoke with the script results
254+
* @return {PythonShell} The PythonShell instance
255+
*/
256+
static runString(code:string, options?:Options, callback?:(err:PythonShellError, output?:any[])=>any) {
257+
258+
// put code in temp file
259+
let randomInt = PythonShell.getRandomInt();
260+
let filePath = tmpdir + sep + `pythonShellFile${randomInt}.py`
261+
writeFileSync(filePath, code);
262+
263+
return PythonShell.run(filePath, options, callback);
264+
};
265+
249266
/**
250267
* Parses an error thrown from the Python process through stderr
251268
* @param {string|Buffer} data The stderr contents to parse
@@ -272,6 +289,13 @@ export class PythonShell extends EventEmitter{
272289
return error;
273290
};
274291

292+
/**
293+
* gets a random int from 0-10000000000
294+
*/
295+
private static getRandomInt(){
296+
return Math.floor(Math.random()*10000000000);
297+
}
298+
275299
/**
276300
* Sends a message to the Python shell through stdin
277301
* Override this method to format data to be sent to the Python process

test/test-python-shell.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,26 @@ describe('PythonShell', function () {
5454
})
5555
})
5656

57+
describe('#runString(script, options)', function () {
58+
before(()=>{
59+
PythonShell.defaultOptions = {};
60+
})
61+
it('should be able to execute a string of python code', function (done) {
62+
PythonShell.runString('print("hello");print("world")', null, function (err, results) {
63+
if (err) return done(err);
64+
results.should.be.an.Array().and.have.lengthOf(2);
65+
results.should.eql(['hello', 'world']);
66+
done();
67+
});
68+
});
69+
after(()=>{
70+
PythonShell.defaultOptions = {
71+
// reset to match initial value
72+
scriptPath: './test/python'
73+
};
74+
})
75+
});
76+
5777
describe('#run(script, options)', function () {
5878
it('should run the script and return output data', function (done) {
5979
PythonShell.run('echo_args.py', {

0 commit comments

Comments
 (0)