Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,24 +286,23 @@ export class PythonShell extends EventEmitter {
* @returns rejects promise w/ string error output if syntax failure
*/
static async checkSyntax(code: string) {
const randomInt = getRandomInt();
const filePath = tmpdir() + sep + `pythonShellSyntaxCheck${randomInt}.py`

const writeFilePromise = promisify(writeFile)
return writeFilePromise(filePath, code).then(() => {
return this.checkSyntaxFile(filePath)
})
const pythonPath = this.getPythonPath()
let compileCommand = `${pythonPath} -c "compile('${code}', '', 'exec');"`
return execPromise(compileCommand)
}

static getPythonPath() {
return this.defaultOptions.pythonPath ? this.defaultOptions.pythonPath : this.defaultPythonPath;
}


/**
* checks syntax without executing code
* checks syntax of a file without executing code
* @returns {Promise} rejects w/ stderr if syntax failure
*/
static async checkSyntaxFile(filePath: string) {

const pythonPath = this.getPythonPath()
let compileCommand = `${pythonPath} -m py_compile ${filePath}`
return execPromise(compileCommand)
Expand Down
Loading