(script: string, source: string, filename: string)
| 555 | |
| 556 | pythonAvailable = false; |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | import { spawn } from "node:child_process"; |
| 561 | |
| 562 | async function runPythonWithStdin(script: string, source: string, filename: string): Promise<any> { |
| 563 | if (!(await findPython())) return null; |
| 564 | |
| 565 | return new Promise((resolve) => { |
| 566 | const proc = spawn(pythonCmd, ["-c", script, filename], { |
| 567 | timeout: 10000, |
| 568 | env: { ...process.env, PYTHONDONTWRITEBYTECODE: "1" }, |
| 569 | stdio: ["pipe", "pipe", "pipe"], |
| 570 | }); |
| 571 | |
| 572 | let stdout = ""; |
| 573 | let stderr = ""; |
| 574 | |
| 575 | proc.stdout.on("data", (data: Buffer) => { stdout += data.toString(); }); |
| 576 | proc.stderr.on("data", (data: Buffer) => { stderr += data.toString(); }); |
| 577 | |
| 578 | proc.on("close", (code) => { |
| 579 | if (code !== 0 || !stdout.trim()) { |
| 580 | resolve(null); |
| 581 | return; |
| 582 | } |
| 583 | try { |
| 584 | resolve(JSON.parse(stdout.trim())); |
| 585 | } catch { |
| 586 | resolve(null); |
| 587 | } |
| 588 | }); |
| 589 | |
| 590 | proc.on("error", () => resolve(null)); |
| 591 | |
| 592 | proc.stdin.write(source); |
| 593 | proc.stdin.end(); |
no test coverage detected