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