blob: 49e2a75137e4ab27446bf9a9bb3b4ba1fc38c030 [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 Stinnerb8fa1352020-12-15 14:34:19 +010010#include "pycore_initconfig.h" // _PyStatus_NO_MEMORY
11#include "pycore_interp.h" // PyInterpreterState.atexit
Victor Stinner83d52042020-12-14 22:40:40 +010012#include "pycore_pystate.h" // _PyInterpreterState_GET
Collin Winter3e81ec82007-03-23 22:46:49 +000013
Collin Winter670e6922007-03-21 02:57:17 +000014/* ===================================================================== */
15/* Callback machinery. */
16
Victor Stinner83d52042020-12-14 22:40:40 +010017static inline struct atexit_state*
Victor Stinnerb8fa1352020-12-15 14:34:19 +010018get_atexit_state(void)
Hai Shif707d942020-03-16 21:15:01 +080019{
Victor Stinnerb8fa1352020-12-15 14:34:19 +010020 PyInterpreterState *interp = _PyInterpreterState_GET();
21 return &interp->atexit;
Hai Shif707d942020-03-16 21:15:01 +080022}
Christian Heimes9c94ba42008-10-30 21:34:02 +000023
Collin Winter670e6922007-03-21 02:57:17 +000024
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020025static void
Victor Stinner83d52042020-12-14 22:40:40 +010026atexit_delete_cb(struct atexit_state *state, int i)
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020027{
Victor Stinnerb8fa1352020-12-15 14:34:19 +010028 atexit_callback *cb = state->callbacks[i];
29 state->callbacks[i] = NULL;
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020030
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020031 Py_DECREF(cb->func);
32 Py_DECREF(cb->args);
33 Py_XDECREF(cb->kwargs);
34 PyMem_Free(cb);
35}
36
Victor Stinner83d52042020-12-14 22:40:40 +010037
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020038/* Clear all callbacks without calling them */
39static void
Victor Stinner83d52042020-12-14 22:40:40 +010040atexit_cleanup(struct atexit_state *state)
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020041{
42 atexit_callback *cb;
Victor Stinner83d52042020-12-14 22:40:40 +010043 for (int i = 0; i < state->ncallbacks; i++) {
Victor Stinnerb8fa1352020-12-15 14:34:19 +010044 cb = state->callbacks[i];
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020045 if (cb == NULL)
46 continue;
47
Victor Stinner83d52042020-12-14 22:40:40 +010048 atexit_delete_cb(state, i);
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020049 }
Victor Stinner83d52042020-12-14 22:40:40 +010050 state->ncallbacks = 0;
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020051}
52
Victor Stinnerb8fa1352020-12-15 14:34:19 +010053
54PyStatus
55_PyAtExit_Init(PyThreadState *tstate)
56{
57 struct atexit_state *state = &tstate->interp->atexit;
58 // _PyAtExit_Init() must only be called once
59 assert(state->callbacks == NULL);
60
61 state->callback_len = 32;
62 state->ncallbacks = 0;
63 state->callbacks = PyMem_New(atexit_callback*, state->callback_len);
64 if (state->callbacks == NULL) {
65 return _PyStatus_NO_MEMORY();
66 }
67 return _PyStatus_OK();
68}
69
70
71void
72_PyAtExit_Fini(PyInterpreterState *interp)
73{
74 struct atexit_state *state = &interp->atexit;
75 atexit_cleanup(state);
76 PyMem_Free(state->callbacks);
Victor Stinner3ca2b8f2020-12-15 17:12:02 +010077 state->callbacks = NULL;
Victor Stinnerb8fa1352020-12-15 14:34:19 +010078}
79
Collin Winter670e6922007-03-21 02:57:17 +000080
Skip Montanaro711552b2008-09-23 00:52:29 +000081static void
Victor Stinner3ca2b8f2020-12-15 17:12:02 +010082atexit_callfuncs(struct atexit_state *state)
Collin Winter670e6922007-03-21 02:57:17 +000083{
Victor Stinner83d52042020-12-14 22:40:40 +010084 assert(!PyErr_Occurred());
Christian Heimes9c94ba42008-10-30 21:34:02 +000085
Victor Stinner83d52042020-12-14 22:40:40 +010086 if (state->ncallbacks == 0) {
Christian Heimes9c94ba42008-10-30 21:34:02 +000087 return;
Victor Stinner83d52042020-12-14 22:40:40 +010088 }
Christian Heimes9c94ba42008-10-30 21:34:02 +000089
Victor Stinner83d52042020-12-14 22:40:40 +010090 for (int i = state->ncallbacks - 1; i >= 0; i--) {
Victor Stinnerb8fa1352020-12-15 14:34:19 +010091 atexit_callback *cb = state->callbacks[i];
Victor Stinner83d52042020-12-14 22:40:40 +010092 if (cb == NULL) {
Collin Winter670e6922007-03-21 02:57:17 +000093 continue;
Victor Stinner83d52042020-12-14 22:40:40 +010094 }
Collin Winter670e6922007-03-21 02:57:17 +000095
Victor Stinner83d52042020-12-14 22:40:40 +010096 PyObject *res = PyObject_Call(cb->func, cb->args, cb->kwargs);
97 if (res == NULL) {
Victor Stinner3ca2b8f2020-12-15 17:12:02 +010098 _PyErr_WriteUnraisableMsg("in atexit callback", cb->func);
Collin Winter670e6922007-03-21 02:57:17 +000099 }
Victor Stinner83d52042020-12-14 22:40:40 +0100100 else {
101 Py_DECREF(res);
102 }
Collin Winter670e6922007-03-21 02:57:17 +0000103 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000104
Victor Stinner83d52042020-12-14 22:40:40 +0100105 atexit_cleanup(state);
Skip Montanaro711552b2008-09-23 00:52:29 +0000106
Victor Stinner3ca2b8f2020-12-15 17:12:02 +0100107 assert(!PyErr_Occurred());
Collin Winter670e6922007-03-21 02:57:17 +0000108}
109
Victor Stinner357704c2020-12-14 23:07:54 +0100110
111void
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100112_PyAtExit_Call(PyThreadState *tstate)
Victor Stinner357704c2020-12-14 23:07:54 +0100113{
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100114 struct atexit_state *state = &tstate->interp->atexit;
Victor Stinner3ca2b8f2020-12-15 17:12:02 +0100115 atexit_callfuncs(state);
Victor Stinner357704c2020-12-14 23:07:54 +0100116}
117
118
Collin Winter670e6922007-03-21 02:57:17 +0000119/* ===================================================================== */
120/* Module methods. */
121
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100122
Collin Winter670e6922007-03-21 02:57:17 +0000123PyDoc_STRVAR(atexit_register__doc__,
124"register(func, *args, **kwargs) -> func\n\
125\n\
126Register a function to be executed upon normal program termination\n\
127\n\
128 func - function to be called at exit\n\
129 args - optional arguments to pass to func\n\
130 kwargs - optional keyword arguments to pass to func\n\
131\n\
132 func is returned to facilitate usage as a decorator.");
133
134static PyObject *
Victor Stinner83d52042020-12-14 22:40:40 +0100135atexit_register(PyObject *module, PyObject *args, PyObject *kwargs)
Collin Winter670e6922007-03-21 02:57:17 +0000136{
Collin Winter670e6922007-03-21 02:57:17 +0000137 if (PyTuple_GET_SIZE(args) == 0) {
138 PyErr_SetString(PyExc_TypeError,
139 "register() takes at least 1 argument (0 given)");
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200140 return NULL;
Collin Winter670e6922007-03-21 02:57:17 +0000141 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000142
Victor Stinner83d52042020-12-14 22:40:40 +0100143 PyObject *func = PyTuple_GET_ITEM(args, 0);
Collin Winter670e6922007-03-21 02:57:17 +0000144 if (!PyCallable_Check(func)) {
145 PyErr_SetString(PyExc_TypeError,
146 "the first argument must be callable");
147 return NULL;
148 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000149
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100150 struct atexit_state *state = get_atexit_state();
Victor Stinner83d52042020-12-14 22:40:40 +0100151 if (state->ncallbacks >= state->callback_len) {
152 atexit_callback **r;
153 state->callback_len += 16;
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100154 size_t size = sizeof(atexit_callback*) * (size_t)state->callback_len;
155 r = (atexit_callback**)PyMem_Realloc(state->callbacks, size);
Victor Stinner3ca2b8f2020-12-15 17:12:02 +0100156 if (r == NULL) {
Victor Stinner83d52042020-12-14 22:40:40 +0100157 return PyErr_NoMemory();
Victor Stinner3ca2b8f2020-12-15 17:12:02 +0100158 }
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100159 state->callbacks = r;
Victor Stinner83d52042020-12-14 22:40:40 +0100160 }
Collin Winter670e6922007-03-21 02:57:17 +0000161
Victor Stinner83d52042020-12-14 22:40:40 +0100162 atexit_callback *callback = PyMem_Malloc(sizeof(atexit_callback));
163 if (callback == NULL) {
164 return PyErr_NoMemory();
165 }
166
167 callback->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
168 if (callback->args == NULL) {
169 PyMem_Free(callback);
Collin Winter670e6922007-03-21 02:57:17 +0000170 return NULL;
171 }
Victor Stinner83d52042020-12-14 22:40:40 +0100172 callback->func = Py_NewRef(func);
173 callback->kwargs = Py_XNewRef(kwargs);
Christian Heimes9c94ba42008-10-30 21:34:02 +0000174
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100175 state->callbacks[state->ncallbacks++] = callback;
Christian Heimes9c94ba42008-10-30 21:34:02 +0000176
Victor Stinner83d52042020-12-14 22:40:40 +0100177 return Py_NewRef(func);
Collin Winter670e6922007-03-21 02:57:17 +0000178}
179
Skip Montanaro28a181c2007-08-06 20:59:28 +0000180PyDoc_STRVAR(atexit_run_exitfuncs__doc__,
181"_run_exitfuncs() -> None\n\
182\n\
Victor Stinner3ca2b8f2020-12-15 17:12:02 +0100183Run all registered exit functions.\n\
184\n\
185If a callaback raises an exception, it is logged with sys.unraisablehook.");
Skip Montanaro28a181c2007-08-06 20:59:28 +0000186
Collin Winter670e6922007-03-21 02:57:17 +0000187static PyObject *
Victor Stinner83d52042020-12-14 22:40:40 +0100188atexit_run_exitfuncs(PyObject *module, PyObject *unused)
Collin Winter670e6922007-03-21 02:57:17 +0000189{
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100190 struct atexit_state *state = get_atexit_state();
Victor Stinner3ca2b8f2020-12-15 17:12:02 +0100191 atexit_callfuncs(state);
Collin Winter670e6922007-03-21 02:57:17 +0000192 Py_RETURN_NONE;
193}
194
Skip Montanaro28a181c2007-08-06 20:59:28 +0000195PyDoc_STRVAR(atexit_clear__doc__,
196"_clear() -> None\n\
197\n\
198Clear the list of previously registered exit functions.");
199
Collin Winter670e6922007-03-21 02:57:17 +0000200static PyObject *
Victor Stinner83d52042020-12-14 22:40:40 +0100201atexit_clear(PyObject *module, PyObject *unused)
Collin Winter670e6922007-03-21 02:57:17 +0000202{
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100203 atexit_cleanup(get_atexit_state());
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200204 Py_RETURN_NONE;
205}
206
207PyDoc_STRVAR(atexit_ncallbacks__doc__,
208"_ncallbacks() -> int\n\
209\n\
210Return the number of registered exit functions.");
211
212static PyObject *
Victor Stinner83d52042020-12-14 22:40:40 +0100213atexit_ncallbacks(PyObject *module, PyObject *unused)
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200214{
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100215 struct atexit_state *state = get_atexit_state();
Victor Stinner83d52042020-12-14 22:40:40 +0100216 return PyLong_FromSsize_t(state->ncallbacks);
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200217}
218
Skip Montanaro28a181c2007-08-06 20:59:28 +0000219PyDoc_STRVAR(atexit_unregister__doc__,
220"unregister(func) -> None\n\
221\n\
Martin Panter7462b6492015-11-02 03:37:02 +0000222Unregister an exit function which was previously registered using\n\
Skip Montanaro28a181c2007-08-06 20:59:28 +0000223atexit.register\n\
224\n\
225 func - function to be unregistered");
226
Collin Winter670e6922007-03-21 02:57:17 +0000227static PyObject *
Victor Stinner83d52042020-12-14 22:40:40 +0100228atexit_unregister(PyObject *module, PyObject *func)
Collin Winter670e6922007-03-21 02:57:17 +0000229{
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100230 struct atexit_state *state = get_atexit_state();
Victor Stinner83d52042020-12-14 22:40:40 +0100231 for (int i = 0; i < state->ncallbacks; i++)
Collin Winter670e6922007-03-21 02:57:17 +0000232 {
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100233 atexit_callback *cb = state->callbacks[i];
Victor Stinner83d52042020-12-14 22:40:40 +0100234 if (cb == NULL) {
Collin Winter670e6922007-03-21 02:57:17 +0000235 continue;
Victor Stinner83d52042020-12-14 22:40:40 +0100236 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000237
Victor Stinner83d52042020-12-14 22:40:40 +0100238 int eq = PyObject_RichCompareBool(cb->func, func, Py_EQ);
239 if (eq < 0) {
Collin Winter670e6922007-03-21 02:57:17 +0000240 return NULL;
Victor Stinner83d52042020-12-14 22:40:40 +0100241 }
242 if (eq) {
243 atexit_delete_cb(state, i);
244 }
Collin Winter670e6922007-03-21 02:57:17 +0000245 }
246 Py_RETURN_NONE;
247}
248
Victor Stinner83d52042020-12-14 22:40:40 +0100249
Collin Winter670e6922007-03-21 02:57:17 +0000250static PyMethodDef atexit_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200251 {"register", (PyCFunction)(void(*)(void)) atexit_register, METH_VARARGS|METH_KEYWORDS,
Collin Winter670e6922007-03-21 02:57:17 +0000252 atexit_register__doc__},
253 {"_clear", (PyCFunction) atexit_clear, METH_NOARGS,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000254 atexit_clear__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000255 {"unregister", (PyCFunction) atexit_unregister, METH_O,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000256 atexit_unregister__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000257 {"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000258 atexit_run_exitfuncs__doc__},
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200259 {"_ncallbacks", (PyCFunction) atexit_ncallbacks, METH_NOARGS,
260 atexit_ncallbacks__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000261 {NULL, NULL} /* sentinel */
262};
263
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100264
Collin Winter670e6922007-03-21 02:57:17 +0000265/* ===================================================================== */
266/* Initialization function. */
267
268PyDoc_STRVAR(atexit__doc__,
linchiwei12352bf4702020-07-27 07:33:00 +0800269"allow programmer to define multiple exit functions to be executed\n\
Collin Winter670e6922007-03-21 02:57:17 +0000270upon normal program termination.\n\
271\n\
Skip Montanaro28a181c2007-08-06 20:59:28 +0000272Two public functions, register and unregister, are defined.\n\
Collin Winter670e6922007-03-21 02:57:17 +0000273");
274
Martin v. Löwis1a214512008-06-11 05:26:20 +0000275static struct PyModuleDef atexitmodule = {
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200276 PyModuleDef_HEAD_INIT,
Victor Stinner83d52042020-12-14 22:40:40 +0100277 .m_name = "atexit",
278 .m_doc = atexit__doc__,
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100279 .m_size = 0,
Victor Stinner83d52042020-12-14 22:40:40 +0100280 .m_methods = atexit_methods,
Martin v. Löwis1a214512008-06-11 05:26:20 +0000281};
282
Collin Winter670e6922007-03-21 02:57:17 +0000283PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000284PyInit_atexit(void)
Collin Winter670e6922007-03-21 02:57:17 +0000285{
Marcel Plch776407f2017-12-20 11:17:58 +0100286 return PyModuleDef_Init(&atexitmodule);
Collin Winter670e6922007-03-21 02:57:17 +0000287}