(
input: { code: string; timeoutMs: number },
runtime: ReplWrapperRuntime,
)
| 307 | } |
| 308 | |
| 309 | async execute( |
| 310 | input: { code: string; timeoutMs: number }, |
| 311 | runtime: ReplWrapperRuntime, |
| 312 | ): Promise<{ output: string; error?: string }> { |
| 313 | return this.runExclusive(async () => { |
| 314 | await this.ensureStarted() |
| 315 | const child = this.child |
| 316 | if (!child?.stdin.writable) { |
| 317 | throw new Error('Python REPL kernel stdin is not writable') |
| 318 | } |
| 319 | |
| 320 | const execId = randomUUID() |
| 321 | const pending = await new Promise<PythonKernelExecResultMessage>((resolve, reject) => { |
| 322 | this.pendingExecs.set(execId, { resolve, reject, runtime }) |
| 323 | child.stdin.write( |
| 324 | `${JSON.stringify({ |
| 325 | type: 'exec', |
| 326 | id: execId, |
| 327 | code: input.code, |
| 328 | timeout_ms: input.timeoutMs, |
| 329 | })}\n`, |
| 330 | ) |
| 331 | }).finally(() => { |
| 332 | this.pendingExecs.delete(execId) |
| 333 | }) |
| 334 | |
| 335 | if (!pending.ok) { |
| 336 | return { |
| 337 | output: pending.output, |
| 338 | error: pending.error ?? 'Python REPL execution failed', |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return { |
| 343 | output: pending.output, |
| 344 | } |
| 345 | }) |
| 346 | } |
| 347 | |
| 348 | async reset(): Promise<void> { |
| 349 | await this.runExclusive(async () => { |
no test coverage detected