Skip to content

Commit 515c013

Browse files
authored
fix playground: do not print traceback for SystemExit (#555)
Fixes #535. ~~It seems that we don't actually need to call `sys.exit(0)` at the end of `execute_spy_main`.~~
2 parents 5e06fff + cad6389 commit 515c013

4 files changed

Lines changed: 15 additions & 12 deletions

File tree

examples/1_high_level/exit_code.spy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""
2+
Things to notice:
3+
- `main()` can take `argv` as a `list[str]` and can return an `i32` exit code
4+
- the exit code is propagated to the OS
5+
- `argv` contains the script/executable name and the script arguments
6+
"""
7+
18
def main(argv: list[str]) -> i32:
29
print("argv:")
310
for arg in argv:

playground/main.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@
3232
libspy.LIBSPY_WASM = str(URL.new("./libspy.mjs", document.baseURI))
3333

3434

35-
def spy_main(argv):
36-
try:
37-
spy.cli.app(argv)
38-
except SystemExit:
39-
pass
40-
41-
4235
# =========== GUI code ==========
4336

4437

@@ -152,7 +145,7 @@ def run_spy_file_with_args(argv: list[str]):
152145
with open(display_filename, "w") as f:
153146
f.write(text)
154147

155-
spy_main(argv)
148+
spy.cli.app(argv)
156149

157150

158151
def run_click(event):

playground/pyscript.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ packages = [ "micropip"]
44
[files]
55
"pyscript.toml" = ""
66
"hello_add.spy" = "examples/1_high_level/"
7+
"exit_code.spy" = "examples/1_high_level/"
78
"bluefunc_pi.spy" = "examples/2_metaprogramming/"
89
"bluefunc_adder.spy" = "examples/2_metaprogramming/"
910
"point.spy" = "examples/3_low_level/"

spy/cli/_runners.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@
2929

3030
async def _pyodide_main(user_func: Callable, args: "Base_Args") -> None:
3131
"""
32-
For some reasons, it seems that pyodide doesn't print exceptions
33-
uncaught exceptions which escapes an asyncio task. This is a small wrapper
34-
to ensure that we display a proper traceback in that case
32+
For some reasons, it seems that pyodide doesn't print uncaught exceptions
33+
which escapes an asyncio task. This is a small wrapper to ensure that we
34+
display a proper traceback in that case.
3535
"""
3636
try:
3737
await _run_command(user_func, args)
38-
except BaseException:
38+
except BaseException as exc:
39+
if isinstance(exc, SystemExit):
40+
return
3941
traceback.print_exc()
4042

4143

0 commit comments

Comments
 (0)