sys.exit 是否等同于引发 SystemExit?
Is sys.exit equivalent to raise SystemExit?
根据 sys.exit
and SystemExit
上的文档,似乎
def sys.exit(return_value=None): # or return_value=0
raise SystemExit(return_value)
这是正确的还是 sys.exit
之前做过其他事情?
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;
}
它只会引发 SystemExit 而不会做任何其他事情
根据 Python/sysmodule.c
,提高 SystemExit
就是它所做的一切。
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;
}
是的,提高 SystemExit
和调用 sys.exit
在功能上是等价的。 See sys
module source.
PyErr_SetObject
函数是 CPython 实现引发 Python 异常的方式。
根据 sys.exit
and SystemExit
上的文档,似乎
def sys.exit(return_value=None): # or return_value=0
raise SystemExit(return_value)
这是正确的还是 sys.exit
之前做过其他事情?
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;
}
它只会引发 SystemExit 而不会做任何其他事情
根据 Python/sysmodule.c
,提高 SystemExit
就是它所做的一切。
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;
}
是的,提高 SystemExit
和调用 sys.exit
在功能上是等价的。 See sys
module source.
PyErr_SetObject
函数是 CPython 实现引发 Python 异常的方式。