From f55c9959c6234741e142be1ca343412f4fc81c84 Mon Sep 17 00:00:00 2001 From: pmp-p Date: Sun, 12 Nov 2023 09:10:47 +0100 Subject: [PATCH] pkg/emsdk embind/ CSI-ESC --- pygbag/__init__.py | 16 ++++++++++++---- pygbag/__main__.py | 2 ++ pygbag/support/cross/aio/pep0723.py | 7 +++++-- pygbag/support/pythonrc.py | 20 ++++++++++++-------- scripts/build-loader.sh | 13 +++++++++---- static/pythons.js | 8 ++++---- 6 files changed, 44 insertions(+), 22 deletions(-) diff --git a/pygbag/__init__.py b/pygbag/__init__.py index 1ed73cf..4f39263 100644 --- a/pygbag/__init__.py +++ b/pygbag/__init__.py @@ -28,6 +28,8 @@ # WaPy<=>CPython compat +import builtins + try: # embed builtin module handles I/O on wasm import embed @@ -35,7 +37,7 @@ # aio function implemented only on stackless WaPy sched_yield except: - import builtins + builtins.sched_yield = lambda: None @@ -50,13 +52,19 @@ def print_exception(e, out=sys.stderr, **kw): sys.print_exception = print_exception -def ESC(*argv): +def ESC(*argv, flush=False): for arg in argv: sys.__stdout__.write(chr(0x1B)) sys.__stdout__.write(arg) - embed.flush() + if flush: + sys.stdout.flush() def CSI(*argv): for arg in argv: - ESC(f"[{arg}") + ESC(f"[{arg}", flush=False) + sys.stdout.flush() + + +builtins.CSI = CSI +builtins.ESC = ESC diff --git a/pygbag/__main__.py b/pygbag/__main__.py index f307e49..d33fd59 100644 --- a/pygbag/__main__.py +++ b/pygbag/__main__.py @@ -100,6 +100,8 @@ def truc(*argv, **kw): print("truc", argv, kw) fakehost.window = NoOp("platform.window") + fakehost.window.console = NoOp("platform.window.console") + fakehost.window.console.log = print fakehost.window.get_terminal_console = truc import aio.filelike diff --git a/pygbag/support/cross/aio/pep0723.py b/pygbag/support/cross/aio/pep0723.py index effc0cc..6ada1ef 100644 --- a/pygbag/support/cross/aio/pep0723.py +++ b/pygbag/support/cross/aio/pep0723.py @@ -10,6 +10,7 @@ from pathlib import Path import re +print(sys.path) import tomllib import json @@ -138,7 +139,7 @@ def install(pkg_file, sconf=None): if pkg_file not in HISTORY: HISTORY.append(pkg_file) importlib.invalidate_caches() - print("142: {pkg_file} installed") + print(f"142: {pkg_file} installed") except FileExistsError as ex: print(f"38: {pkg_file} already installed (or partially)", ex) except Exception as ex: @@ -354,7 +355,9 @@ async def check_list(code=None, filename=None): # we want host to load wasm packages too # so make pure/bin folder first for imports - sys.path.insert(0, env.as_posix()) + + if env.as_posix() not in sys.path: + sys.path.insert(0, env.as_posix()) sconf = __import__("sysconfig").get_paths() sconf["purelib"] = sconf["platlib"] = env.as_posix() diff --git a/pygbag/support/pythonrc.py b/pygbag/support/pythonrc.py index e2bd1e5..b27057b 100644 --- a/pygbag/support/pythonrc.py +++ b/pygbag/support/pythonrc.py @@ -1438,17 +1438,21 @@ def print_pg_bar(total, iteration): # x10 mouse and xterm stuff # https://github.com/muesli/termenv/pull/104 # https://xtermjs.org/docs/api/vtfeatures/ -def ESC(*argv): - for arg in argv: - sys.__stdout__.write(chr(0x1B)) - sys.__stdout__.write(arg) - embed.flush() +if not aio.cross.simulator: + def ESC(*argv): + for arg in argv: + sys.__stdout__.write(chr(0x1B)) + sys.__stdout__.write(arg) + embed.flush() -def CSI(*argv): - for arg in argv: - ESC(f"[{arg}") + def CSI(*argv): + for arg in argv: + ESC(f"[{arg}") + + builtins.ESC = ESC + builtins.CSI = CSI try: console diff --git a/scripts/build-loader.sh b/scripts/build-loader.sh index 347960e..fef5d47 100755 --- a/scripts/build-loader.sh +++ b/scripts/build-loader.sh @@ -84,8 +84,8 @@ fi export PATCH_FS="--preload-file $(realpath platform_wasm/platform_wasm)@/data/data/org.python/assets/site-packages/platform_wasm" - -LOPTS="-sMAIN_MODULE --bind -fno-rtti" +# =2 will break pyodide module reuses +LOPTS="-sMAIN_MODULE=1" # O0/g3 is much faster to build and easier to debug @@ -250,9 +250,11 @@ then " 1>&2 +# -std=gnu99 -std=c++23 + cat > final_link.sh < max_width) { if (vm.config.debug) console.warn("too wide : have",max_width,"want",want_w) want_w = max_width - want_h = want_h / ar + want_h = Math.trunc(want_h / ar) } @@ -805,13 +805,13 @@ console.warn("TODO: user defined canvas") if (want_h > max_height) { //console.warn ("Too much H") want_h = max_height - want_w = want_h * ar + want_w = Math.trunc(want_h * ar) } if (want_w > max_width) { //console.warn("Too much W") want_w = max_width - want_h = want_h / ar + want_h = Math.trunc(want_h / ar) } // restore phy size