Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 1 | /* |
| 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 Winter | 3e81ec8 | 2007-03-23 22:46:49 +0000 | [diff] [blame] | 11 | /* Forward declaration (for atexit_cleanup) */ |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 12 | static PyObject *atexit_clear(PyObject*, PyObject*); |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 13 | /* Forward declaration of module object */ |
| 14 | static struct PyModuleDef atexitmodule; |
Collin Winter | 3e81ec8 | 2007-03-23 22:46:49 +0000 | [diff] [blame] | 15 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 16 | /* ===================================================================== */ |
| 17 | /* Callback machinery. */ |
| 18 | |
| 19 | typedef struct { |
| 20 | PyObject *func; |
| 21 | PyObject *args; |
| 22 | PyObject *kwargs; |
| 23 | } atexit_callback; |
| 24 | |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 25 | typedef struct { |
| 26 | atexit_callback **atexit_callbacks; |
| 27 | int ncallbacks; |
| 28 | int callback_len; |
| 29 | } atexitmodule_state; |
| 30 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 31 | static inline atexitmodule_state* |
| 32 | get_atexit_state(PyObject *module) |
| 33 | { |
| 34 | void *state = PyModule_GetState(module); |
| 35 | assert(state != NULL); |
| 36 | return (atexitmodule_state *)state; |
| 37 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 38 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 39 | |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 40 | static void |
| 41 | atexit_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 */ |
| 54 | static void |
| 55 | atexit_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 Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 69 | /* Installed into pylifecycle.c's atexit mechanism */ |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 70 | |
Skip Montanaro | 711552b | 2008-09-23 00:52:29 +0000 | [diff] [blame] | 71 | static void |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 72 | atexit_callfuncs(PyObject *module) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 73 | { |
| 74 | PyObject *exc_type = NULL, *exc_value, *exc_tb, *r; |
| 75 | atexit_callback *cb; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 76 | atexitmodule_state *modstate; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 77 | int i; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 78 | |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 79 | if (module == NULL) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 80 | return; |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 81 | modstate = get_atexit_state(module); |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 82 | |
| 83 | if (modstate->ncallbacks == 0) |
| 84 | return; |
| 85 | |
| 86 | |
| 87 | for (i = modstate->ncallbacks - 1; i >= 0; i--) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 88 | { |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 89 | cb = modstate->atexit_callbacks[i]; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 90 | 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 Norwitz | 7d71fb8 | 2007-03-21 04:45:04 +0000 | [diff] [blame] | 96 | /* Maintain the last exception, but don't leak if there are |
| 97 | multiple exceptions. */ |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 98 | if (exc_type) { |
| 99 | Py_DECREF(exc_type); |
Neal Norwitz | 7d71fb8 | 2007-03-21 04:45:04 +0000 | [diff] [blame] | 100 | Py_XDECREF(exc_value); |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 101 | Py_XDECREF(exc_tb); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 102 | } |
| 103 | PyErr_Fetch(&exc_type, &exc_value, &exc_tb); |
Serhiy Storchaka | 3fd54d4 | 2017-06-12 08:25:04 +0300 | [diff] [blame] | 104 | if (!PyErr_GivenExceptionMatches(exc_type, PyExc_SystemExit)) { |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 105 | PySys_WriteStderr("Error in atexit._run_exitfuncs:\n"); |
Victor Stinner | 358e11d | 2011-01-05 03:54:25 +0000 | [diff] [blame] | 106 | PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 107 | PyErr_Display(exc_type, exc_value, exc_tb); |
| 108 | } |
| 109 | } |
| 110 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 111 | |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 112 | atexit_cleanup(modstate); |
Skip Montanaro | 711552b | 2008-09-23 00:52:29 +0000 | [diff] [blame] | 113 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 114 | if (exc_type) |
| 115 | PyErr_Restore(exc_type, exc_value, exc_tb); |
| 116 | } |
| 117 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 118 | /* ===================================================================== */ |
| 119 | /* Module methods. */ |
| 120 | |
| 121 | PyDoc_STRVAR(atexit_register__doc__, |
| 122 | "register(func, *args, **kwargs) -> func\n\ |
| 123 | \n\ |
| 124 | Register 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 | |
| 132 | static PyObject * |
| 133 | atexit_register(PyObject *self, PyObject *args, PyObject *kwargs) |
| 134 | { |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 135 | atexitmodule_state *modstate; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 136 | atexit_callback *new_callback; |
| 137 | PyObject *func = NULL; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 138 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 139 | modstate = get_atexit_state(self); |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 140 | |
| 141 | if (modstate->ncallbacks >= modstate->callback_len) { |
Collin Winter | 3e81ec8 | 2007-03-23 22:46:49 +0000 | [diff] [blame] | 142 | atexit_callback **r; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 143 | modstate->callback_len += 16; |
| 144 | r = (atexit_callback**)PyMem_Realloc(modstate->atexit_callbacks, |
| 145 | sizeof(atexit_callback*) * modstate->callback_len); |
Collin Winter | 3e81ec8 | 2007-03-23 22:46:49 +0000 | [diff] [blame] | 146 | if (r == NULL) |
| 147 | return PyErr_NoMemory(); |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 148 | modstate->atexit_callbacks = r; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 149 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 150 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 151 | if (PyTuple_GET_SIZE(args) == 0) { |
| 152 | PyErr_SetString(PyExc_TypeError, |
| 153 | "register() takes at least 1 argument (0 given)"); |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 154 | return NULL; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 155 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 156 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 157 | 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 Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 163 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 164 | new_callback = PyMem_Malloc(sizeof(atexit_callback)); |
| 165 | if (new_callback == NULL) |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 166 | return PyErr_NoMemory(); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 167 | |
| 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 Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 177 | |
| 178 | modstate->atexit_callbacks[modstate->ncallbacks++] = new_callback; |
| 179 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 180 | Py_INCREF(func); |
| 181 | return func; |
| 182 | } |
| 183 | |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 184 | PyDoc_STRVAR(atexit_run_exitfuncs__doc__, |
| 185 | "_run_exitfuncs() -> None\n\ |
| 186 | \n\ |
| 187 | Run all registered exit functions."); |
| 188 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 189 | static PyObject * |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 190 | atexit_run_exitfuncs(PyObject *self, PyObject *unused) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 191 | { |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 192 | atexit_callfuncs(self); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 193 | if (PyErr_Occurred()) |
| 194 | return NULL; |
| 195 | Py_RETURN_NONE; |
| 196 | } |
| 197 | |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 198 | PyDoc_STRVAR(atexit_clear__doc__, |
| 199 | "_clear() -> None\n\ |
| 200 | \n\ |
| 201 | Clear the list of previously registered exit functions."); |
| 202 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 203 | static PyObject * |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 204 | atexit_clear(PyObject *self, PyObject *unused) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 205 | { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 206 | atexit_cleanup(get_atexit_state(self)); |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 207 | Py_RETURN_NONE; |
| 208 | } |
| 209 | |
| 210 | PyDoc_STRVAR(atexit_ncallbacks__doc__, |
| 211 | "_ncallbacks() -> int\n\ |
| 212 | \n\ |
| 213 | Return the number of registered exit functions."); |
| 214 | |
| 215 | static PyObject * |
| 216 | atexit_ncallbacks(PyObject *self, PyObject *unused) |
| 217 | { |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 218 | atexitmodule_state *modstate; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 219 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 220 | modstate = get_atexit_state(self); |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 221 | |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 222 | return PyLong_FromSsize_t(modstate->ncallbacks); |
| 223 | } |
| 224 | |
| 225 | static int |
| 226 | atexit_m_traverse(PyObject *self, visitproc visit, void *arg) |
| 227 | { |
| 228 | int i; |
| 229 | atexitmodule_state *modstate; |
| 230 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 231 | modstate = (atexitmodule_state *)PyModule_GetState(self); |
Victor Stinner | 5b1ef20 | 2020-03-17 18:09:46 +0100 | [diff] [blame] | 232 | |
| 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 Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 240 | } |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static int |
| 245 | atexit_m_clear(PyObject *self) |
| 246 | { |
| 247 | atexitmodule_state *modstate; |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 248 | modstate = (atexitmodule_state *)PyModule_GetState(self); |
Victor Stinner | 5b1ef20 | 2020-03-17 18:09:46 +0100 | [diff] [blame] | 249 | atexit_cleanup(modstate); |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 250 | return 0; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Stefan Krah | 650365b | 2012-03-27 11:49:21 +0200 | [diff] [blame] | 253 | static void |
| 254 | atexit_free(PyObject *m) |
| 255 | { |
| 256 | atexitmodule_state *modstate; |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 257 | modstate = (atexitmodule_state *)PyModule_GetState(m); |
Victor Stinner | 5b1ef20 | 2020-03-17 18:09:46 +0100 | [diff] [blame] | 258 | atexit_cleanup(modstate); |
| 259 | PyMem_Free(modstate->atexit_callbacks); |
Stefan Krah | 650365b | 2012-03-27 11:49:21 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 262 | PyDoc_STRVAR(atexit_unregister__doc__, |
| 263 | "unregister(func) -> None\n\ |
| 264 | \n\ |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 265 | Unregister an exit function which was previously registered using\n\ |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 266 | atexit.register\n\ |
| 267 | \n\ |
| 268 | func - function to be unregistered"); |
| 269 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 270 | static PyObject * |
| 271 | atexit_unregister(PyObject *self, PyObject *func) |
| 272 | { |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 273 | atexitmodule_state *modstate; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 274 | atexit_callback *cb; |
| 275 | int i, eq; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 276 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 277 | modstate = get_atexit_state(self); |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 278 | |
| 279 | for (i = 0; i < modstate->ncallbacks; i++) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 280 | { |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 281 | cb = modstate->atexit_callbacks[i]; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 282 | if (cb == NULL) |
| 283 | continue; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 284 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 285 | eq = PyObject_RichCompareBool(cb->func, func, Py_EQ); |
| 286 | if (eq < 0) |
| 287 | return NULL; |
| 288 | if (eq) |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 289 | atexit_delete_cb(modstate, i); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 290 | } |
| 291 | Py_RETURN_NONE; |
| 292 | } |
| 293 | |
| 294 | static PyMethodDef atexit_methods[] = { |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 295 | {"register", (PyCFunction)(void(*)(void)) atexit_register, METH_VARARGS|METH_KEYWORDS, |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 296 | atexit_register__doc__}, |
| 297 | {"_clear", (PyCFunction) atexit_clear, METH_NOARGS, |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 298 | atexit_clear__doc__}, |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 299 | {"unregister", (PyCFunction) atexit_unregister, METH_O, |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 300 | atexit_unregister__doc__}, |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 301 | {"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS, |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 302 | atexit_run_exitfuncs__doc__}, |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 303 | {"_ncallbacks", (PyCFunction) atexit_ncallbacks, METH_NOARGS, |
| 304 | atexit_ncallbacks__doc__}, |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 305 | {NULL, NULL} /* sentinel */ |
| 306 | }; |
| 307 | |
| 308 | /* ===================================================================== */ |
| 309 | /* Initialization function. */ |
| 310 | |
| 311 | PyDoc_STRVAR(atexit__doc__, |
linchiwei123 | 52bf470 | 2020-07-27 07:33:00 +0800 | [diff] [blame] | 312 | "allow programmer to define multiple exit functions to be executed\n\ |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 313 | upon normal program termination.\n\ |
| 314 | \n\ |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 315 | Two public functions, register and unregister, are defined.\n\ |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 316 | "); |
| 317 | |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 318 | static int |
| 319 | atexit_exec(PyObject *m) { |
| 320 | atexitmodule_state *modstate; |
| 321 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 322 | modstate = get_atexit_state(m); |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 323 | 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 | |
| 334 | static PyModuleDef_Slot atexit_slots[] = { |
| 335 | {Py_mod_exec, atexit_exec}, |
| 336 | {0, NULL} |
| 337 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 338 | |
| 339 | static struct PyModuleDef atexitmodule = { |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 340 | PyModuleDef_HEAD_INIT, |
| 341 | "atexit", |
| 342 | atexit__doc__, |
| 343 | sizeof(atexitmodule_state), |
| 344 | atexit_methods, |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 345 | atexit_slots, |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 346 | atexit_m_traverse, |
| 347 | atexit_m_clear, |
| 348 | (freefunc)atexit_free |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 351 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 352 | PyInit_atexit(void) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 353 | { |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 354 | return PyModuleDef_Init(&atexitmodule); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 355 | } |