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