blob: 1d6d6e53cfbfbe8bb7b312bc7f253095dec67511 [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
31#define GET_ATEXIT_STATE(mod) ((atexitmodule_state*)PyModule_GetState(mod))
32
Collin Winter670e6922007-03-21 02:57:17 +000033
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020034static void
35atexit_delete_cb(atexitmodule_state *modstate, int i)
36{
37 atexit_callback *cb;
38
39 cb = modstate->atexit_callbacks[i];
40 modstate->atexit_callbacks[i] = NULL;
41 Py_DECREF(cb->func);
42 Py_DECREF(cb->args);
43 Py_XDECREF(cb->kwargs);
44 PyMem_Free(cb);
45}
46
47/* Clear all callbacks without calling them */
48static void
49atexit_cleanup(atexitmodule_state *modstate)
50{
51 atexit_callback *cb;
52 int i;
53 for (i = 0; i < modstate->ncallbacks; i++) {
54 cb = modstate->atexit_callbacks[i];
55 if (cb == NULL)
56 continue;
57
58 atexit_delete_cb(modstate, i);
59 }
60 modstate->ncallbacks = 0;
61}
62
Nick Coghland6009512014-11-20 21:39:37 +100063/* Installed into pylifecycle.c's atexit mechanism */
Collin Winter670e6922007-03-21 02:57:17 +000064
Skip Montanaro711552b2008-09-23 00:52:29 +000065static void
Marcel Plch776407f2017-12-20 11:17:58 +010066atexit_callfuncs(PyObject *module)
Collin Winter670e6922007-03-21 02:57:17 +000067{
68 PyObject *exc_type = NULL, *exc_value, *exc_tb, *r;
69 atexit_callback *cb;
Christian Heimes9c94ba42008-10-30 21:34:02 +000070 atexitmodule_state *modstate;
Collin Winter670e6922007-03-21 02:57:17 +000071 int i;
Christian Heimes9c94ba42008-10-30 21:34:02 +000072
Christian Heimes9c94ba42008-10-30 21:34:02 +000073 if (module == NULL)
Collin Winter670e6922007-03-21 02:57:17 +000074 return;
Christian Heimes9c94ba42008-10-30 21:34:02 +000075 modstate = GET_ATEXIT_STATE(module);
76
77 if (modstate->ncallbacks == 0)
78 return;
79
80
81 for (i = modstate->ncallbacks - 1; i >= 0; i--)
Collin Winter670e6922007-03-21 02:57:17 +000082 {
Christian Heimes9c94ba42008-10-30 21:34:02 +000083 cb = modstate->atexit_callbacks[i];
Collin Winter670e6922007-03-21 02:57:17 +000084 if (cb == NULL)
85 continue;
86
87 r = PyObject_Call(cb->func, cb->args, cb->kwargs);
88 Py_XDECREF(r);
89 if (r == NULL) {
Neal Norwitz7d71fb82007-03-21 04:45:04 +000090 /* Maintain the last exception, but don't leak if there are
91 multiple exceptions. */
Collin Winter670e6922007-03-21 02:57:17 +000092 if (exc_type) {
93 Py_DECREF(exc_type);
Neal Norwitz7d71fb82007-03-21 04:45:04 +000094 Py_XDECREF(exc_value);
Serhiy Storchaka009b8112015-03-18 21:53:15 +020095 Py_XDECREF(exc_tb);
Collin Winter670e6922007-03-21 02:57:17 +000096 }
97 PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
Serhiy Storchaka3fd54d42017-06-12 08:25:04 +030098 if (!PyErr_GivenExceptionMatches(exc_type, PyExc_SystemExit)) {
Collin Winter670e6922007-03-21 02:57:17 +000099 PySys_WriteStderr("Error in atexit._run_exitfuncs:\n");
Victor Stinner358e11d2011-01-05 03:54:25 +0000100 PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb);
Collin Winter670e6922007-03-21 02:57:17 +0000101 PyErr_Display(exc_type, exc_value, exc_tb);
102 }
103 }
104 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000105
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200106 atexit_cleanup(modstate);
Skip Montanaro711552b2008-09-23 00:52:29 +0000107
Collin Winter670e6922007-03-21 02:57:17 +0000108 if (exc_type)
109 PyErr_Restore(exc_type, exc_value, exc_tb);
110}
111
Collin Winter670e6922007-03-21 02:57:17 +0000112/* ===================================================================== */
113/* Module methods. */
114
115PyDoc_STRVAR(atexit_register__doc__,
116"register(func, *args, **kwargs) -> func\n\
117\n\
118Register a function to be executed upon normal program termination\n\
119\n\
120 func - function to be called at exit\n\
121 args - optional arguments to pass to func\n\
122 kwargs - optional keyword arguments to pass to func\n\
123\n\
124 func is returned to facilitate usage as a decorator.");
125
126static PyObject *
127atexit_register(PyObject *self, PyObject *args, PyObject *kwargs)
128{
Christian Heimes9c94ba42008-10-30 21:34:02 +0000129 atexitmodule_state *modstate;
Collin Winter670e6922007-03-21 02:57:17 +0000130 atexit_callback *new_callback;
131 PyObject *func = NULL;
Christian Heimes9c94ba42008-10-30 21:34:02 +0000132
133 modstate = GET_ATEXIT_STATE(self);
134
135 if (modstate->ncallbacks >= modstate->callback_len) {
Collin Winter3e81ec82007-03-23 22:46:49 +0000136 atexit_callback **r;
Christian Heimes9c94ba42008-10-30 21:34:02 +0000137 modstate->callback_len += 16;
138 r = (atexit_callback**)PyMem_Realloc(modstate->atexit_callbacks,
139 sizeof(atexit_callback*) * modstate->callback_len);
Collin Winter3e81ec82007-03-23 22:46:49 +0000140 if (r == NULL)
141 return PyErr_NoMemory();
Christian Heimes9c94ba42008-10-30 21:34:02 +0000142 modstate->atexit_callbacks = r;
Collin Winter670e6922007-03-21 02:57:17 +0000143 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000144
Collin Winter670e6922007-03-21 02:57:17 +0000145 if (PyTuple_GET_SIZE(args) == 0) {
146 PyErr_SetString(PyExc_TypeError,
147 "register() takes at least 1 argument (0 given)");
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200148 return NULL;
Collin Winter670e6922007-03-21 02:57:17 +0000149 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000150
Collin Winter670e6922007-03-21 02:57:17 +0000151 func = PyTuple_GET_ITEM(args, 0);
152 if (!PyCallable_Check(func)) {
153 PyErr_SetString(PyExc_TypeError,
154 "the first argument must be callable");
155 return NULL;
156 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000157
Collin Winter670e6922007-03-21 02:57:17 +0000158 new_callback = PyMem_Malloc(sizeof(atexit_callback));
159 if (new_callback == NULL)
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200160 return PyErr_NoMemory();
Collin Winter670e6922007-03-21 02:57:17 +0000161
162 new_callback->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
163 if (new_callback->args == NULL) {
164 PyMem_Free(new_callback);
165 return NULL;
166 }
167 new_callback->func = func;
168 new_callback->kwargs = kwargs;
169 Py_INCREF(func);
170 Py_XINCREF(kwargs);
Christian Heimes9c94ba42008-10-30 21:34:02 +0000171
172 modstate->atexit_callbacks[modstate->ncallbacks++] = new_callback;
173
Collin Winter670e6922007-03-21 02:57:17 +0000174 Py_INCREF(func);
175 return func;
176}
177
Skip Montanaro28a181c2007-08-06 20:59:28 +0000178PyDoc_STRVAR(atexit_run_exitfuncs__doc__,
179"_run_exitfuncs() -> None\n\
180\n\
181Run all registered exit functions.");
182
Collin Winter670e6922007-03-21 02:57:17 +0000183static PyObject *
Christian Heimes9c94ba42008-10-30 21:34:02 +0000184atexit_run_exitfuncs(PyObject *self, PyObject *unused)
Collin Winter670e6922007-03-21 02:57:17 +0000185{
Marcel Plch776407f2017-12-20 11:17:58 +0100186 atexit_callfuncs(self);
Collin Winter670e6922007-03-21 02:57:17 +0000187 if (PyErr_Occurred())
188 return NULL;
189 Py_RETURN_NONE;
190}
191
Skip Montanaro28a181c2007-08-06 20:59:28 +0000192PyDoc_STRVAR(atexit_clear__doc__,
193"_clear() -> None\n\
194\n\
195Clear the list of previously registered exit functions.");
196
Collin Winter670e6922007-03-21 02:57:17 +0000197static PyObject *
Christian Heimes9c94ba42008-10-30 21:34:02 +0000198atexit_clear(PyObject *self, PyObject *unused)
Collin Winter670e6922007-03-21 02:57:17 +0000199{
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200200 atexit_cleanup(GET_ATEXIT_STATE(self));
201 Py_RETURN_NONE;
202}
203
204PyDoc_STRVAR(atexit_ncallbacks__doc__,
205"_ncallbacks() -> int\n\
206\n\
207Return the number of registered exit functions.");
208
209static PyObject *
210atexit_ncallbacks(PyObject *self, PyObject *unused)
211{
Christian Heimes9c94ba42008-10-30 21:34:02 +0000212 atexitmodule_state *modstate;
Christian Heimes9c94ba42008-10-30 21:34:02 +0000213
214 modstate = GET_ATEXIT_STATE(self);
215
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200216 return PyLong_FromSsize_t(modstate->ncallbacks);
217}
218
219static int
220atexit_m_traverse(PyObject *self, visitproc visit, void *arg)
221{
222 int i;
223 atexitmodule_state *modstate;
224
225 modstate = GET_ATEXIT_STATE(self);
Marcel Plch776407f2017-12-20 11:17:58 +0100226 if (modstate != NULL) {
227 for (i = 0; i < modstate->ncallbacks; i++) {
228 atexit_callback *cb = modstate->atexit_callbacks[i];
229 if (cb == NULL)
230 continue;
231 Py_VISIT(cb->func);
232 Py_VISIT(cb->args);
233 Py_VISIT(cb->kwargs);
234 }
Collin Winter670e6922007-03-21 02:57:17 +0000235 }
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200236 return 0;
237}
238
239static int
240atexit_m_clear(PyObject *self)
241{
242 atexitmodule_state *modstate;
243 modstate = GET_ATEXIT_STATE(self);
Marcel Plch776407f2017-12-20 11:17:58 +0100244 if (modstate != NULL) {
245 atexit_cleanup(modstate);
246 }
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200247 return 0;
Collin Winter670e6922007-03-21 02:57:17 +0000248}
249
Stefan Krah650365b2012-03-27 11:49:21 +0200250static void
251atexit_free(PyObject *m)
252{
253 atexitmodule_state *modstate;
254 modstate = GET_ATEXIT_STATE(m);
Marcel Plch776407f2017-12-20 11:17:58 +0100255 if (modstate != NULL) {
256 atexit_cleanup(modstate);
257 PyMem_Free(modstate->atexit_callbacks);
258 }
Stefan Krah650365b2012-03-27 11:49:21 +0200259}
260
Skip Montanaro28a181c2007-08-06 20:59:28 +0000261PyDoc_STRVAR(atexit_unregister__doc__,
262"unregister(func) -> None\n\
263\n\
Martin Panter7462b6492015-11-02 03:37:02 +0000264Unregister an exit function which was previously registered using\n\
Skip Montanaro28a181c2007-08-06 20:59:28 +0000265atexit.register\n\
266\n\
267 func - function to be unregistered");
268
Collin Winter670e6922007-03-21 02:57:17 +0000269static PyObject *
270atexit_unregister(PyObject *self, PyObject *func)
271{
Christian Heimes9c94ba42008-10-30 21:34:02 +0000272 atexitmodule_state *modstate;
Collin Winter670e6922007-03-21 02:57:17 +0000273 atexit_callback *cb;
274 int i, eq;
Christian Heimes9c94ba42008-10-30 21:34:02 +0000275
276 modstate = GET_ATEXIT_STATE(self);
277
278 for (i = 0; i < modstate->ncallbacks; i++)
Collin Winter670e6922007-03-21 02:57:17 +0000279 {
Christian Heimes9c94ba42008-10-30 21:34:02 +0000280 cb = modstate->atexit_callbacks[i];
Collin Winter670e6922007-03-21 02:57:17 +0000281 if (cb == NULL)
282 continue;
Christian Heimes9c94ba42008-10-30 21:34:02 +0000283
Collin Winter670e6922007-03-21 02:57:17 +0000284 eq = PyObject_RichCompareBool(cb->func, func, Py_EQ);
285 if (eq < 0)
286 return NULL;
287 if (eq)
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200288 atexit_delete_cb(modstate, i);
Collin Winter670e6922007-03-21 02:57:17 +0000289 }
290 Py_RETURN_NONE;
291}
292
293static PyMethodDef atexit_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200294 {"register", (PyCFunction)(void(*)(void)) atexit_register, METH_VARARGS|METH_KEYWORDS,
Collin Winter670e6922007-03-21 02:57:17 +0000295 atexit_register__doc__},
296 {"_clear", (PyCFunction) atexit_clear, METH_NOARGS,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000297 atexit_clear__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000298 {"unregister", (PyCFunction) atexit_unregister, METH_O,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000299 atexit_unregister__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000300 {"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000301 atexit_run_exitfuncs__doc__},
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200302 {"_ncallbacks", (PyCFunction) atexit_ncallbacks, METH_NOARGS,
303 atexit_ncallbacks__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000304 {NULL, NULL} /* sentinel */
305};
306
307/* ===================================================================== */
308/* Initialization function. */
309
310PyDoc_STRVAR(atexit__doc__,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000311"allow programmer to define multiple exit functions to be executed\
Collin Winter670e6922007-03-21 02:57:17 +0000312upon normal program termination.\n\
313\n\
Skip Montanaro28a181c2007-08-06 20:59:28 +0000314Two public functions, register and unregister, are defined.\n\
Collin Winter670e6922007-03-21 02:57:17 +0000315");
316
Marcel Plch776407f2017-12-20 11:17:58 +0100317static int
318atexit_exec(PyObject *m) {
319 atexitmodule_state *modstate;
320
321 modstate = GET_ATEXIT_STATE(m);
322 modstate->callback_len = 32;
323 modstate->ncallbacks = 0;
324 modstate->atexit_callbacks = PyMem_New(atexit_callback*,
325 modstate->callback_len);
326 if (modstate->atexit_callbacks == NULL)
327 return -1;
328
329 _Py_PyAtExit(atexit_callfuncs, m);
330 return 0;
331}
332
333static PyModuleDef_Slot atexit_slots[] = {
334 {Py_mod_exec, atexit_exec},
335 {0, NULL}
336};
Martin v. Löwis1a214512008-06-11 05:26:20 +0000337
338static struct PyModuleDef atexitmodule = {
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200339 PyModuleDef_HEAD_INIT,
340 "atexit",
341 atexit__doc__,
342 sizeof(atexitmodule_state),
343 atexit_methods,
Marcel Plch776407f2017-12-20 11:17:58 +0100344 atexit_slots,
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200345 atexit_m_traverse,
346 atexit_m_clear,
347 (freefunc)atexit_free
Martin v. Löwis1a214512008-06-11 05:26:20 +0000348};
349
Collin Winter670e6922007-03-21 02:57:17 +0000350PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000351PyInit_atexit(void)
Collin Winter670e6922007-03-21 02:57:17 +0000352{
Marcel Plch776407f2017-12-20 11:17:58 +0100353 return PyModuleDef_Init(&atexitmodule);
Collin Winter670e6922007-03-21 02:57:17 +0000354}