blob: 95c653cf4782a23c1cdd5f01346610aa5d607622 [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
Victor Stinnerbcb094b2021-02-19 15:10:45 +010055_PyAtExit_Init(PyInterpreterState *interp)
Victor Stinnerb8fa1352020-12-15 14:34:19 +010056{
Victor Stinnerbcb094b2021-02-19 15:10:45 +010057 struct atexit_state *state = &interp->atexit;
Victor Stinnerb8fa1352020-12-15 14:34:19 +010058 // _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
Miss Islington (bot)934a24c2021-12-09 07:16:09 -080096 // bpo-46025: Increment the refcount of cb->func as the call itself may unregister it
97 PyObject* the_func = Py_NewRef(cb->func);
Victor Stinner83d52042020-12-14 22:40:40 +010098 PyObject *res = PyObject_Call(cb->func, cb->args, cb->kwargs);
99 if (res == NULL) {
Miss Islington (bot)934a24c2021-12-09 07:16:09 -0800100 _PyErr_WriteUnraisableMsg("in atexit callback", the_func);
Collin Winter670e6922007-03-21 02:57:17 +0000101 }
Victor Stinner83d52042020-12-14 22:40:40 +0100102 else {
103 Py_DECREF(res);
104 }
Miss Islington (bot)934a24c2021-12-09 07:16:09 -0800105 Py_DECREF(the_func);
Collin Winter670e6922007-03-21 02:57:17 +0000106 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000107
Victor Stinner83d52042020-12-14 22:40:40 +0100108 atexit_cleanup(state);
Skip Montanaro711552b2008-09-23 00:52:29 +0000109
Victor Stinner3ca2b8f2020-12-15 17:12:02 +0100110 assert(!PyErr_Occurred());
Collin Winter670e6922007-03-21 02:57:17 +0000111}
112
Victor Stinner357704c2020-12-14 23:07:54 +0100113
114void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100115_PyAtExit_Call(PyInterpreterState *interp)
Victor Stinner357704c2020-12-14 23:07:54 +0100116{
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100117 struct atexit_state *state = &interp->atexit;
Victor Stinner3ca2b8f2020-12-15 17:12:02 +0100118 atexit_callfuncs(state);
Victor Stinner357704c2020-12-14 23:07:54 +0100119}
120
121
Collin Winter670e6922007-03-21 02:57:17 +0000122/* ===================================================================== */
123/* Module methods. */
124
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100125
Collin Winter670e6922007-03-21 02:57:17 +0000126PyDoc_STRVAR(atexit_register__doc__,
127"register(func, *args, **kwargs) -> func\n\
128\n\
129Register a function to be executed upon normal program termination\n\
130\n\
131 func - function to be called at exit\n\
132 args - optional arguments to pass to func\n\
133 kwargs - optional keyword arguments to pass to func\n\
134\n\
135 func is returned to facilitate usage as a decorator.");
136
137static PyObject *
Victor Stinner83d52042020-12-14 22:40:40 +0100138atexit_register(PyObject *module, PyObject *args, PyObject *kwargs)
Collin Winter670e6922007-03-21 02:57:17 +0000139{
Collin Winter670e6922007-03-21 02:57:17 +0000140 if (PyTuple_GET_SIZE(args) == 0) {
141 PyErr_SetString(PyExc_TypeError,
142 "register() takes at least 1 argument (0 given)");
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200143 return NULL;
Collin Winter670e6922007-03-21 02:57:17 +0000144 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000145
Victor Stinner83d52042020-12-14 22:40:40 +0100146 PyObject *func = PyTuple_GET_ITEM(args, 0);
Collin Winter670e6922007-03-21 02:57:17 +0000147 if (!PyCallable_Check(func)) {
148 PyErr_SetString(PyExc_TypeError,
149 "the first argument must be callable");
150 return NULL;
151 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000152
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100153 struct atexit_state *state = get_atexit_state();
Victor Stinner83d52042020-12-14 22:40:40 +0100154 if (state->ncallbacks >= state->callback_len) {
155 atexit_callback **r;
156 state->callback_len += 16;
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100157 size_t size = sizeof(atexit_callback*) * (size_t)state->callback_len;
158 r = (atexit_callback**)PyMem_Realloc(state->callbacks, size);
Victor Stinner3ca2b8f2020-12-15 17:12:02 +0100159 if (r == NULL) {
Victor Stinner83d52042020-12-14 22:40:40 +0100160 return PyErr_NoMemory();
Victor Stinner3ca2b8f2020-12-15 17:12:02 +0100161 }
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100162 state->callbacks = r;
Victor Stinner83d52042020-12-14 22:40:40 +0100163 }
Collin Winter670e6922007-03-21 02:57:17 +0000164
Victor Stinner83d52042020-12-14 22:40:40 +0100165 atexit_callback *callback = PyMem_Malloc(sizeof(atexit_callback));
166 if (callback == NULL) {
167 return PyErr_NoMemory();
168 }
169
170 callback->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
171 if (callback->args == NULL) {
172 PyMem_Free(callback);
Collin Winter670e6922007-03-21 02:57:17 +0000173 return NULL;
174 }
Victor Stinner83d52042020-12-14 22:40:40 +0100175 callback->func = Py_NewRef(func);
176 callback->kwargs = Py_XNewRef(kwargs);
Christian Heimes9c94ba42008-10-30 21:34:02 +0000177
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100178 state->callbacks[state->ncallbacks++] = callback;
Christian Heimes9c94ba42008-10-30 21:34:02 +0000179
Victor Stinner83d52042020-12-14 22:40:40 +0100180 return Py_NewRef(func);
Collin Winter670e6922007-03-21 02:57:17 +0000181}
182
Skip Montanaro28a181c2007-08-06 20:59:28 +0000183PyDoc_STRVAR(atexit_run_exitfuncs__doc__,
184"_run_exitfuncs() -> None\n\
185\n\
Victor Stinner3ca2b8f2020-12-15 17:12:02 +0100186Run all registered exit functions.\n\
187\n\
188If a callaback raises an exception, it is logged with sys.unraisablehook.");
Skip Montanaro28a181c2007-08-06 20:59:28 +0000189
Collin Winter670e6922007-03-21 02:57:17 +0000190static PyObject *
Victor Stinner83d52042020-12-14 22:40:40 +0100191atexit_run_exitfuncs(PyObject *module, PyObject *unused)
Collin Winter670e6922007-03-21 02:57:17 +0000192{
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100193 struct atexit_state *state = get_atexit_state();
Victor Stinner3ca2b8f2020-12-15 17:12:02 +0100194 atexit_callfuncs(state);
Collin Winter670e6922007-03-21 02:57:17 +0000195 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 *
Victor Stinner83d52042020-12-14 22:40:40 +0100204atexit_clear(PyObject *module, PyObject *unused)
Collin Winter670e6922007-03-21 02:57:17 +0000205{
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100206 atexit_cleanup(get_atexit_state());
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 *
Victor Stinner83d52042020-12-14 22:40:40 +0100216atexit_ncallbacks(PyObject *module, PyObject *unused)
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200217{
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100218 struct atexit_state *state = get_atexit_state();
Victor Stinner83d52042020-12-14 22:40:40 +0100219 return PyLong_FromSsize_t(state->ncallbacks);
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200220}
221
Skip Montanaro28a181c2007-08-06 20:59:28 +0000222PyDoc_STRVAR(atexit_unregister__doc__,
223"unregister(func) -> None\n\
224\n\
Martin Panter7462b6492015-11-02 03:37:02 +0000225Unregister an exit function which was previously registered using\n\
Skip Montanaro28a181c2007-08-06 20:59:28 +0000226atexit.register\n\
227\n\
228 func - function to be unregistered");
229
Collin Winter670e6922007-03-21 02:57:17 +0000230static PyObject *
Victor Stinner83d52042020-12-14 22:40:40 +0100231atexit_unregister(PyObject *module, PyObject *func)
Collin Winter670e6922007-03-21 02:57:17 +0000232{
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100233 struct atexit_state *state = get_atexit_state();
Victor Stinner83d52042020-12-14 22:40:40 +0100234 for (int i = 0; i < state->ncallbacks; i++)
Collin Winter670e6922007-03-21 02:57:17 +0000235 {
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100236 atexit_callback *cb = state->callbacks[i];
Victor Stinner83d52042020-12-14 22:40:40 +0100237 if (cb == NULL) {
Collin Winter670e6922007-03-21 02:57:17 +0000238 continue;
Victor Stinner83d52042020-12-14 22:40:40 +0100239 }
Christian Heimes9c94ba42008-10-30 21:34:02 +0000240
Victor Stinner83d52042020-12-14 22:40:40 +0100241 int eq = PyObject_RichCompareBool(cb->func, func, Py_EQ);
242 if (eq < 0) {
Collin Winter670e6922007-03-21 02:57:17 +0000243 return NULL;
Victor Stinner83d52042020-12-14 22:40:40 +0100244 }
245 if (eq) {
246 atexit_delete_cb(state, i);
247 }
Collin Winter670e6922007-03-21 02:57:17 +0000248 }
249 Py_RETURN_NONE;
250}
251
Victor Stinner83d52042020-12-14 22:40:40 +0100252
Collin Winter670e6922007-03-21 02:57:17 +0000253static PyMethodDef atexit_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200254 {"register", (PyCFunction)(void(*)(void)) atexit_register, METH_VARARGS|METH_KEYWORDS,
Collin Winter670e6922007-03-21 02:57:17 +0000255 atexit_register__doc__},
256 {"_clear", (PyCFunction) atexit_clear, METH_NOARGS,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000257 atexit_clear__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000258 {"unregister", (PyCFunction) atexit_unregister, METH_O,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000259 atexit_unregister__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000260 {"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000261 atexit_run_exitfuncs__doc__},
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200262 {"_ncallbacks", (PyCFunction) atexit_ncallbacks, METH_NOARGS,
263 atexit_ncallbacks__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000264 {NULL, NULL} /* sentinel */
265};
266
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100267
Collin Winter670e6922007-03-21 02:57:17 +0000268/* ===================================================================== */
269/* Initialization function. */
270
271PyDoc_STRVAR(atexit__doc__,
linchiwei12352bf4702020-07-27 07:33:00 +0800272"allow programmer to define multiple exit functions to be executed\n\
Collin Winter670e6922007-03-21 02:57:17 +0000273upon normal program termination.\n\
274\n\
Skip Montanaro28a181c2007-08-06 20:59:28 +0000275Two public functions, register and unregister, are defined.\n\
Collin Winter670e6922007-03-21 02:57:17 +0000276");
277
Martin v. Löwis1a214512008-06-11 05:26:20 +0000278static struct PyModuleDef atexitmodule = {
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200279 PyModuleDef_HEAD_INIT,
Victor Stinner83d52042020-12-14 22:40:40 +0100280 .m_name = "atexit",
281 .m_doc = atexit__doc__,
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100282 .m_size = 0,
Victor Stinner83d52042020-12-14 22:40:40 +0100283 .m_methods = atexit_methods,
Martin v. Löwis1a214512008-06-11 05:26:20 +0000284};
285
Collin Winter670e6922007-03-21 02:57:17 +0000286PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000287PyInit_atexit(void)
Collin Winter670e6922007-03-21 02:57:17 +0000288{
Marcel Plch776407f2017-12-20 11:17:58 +0100289 return PyModuleDef_Init(&atexitmodule);
Collin Winter670e6922007-03-21 02:57:17 +0000290}