From 92a1a5c0145155c78e852f34561047e2737c906b Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Wed, 9 Nov 2022 21:37:36 -0800 Subject: [PATCH] Fix exit in command line runner (#3241) Don't set error code to 1 if the program calls exit(0)! --- docs/project/changelog.md | 4 ++-- src/tests/test_cmdline_runner.py | 19 +++++++++++++++++++ tools/python | 8 ++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/docs/project/changelog.md b/docs/project/changelog.md index 0b48e1ebb..678502194 100644 --- a/docs/project/changelog.md +++ b/docs/project/changelog.md @@ -20,8 +20,8 @@ substitutions: - {{ Enhancement }} Added a system for making Pyodide virtual environments. This is for testing out of tree builds. For more information, see [the documentation](https://pyodide.org/en/stable/development/out-of-tree.html). - {pr}`2976`, {pr}`3039`, {pr}`3040`, {pr}`3044`, {pr}`3044`, {pr}`3096`, - {pr}`3098`, {pr}`3108`, {pr}`3109` + {pr}`2976`, {pr}`3039`, {pr}`3040`, {pr}`3044`, {pr}`3096`, {pr}`3098`, + {pr}`3108`, {pr}`3109`, {pr}`3241` - {{ Enhancement }} Users can do a static import of `pyodide/pyodide.asm.js` to avoid issues with dynamic imports. This allows the use of Pyodide with diff --git a/src/tests/test_cmdline_runner.py b/src/tests/test_cmdline_runner.py index f4816ba20..fda07887e 100644 --- a/src/tests/test_cmdline_runner.py +++ b/src/tests/test_cmdline_runner.py @@ -425,3 +425,22 @@ def test_pypa_index(tmp_path): stdout.strip().rsplit("\n", 1)[-1] == "Successfully installed attrs-* micropip-* numpy-* sharedlib-test-py-*" ) + + +def test_sys_exit(selenium, venv): + result = subprocess.run( + [venv / "bin/python", "-c", "import sys; sys.exit(0)"], + capture_output=True, + encoding="utf-8", + ) + assert result.returncode == 0 + assert result.stdout == "" + assert result.stderr == "" + result = subprocess.run( + [venv / "bin/python", "-c", "import sys; sys.exit(12)"], + capture_output=True, + encoding="utf-8", + ) + assert result.returncode == 12 + assert result.stdout == "" + assert result.stderr == "" diff --git a/tools/python b/tools/python index d880fc984..4e65f4fee 100755 --- a/tools/python +++ b/tools/python @@ -241,8 +241,12 @@ async function main() { try { errcode = py._module._run_main(); } catch (e) { - // If there is some sort of error, add the Python tracebook in addition - // to the JavaScript traceback + // If someone called exit, just exit with the right return code. + if(e.constructor.name === "ExitStatus"){ + process.exit(e.status); + } + // Otherwise if there is some sort of error, include the Python + // tracebook in addition to the JavaScript traceback py._module._dump_traceback(); throw e; }