2016-03-30 20 views

risposta

7

Secondo Python/sysmodule.c, sollevando SystemExit è tutto ciò che fa.

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; 
} 
3

Come si può vedere il codice sorgente 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; 
} 

è alzare solo SystemExit e non fa niente altro

+0

grazie :) a proposito, se si fa clic su un numero di riga su github, si ottiene un collegamento a tale, ad es. https://github.com/python-git/python/blob/715a6e5035bb21ac49382772076ec4c630d6e960/Python/sysmodule.c#L207 –

3

Sì, alzando SystemExit e chiamando sys.exit sono funzionalmente equivalenti. See sys module source.

La funzione PyErr_SetObject è come CPython implementa l'innalzamento di un'eccezione Python.

+0

grazie - si noti che quando si collega a linee specifiche in codice github, si consiglia di premere 'y' prima in ordine per ottenere l'URL canonico per il commit al quale sei attualmente, ad es oggi https://github.com/python/cpython/blob/2ef4bc37aa5e05910fbdfe7b804a8a5855d87652/Python/sysmodule.c#L262 - i commit futuri potrebbero spostare seriamente la linea –

+0

grazie a Tobias, modificato. – tom