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 |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 66 | atexit_callfuncs(void) |
| 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 | PyObject *module; |
| 71 | atexitmodule_state *modstate; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 72 | int i; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 73 | |
| 74 | module = PyState_FindModule(&atexitmodule); |
| 75 | if (module == NULL) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 76 | return; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 77 | modstate = GET_ATEXIT_STATE(module); |
| 78 | |
| 79 | if (modstate->ncallbacks == 0) |
| 80 | return; |
| 81 | |
| 82 | |
| 83 | for (i = modstate->ncallbacks - 1; i >= 0; i--) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 84 | { |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 85 | cb = modstate->atexit_callbacks[i]; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 86 | if (cb == NULL) |
| 87 | continue; |
| 88 | |
| 89 | r = PyObject_Call(cb->func, cb->args, cb->kwargs); |
| 90 | Py_XDECREF(r); |
| 91 | if (r == NULL) { |
Neal Norwitz | 7d71fb8 | 2007-03-21 04:45:04 +0000 | [diff] [blame] | 92 | /* Maintain the last exception, but don't leak if there are |
| 93 | multiple exceptions. */ |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 94 | if (exc_type) { |
| 95 | Py_DECREF(exc_type); |
Neal Norwitz | 7d71fb8 | 2007-03-21 04:45:04 +0000 | [diff] [blame] | 96 | Py_XDECREF(exc_value); |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 97 | Py_XDECREF(exc_tb); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 98 | } |
| 99 | PyErr_Fetch(&exc_type, &exc_value, &exc_tb); |
| 100 | if (!PyErr_ExceptionMatches(PyExc_SystemExit)) { |
| 101 | PySys_WriteStderr("Error in atexit._run_exitfuncs:\n"); |
Victor Stinner | 358e11d | 2011-01-05 03:54:25 +0000 | [diff] [blame] | 102 | PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 103 | PyErr_Display(exc_type, exc_value, exc_tb); |
| 104 | } |
| 105 | } |
| 106 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 107 | |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 108 | atexit_cleanup(modstate); |
Skip Montanaro | 711552b | 2008-09-23 00:52:29 +0000 | [diff] [blame] | 109 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 110 | if (exc_type) |
| 111 | PyErr_Restore(exc_type, exc_value, exc_tb); |
| 112 | } |
| 113 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 114 | /* ===================================================================== */ |
| 115 | /* Module methods. */ |
| 116 | |
| 117 | PyDoc_STRVAR(atexit_register__doc__, |
| 118 | "register(func, *args, **kwargs) -> func\n\ |
| 119 | \n\ |
| 120 | Register a function to be executed upon normal program termination\n\ |
| 121 | \n\ |
| 122 | func - function to be called at exit\n\ |
| 123 | args - optional arguments to pass to func\n\ |
| 124 | kwargs - optional keyword arguments to pass to func\n\ |
| 125 | \n\ |
| 126 | func is returned to facilitate usage as a decorator."); |
| 127 | |
| 128 | static PyObject * |
| 129 | atexit_register(PyObject *self, PyObject *args, PyObject *kwargs) |
| 130 | { |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 131 | atexitmodule_state *modstate; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 132 | atexit_callback *new_callback; |
| 133 | PyObject *func = NULL; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 134 | |
| 135 | modstate = GET_ATEXIT_STATE(self); |
| 136 | |
| 137 | if (modstate->ncallbacks >= modstate->callback_len) { |
Collin Winter | 3e81ec8 | 2007-03-23 22:46:49 +0000 | [diff] [blame] | 138 | atexit_callback **r; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 139 | modstate->callback_len += 16; |
| 140 | r = (atexit_callback**)PyMem_Realloc(modstate->atexit_callbacks, |
| 141 | sizeof(atexit_callback*) * modstate->callback_len); |
Collin Winter | 3e81ec8 | 2007-03-23 22:46:49 +0000 | [diff] [blame] | 142 | if (r == NULL) |
| 143 | return PyErr_NoMemory(); |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 144 | modstate->atexit_callbacks = r; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 145 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 146 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 147 | if (PyTuple_GET_SIZE(args) == 0) { |
| 148 | PyErr_SetString(PyExc_TypeError, |
| 149 | "register() takes at least 1 argument (0 given)"); |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 150 | return NULL; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 151 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 152 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 153 | func = PyTuple_GET_ITEM(args, 0); |
| 154 | if (!PyCallable_Check(func)) { |
| 155 | PyErr_SetString(PyExc_TypeError, |
| 156 | "the first argument must be callable"); |
| 157 | return NULL; |
| 158 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 159 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 160 | new_callback = PyMem_Malloc(sizeof(atexit_callback)); |
| 161 | if (new_callback == NULL) |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 162 | return PyErr_NoMemory(); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 163 | |
| 164 | new_callback->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 165 | if (new_callback->args == NULL) { |
| 166 | PyMem_Free(new_callback); |
| 167 | return NULL; |
| 168 | } |
| 169 | new_callback->func = func; |
| 170 | new_callback->kwargs = kwargs; |
| 171 | Py_INCREF(func); |
| 172 | Py_XINCREF(kwargs); |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 173 | |
| 174 | modstate->atexit_callbacks[modstate->ncallbacks++] = new_callback; |
| 175 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 176 | Py_INCREF(func); |
| 177 | return func; |
| 178 | } |
| 179 | |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 180 | PyDoc_STRVAR(atexit_run_exitfuncs__doc__, |
| 181 | "_run_exitfuncs() -> None\n\ |
| 182 | \n\ |
| 183 | Run all registered exit functions."); |
| 184 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 185 | static PyObject * |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 186 | atexit_run_exitfuncs(PyObject *self, PyObject *unused) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 187 | { |
| 188 | atexit_callfuncs(); |
| 189 | if (PyErr_Occurred()) |
| 190 | return NULL; |
| 191 | Py_RETURN_NONE; |
| 192 | } |
| 193 | |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 194 | PyDoc_STRVAR(atexit_clear__doc__, |
| 195 | "_clear() -> None\n\ |
| 196 | \n\ |
| 197 | Clear the list of previously registered exit functions."); |
| 198 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 199 | static PyObject * |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 200 | atexit_clear(PyObject *self, PyObject *unused) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 201 | { |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 202 | atexit_cleanup(GET_ATEXIT_STATE(self)); |
| 203 | Py_RETURN_NONE; |
| 204 | } |
| 205 | |
| 206 | PyDoc_STRVAR(atexit_ncallbacks__doc__, |
| 207 | "_ncallbacks() -> int\n\ |
| 208 | \n\ |
| 209 | Return the number of registered exit functions."); |
| 210 | |
| 211 | static PyObject * |
| 212 | atexit_ncallbacks(PyObject *self, PyObject *unused) |
| 213 | { |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 214 | atexitmodule_state *modstate; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 215 | |
| 216 | modstate = GET_ATEXIT_STATE(self); |
| 217 | |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 218 | return PyLong_FromSsize_t(modstate->ncallbacks); |
| 219 | } |
| 220 | |
| 221 | static int |
| 222 | atexit_m_traverse(PyObject *self, visitproc visit, void *arg) |
| 223 | { |
| 224 | int i; |
| 225 | atexitmodule_state *modstate; |
| 226 | |
| 227 | modstate = GET_ATEXIT_STATE(self); |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 228 | for (i = 0; i < modstate->ncallbacks; i++) { |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 229 | atexit_callback *cb = modstate->atexit_callbacks[i]; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 230 | if (cb == NULL) |
| 231 | continue; |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 232 | Py_VISIT(cb->func); |
| 233 | Py_VISIT(cb->args); |
| 234 | Py_VISIT(cb->kwargs); |
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); |
| 244 | atexit_cleanup(modstate); |
| 245 | return 0; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Stefan Krah | 650365b | 2012-03-27 11:49:21 +0200 | [diff] [blame] | 248 | static void |
| 249 | atexit_free(PyObject *m) |
| 250 | { |
| 251 | atexitmodule_state *modstate; |
| 252 | modstate = GET_ATEXIT_STATE(m); |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 253 | atexit_cleanup(modstate); |
Stefan Krah | 650365b | 2012-03-27 11:49:21 +0200 | [diff] [blame] | 254 | PyMem_Free(modstate->atexit_callbacks); |
| 255 | } |
| 256 | |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 257 | PyDoc_STRVAR(atexit_unregister__doc__, |
| 258 | "unregister(func) -> None\n\ |
| 259 | \n\ |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 260 | Unregister an exit function which was previously registered using\n\ |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 261 | atexit.register\n\ |
| 262 | \n\ |
| 263 | func - function to be unregistered"); |
| 264 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 265 | static PyObject * |
| 266 | atexit_unregister(PyObject *self, PyObject *func) |
| 267 | { |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 268 | atexitmodule_state *modstate; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 269 | atexit_callback *cb; |
| 270 | int i, eq; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 271 | |
| 272 | modstate = GET_ATEXIT_STATE(self); |
| 273 | |
| 274 | for (i = 0; i < modstate->ncallbacks; i++) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 275 | { |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 276 | cb = modstate->atexit_callbacks[i]; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 277 | if (cb == NULL) |
| 278 | continue; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 279 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 280 | eq = PyObject_RichCompareBool(cb->func, func, Py_EQ); |
| 281 | if (eq < 0) |
| 282 | return NULL; |
| 283 | if (eq) |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 284 | atexit_delete_cb(modstate, i); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 285 | } |
| 286 | Py_RETURN_NONE; |
| 287 | } |
| 288 | |
| 289 | static PyMethodDef atexit_methods[] = { |
| 290 | {"register", (PyCFunction) atexit_register, METH_VARARGS|METH_KEYWORDS, |
| 291 | atexit_register__doc__}, |
| 292 | {"_clear", (PyCFunction) atexit_clear, METH_NOARGS, |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 293 | atexit_clear__doc__}, |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 294 | {"unregister", (PyCFunction) atexit_unregister, METH_O, |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 295 | atexit_unregister__doc__}, |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 296 | {"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS, |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 297 | atexit_run_exitfuncs__doc__}, |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 298 | {"_ncallbacks", (PyCFunction) atexit_ncallbacks, METH_NOARGS, |
| 299 | atexit_ncallbacks__doc__}, |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 300 | {NULL, NULL} /* sentinel */ |
| 301 | }; |
| 302 | |
| 303 | /* ===================================================================== */ |
| 304 | /* Initialization function. */ |
| 305 | |
| 306 | PyDoc_STRVAR(atexit__doc__, |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 307 | "allow programmer to define multiple exit functions to be executed\ |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 308 | upon normal program termination.\n\ |
| 309 | \n\ |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 310 | Two public functions, register and unregister, are defined.\n\ |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 311 | "); |
| 312 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 313 | |
| 314 | static struct PyModuleDef atexitmodule = { |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 315 | PyModuleDef_HEAD_INIT, |
| 316 | "atexit", |
| 317 | atexit__doc__, |
| 318 | sizeof(atexitmodule_state), |
| 319 | atexit_methods, |
| 320 | NULL, |
| 321 | atexit_m_traverse, |
| 322 | atexit_m_clear, |
| 323 | (freefunc)atexit_free |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 324 | }; |
| 325 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 326 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 327 | PyInit_atexit(void) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 328 | { |
| 329 | PyObject *m; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 330 | atexitmodule_state *modstate; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 331 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 332 | m = PyModule_Create(&atexitmodule); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 333 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 334 | return NULL; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 335 | |
| 336 | modstate = GET_ATEXIT_STATE(m); |
| 337 | modstate->callback_len = 32; |
| 338 | modstate->ncallbacks = 0; |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 339 | modstate->atexit_callbacks = PyMem_New(atexit_callback*, |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 340 | modstate->callback_len); |
| 341 | if (modstate->atexit_callbacks == NULL) |
| 342 | return NULL; |
| 343 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 344 | _Py_PyAtExit(atexit_callfuncs); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 345 | return m; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 346 | } |