2007-03-21 10:57:17 +08:00
|
|
|
/*
|
|
|
|
* atexit - allow programmer to define multiple exit functions to be executed
|
|
|
|
* upon normal program termination.
|
|
|
|
*
|
|
|
|
* Translated from atexit.py by Collin Winter.
|
|
|
|
+ Copyright 2007 Python Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Python.h"
|
2020-12-15 05:40:40 +08:00
|
|
|
#include "pycore_interp.h" // PyInterpreterState.atexit_func
|
|
|
|
#include "pycore_pystate.h" // _PyInterpreterState_GET
|
2007-03-24 06:46:49 +08:00
|
|
|
|
2007-03-21 10:57:17 +08:00
|
|
|
/* ===================================================================== */
|
|
|
|
/* Callback machinery. */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject *func;
|
|
|
|
PyObject *args;
|
|
|
|
PyObject *kwargs;
|
|
|
|
} atexit_callback;
|
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
struct atexit_state {
|
2008-10-31 05:34:02 +08:00
|
|
|
atexit_callback **atexit_callbacks;
|
|
|
|
int ncallbacks;
|
|
|
|
int callback_len;
|
2020-12-15 05:40:40 +08:00
|
|
|
};
|
2008-10-31 05:34:02 +08:00
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
static inline struct atexit_state*
|
2020-03-16 21:15:01 +08:00
|
|
|
get_atexit_state(PyObject *module)
|
|
|
|
{
|
|
|
|
void *state = PyModule_GetState(module);
|
|
|
|
assert(state != NULL);
|
2020-12-15 05:40:40 +08:00
|
|
|
return (struct atexit_state *)state;
|
2020-03-16 21:15:01 +08:00
|
|
|
}
|
2008-10-31 05:34:02 +08:00
|
|
|
|
2007-03-21 10:57:17 +08:00
|
|
|
|
2013-08-02 02:56:12 +08:00
|
|
|
static void
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_delete_cb(struct atexit_state *state, int i)
|
2013-08-02 02:56:12 +08:00
|
|
|
{
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_callback *cb = state->atexit_callbacks[i];
|
|
|
|
state->atexit_callbacks[i] = NULL;
|
2013-08-02 02:56:12 +08:00
|
|
|
|
|
|
|
Py_DECREF(cb->func);
|
|
|
|
Py_DECREF(cb->args);
|
|
|
|
Py_XDECREF(cb->kwargs);
|
|
|
|
PyMem_Free(cb);
|
|
|
|
}
|
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
|
2013-08-02 02:56:12 +08:00
|
|
|
/* Clear all callbacks without calling them */
|
|
|
|
static void
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_cleanup(struct atexit_state *state)
|
2013-08-02 02:56:12 +08:00
|
|
|
{
|
|
|
|
atexit_callback *cb;
|
2020-12-15 05:40:40 +08:00
|
|
|
for (int i = 0; i < state->ncallbacks; i++) {
|
|
|
|
cb = state->atexit_callbacks[i];
|
2013-08-02 02:56:12 +08:00
|
|
|
if (cb == NULL)
|
|
|
|
continue;
|
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_delete_cb(state, i);
|
2013-08-02 02:56:12 +08:00
|
|
|
}
|
2020-12-15 05:40:40 +08:00
|
|
|
state->ncallbacks = 0;
|
2013-08-02 02:56:12 +08:00
|
|
|
}
|
|
|
|
|
2014-11-20 19:39:37 +08:00
|
|
|
/* Installed into pylifecycle.c's atexit mechanism */
|
2007-03-21 10:57:17 +08:00
|
|
|
|
2008-09-23 08:52:29 +08:00
|
|
|
static void
|
2020-12-15 06:07:54 +08:00
|
|
|
atexit_callfuncs(PyObject *module, int ignore_exc)
|
2007-03-21 10:57:17 +08:00
|
|
|
{
|
2020-12-15 05:40:40 +08:00
|
|
|
assert(!PyErr_Occurred());
|
2008-10-31 05:34:02 +08:00
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
if (module == NULL) {
|
2007-03-21 10:57:17 +08:00
|
|
|
return;
|
2020-12-15 05:40:40 +08:00
|
|
|
}
|
2008-10-31 05:34:02 +08:00
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
struct atexit_state *state = get_atexit_state(module);
|
|
|
|
if (state->ncallbacks == 0) {
|
2008-10-31 05:34:02 +08:00
|
|
|
return;
|
2020-12-15 05:40:40 +08:00
|
|
|
}
|
2008-10-31 05:34:02 +08:00
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
PyObject *exc_type = NULL, *exc_value, *exc_tb;
|
|
|
|
for (int i = state->ncallbacks - 1; i >= 0; i--) {
|
|
|
|
atexit_callback *cb = state->atexit_callbacks[i];
|
|
|
|
if (cb == NULL) {
|
2007-03-21 10:57:17 +08:00
|
|
|
continue;
|
2020-12-15 05:40:40 +08:00
|
|
|
}
|
2007-03-21 10:57:17 +08:00
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
PyObject *res = PyObject_Call(cb->func, cb->args, cb->kwargs);
|
|
|
|
if (res == NULL) {
|
2020-12-15 06:07:54 +08:00
|
|
|
if (ignore_exc) {
|
|
|
|
_PyErr_WriteUnraisableMsg("in atexit callback", cb->func);
|
2007-03-21 10:57:17 +08:00
|
|
|
}
|
2020-12-15 06:07:54 +08:00
|
|
|
else {
|
|
|
|
/* Maintain the last exception, but don't leak if there are
|
|
|
|
multiple exceptions. */
|
|
|
|
if (exc_type) {
|
|
|
|
Py_DECREF(exc_type);
|
|
|
|
Py_XDECREF(exc_value);
|
|
|
|
Py_XDECREF(exc_tb);
|
|
|
|
}
|
|
|
|
PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
|
|
|
|
if (!PyErr_GivenExceptionMatches(exc_type, PyExc_SystemExit)) {
|
|
|
|
PySys_WriteStderr("Error in atexit._run_exitfuncs:\n");
|
|
|
|
PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb);
|
|
|
|
PyErr_Display(exc_type, exc_value, exc_tb);
|
|
|
|
}
|
2007-03-21 10:57:17 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-15 05:40:40 +08:00
|
|
|
else {
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
2007-03-21 10:57:17 +08:00
|
|
|
}
|
2008-10-31 05:34:02 +08:00
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_cleanup(state);
|
2008-09-23 08:52:29 +08:00
|
|
|
|
2020-12-15 06:07:54 +08:00
|
|
|
if (ignore_exc) {
|
|
|
|
assert(!PyErr_Occurred());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (exc_type) {
|
|
|
|
PyErr_Restore(exc_type, exc_value, exc_tb);
|
|
|
|
}
|
2020-12-15 05:40:40 +08:00
|
|
|
}
|
2007-03-21 10:57:17 +08:00
|
|
|
}
|
|
|
|
|
2020-12-15 06:07:54 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
_PyAtExit_Call(PyObject *module)
|
|
|
|
{
|
|
|
|
atexit_callfuncs(module, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-21 10:57:17 +08:00
|
|
|
/* ===================================================================== */
|
|
|
|
/* Module methods. */
|
|
|
|
|
|
|
|
PyDoc_STRVAR(atexit_register__doc__,
|
|
|
|
"register(func, *args, **kwargs) -> func\n\
|
|
|
|
\n\
|
|
|
|
Register a function to be executed upon normal program termination\n\
|
|
|
|
\n\
|
|
|
|
func - function to be called at exit\n\
|
|
|
|
args - optional arguments to pass to func\n\
|
|
|
|
kwargs - optional keyword arguments to pass to func\n\
|
|
|
|
\n\
|
|
|
|
func is returned to facilitate usage as a decorator.");
|
|
|
|
|
|
|
|
static PyObject *
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_register(PyObject *module, PyObject *args, PyObject *kwargs)
|
2007-03-21 10:57:17 +08:00
|
|
|
{
|
|
|
|
if (PyTuple_GET_SIZE(args) == 0) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"register() takes at least 1 argument (0 given)");
|
2015-03-19 03:53:15 +08:00
|
|
|
return NULL;
|
2007-03-21 10:57:17 +08:00
|
|
|
}
|
2008-10-31 05:34:02 +08:00
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
PyObject *func = PyTuple_GET_ITEM(args, 0);
|
2007-03-21 10:57:17 +08:00
|
|
|
if (!PyCallable_Check(func)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"the first argument must be callable");
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-10-31 05:34:02 +08:00
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
struct atexit_state *state = get_atexit_state(module);
|
|
|
|
if (state->ncallbacks >= state->callback_len) {
|
|
|
|
atexit_callback **r;
|
|
|
|
state->callback_len += 16;
|
|
|
|
r = (atexit_callback**)PyMem_Realloc(state->atexit_callbacks,
|
|
|
|
sizeof(atexit_callback*) * state->callback_len);
|
|
|
|
if (r == NULL)
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
state->atexit_callbacks = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
atexit_callback *callback = PyMem_Malloc(sizeof(atexit_callback));
|
|
|
|
if (callback == NULL) {
|
2015-03-19 03:53:15 +08:00
|
|
|
return PyErr_NoMemory();
|
2020-12-15 05:40:40 +08:00
|
|
|
}
|
2007-03-21 10:57:17 +08:00
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
callback->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
|
|
|
|
if (callback->args == NULL) {
|
|
|
|
PyMem_Free(callback);
|
2007-03-21 10:57:17 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-12-15 05:40:40 +08:00
|
|
|
callback->func = Py_NewRef(func);
|
|
|
|
callback->kwargs = Py_XNewRef(kwargs);
|
2008-10-31 05:34:02 +08:00
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
state->atexit_callbacks[state->ncallbacks++] = callback;
|
2008-10-31 05:34:02 +08:00
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
return Py_NewRef(func);
|
2007-03-21 10:57:17 +08:00
|
|
|
}
|
|
|
|
|
2007-08-07 04:59:28 +08:00
|
|
|
PyDoc_STRVAR(atexit_run_exitfuncs__doc__,
|
|
|
|
"_run_exitfuncs() -> None\n\
|
|
|
|
\n\
|
|
|
|
Run all registered exit functions.");
|
|
|
|
|
2007-03-21 10:57:17 +08:00
|
|
|
static PyObject *
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_run_exitfuncs(PyObject *module, PyObject *unused)
|
2007-03-21 10:57:17 +08:00
|
|
|
{
|
2020-12-15 06:07:54 +08:00
|
|
|
atexit_callfuncs(module, 0);
|
2020-12-15 05:40:40 +08:00
|
|
|
if (PyErr_Occurred()) {
|
2007-03-21 10:57:17 +08:00
|
|
|
return NULL;
|
2020-12-15 05:40:40 +08:00
|
|
|
}
|
2007-03-21 10:57:17 +08:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2007-08-07 04:59:28 +08:00
|
|
|
PyDoc_STRVAR(atexit_clear__doc__,
|
|
|
|
"_clear() -> None\n\
|
|
|
|
\n\
|
|
|
|
Clear the list of previously registered exit functions.");
|
|
|
|
|
2007-03-21 10:57:17 +08:00
|
|
|
static PyObject *
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_clear(PyObject *module, PyObject *unused)
|
2013-08-02 02:56:12 +08:00
|
|
|
{
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_cleanup(get_atexit_state(module));
|
2013-08-02 02:56:12 +08:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(atexit_ncallbacks__doc__,
|
|
|
|
"_ncallbacks() -> int\n\
|
|
|
|
\n\
|
|
|
|
Return the number of registered exit functions.");
|
|
|
|
|
|
|
|
static PyObject *
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_ncallbacks(PyObject *module, PyObject *unused)
|
2007-03-21 10:57:17 +08:00
|
|
|
{
|
2020-12-15 05:40:40 +08:00
|
|
|
struct atexit_state *state = get_atexit_state(module);
|
|
|
|
return PyLong_FromSsize_t(state->ncallbacks);
|
2013-08-02 02:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_m_traverse(PyObject *module, visitproc visit, void *arg)
|
2013-08-02 02:56:12 +08:00
|
|
|
{
|
2020-12-15 05:40:40 +08:00
|
|
|
struct atexit_state *state = (struct atexit_state *)PyModule_GetState(module);
|
|
|
|
for (int i = 0; i < state->ncallbacks; i++) {
|
|
|
|
atexit_callback *cb = state->atexit_callbacks[i];
|
2020-03-18 01:09:46 +08:00
|
|
|
if (cb == NULL)
|
|
|
|
continue;
|
|
|
|
Py_VISIT(cb->func);
|
|
|
|
Py_VISIT(cb->args);
|
|
|
|
Py_VISIT(cb->kwargs);
|
2007-03-21 10:57:17 +08:00
|
|
|
}
|
2013-08-02 02:56:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_m_clear(PyObject *module)
|
2013-08-02 02:56:12 +08:00
|
|
|
{
|
2020-12-15 05:40:40 +08:00
|
|
|
struct atexit_state *state = (struct atexit_state *)PyModule_GetState(module);
|
|
|
|
atexit_cleanup(state);
|
2013-08-02 02:56:12 +08:00
|
|
|
return 0;
|
2007-03-21 10:57:17 +08:00
|
|
|
}
|
|
|
|
|
2012-03-27 17:49:21 +08:00
|
|
|
static void
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_free(PyObject *module)
|
2012-03-27 17:49:21 +08:00
|
|
|
{
|
2020-12-15 05:40:40 +08:00
|
|
|
struct atexit_state *state = (struct atexit_state *)PyModule_GetState(module);
|
|
|
|
atexit_cleanup(state);
|
|
|
|
PyMem_Free(state->atexit_callbacks);
|
2012-03-27 17:49:21 +08:00
|
|
|
}
|
|
|
|
|
2007-08-07 04:59:28 +08:00
|
|
|
PyDoc_STRVAR(atexit_unregister__doc__,
|
|
|
|
"unregister(func) -> None\n\
|
|
|
|
\n\
|
2015-11-02 11:37:02 +08:00
|
|
|
Unregister an exit function which was previously registered using\n\
|
2007-08-07 04:59:28 +08:00
|
|
|
atexit.register\n\
|
|
|
|
\n\
|
|
|
|
func - function to be unregistered");
|
|
|
|
|
2007-03-21 10:57:17 +08:00
|
|
|
static PyObject *
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_unregister(PyObject *module, PyObject *func)
|
2007-03-21 10:57:17 +08:00
|
|
|
{
|
2020-12-15 05:40:40 +08:00
|
|
|
struct atexit_state *state = get_atexit_state(module);
|
|
|
|
for (int i = 0; i < state->ncallbacks; i++)
|
2007-03-21 10:57:17 +08:00
|
|
|
{
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_callback *cb = state->atexit_callbacks[i];
|
|
|
|
if (cb == NULL) {
|
2007-03-21 10:57:17 +08:00
|
|
|
continue;
|
2020-12-15 05:40:40 +08:00
|
|
|
}
|
2008-10-31 05:34:02 +08:00
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
int eq = PyObject_RichCompareBool(cb->func, func, Py_EQ);
|
|
|
|
if (eq < 0) {
|
2007-03-21 10:57:17 +08:00
|
|
|
return NULL;
|
2020-12-15 05:40:40 +08:00
|
|
|
}
|
|
|
|
if (eq) {
|
|
|
|
atexit_delete_cb(state, i);
|
|
|
|
}
|
2007-03-21 10:57:17 +08:00
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2020-12-15 05:40:40 +08:00
|
|
|
|
2007-03-21 10:57:17 +08:00
|
|
|
static PyMethodDef atexit_methods[] = {
|
2018-11-27 19:27:31 +08:00
|
|
|
{"register", (PyCFunction)(void(*)(void)) atexit_register, METH_VARARGS|METH_KEYWORDS,
|
2007-03-21 10:57:17 +08:00
|
|
|
atexit_register__doc__},
|
|
|
|
{"_clear", (PyCFunction) atexit_clear, METH_NOARGS,
|
2007-08-07 04:59:28 +08:00
|
|
|
atexit_clear__doc__},
|
2007-03-21 10:57:17 +08:00
|
|
|
{"unregister", (PyCFunction) atexit_unregister, METH_O,
|
2007-08-07 04:59:28 +08:00
|
|
|
atexit_unregister__doc__},
|
2007-03-21 10:57:17 +08:00
|
|
|
{"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS,
|
2007-08-07 04:59:28 +08:00
|
|
|
atexit_run_exitfuncs__doc__},
|
2013-08-02 02:56:12 +08:00
|
|
|
{"_ncallbacks", (PyCFunction) atexit_ncallbacks, METH_NOARGS,
|
|
|
|
atexit_ncallbacks__doc__},
|
2007-03-21 10:57:17 +08:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ===================================================================== */
|
|
|
|
/* Initialization function. */
|
|
|
|
|
|
|
|
PyDoc_STRVAR(atexit__doc__,
|
2020-07-27 07:33:00 +08:00
|
|
|
"allow programmer to define multiple exit functions to be executed\n\
|
2007-03-21 10:57:17 +08:00
|
|
|
upon normal program termination.\n\
|
|
|
|
\n\
|
2007-08-07 04:59:28 +08:00
|
|
|
Two public functions, register and unregister, are defined.\n\
|
2007-03-21 10:57:17 +08:00
|
|
|
");
|
|
|
|
|
2017-12-20 18:17:58 +08:00
|
|
|
static int
|
2020-12-15 05:40:40 +08:00
|
|
|
atexit_exec(PyObject *module)
|
|
|
|
{
|
|
|
|
struct atexit_state *state = get_atexit_state(module);
|
|
|
|
state->callback_len = 32;
|
|
|
|
state->ncallbacks = 0;
|
|
|
|
state->atexit_callbacks = PyMem_New(atexit_callback*, state->callback_len);
|
|
|
|
if (state->atexit_callbacks == NULL) {
|
2017-12-20 18:17:58 +08:00
|
|
|
return -1;
|
2020-12-15 05:40:40 +08:00
|
|
|
}
|
2017-12-20 18:17:58 +08:00
|
|
|
|
2020-12-15 06:07:54 +08:00
|
|
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
|
|
|
interp->atexit_module = module;
|
2017-12-20 18:17:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyModuleDef_Slot atexit_slots[] = {
|
|
|
|
{Py_mod_exec, atexit_exec},
|
|
|
|
{0, NULL}
|
|
|
|
};
|
2008-06-11 13:26:20 +08:00
|
|
|
|
|
|
|
static struct PyModuleDef atexitmodule = {
|
2013-08-02 02:56:12 +08:00
|
|
|
PyModuleDef_HEAD_INIT,
|
2020-12-15 05:40:40 +08:00
|
|
|
.m_name = "atexit",
|
|
|
|
.m_doc = atexit__doc__,
|
|
|
|
.m_size = sizeof(struct atexit_state),
|
|
|
|
.m_methods = atexit_methods,
|
|
|
|
.m_slots = atexit_slots,
|
|
|
|
.m_traverse = atexit_m_traverse,
|
|
|
|
.m_clear = atexit_m_clear,
|
|
|
|
.m_free = (freefunc)atexit_free
|
2008-06-11 13:26:20 +08:00
|
|
|
};
|
|
|
|
|
2007-03-21 10:57:17 +08:00
|
|
|
PyMODINIT_FUNC
|
2008-06-11 13:26:20 +08:00
|
|
|
PyInit_atexit(void)
|
2007-03-21 10:57:17 +08:00
|
|
|
{
|
2017-12-20 18:17:58 +08:00
|
|
|
return PyModuleDef_Init(&atexitmodule);
|
2007-03-21 10:57:17 +08:00
|
|
|
}
|