fix(python): run the Python Console in a Docker or Compose interpreter's container (#6686)

With a Docker or Compose run target as the project interpreter, the Python Console reported "No Python interpreter" because it only asked for a local interpreter. When there is none but a run target interpreter is set, the console now starts its driver as a python run, which Run already routes into the target's container (project mounted, the target's interpreter, docker stop on Stop). The driver exits on SIGTERM itself, since as a container's PID 1 it would otherwise ignore Stop until docker's SIGKILL.

Closes #6685.

Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
LU XIANGRUIтаwildcatDownstairs authored and GitHub committed 2026-11-28 20:49:10 +08:00
1 джерело 482143769f
коміт 611440f77f
4 files changed
+43 -3

No files matched your search

+4
Переглянути файл
@@ -4,11 +4,15 @@
import json
import os
import reprlib
import signal
import sys
import textwrap
import types
RS = "\x1e"
# In a container (`docker compose run` has no `--init`) the driver is PID 1, which ignores
# SIGTERM without a handler: Stop would wait for docker's SIGKILL after 10 s.
signal.signal(signal.SIGTERM, lambda *_: os._exit(128 + signal.SIGTERM))
_out = sys.stdout
_repr = reprlib.Repr()
_repr.maxstring = _repr.maxother = 200
+9
Переглянути файл
@@ -130,3 +130,12 @@ it.skipIf(!DJANGO)(
},
30_000,
);
it.skipIf(!PYTHON)("exits on SIGTERM itself, also as a container's PID 1, which ignores it without a handler", () => {
const result = spawnSync(PYTHON!, ["-I", "-S", "-u", DRIVER], {
input: request("import os, signal, time\nos.kill(os.getpid(), signal.SIGTERM)\ntime.sleep(5)"),
encoding: "utf8",
timeout: 4000,
});
expect(result.signal).toBeNull();
expect(result.status).toBe(143);
});
+26 -2
Переглянути файл
@@ -6,6 +6,9 @@ const ipc = vi.hoisted(() => ({
exited: (_e: { run: number; code: number | null }) => {},
started: null as null | { resolve: (id: number) => void; channel: { onmessage?: (c: OutputChunk) => void } },
stdin: vi.fn(),
python: "/venv/bin/python" as string | null,
container: null as null | { target: string },
command: "",
}));
vi.mock("@tauri-apps/api/core", () => ({ Channel: class {} }));
@@ -25,10 +28,12 @@ vi.mock("@/platform/ipc", async (importOriginal) => {
},
commands: {
runList: () => Promise.resolve([]),
sdkPythonEffective: () => ok("/venv/bin/python"),
sdkPythonEffective: () => ok(ipc.python),
sdkContainerPython: () => ok(ipc.container),
findFilesNamed: () => ok([]),
runStart: (_config: unknown, channel: NonNullable<typeof ipc.started>["channel"]) =>
runStart: (config: { command: string }, channel: NonNullable<typeof ipc.started>["channel"]) =>
new Promise((resolve) => {
ipc.command = config.command;
ipc.started = { channel, resolve: (id) => resolve({ status: "ok", data: { id } }) };
}),
runStdin: (id: number, data: string) => {
@@ -54,6 +59,8 @@ beforeEach(() => {
stopConsole();
ipc.started = null;
ipc.stdin.mockClear();
ipc.python = "/venv/bin/python";
ipc.container = null;
});
it("runs code sent while starting once the driver is ready, even before the start call returns", async () => {
@@ -98,3 +105,20 @@ it("reads the settings module manage.py sets", () => {
expect(settingsModule(`os.environ.setdefault("DJANGO_SETTINGS_MODULE", f"{SITE}.settings")`)).toBeNull();
expect(settingsModule("print('hi')")).toBeNull();
});
it("runs in the container when the project interpreter is a Docker or Compose target's", async () => {
ipc.python = null;
ipc.container = { target: "web" };
execute("x = 1");
await settle();
expect(ipc.command).toBe("python");
expect(useConsole.getState().noInterpreter).toBe(false);
});
it("asks for an interpreter when there is none, local or in a container", async () => {
ipc.python = null;
execute("x = 1");
await settle();
expect(ipc.started).toBeNull();
expect(useConsole.getState().noInterpreter).toBe(true);
});
+4 -1
Переглянути файл
@@ -128,7 +128,10 @@ export async function startConsole() {
try {
// The interpreter the status bar shows: in a workspace, the one of the member the active file is in.
const active = editorService().activePath();
const python = await call(commands.sdkPythonEffective(active ? dirname(active) : null));
const python =
(await call(commands.sdkPythonEffective(active ? dirname(active) : null))) ??
// A Docker / Compose interpreter: Run swaps `python` for the target's and starts the container.
((await call(commands.sdkContainerPython()).catch(() => null)) ? "python" : null);
if (mine !== generation) return;
if (!python) {
queue.length = 0;