6

According to the documentation on sys.exit and SystemExit, it seems that

def sys.exit(return_value=None): # or return_value=0 raise SystemExit(return_value) 

is that correct or does sys.exit do something else before?

3 Answers 3

8

According to Python/sysmodule.c, raising SystemExit is all it does.

static PyObject * sys_exit(PyObject *self, PyObject *args) { PyObject *exit_code = 0; if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code)) return NULL; /* Raise SystemExit so callers may catch it or clean up. */ PyErr_SetObject(PyExc_SystemExit, exit_code); return NULL; } 
Sign up to request clarification or add additional context in comments.

Comments

3

As you can see at source code https://github.com/python-git/python/blob/715a6e5035bb21ac49382772076ec4c630d6e960/Python/sysmodule.c

static PyObject * sys_exit(PyObject *self, PyObject *args) { PyObject *exit_code = 0; if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code)) return NULL; /* Raise SystemExit so callers may catch it or clean up. */ PyErr_SetObject(PyExc_SystemExit, exit_code); return NULL; } 

it's only raise SystemExit and doesn't do anything else

1 Comment

thanks :) by the way, if you click on a line number on github, you get a link to that, e.g. github.com/python-git/python/blob/…
3

Yes, raising SystemExit and calling sys.exit are functionally equivalent. See sys module source.

The PyErr_SetObject function is how CPython implements the raising of a Python exception.

1 Comment

thanks - note that when linking to specific lines in github code, it is recommended to press y first in order to get the canonical URL for the commit you're currently at, e.g. today github.com/python/cpython/blob/… - future commits may severely shift the line

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.