fix: preserve sys.exit() exit codes in non-interactive mode#15158
Open
Sarah-2003 wants to merge 1 commit intoipython:mainfrom
Open
fix: preserve sys.exit() exit codes in non-interactive mode#15158Sarah-2003 wants to merge 1 commit intoipython:mainfrom
Sarah-2003 wants to merge 1 commit intoipython:mainfrom
Conversation
When running `ipython -c "import sys;sys.exit(2)"`, IPython incorrectly returned exit code 1 instead of 2. The exit code was hardcoded to 1 in the start() method of TerminalIPythonApp regardless of the actual code passed to sys.exit(). Extract the original exit code from the stored SystemExit exception in last_execution_result.error_in_exec. Also handle SystemExit separately in the file execution path in _run_cmd_line_code(). Fixes ipython#15132
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.
Summary
Fixes #15132.
When running
ipython -c "import sys;sys.exit(2)", IPython returned exit code 1 instead of 2. Regular Python correctly returns 2.Root cause:
TerminalIPythonApp.start()inipapp.pyhardcodedsys.exit(1)for all failed executions, regardless of the actual exit code passed tosys.exit().Changes
IPython/terminal/ipapp.py-- Extract the original exit code fromlast_execution_result.error_in_execwhen the error is aSystemExit. Falls back to exit code 1 for non-SystemExit failures.IPython/core/shellapp.py-- Add a separateexcept SystemExitclause in the file execution path of_run_cmd_line_code()to preserve exit codes when running script files.tests/test_shellapp.py-- Add 8 tests covering exit code preservation for-cexecution and script file execution (codes 0, 1, 2, 42, no-arg, normal execution).How it works
When
sys.exit(N)is called inside user code,InteractiveShell.run_code()catches theSystemExitand stores it inresult.error_in_exec. The fix readserror_in_exec.codeto extract the original exit code instead of discarding it.Edge cases handled:
sys.exit(0)andsys.exit()-- exit code 0sys.exit(N)for any integer N -- preserves Nsys.exit("message")-- exit code 1 (matches Python behavior)Test plan
ipython -c "import sys; sys.exit(2)"returns code 2 (was 1)ipython -c "import sys; sys.exit(42)"returns code 42 (was 1)ipython -c "import sys; sys.exit(0)"returns code 0ipython -c "print('hello')"returns code 0 (unchanged)sys.exit(2)returns code 2