hack hack hack until pyterminal works also in workers

This commit is contained in:
Antonio Cuni
2023-09-29 19:09:20 +02:00
parent 1867b8b89f
commit eaf4da7c21
4 changed files with 18 additions and 41 deletions

View File

@@ -75,9 +75,19 @@ const registerModule = ({ XWorker: $XWorker, interpreter, io }) => {
interpreter.runPython(stdlib, { globals: interpreter.runPython("{}") });
};
// this is a super-ugly workaround for a polyscript issue: in theory I should
// define a codeBeforeRunWorker hook inside py-terminal.js, but since it's a
// lazy plugin, it is too late: https://github.com/pyscript/polyscript/issues/52
const py_terminal_codeBeforeRunner = `
from pyscript import pyterminal
pyterminal.init()
`;
const workerHooks = {
codeBeforeRunWorker: () =>
[stdlib, ...hooks.codeBeforeRunWorker].map(dedent).join("\n"),
[stdlib, py_terminal_codeBeforeRunner, ...hooks.codeBeforeRunWorker]
.map(dedent)
.join("\n"),
codeBeforeRunWorkerAsync: () =>
[stdlib, ...hooks.codeBeforeRunWorkerAsync].map(dedent).join("\n"),
codeAfterRunWorker: () =>
@@ -135,7 +145,8 @@ for (const [TYPE, interpreter] of TYPES) {
errors.set(element, error);
},
...workerHooks,
onWorkerReady(_, xworker) {
async onWorkerReady(_, xworker) {
await plugins; // workaround for a polyscript bug
assign(xworker.sync, sync);
for (const callback of hooks.onWorkerReady)
callback(_, xworker);

View File

@@ -64,52 +64,14 @@ hooks.onInterpreterReady.add(function override(pyScript) {
pyodide.setStderr({ raw: myStdout });
});
hooks.onWorkerReady.add(function override(pyScript) {
hooks.onWorkerReady.add(function (_, xworker) {
console.log("hello onWorkerReady");
const t = makePyTerminal();
if (!t) {
console.log("<py-terminal> not found, nothing to do");
return;
}
console.log(pyScript);
});
// this is mostly pseudo-code for what it *should* happen for the workers case
/*
addEventListener("py:ready", (event) => {
console.log("hello py:ready");
if (!event.detail.worker) {
return;
}
const t = makePyTerminal();
if (!t) {
console.log("<py-terminal> not found, nothing to do");
return;
}
const xworker = event.target.xworker;
xworker.sync.pyterminal_readline = t.readline;
xworker.sync.pyterminal_write = t.write;
// XXX: I know that the following lines don't work, but this is more or
// less what I would like to happen
const something = ???;
something.io.stdout = (s, ...rest) => {
// this is JS code, and we cannot send arbitrary JS code from the main
// to the worker. So maybe a solution is to hardcode this logic
// directly inside the worker code?
xworker.sync.pyterminal_write(s);
}
something.io.stderr = (s, ...rest) => {
xworker.sync.pyterminal_write(s);
}
something.runPython(`
import builtins
import pyscript
builtins.input = sync.pyterminal_readline
`)
});
*/

View File

@@ -24,6 +24,9 @@
# XXX we have a problem with the error plugin: this line should
# just go to the terminal, NOT to the red box in the DOM
print("this goes to stderr", file=sys.stderr)
import code
code.interact()
</script>
<py-terminal></py-terminal>

View File

@@ -4,6 +4,7 @@ declare namespace _default {
"display.py": string;
"event_handling.py": string;
"magic_js.py": string;
"pyterminal.py": string;
"util.py": string;
};
let pyweb: {