Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is sys.exit equivalent to raise SystemExit?

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?

like image 931
Tobias Kienzler Avatar asked Mar 30 '16 07:03

Tobias Kienzler


2 Answers

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;
}
like image 101
falsetru Avatar answered Oct 12 '22 02:10

falsetru


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

like image 32
vblazhnov Avatar answered Oct 12 '22 01:10

vblazhnov