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" |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 10 | #include "pycore_interp.h" // PyInterpreterState.atexit_func |
| 11 | #include "pycore_pystate.h" // _PyInterpreterState_GET |
Collin Winter | 3e81ec8 | 2007-03-23 22:46:49 +0000 | [diff] [blame] | 12 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 13 | /* ===================================================================== */ |
| 14 | /* Callback machinery. */ |
| 15 | |
| 16 | typedef struct { |
| 17 | PyObject *func; |
| 18 | PyObject *args; |
| 19 | PyObject *kwargs; |
| 20 | } atexit_callback; |
| 21 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 22 | struct atexit_state { |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 23 | atexit_callback **atexit_callbacks; |
| 24 | int ncallbacks; |
| 25 | int callback_len; |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 26 | }; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 27 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 28 | static inline struct atexit_state* |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 29 | get_atexit_state(PyObject *module) |
| 30 | { |
| 31 | void *state = PyModule_GetState(module); |
| 32 | assert(state != NULL); |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 33 | return (struct atexit_state *)state; |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 34 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 35 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 36 | |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 37 | static void |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 38 | atexit_delete_cb(struct atexit_state *state, int i) |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 39 | { |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 40 | atexit_callback *cb = state->atexit_callbacks[i]; |
| 41 | state->atexit_callbacks[i] = NULL; |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 42 | |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 43 | Py_DECREF(cb->func); |
| 44 | Py_DECREF(cb->args); |
| 45 | Py_XDECREF(cb->kwargs); |
| 46 | PyMem_Free(cb); |
| 47 | } |
| 48 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 49 | |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 50 | /* Clear all callbacks without calling them */ |
| 51 | static void |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 52 | atexit_cleanup(struct atexit_state *state) |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 53 | { |
| 54 | atexit_callback *cb; |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 55 | for (int i = 0; i < state->ncallbacks; i++) { |
| 56 | cb = state->atexit_callbacks[i]; |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 57 | if (cb == NULL) |
| 58 | continue; |
| 59 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 60 | atexit_delete_cb(state, i); |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 61 | } |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 62 | state->ncallbacks = 0; |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 65 | /* Installed into pylifecycle.c's atexit mechanism */ |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 66 | |
Skip Montanaro | 711552b | 2008-09-23 00:52:29 +0000 | [diff] [blame] | 67 | static void |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 68 | atexit_callfuncs(PyObject *module) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 69 | { |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 70 | assert(!PyErr_Occurred()); |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 71 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 72 | if (module == NULL) { |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 73 | return; |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 74 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 75 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 76 | struct atexit_state *state = get_atexit_state(module); |
| 77 | if (state->ncallbacks == 0) { |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 78 | return; |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 79 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 80 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 81 | PyObject *exc_type = NULL, *exc_value, *exc_tb; |
| 82 | for (int i = state->ncallbacks - 1; i >= 0; i--) { |
| 83 | atexit_callback *cb = state->atexit_callbacks[i]; |
| 84 | if (cb == NULL) { |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 85 | continue; |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 86 | } |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 87 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 88 | PyObject *res = PyObject_Call(cb->func, cb->args, cb->kwargs); |
| 89 | if (res == NULL) { |
Neal Norwitz | 7d71fb8 | 2007-03-21 04:45:04 +0000 | [diff] [blame] | 90 | /* Maintain the last exception, but don't leak if there are |
| 91 | multiple exceptions. */ |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 92 | if (exc_type) { |
| 93 | Py_DECREF(exc_type); |
Neal Norwitz | 7d71fb8 | 2007-03-21 04:45:04 +0000 | [diff] [blame] | 94 | Py_XDECREF(exc_value); |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 95 | Py_XDECREF(exc_tb); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 96 | } |
| 97 | PyErr_Fetch(&exc_type, &exc_value, &exc_tb); |
Serhiy Storchaka | 3fd54d4 | 2017-06-12 08:25:04 +0300 | [diff] [blame] | 98 | if (!PyErr_GivenExceptionMatches(exc_type, PyExc_SystemExit)) { |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 99 | PySys_WriteStderr("Error in atexit._run_exitfuncs:\n"); |
Victor Stinner | 358e11d | 2011-01-05 03:54:25 +0000 | [diff] [blame] | 100 | PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 101 | PyErr_Display(exc_type, exc_value, exc_tb); |
| 102 | } |
| 103 | } |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 104 | else { |
| 105 | Py_DECREF(res); |
| 106 | } |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 107 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 108 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 109 | atexit_cleanup(state); |
Skip Montanaro | 711552b | 2008-09-23 00:52:29 +0000 | [diff] [blame] | 110 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 111 | if (exc_type) { |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 112 | PyErr_Restore(exc_type, exc_value, exc_tb); |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 113 | } |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 116 | /* ===================================================================== */ |
| 117 | /* Module methods. */ |
| 118 | |
| 119 | PyDoc_STRVAR(atexit_register__doc__, |
| 120 | "register(func, *args, **kwargs) -> func\n\ |
| 121 | \n\ |
| 122 | Register a function to be executed upon normal program termination\n\ |
| 123 | \n\ |
| 124 | func - function to be called at exit\n\ |
| 125 | args - optional arguments to pass to func\n\ |
| 126 | kwargs - optional keyword arguments to pass to func\n\ |
| 127 | \n\ |
| 128 | func is returned to facilitate usage as a decorator."); |
| 129 | |
| 130 | static PyObject * |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 131 | atexit_register(PyObject *module, PyObject *args, PyObject *kwargs) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 132 | { |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 133 | if (PyTuple_GET_SIZE(args) == 0) { |
| 134 | PyErr_SetString(PyExc_TypeError, |
| 135 | "register() takes at least 1 argument (0 given)"); |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 136 | return NULL; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 137 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 138 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 139 | PyObject *func = PyTuple_GET_ITEM(args, 0); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 140 | if (!PyCallable_Check(func)) { |
| 141 | PyErr_SetString(PyExc_TypeError, |
| 142 | "the first argument must be callable"); |
| 143 | return NULL; |
| 144 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 145 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 146 | struct atexit_state *state = get_atexit_state(module); |
| 147 | if (state->ncallbacks >= state->callback_len) { |
| 148 | atexit_callback **r; |
| 149 | state->callback_len += 16; |
| 150 | r = (atexit_callback**)PyMem_Realloc(state->atexit_callbacks, |
| 151 | sizeof(atexit_callback*) * state->callback_len); |
| 152 | if (r == NULL) |
| 153 | return PyErr_NoMemory(); |
| 154 | state->atexit_callbacks = r; |
| 155 | } |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 156 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 157 | atexit_callback *callback = PyMem_Malloc(sizeof(atexit_callback)); |
| 158 | if (callback == NULL) { |
| 159 | return PyErr_NoMemory(); |
| 160 | } |
| 161 | |
| 162 | callback->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 163 | if (callback->args == NULL) { |
| 164 | PyMem_Free(callback); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 165 | return NULL; |
| 166 | } |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 167 | callback->func = Py_NewRef(func); |
| 168 | callback->kwargs = Py_XNewRef(kwargs); |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 169 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 170 | state->atexit_callbacks[state->ncallbacks++] = callback; |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 171 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 172 | return Py_NewRef(func); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 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 * |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 181 | atexit_run_exitfuncs(PyObject *module, PyObject *unused) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 182 | { |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 183 | atexit_callfuncs(module); |
| 184 | if (PyErr_Occurred()) { |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 185 | return NULL; |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 186 | } |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 187 | Py_RETURN_NONE; |
| 188 | } |
| 189 | |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 190 | PyDoc_STRVAR(atexit_clear__doc__, |
| 191 | "_clear() -> None\n\ |
| 192 | \n\ |
| 193 | Clear the list of previously registered exit functions."); |
| 194 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 195 | static PyObject * |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 196 | atexit_clear(PyObject *module, PyObject *unused) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 197 | { |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 198 | atexit_cleanup(get_atexit_state(module)); |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 199 | Py_RETURN_NONE; |
| 200 | } |
| 201 | |
| 202 | PyDoc_STRVAR(atexit_ncallbacks__doc__, |
| 203 | "_ncallbacks() -> int\n\ |
| 204 | \n\ |
| 205 | Return the number of registered exit functions."); |
| 206 | |
| 207 | static PyObject * |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 208 | atexit_ncallbacks(PyObject *module, PyObject *unused) |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 209 | { |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 210 | struct atexit_state *state = get_atexit_state(module); |
| 211 | return PyLong_FromSsize_t(state->ncallbacks); |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static int |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 215 | atexit_m_traverse(PyObject *module, visitproc visit, void *arg) |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 216 | { |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 217 | struct atexit_state *state = (struct atexit_state *)PyModule_GetState(module); |
| 218 | for (int i = 0; i < state->ncallbacks; i++) { |
| 219 | atexit_callback *cb = state->atexit_callbacks[i]; |
Victor Stinner | 5b1ef20 | 2020-03-17 18:09:46 +0100 | [diff] [blame] | 220 | if (cb == NULL) |
| 221 | continue; |
| 222 | Py_VISIT(cb->func); |
| 223 | Py_VISIT(cb->args); |
| 224 | Py_VISIT(cb->kwargs); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 225 | } |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static int |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 230 | atexit_m_clear(PyObject *module) |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 231 | { |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 232 | struct atexit_state *state = (struct atexit_state *)PyModule_GetState(module); |
| 233 | atexit_cleanup(state); |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 234 | return 0; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Stefan Krah | 650365b | 2012-03-27 11:49:21 +0200 | [diff] [blame] | 237 | static void |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 238 | atexit_free(PyObject *module) |
Stefan Krah | 650365b | 2012-03-27 11:49:21 +0200 | [diff] [blame] | 239 | { |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 240 | struct atexit_state *state = (struct atexit_state *)PyModule_GetState(module); |
| 241 | atexit_cleanup(state); |
| 242 | PyMem_Free(state->atexit_callbacks); |
Stefan Krah | 650365b | 2012-03-27 11:49:21 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 245 | PyDoc_STRVAR(atexit_unregister__doc__, |
| 246 | "unregister(func) -> None\n\ |
| 247 | \n\ |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 248 | Unregister an exit function which was previously registered using\n\ |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 249 | atexit.register\n\ |
| 250 | \n\ |
| 251 | func - function to be unregistered"); |
| 252 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 253 | static PyObject * |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 254 | atexit_unregister(PyObject *module, PyObject *func) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 255 | { |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 256 | struct atexit_state *state = get_atexit_state(module); |
| 257 | for (int i = 0; i < state->ncallbacks; i++) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 258 | { |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 259 | atexit_callback *cb = state->atexit_callbacks[i]; |
| 260 | if (cb == NULL) { |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 261 | continue; |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 262 | } |
Christian Heimes | 9c94ba4 | 2008-10-30 21:34:02 +0000 | [diff] [blame] | 263 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 264 | int eq = PyObject_RichCompareBool(cb->func, func, Py_EQ); |
| 265 | if (eq < 0) { |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 266 | return NULL; |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 267 | } |
| 268 | if (eq) { |
| 269 | atexit_delete_cb(state, i); |
| 270 | } |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 271 | } |
| 272 | Py_RETURN_NONE; |
| 273 | } |
| 274 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 275 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 276 | static PyMethodDef atexit_methods[] = { |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 277 | {"register", (PyCFunction)(void(*)(void)) atexit_register, METH_VARARGS|METH_KEYWORDS, |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 278 | atexit_register__doc__}, |
| 279 | {"_clear", (PyCFunction) atexit_clear, METH_NOARGS, |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 280 | atexit_clear__doc__}, |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 281 | {"unregister", (PyCFunction) atexit_unregister, METH_O, |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 282 | atexit_unregister__doc__}, |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 283 | {"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS, |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 284 | atexit_run_exitfuncs__doc__}, |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 285 | {"_ncallbacks", (PyCFunction) atexit_ncallbacks, METH_NOARGS, |
| 286 | atexit_ncallbacks__doc__}, |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 287 | {NULL, NULL} /* sentinel */ |
| 288 | }; |
| 289 | |
| 290 | /* ===================================================================== */ |
| 291 | /* Initialization function. */ |
| 292 | |
| 293 | PyDoc_STRVAR(atexit__doc__, |
linchiwei123 | 52bf470 | 2020-07-27 07:33:00 +0800 | [diff] [blame] | 294 | "allow programmer to define multiple exit functions to be executed\n\ |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 295 | upon normal program termination.\n\ |
| 296 | \n\ |
Skip Montanaro | 28a181c | 2007-08-06 20:59:28 +0000 | [diff] [blame] | 297 | Two public functions, register and unregister, are defined.\n\ |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 298 | "); |
| 299 | |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 300 | static int |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 301 | atexit_exec(PyObject *module) |
| 302 | { |
| 303 | struct atexit_state *state = get_atexit_state(module); |
| 304 | state->callback_len = 32; |
| 305 | state->ncallbacks = 0; |
| 306 | state->atexit_callbacks = PyMem_New(atexit_callback*, state->callback_len); |
| 307 | if (state->atexit_callbacks == NULL) { |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 308 | return -1; |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 309 | } |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 310 | |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 311 | PyInterpreterState *is = _PyInterpreterState_GET(); |
| 312 | is->atexit_func = atexit_callfuncs; |
| 313 | is->atexit_module = module; |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static PyModuleDef_Slot atexit_slots[] = { |
| 318 | {Py_mod_exec, atexit_exec}, |
| 319 | {0, NULL} |
| 320 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 321 | |
| 322 | static struct PyModuleDef atexitmodule = { |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 323 | PyModuleDef_HEAD_INIT, |
Victor Stinner | 83d5204 | 2020-12-14 22:40:40 +0100 | [diff] [blame^] | 324 | .m_name = "atexit", |
| 325 | .m_doc = atexit__doc__, |
| 326 | .m_size = sizeof(struct atexit_state), |
| 327 | .m_methods = atexit_methods, |
| 328 | .m_slots = atexit_slots, |
| 329 | .m_traverse = atexit_m_traverse, |
| 330 | .m_clear = atexit_m_clear, |
| 331 | .m_free = (freefunc)atexit_free |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 332 | }; |
| 333 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 334 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 335 | PyInit_atexit(void) |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 336 | { |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 337 | return PyModuleDef_Init(&atexitmodule); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 338 | } |