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