PyTerminal: use Pyodide instead of Python (#1833)

This commit is contained in:
Andrea Giammarchi
2023-11-03 17:59:11 +01:00
committed by GitHub
parent aef028be6e
commit 3e2a67d434
3 changed files with 26 additions and 13 deletions

View File

@@ -81,26 +81,23 @@ const pyTerminal = async () => {
const workerReady = ({ interpreter }, { sync }) => {
sync.pyterminal_drop_hooks();
const decoder = new TextDecoder();
let data = "";
const generic = {
isatty: true,
write(buffer) {
sync.pyterminal_write(decoder.decode(buffer));
data = decoder.decode(buffer);
sync.pyterminal_write(data);
return buffer.length;
},
};
interpreter.setStdout(generic);
interpreter.setStderr(generic);
interpreter.setStdin({
isatty: true,
stdin: () => sync.pyterminal_read(data),
});
};
// run in python code able to replace builtins.input
// using the xworker.sync non blocking prompt
const codeBefore = `
import builtins
from pyscript import sync as _sync
builtins.input = lambda prompt: _sync.pyterminal_read(prompt)
`;
// at the end of the code, make the terminal interactive
const codeAfter = `
import code as _code
@@ -121,7 +118,6 @@ const pyTerminal = async () => {
// allow a worker to drop main thread hooks ASAP
xworker.sync.pyterminal_drop_hooks = () => {
hooks.worker.onReady.delete(workerReady);
hooks.worker.codeBeforeRun.delete(codeBefore);
hooks.worker.codeAfterRun.delete(codeAfter);
};
});
@@ -129,7 +125,6 @@ const pyTerminal = async () => {
// setup remote thread JS/Python code for whenever the
// worker is ready to become a terminal
hooks.worker.onReady.add(workerReady);
hooks.worker.codeBeforeRun.add(codeBefore);
hooks.worker.codeAfterRun.add(codeAfter);
} else {
// in the main case, just bootstrap XTerm without