Fix #15132: preserve sys.exit() exit codes in non-interactive mode - #15367
Open
MsfPablo wants to merge 1 commit into
Open
Fix #15132: preserve sys.exit() exit codes in non-interactive mode#15367MsfPablo wants to merge 1 commit into
MsfPablo wants to merge 1 commit into
Conversation
…mode When running `ipython -c "import sys;sys.exit(N)"` or `ipython script.py` (with a script that calls sys.exit(N)), IPython returned exit code 1 instead of the value passed to sys.exit(). The exit code was hardcoded in TerminalIPythonApp.start() and the file execution branch of InteractiveShellApp._run_cmd_line_code(). Extract the original exit code from the SystemExit exception stored on the shell's last_execution_result and propagate it; default to 1 only for genuine execution failures. Handle SystemExit before the bare except: in the file execution path so it exits quietly (without a traceback), matching Python's own behavior. Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #15132
When running
ipython -c "import sys;sys.exit(N)"or executing a script viaipython script.pythat callssys.exit(N), IPython returned exit code 1 instead of the value passed tosys.exit(). The exit code was hardcoded in two places:IPython/terminal/ipapp.py—TerminalIPythonApp.start()calledsys.exit(1)whenever the last execution failed, regardless of whether the failure was aSystemExitraised by user code.IPython/core/shellapp.py—_run_cmd_line_code()had the same hardcodedself.exit(1)for file execution errors.This change extracts the original exit code from the
SystemExitexception thatInteractiveShellstores onlast_execution_result.error_in_exec(and that_exec_fileraises in the file path), and propagates it to the process exit. Exit code 1 is preserved only for genuine execution failures that aren'tSystemExit.Behavior:
sys.exit(0)/sys.exit()→ exit code 0sys.exit(N)for any integerN→ preservesNSystemExitexecution failures → exit code 1 (unchanged)SystemExitin script file path → exits quietly, no tracebackAI disclosure
This PR was authored with assistance from Claude (Anthropic). The implementation, tests, and review were AI-assisted.
Co-Authored-By: Claude noreply@anthropic.com