10

Assuming your script doesn't exit due to any kind of failure (exception, syntax error) and the script doesn't exit due to sys.exit() or os._exit(), how does Python figure out what exit code to exit with?

It seems to be 0, which makes sense since it indicates no-error on *nix systems. However, will that always be the case that it exits with 0 (except for the cases above)? Surprisingly after quite a bit of online searching, I couldn't find anything which explicitly said that it would exit with 0 unless otherwise specified.

0

4 Answers 4

11

sys.exit documents a default exit status of 0, and os._exit's docs specify a UNIX-like OS constant for "normal" exit status, os.EX_OK, but there is no documented guarantee I can find for the exit status in general.

Aside from that, the best I can give you is that in CPython, the python executable (including python.exe/pythonw.exe on Windows) is implemented in python.c by calling Py_Main and returning whatever it returns; per the documented guarantees on Py_Main, the exit status is:

0 if the interpreter exits normally (i.e., without an exception), 1 if the interpreter exits due to an exception, or 2 if the parameter list does not represent a valid Python command line.

Note that if an otherwise unhandled SystemExit is raised, this function will not return 1, but exit the process, as long as Py_InspectFlag is not set.

so this implies that simply running off the end of the __main__ module without an active exception should always return 0 for CPython, though alternate interpreters are not technically required to do the same.

This tracks with the implied exit status rules expected of most applications; while nothing explicitly says Python has to follow those rules, it would be extremely unusual for a tool that grew up in the command line UNIX-like world to violate those conventions.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ShadowRanger. Martin Broadhurst's answer (tracing more through the code) confirms the Py_Main documentation.
9

If you look at the cpython source code:

  1. main() in Programs/python.c returns the return value of Py_Main()
  2. Py_Main() in Modules/main.c returns the return value of run_file()
  3. run_file(), also in Modules/main.c returns 0 unless PyRun_AnyFileExFlags() returns non-zero
  4. PyRun_AnyFileExFlags() in Python/pythonrun.c will exit() in the event of a SystemExit exception and so will not return if the script sets an exit code. It will only return non-zero if there is an internal error.

So the return value of run_file() is what makes the default exit code of a script 0.

Comments

1

Python's default exit code is 0. This is documented here https://docs.python.org/2/library/exceptions.html#exceptions.SystemExit

However, the exit code will be whatever the is specified in the exit() or quit() functions. So, if you are using a script, you should always check within the script or script documentation what that particular script will return. For example: you could launch python in a terminal, and just type quit(1) and it exits with return code of 1 even though there are no errors.

1 Comment

This isn't quite right because a SystemExit is not raised if the script doesn't call sys.exit().
0

Exiting with 0 predates Python by a long time, it is part of the POSIX standard. All well-behaved programs indicate a successful exit with 0.

2 Comments

Yes, I am aware of that 0 has long been a *nix standard meaning no-error. I just want to see it in writing (or in Python source code) that it always uses 0 if there were no exceptions, errors or explicit requests to exit
@user2926055, that doesn't mean that the convention is adhered to by Python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.