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