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