blob: 8cef64ceb9b6bffb584674d3fb782980c2524f41 [file] [log] [blame]
Collin Winter670e6922007-03-21 02:57:17 +00001/*
2 * atexit - allow programmer to define multiple exit functions to be executed
3 * upon normal program termination.
4 *
5 * Translated from atexit.py by Collin Winter.
6 + Copyright 2007 Python Software Foundation.
7 */
8
9#include "Python.h"
10
Collin Winter3e81ec82007-03-23 22:46:49 +000011/* Forward declaration (for atexit_cleanup) */
Christian Heimes9c94ba42008-10-30 21:34:02 +000012static PyObject *atexit_clear(PyObject*, PyObject*);
Christian Heimes9c94ba42008-10-30 21:34:02 +000013/* Forward declaration of module object */
14static struct PyModuleDef atexitmodule;
Collin Winter3e81ec82007-03-23 22:46:49 +000015
Collin Winter670e6922007-03-21 02:57:17 +000016/* ===================================================================== */
17/* Callback machinery. */
18
19typedef struct {
20 PyObject *func;
21 PyObject *args;
22 PyObject *kwargs;
23} atexit_callback;
24
Christian Heimes9c94ba42008-10-30 21:34:02 +000025typedef struct {
26 atexit_callback **atexit_callbacks;
27 int ncallbacks;
28 int callback_len;
29} atexitmodule_state;
30
Hai Shif707d942020-03-16 21:15:01 +080031static inline atexitmodule_state*
32get_atexit_state(PyObject *module)
33{
34 void *state = PyModule_GetState(module);
35 assert(state != NULL);
36 return (atexitmodule_state *)state;
37}
Christian Heimes9c94ba42008-10-30 21:34:02 +000038
Collin Winter670e6922007-03-21 02:57:17 +000039
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020040static void
41atexit_delete_cb(atexitmodule_state *modstate, int i)
42{
43 atexit_callback *cb;
44
45 cb = modstate->atexit_callbacks[i];
46 modstate->atexit_callbacks[i] = NULL;
47 Py_DECREF(cb->func);
48 Py_DECREF(cb->args);
49 Py_XDECREF(cb->kwargs);
50 PyMem_Free(cb);
51}
52
53/* Clear all callbacks without calling them */
54static void
55atexit_cleanup(atexitmodule_state *modstate)
56{
57 atexit_callback *cb;
58 int i;
59 for (i = 0; i < modstate->ncallbacks; i++) {
60 cb = modstate->atexit_callbacks[i];
61 if (cb == NULL)
62 continue;
63
64 atexit_delete_cb(modstate, i);
65 }
66 modstate->ncallbacks = 0;
67}
68
Nick Coghland6009512014-11-20 21:39:37 +100069/* Installed into pylifecycle.c's atexit mechanism */
Collin Winter670e6922007-03-21 02:57:17 +000070
Skip Montanaro711552b2008-09-23 00:52:29 +000071static void
Marcel Plch776407f2017-12-20 11:17:58 +010072atexit_callfuncs(PyObject *module)
Collin Winter670e6922007-03-21 02:57:17 +000073{
74 PyObject *exc_type = NULL, *exc_value, *exc_tb, *r;
75 atexit_callback *cb;
Christian Heimes9c94ba42008-10-30 21:34:02 +000076 atexitmodule_state *modstate;
Collin Winter670e6922007-03-21 02:57:17 +000077 int i;
Christian Heimes9c94ba42008-10-30 21:34:02 +000078
Christian Heimes9c94ba42008-10-30 21:34:02 +000079 if (module == NULL)
Collin Winter670e6922007-03-21 02:57:17 +000080 return;
Hai Shif707d942020-03-16 21:15:01 +080081 modstate = get_atexit_state(module);
Christian Heimes9c94ba42008-10-30 21:34:02 +000082
83 if (modstate->ncallbacks == 0)
84 return;
85
86
87 for (i = modstate->ncallbacks - 1; i >= 0; i--)
Collin Winter670e6922007-03-21 02:57:17 +000088 {
Christian Heimes9c94ba42008-10-30 21:34:02 +000089 cb = modstate->atexit_callbacks[i];
Collin Winter670e6922007-03-21 02:57:17 +000090 if (cb == NULL)
91 continue;
92
93 r = PyObject_Call(cb->func, cb->args, cb->kwargs);
94 Py_XDECREF(r);
95 if (r == NULL) {
Neal Norwitz7d71fb82007-03-21 04:45:04 +000096 /* Maintain the last exception, but don't leak if there are
97 multiple exceptions. */
Collin Winter670e6922007-03-21 02:57:17 +000098 if (exc_type) {
99 Py_DECREF(exc_type);
Neal Norwitz7d71fb82007-03-21 04:45:04 +0000100 Py_XDECREF(exc_value);
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200101 Py_XDECREF(exc_tb);
Collin Winter670e6922007-03-21 02:57:17 +0000102 }
103 PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
Serhiy Storchaka3fd54d42017-06-12 08:25:04 +0300104 if (!PyErr_GivenExceptionMatches(exc_type, PyExc_SystemExit)) {
Collin Winter670e6922007-03-21 02:57:17 +0000105 PySys_WriteStderr("Error in atexit._run_exitfuncs:\n");
Victor Stinner358e11d2011-01-05 03:54:25 +0000106 PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb);
Collin Winter670e6922007-03-21 02:57:17 +0000107 PyErr_Display(exc_type, exc_value, exc_tb);
108 }
109 }
110 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000111
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200112 atexit_cleanup(modstate);
Skip Montanaro711552b2008-09-23 00:52:29 +0000113
Collin Winter670e6922007-03-21 02:57:17 +0000114 if (exc_type)
115 PyErr_Restore(exc_type, exc_value, exc_tb);
116}
117
Collin Winter670e6922007-03-21 02:57:17 +0000118/* ===================================================================== */
119/* Module methods. */
120
121PyDoc_STRVAR(atexit_register__doc__,
122"register(func, *args, **kwargs) -> func\n\
123\n\
124Register a function to be executed upon normal program termination\n\
125\n\
126 func - function to be called at exit\n\
127 args - optional arguments to pass to func\n\
128 kwargs - optional keyword arguments to pass to func\n\
129\n\
130 func is returned to facilitate usage as a decorator.");
131
132static PyObject *
133atexit_register(PyObject *self, PyObject *args, PyObject *kwargs)
134{
Christian Heimes9c94ba42008-10-30 21:34:02 +0000135 atexitmodule_state *modstate;
Collin Winter670e6922007-03-21 02:57:17 +0000136 atexit_callback *new_callback;
137 PyObject *func = NULL;
Christian Heimes9c94ba42008-10-30 21:34:02 +0000138
Hai Shif707d942020-03-16 21:15:01 +0800139 modstate = get_atexit_state(self);
Christian Heimes9c94ba42008-10-30 21:34:02 +0000140
141 if (modstate->ncallbacks >= modstate->callback_len) {
Collin Winter3e81ec82007-03-23 22:46:49 +0000142 atexit_callback **r;
Christian Heimes9c94ba42008-10-30 21:34:02 +0000143 modstate->callback_len += 16;
144 r = (atexit_callback**)PyMem_Realloc(modstate->atexit_callbacks,
145 sizeof(atexit_callback*) * modstate->callback_len);
Collin Winter3e81ec82007-03-23 22:46:49 +0000146 if (r == NULL)
147 return PyErr_NoMemory();
Christian Heimes9c94ba42008-10-30 21:34:02 +0000148 modstate->atexit_callbacks = r;
Collin Winter670e6922007-03-21 02:57:17 +0000149 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000150
Collin Winter670e6922007-03-21 02:57:17 +0000151 if (PyTuple_GET_SIZE(args) == 0) {
152 PyErr_SetString(PyExc_TypeError,
153 "register() takes at least 1 argument (0 given)");
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200154 return NULL;
Collin Winter670e6922007-03-21 02:57:17 +0000155 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000156
Collin Winter670e6922007-03-21 02:57:17 +0000157 func = PyTuple_GET_ITEM(args, 0);
158 if (!PyCallable_Check(func)) {
159 PyErr_SetString(PyExc_TypeError,
160 "the first argument must be callable");
161 return NULL;
162 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000163
Collin Winter670e6922007-03-21 02:57:17 +0000164 new_callback = PyMem_Malloc(sizeof(atexit_callback));
165 if (new_callback == NULL)
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200166 return PyErr_NoMemory();
Collin Winter670e6922007-03-21 02:57:17 +0000167
168 new_callback->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
169 if (new_callback->args == NULL) {
170 PyMem_Free(new_callback);
171 return NULL;
172 }
173 new_callback->func = func;
174 new_callback->kwargs = kwargs;
175 Py_INCREF(func);
176 Py_XINCREF(kwargs);
Christian Heimes9c94ba42008-10-30 21:34:02 +0000177
178 modstate->atexit_callbacks[modstate->ncallbacks++] = new_callback;
179
Collin Winter670e6922007-03-21 02:57:17 +0000180 Py_INCREF(func);
181 return func;
182}
183
Skip Montanaro28a181c2007-08-06 20:59:28 +0000184PyDoc_STRVAR(atexit_run_exitfuncs__doc__,
185"_run_exitfuncs() -> None\n\
186\n\
187Run all registered exit functions.");
188
Collin Winter670e6922007-03-21 02:57:17 +0000189static PyObject *
Christian Heimes9c94ba42008-10-30 21:34:02 +0000190atexit_run_exitfuncs(PyObject *self, PyObject *unused)
Collin Winter670e6922007-03-21 02:57:17 +0000191{
Marcel Plch776407f2017-12-20 11:17:58 +0100192 atexit_callfuncs(self);
Collin Winter670e6922007-03-21 02:57:17 +0000193 if (PyErr_Occurred())
194 return NULL;
195 Py_RETURN_NONE;
196}
197
Skip Montanaro28a181c2007-08-06 20:59:28 +0000198PyDoc_STRVAR(atexit_clear__doc__,
199"_clear() -> None\n\
200\n\
201Clear the list of previously registered exit functions.");
202
Collin Winter670e6922007-03-21 02:57:17 +0000203static PyObject *
Christian Heimes9c94ba42008-10-30 21:34:02 +0000204atexit_clear(PyObject *self, PyObject *unused)
Collin Winter670e6922007-03-21 02:57:17 +0000205{
Hai Shif707d942020-03-16 21:15:01 +0800206 atexit_cleanup(get_atexit_state(self));
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200207 Py_RETURN_NONE;
208}
209
210PyDoc_STRVAR(atexit_ncallbacks__doc__,
211"_ncallbacks() -> int\n\
212\n\
213Return the number of registered exit functions.");
214
215static PyObject *
216atexit_ncallbacks(PyObject *self, PyObject *unused)
217{
Christian Heimes9c94ba42008-10-30 21:34:02 +0000218 atexitmodule_state *modstate;
Christian Heimes9c94ba42008-10-30 21:34:02 +0000219
Hai Shif707d942020-03-16 21:15:01 +0800220 modstate = get_atexit_state(self);
Christian Heimes9c94ba42008-10-30 21:34:02 +0000221
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200222 return PyLong_FromSsize_t(modstate->ncallbacks);
223}
224
225static int
226atexit_m_traverse(PyObject *self, visitproc visit, void *arg)
227{
228 int i;
229 atexitmodule_state *modstate;
230
Hai Shif707d942020-03-16 21:15:01 +0800231 modstate = (atexitmodule_state *)PyModule_GetState(self);
Victor Stinner5b1ef202020-03-17 18:09:46 +0100232
233 for (i = 0; i < modstate->ncallbacks; i++) {
234 atexit_callback *cb = modstate->atexit_callbacks[i];
235 if (cb == NULL)
236 continue;
237 Py_VISIT(cb->func);
238 Py_VISIT(cb->args);
239 Py_VISIT(cb->kwargs);
Collin Winter670e6922007-03-21 02:57:17 +0000240 }
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200241 return 0;
242}
243
244static int
245atexit_m_clear(PyObject *self)
246{
247 atexitmodule_state *modstate;
Hai Shif707d942020-03-16 21:15:01 +0800248 modstate = (atexitmodule_state *)PyModule_GetState(self);
Victor Stinner5b1ef202020-03-17 18:09:46 +0100249 atexit_cleanup(modstate);
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200250 return 0;
Collin Winter670e6922007-03-21 02:57:17 +0000251}
252
Stefan Krah650365b2012-03-27 11:49:21 +0200253static void
254atexit_free(PyObject *m)
255{
256 atexitmodule_state *modstate;
Hai Shif707d942020-03-16 21:15:01 +0800257 modstate = (atexitmodule_state *)PyModule_GetState(m);
Victor Stinner5b1ef202020-03-17 18:09:46 +0100258 atexit_cleanup(modstate);
259 PyMem_Free(modstate->atexit_callbacks);
Stefan Krah650365b2012-03-27 11:49:21 +0200260}
261
Skip Montanaro28a181c2007-08-06 20:59:28 +0000262PyDoc_STRVAR(atexit_unregister__doc__,
263"unregister(func) -> None\n\
264\n\
Martin Panter7462b6492015-11-02 03:37:02 +0000265Unregister an exit function which was previously registered using\n\
Skip Montanaro28a181c2007-08-06 20:59:28 +0000266atexit.register\n\
267\n\
268 func - function to be unregistered");
269
Collin Winter670e6922007-03-21 02:57:17 +0000270static PyObject *
271atexit_unregister(PyObject *self, PyObject *func)
272{
Christian Heimes9c94ba42008-10-30 21:34:02 +0000273 atexitmodule_state *modstate;
Collin Winter670e6922007-03-21 02:57:17 +0000274 atexit_callback *cb;
275 int i, eq;
Christian Heimes9c94ba42008-10-30 21:34:02 +0000276
Hai Shif707d942020-03-16 21:15:01 +0800277 modstate = get_atexit_state(self);
Christian Heimes9c94ba42008-10-30 21:34:02 +0000278
279 for (i = 0; i < modstate->ncallbacks; i++)
Collin Winter670e6922007-03-21 02:57:17 +0000280 {
Christian Heimes9c94ba42008-10-30 21:34:02 +0000281 cb = modstate->atexit_callbacks[i];
Collin Winter670e6922007-03-21 02:57:17 +0000282 if (cb == NULL)
283 continue;
Christian Heimes9c94ba42008-10-30 21:34:02 +0000284
Collin Winter670e6922007-03-21 02:57:17 +0000285 eq = PyObject_RichCompareBool(cb->func, func, Py_EQ);
286 if (eq < 0)
287 return NULL;
288 if (eq)
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200289 atexit_delete_cb(modstate, i);
Collin Winter670e6922007-03-21 02:57:17 +0000290 }
291 Py_RETURN_NONE;
292}
293
294static PyMethodDef atexit_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200295 {"register", (PyCFunction)(void(*)(void)) atexit_register, METH_VARARGS|METH_KEYWORDS,
Collin Winter670e6922007-03-21 02:57:17 +0000296 atexit_register__doc__},
297 {"_clear", (PyCFunction) atexit_clear, METH_NOARGS,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000298 atexit_clear__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000299 {"unregister", (PyCFunction) atexit_unregister, METH_O,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000300 atexit_unregister__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000301 {"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000302 atexit_run_exitfuncs__doc__},
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200303 {"_ncallbacks", (PyCFunction) atexit_ncallbacks, METH_NOARGS,
304 atexit_ncallbacks__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000305 {NULL, NULL} /* sentinel */
306};
307
308/* ===================================================================== */
309/* Initialization function. */
310
311PyDoc_STRVAR(atexit__doc__,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000312"allow programmer to define multiple exit functions to be executed\
Collin Winter670e6922007-03-21 02:57:17 +0000313upon normal program termination.\n\
314\n\
Skip Montanaro28a181c2007-08-06 20:59:28 +0000315Two public functions, register and unregister, are defined.\n\
Collin Winter670e6922007-03-21 02:57:17 +0000316");
317
Marcel Plch776407f2017-12-20 11:17:58 +0100318static int
319atexit_exec(PyObject *m) {
320 atexitmodule_state *modstate;
321
Hai Shif707d942020-03-16 21:15:01 +0800322 modstate = get_atexit_state(m);
Marcel Plch776407f2017-12-20 11:17:58 +0100323 modstate->callback_len = 32;
324 modstate->ncallbacks = 0;
325 modstate->atexit_callbacks = PyMem_New(atexit_callback*,
326 modstate->callback_len);
327 if (modstate->atexit_callbacks == NULL)
328 return -1;
329
330 _Py_PyAtExit(atexit_callfuncs, m);
331 return 0;
332}
333
334static PyModuleDef_Slot atexit_slots[] = {
335 {Py_mod_exec, atexit_exec},
336 {0, NULL}
337};
Martin v. Löwis1a214512008-06-11 05:26:20 +0000338
339static struct PyModuleDef atexitmodule = {
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200340 PyModuleDef_HEAD_INIT,
341 "atexit",
342 atexit__doc__,
343 sizeof(atexitmodule_state),
344 atexit_methods,
Marcel Plch776407f2017-12-20 11:17:58 +0100345 atexit_slots,
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200346 atexit_m_traverse,
347 atexit_m_clear,
348 (freefunc)atexit_free
Martin v. Löwis1a214512008-06-11 05:26:20 +0000349};
350
Collin Winter670e6922007-03-21 02:57:17 +0000351PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000352PyInit_atexit(void)
Collin Winter670e6922007-03-21 02:57:17 +0000353{
Marcel Plch776407f2017-12-20 11:17:58 +0100354 return PyModuleDef_Init(&atexitmodule);
Collin Winter670e6922007-03-21 02:57:17 +0000355}