blob: f1fc871cf7fa9691bef4e0ed316bc91b012166fa [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"
Victor Stinner83d52042020-12-14 22:40:40 +010010#include "pycore_interp.h" // PyInterpreterState.atexit_func
11#include "pycore_pystate.h" // _PyInterpreterState_GET
Collin Winter3e81ec82007-03-23 22:46:49 +000012
Collin Winter670e6922007-03-21 02:57:17 +000013/* ===================================================================== */
14/* Callback machinery. */
15
16typedef struct {
17 PyObject *func;
18 PyObject *args;
19 PyObject *kwargs;
20} atexit_callback;
21
Victor Stinner83d52042020-12-14 22:40:40 +010022struct atexit_state {
Christian Heimes9c94ba42008-10-30 21:34:02 +000023 atexit_callback **atexit_callbacks;
24 int ncallbacks;
25 int callback_len;
Victor Stinner83d52042020-12-14 22:40:40 +010026};
Christian Heimes9c94ba42008-10-30 21:34:02 +000027
Victor Stinner83d52042020-12-14 22:40:40 +010028static inline struct atexit_state*
Hai Shif707d942020-03-16 21:15:01 +080029get_atexit_state(PyObject *module)
30{
31 void *state = PyModule_GetState(module);
32 assert(state != NULL);
Victor Stinner83d52042020-12-14 22:40:40 +010033 return (struct atexit_state *)state;
Hai Shif707d942020-03-16 21:15:01 +080034}
Christian Heimes9c94ba42008-10-30 21:34:02 +000035
Collin Winter670e6922007-03-21 02:57:17 +000036
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020037static void
Victor Stinner83d52042020-12-14 22:40:40 +010038atexit_delete_cb(struct atexit_state *state, int i)
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020039{
Victor Stinner83d52042020-12-14 22:40:40 +010040 atexit_callback *cb = state->atexit_callbacks[i];
41 state->atexit_callbacks[i] = NULL;
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020042
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020043 Py_DECREF(cb->func);
44 Py_DECREF(cb->args);
45 Py_XDECREF(cb->kwargs);
46 PyMem_Free(cb);
47}
48
Victor Stinner83d52042020-12-14 22:40:40 +010049
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020050/* Clear all callbacks without calling them */
51static void
Victor Stinner83d52042020-12-14 22:40:40 +010052atexit_cleanup(struct atexit_state *state)
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020053{
54 atexit_callback *cb;
Victor Stinner83d52042020-12-14 22:40:40 +010055 for (int i = 0; i < state->ncallbacks; i++) {
56 cb = state->atexit_callbacks[i];
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020057 if (cb == NULL)
58 continue;
59
Victor Stinner83d52042020-12-14 22:40:40 +010060 atexit_delete_cb(state, i);
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020061 }
Victor Stinner83d52042020-12-14 22:40:40 +010062 state->ncallbacks = 0;
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020063}
64
Nick Coghland6009512014-11-20 21:39:37 +100065/* Installed into pylifecycle.c's atexit mechanism */
Collin Winter670e6922007-03-21 02:57:17 +000066
Skip Montanaro711552b2008-09-23 00:52:29 +000067static void
Marcel Plch776407f2017-12-20 11:17:58 +010068atexit_callfuncs(PyObject *module)
Collin Winter670e6922007-03-21 02:57:17 +000069{
Victor Stinner83d52042020-12-14 22:40:40 +010070 assert(!PyErr_Occurred());
Christian Heimes9c94ba42008-10-30 21:34:02 +000071
Victor Stinner83d52042020-12-14 22:40:40 +010072 if (module == NULL) {
Collin Winter670e6922007-03-21 02:57:17 +000073 return;
Victor Stinner83d52042020-12-14 22:40:40 +010074 }
Christian Heimes9c94ba42008-10-30 21:34:02 +000075
Victor Stinner83d52042020-12-14 22:40:40 +010076 struct atexit_state *state = get_atexit_state(module);
77 if (state->ncallbacks == 0) {
Christian Heimes9c94ba42008-10-30 21:34:02 +000078 return;
Victor Stinner83d52042020-12-14 22:40:40 +010079 }
Christian Heimes9c94ba42008-10-30 21:34:02 +000080
Victor Stinner83d52042020-12-14 22:40:40 +010081 PyObject *exc_type = NULL, *exc_value, *exc_tb;
82 for (int i = state->ncallbacks - 1; i >= 0; i--) {
83 atexit_callback *cb = state->atexit_callbacks[i];
84 if (cb == NULL) {
Collin Winter670e6922007-03-21 02:57:17 +000085 continue;
Victor Stinner83d52042020-12-14 22:40:40 +010086 }
Collin Winter670e6922007-03-21 02:57:17 +000087
Victor Stinner83d52042020-12-14 22:40:40 +010088 PyObject *res = PyObject_Call(cb->func, cb->args, cb->kwargs);
89 if (res == 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 }
Victor Stinner83d52042020-12-14 22:40:40 +0100104 else {
105 Py_DECREF(res);
106 }
Collin Winter670e6922007-03-21 02:57:17 +0000107 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000108
Victor Stinner83d52042020-12-14 22:40:40 +0100109 atexit_cleanup(state);
Skip Montanaro711552b2008-09-23 00:52:29 +0000110
Victor Stinner83d52042020-12-14 22:40:40 +0100111 if (exc_type) {
Collin Winter670e6922007-03-21 02:57:17 +0000112 PyErr_Restore(exc_type, exc_value, exc_tb);
Victor Stinner83d52042020-12-14 22:40:40 +0100113 }
Collin Winter670e6922007-03-21 02:57:17 +0000114}
115
Collin Winter670e6922007-03-21 02:57:17 +0000116/* ===================================================================== */
117/* Module methods. */
118
119PyDoc_STRVAR(atexit_register__doc__,
120"register(func, *args, **kwargs) -> func\n\
121\n\
122Register a function to be executed upon normal program termination\n\
123\n\
124 func - function to be called at exit\n\
125 args - optional arguments to pass to func\n\
126 kwargs - optional keyword arguments to pass to func\n\
127\n\
128 func is returned to facilitate usage as a decorator.");
129
130static PyObject *
Victor Stinner83d52042020-12-14 22:40:40 +0100131atexit_register(PyObject *module, PyObject *args, PyObject *kwargs)
Collin Winter670e6922007-03-21 02:57:17 +0000132{
Collin Winter670e6922007-03-21 02:57:17 +0000133 if (PyTuple_GET_SIZE(args) == 0) {
134 PyErr_SetString(PyExc_TypeError,
135 "register() takes at least 1 argument (0 given)");
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200136 return NULL;
Collin Winter670e6922007-03-21 02:57:17 +0000137 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000138
Victor Stinner83d52042020-12-14 22:40:40 +0100139 PyObject *func = PyTuple_GET_ITEM(args, 0);
Collin Winter670e6922007-03-21 02:57:17 +0000140 if (!PyCallable_Check(func)) {
141 PyErr_SetString(PyExc_TypeError,
142 "the first argument must be callable");
143 return NULL;
144 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000145
Victor Stinner83d52042020-12-14 22:40:40 +0100146 struct atexit_state *state = get_atexit_state(module);
147 if (state->ncallbacks >= state->callback_len) {
148 atexit_callback **r;
149 state->callback_len += 16;
150 r = (atexit_callback**)PyMem_Realloc(state->atexit_callbacks,
151 sizeof(atexit_callback*) * state->callback_len);
152 if (r == NULL)
153 return PyErr_NoMemory();
154 state->atexit_callbacks = r;
155 }
Collin Winter670e6922007-03-21 02:57:17 +0000156
Victor Stinner83d52042020-12-14 22:40:40 +0100157 atexit_callback *callback = PyMem_Malloc(sizeof(atexit_callback));
158 if (callback == NULL) {
159 return PyErr_NoMemory();
160 }
161
162 callback->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
163 if (callback->args == NULL) {
164 PyMem_Free(callback);
Collin Winter670e6922007-03-21 02:57:17 +0000165 return NULL;
166 }
Victor Stinner83d52042020-12-14 22:40:40 +0100167 callback->func = Py_NewRef(func);
168 callback->kwargs = Py_XNewRef(kwargs);
Christian Heimes9c94ba42008-10-30 21:34:02 +0000169
Victor Stinner83d52042020-12-14 22:40:40 +0100170 state->atexit_callbacks[state->ncallbacks++] = callback;
Christian Heimes9c94ba42008-10-30 21:34:02 +0000171
Victor Stinner83d52042020-12-14 22:40:40 +0100172 return Py_NewRef(func);
Collin Winter670e6922007-03-21 02:57:17 +0000173}
174
Skip Montanaro28a181c2007-08-06 20:59:28 +0000175PyDoc_STRVAR(atexit_run_exitfuncs__doc__,
176"_run_exitfuncs() -> None\n\
177\n\
178Run all registered exit functions.");
179
Collin Winter670e6922007-03-21 02:57:17 +0000180static PyObject *
Victor Stinner83d52042020-12-14 22:40:40 +0100181atexit_run_exitfuncs(PyObject *module, PyObject *unused)
Collin Winter670e6922007-03-21 02:57:17 +0000182{
Victor Stinner83d52042020-12-14 22:40:40 +0100183 atexit_callfuncs(module);
184 if (PyErr_Occurred()) {
Collin Winter670e6922007-03-21 02:57:17 +0000185 return NULL;
Victor Stinner83d52042020-12-14 22:40:40 +0100186 }
Collin Winter670e6922007-03-21 02:57:17 +0000187 Py_RETURN_NONE;
188}
189
Skip Montanaro28a181c2007-08-06 20:59:28 +0000190PyDoc_STRVAR(atexit_clear__doc__,
191"_clear() -> None\n\
192\n\
193Clear the list of previously registered exit functions.");
194
Collin Winter670e6922007-03-21 02:57:17 +0000195static PyObject *
Victor Stinner83d52042020-12-14 22:40:40 +0100196atexit_clear(PyObject *module, PyObject *unused)
Collin Winter670e6922007-03-21 02:57:17 +0000197{
Victor Stinner83d52042020-12-14 22:40:40 +0100198 atexit_cleanup(get_atexit_state(module));
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200199 Py_RETURN_NONE;
200}
201
202PyDoc_STRVAR(atexit_ncallbacks__doc__,
203"_ncallbacks() -> int\n\
204\n\
205Return the number of registered exit functions.");
206
207static PyObject *
Victor Stinner83d52042020-12-14 22:40:40 +0100208atexit_ncallbacks(PyObject *module, PyObject *unused)
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200209{
Victor Stinner83d52042020-12-14 22:40:40 +0100210 struct atexit_state *state = get_atexit_state(module);
211 return PyLong_FromSsize_t(state->ncallbacks);
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200212}
213
214static int
Victor Stinner83d52042020-12-14 22:40:40 +0100215atexit_m_traverse(PyObject *module, visitproc visit, void *arg)
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200216{
Victor Stinner83d52042020-12-14 22:40:40 +0100217 struct atexit_state *state = (struct atexit_state *)PyModule_GetState(module);
218 for (int i = 0; i < state->ncallbacks; i++) {
219 atexit_callback *cb = state->atexit_callbacks[i];
Victor Stinner5b1ef202020-03-17 18:09:46 +0100220 if (cb == NULL)
221 continue;
222 Py_VISIT(cb->func);
223 Py_VISIT(cb->args);
224 Py_VISIT(cb->kwargs);
Collin Winter670e6922007-03-21 02:57:17 +0000225 }
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200226 return 0;
227}
228
229static int
Victor Stinner83d52042020-12-14 22:40:40 +0100230atexit_m_clear(PyObject *module)
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200231{
Victor Stinner83d52042020-12-14 22:40:40 +0100232 struct atexit_state *state = (struct atexit_state *)PyModule_GetState(module);
233 atexit_cleanup(state);
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200234 return 0;
Collin Winter670e6922007-03-21 02:57:17 +0000235}
236
Stefan Krah650365b2012-03-27 11:49:21 +0200237static void
Victor Stinner83d52042020-12-14 22:40:40 +0100238atexit_free(PyObject *module)
Stefan Krah650365b2012-03-27 11:49:21 +0200239{
Victor Stinner83d52042020-12-14 22:40:40 +0100240 struct atexit_state *state = (struct atexit_state *)PyModule_GetState(module);
241 atexit_cleanup(state);
242 PyMem_Free(state->atexit_callbacks);
Stefan Krah650365b2012-03-27 11:49:21 +0200243}
244
Skip Montanaro28a181c2007-08-06 20:59:28 +0000245PyDoc_STRVAR(atexit_unregister__doc__,
246"unregister(func) -> None\n\
247\n\
Martin Panter7462b6492015-11-02 03:37:02 +0000248Unregister an exit function which was previously registered using\n\
Skip Montanaro28a181c2007-08-06 20:59:28 +0000249atexit.register\n\
250\n\
251 func - function to be unregistered");
252
Collin Winter670e6922007-03-21 02:57:17 +0000253static PyObject *
Victor Stinner83d52042020-12-14 22:40:40 +0100254atexit_unregister(PyObject *module, PyObject *func)
Collin Winter670e6922007-03-21 02:57:17 +0000255{
Victor Stinner83d52042020-12-14 22:40:40 +0100256 struct atexit_state *state = get_atexit_state(module);
257 for (int i = 0; i < state->ncallbacks; i++)
Collin Winter670e6922007-03-21 02:57:17 +0000258 {
Victor Stinner83d52042020-12-14 22:40:40 +0100259 atexit_callback *cb = state->atexit_callbacks[i];
260 if (cb == NULL) {
Collin Winter670e6922007-03-21 02:57:17 +0000261 continue;
Victor Stinner83d52042020-12-14 22:40:40 +0100262 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000263
Victor Stinner83d52042020-12-14 22:40:40 +0100264 int eq = PyObject_RichCompareBool(cb->func, func, Py_EQ);
265 if (eq < 0) {
Collin Winter670e6922007-03-21 02:57:17 +0000266 return NULL;
Victor Stinner83d52042020-12-14 22:40:40 +0100267 }
268 if (eq) {
269 atexit_delete_cb(state, i);
270 }
Collin Winter670e6922007-03-21 02:57:17 +0000271 }
272 Py_RETURN_NONE;
273}
274
Victor Stinner83d52042020-12-14 22:40:40 +0100275
Collin Winter670e6922007-03-21 02:57:17 +0000276static PyMethodDef atexit_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200277 {"register", (PyCFunction)(void(*)(void)) atexit_register, METH_VARARGS|METH_KEYWORDS,
Collin Winter670e6922007-03-21 02:57:17 +0000278 atexit_register__doc__},
279 {"_clear", (PyCFunction) atexit_clear, METH_NOARGS,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000280 atexit_clear__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000281 {"unregister", (PyCFunction) atexit_unregister, METH_O,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000282 atexit_unregister__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000283 {"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000284 atexit_run_exitfuncs__doc__},
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200285 {"_ncallbacks", (PyCFunction) atexit_ncallbacks, METH_NOARGS,
286 atexit_ncallbacks__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000287 {NULL, NULL} /* sentinel */
288};
289
290/* ===================================================================== */
291/* Initialization function. */
292
293PyDoc_STRVAR(atexit__doc__,
linchiwei12352bf4702020-07-27 07:33:00 +0800294"allow programmer to define multiple exit functions to be executed\n\
Collin Winter670e6922007-03-21 02:57:17 +0000295upon normal program termination.\n\
296\n\
Skip Montanaro28a181c2007-08-06 20:59:28 +0000297Two public functions, register and unregister, are defined.\n\
Collin Winter670e6922007-03-21 02:57:17 +0000298");
299
Marcel Plch776407f2017-12-20 11:17:58 +0100300static int
Victor Stinner83d52042020-12-14 22:40:40 +0100301atexit_exec(PyObject *module)
302{
303 struct atexit_state *state = get_atexit_state(module);
304 state->callback_len = 32;
305 state->ncallbacks = 0;
306 state->atexit_callbacks = PyMem_New(atexit_callback*, state->callback_len);
307 if (state->atexit_callbacks == NULL) {
Marcel Plch776407f2017-12-20 11:17:58 +0100308 return -1;
Victor Stinner83d52042020-12-14 22:40:40 +0100309 }
Marcel Plch776407f2017-12-20 11:17:58 +0100310
Victor Stinner83d52042020-12-14 22:40:40 +0100311 PyInterpreterState *is = _PyInterpreterState_GET();
312 is->atexit_func = atexit_callfuncs;
313 is->atexit_module = module;
Marcel Plch776407f2017-12-20 11:17:58 +0100314 return 0;
315}
316
317static PyModuleDef_Slot atexit_slots[] = {
318 {Py_mod_exec, atexit_exec},
319 {0, NULL}
320};
Martin v. Löwis1a214512008-06-11 05:26:20 +0000321
322static struct PyModuleDef atexitmodule = {
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200323 PyModuleDef_HEAD_INIT,
Victor Stinner83d52042020-12-14 22:40:40 +0100324 .m_name = "atexit",
325 .m_doc = atexit__doc__,
326 .m_size = sizeof(struct atexit_state),
327 .m_methods = atexit_methods,
328 .m_slots = atexit_slots,
329 .m_traverse = atexit_m_traverse,
330 .m_clear = atexit_m_clear,
331 .m_free = (freefunc)atexit_free
Martin v. Löwis1a214512008-06-11 05:26:20 +0000332};
333
Collin Winter670e6922007-03-21 02:57:17 +0000334PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000335PyInit_atexit(void)
Collin Winter670e6922007-03-21 02:57:17 +0000336{
Marcel Plch776407f2017-12-20 11:17:58 +0100337 return PyModuleDef_Init(&atexitmodule);
Collin Winter670e6922007-03-21 02:57:17 +0000338}