/src/cpython3/Modules/atexitmodule.c
Line | Count | Source |
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 | | #include "pycore_atexit.h" // export _Py_AtExit() |
11 | | #include "pycore_initconfig.h" // _PyStatus_NO_MEMORY |
12 | | #include "pycore_interp.h" // PyInterpreterState.atexit |
13 | | #include "pycore_pystate.h" // _PyInterpreterState_GET |
14 | | |
15 | | /*[clinic input] |
16 | | module atexit |
17 | | [clinic start generated code]*/ |
18 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ac8fd67d15bf23fc]*/ |
19 | | |
20 | | #include "clinic/atexitmodule.c.h" |
21 | | |
22 | | /* ===================================================================== */ |
23 | | /* Callback machinery. */ |
24 | | |
25 | | static inline struct atexit_state* |
26 | | get_atexit_state(void) |
27 | 0 | { |
28 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
29 | 0 | return &interp->atexit; |
30 | 0 | } |
31 | | |
32 | | |
33 | | int |
34 | | PyUnstable_AtExit(PyInterpreterState *interp, |
35 | | atexit_datacallbackfunc func, void *data) |
36 | 0 | { |
37 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
38 | 0 | _Py_EnsureTstateNotNULL(tstate); |
39 | 0 | assert(tstate->interp == interp); |
40 | | |
41 | 0 | atexit_callback *callback = PyMem_Malloc(sizeof(atexit_callback)); |
42 | 0 | if (callback == NULL) { |
43 | 0 | PyErr_NoMemory(); |
44 | 0 | return -1; |
45 | 0 | } |
46 | 0 | callback->func = func; |
47 | 0 | callback->data = data; |
48 | 0 | callback->next = NULL; |
49 | |
|
50 | 0 | struct atexit_state *state = &interp->atexit; |
51 | 0 | _PyAtExit_LockCallbacks(state); |
52 | 0 | atexit_callback *top = state->ll_callbacks; |
53 | 0 | if (top == NULL) { |
54 | 0 | state->ll_callbacks = callback; |
55 | 0 | } |
56 | 0 | else { |
57 | 0 | callback->next = top; |
58 | 0 | state->ll_callbacks = callback; |
59 | 0 | } |
60 | 0 | _PyAtExit_UnlockCallbacks(state); |
61 | 0 | return 0; |
62 | 0 | } |
63 | | |
64 | | |
65 | | /* Clear all callbacks without calling them */ |
66 | | static void |
67 | | atexit_cleanup(struct atexit_state *state) |
68 | 0 | { |
69 | 0 | PyList_Clear(state->callbacks); |
70 | 0 | } |
71 | | |
72 | | |
73 | | PyStatus |
74 | | _PyAtExit_Init(PyInterpreterState *interp) |
75 | 22 | { |
76 | 22 | struct atexit_state *state = &interp->atexit; |
77 | | // _PyAtExit_Init() must only be called once |
78 | 22 | assert(state->callbacks == NULL); |
79 | | |
80 | 22 | state->callbacks = PyList_New(0); |
81 | 22 | if (state->callbacks == NULL) { |
82 | 0 | return _PyStatus_NO_MEMORY(); |
83 | 0 | } |
84 | 22 | return _PyStatus_OK(); |
85 | 22 | } |
86 | | |
87 | | void |
88 | | _PyAtExit_Fini(PyInterpreterState *interp) |
89 | 0 | { |
90 | | // In theory, there shouldn't be any threads left by now, so we |
91 | | // won't lock this. |
92 | 0 | struct atexit_state *state = &interp->atexit; |
93 | 0 | atexit_cleanup(state); |
94 | 0 | Py_CLEAR(state->callbacks); |
95 | |
|
96 | 0 | atexit_callback *next = state->ll_callbacks; |
97 | 0 | state->ll_callbacks = NULL; |
98 | 0 | while (next != NULL) { |
99 | 0 | atexit_callback *callback = next; |
100 | 0 | next = callback->next; |
101 | 0 | atexit_datacallbackfunc exitfunc = callback->func; |
102 | 0 | void *data = callback->data; |
103 | | // It was allocated in _PyAtExit_AddCallback(). |
104 | 0 | PyMem_Free(callback); |
105 | 0 | exitfunc(data); |
106 | 0 | } |
107 | 0 | } |
108 | | |
109 | | static void |
110 | | atexit_callfuncs(struct atexit_state *state) |
111 | 0 | { |
112 | 0 | assert(!PyErr_Occurred()); |
113 | 0 | assert(state->callbacks != NULL); |
114 | 0 | assert(PyList_CheckExact(state->callbacks)); |
115 | | |
116 | | // Create a copy of the list for thread safety |
117 | 0 | PyObject *copy = PyList_GetSlice(state->callbacks, 0, PyList_GET_SIZE(state->callbacks)); |
118 | 0 | if (copy == NULL) |
119 | 0 | { |
120 | 0 | PyErr_FormatUnraisable("Exception ignored while " |
121 | 0 | "copying atexit callbacks"); |
122 | 0 | atexit_cleanup(state); |
123 | 0 | return; |
124 | 0 | } |
125 | | |
126 | 0 | for (Py_ssize_t i = 0; i < PyList_GET_SIZE(copy); ++i) { |
127 | | // We don't have to worry about evil borrowed references, because |
128 | | // no other threads can access this list. |
129 | 0 | PyObject *tuple = PyList_GET_ITEM(copy, i); |
130 | 0 | assert(PyTuple_CheckExact(tuple)); |
131 | | |
132 | 0 | PyObject *func = PyTuple_GET_ITEM(tuple, 0); |
133 | 0 | PyObject *args = PyTuple_GET_ITEM(tuple, 1); |
134 | 0 | PyObject *kwargs = PyTuple_GET_ITEM(tuple, 2); |
135 | | |
136 | 0 | PyObject *res = PyObject_Call(func, |
137 | 0 | args, |
138 | 0 | kwargs == Py_None ? NULL : kwargs); |
139 | 0 | if (res == NULL) { |
140 | 0 | PyErr_FormatUnraisable( |
141 | 0 | "Exception ignored in atexit callback %R", func); |
142 | 0 | } |
143 | 0 | else { |
144 | 0 | Py_DECREF(res); |
145 | 0 | } |
146 | 0 | } |
147 | | |
148 | 0 | Py_DECREF(copy); |
149 | 0 | atexit_cleanup(state); |
150 | |
|
151 | 0 | assert(!PyErr_Occurred()); |
152 | 0 | } |
153 | | |
154 | | |
155 | | void |
156 | | _PyAtExit_Call(PyInterpreterState *interp) |
157 | 0 | { |
158 | 0 | struct atexit_state *state = &interp->atexit; |
159 | 0 | atexit_callfuncs(state); |
160 | 0 | } |
161 | | |
162 | | |
163 | | /* ===================================================================== */ |
164 | | /* Module methods. */ |
165 | | |
166 | | |
167 | | /*[clinic input] |
168 | | atexit.register |
169 | | |
170 | | func: object |
171 | | / |
172 | | *args: tuple |
173 | | **kwargs: dict |
174 | | |
175 | | Register a function to be executed upon normal program termination |
176 | | |
177 | | func - function to be called at exit |
178 | | args - optional arguments to pass to func |
179 | | kwargs - optional keyword arguments to pass to func |
180 | | |
181 | | func is returned to facilitate usage as a decorator. |
182 | | [clinic start generated code]*/ |
183 | | |
184 | | static PyObject * |
185 | | atexit_register_impl(PyObject *module, PyObject *func, PyObject *args, |
186 | | PyObject *kwargs) |
187 | | /*[clinic end generated code: output=c921286994bd8612 input=a682b5a343a82e4a]*/ |
188 | 0 | { |
189 | 0 | if (!PyCallable_Check(func)) { |
190 | 0 | PyErr_SetString(PyExc_TypeError, |
191 | 0 | "the first argument must be callable"); |
192 | 0 | return NULL; |
193 | 0 | } |
194 | 0 | PyObject *callback = PyTuple_Pack(3, func, args, |
195 | 0 | kwargs ? kwargs : Py_None); |
196 | 0 | if (callback == NULL) |
197 | 0 | { |
198 | 0 | return NULL; |
199 | 0 | } |
200 | | |
201 | 0 | struct atexit_state *state = get_atexit_state(); |
202 | | // atexit callbacks go in a LIFO order |
203 | 0 | if (PyList_Insert(state->callbacks, 0, callback) < 0) |
204 | 0 | { |
205 | 0 | Py_DECREF(callback); |
206 | 0 | return NULL; |
207 | 0 | } |
208 | 0 | Py_DECREF(callback); |
209 | |
|
210 | 0 | return Py_NewRef(func); |
211 | 0 | } |
212 | | |
213 | | PyDoc_STRVAR(atexit_run_exitfuncs__doc__, |
214 | | "_run_exitfuncs($module, /)\n\ |
215 | | --\n\ |
216 | | \n\ |
217 | | Run all registered exit functions.\n\ |
218 | | \n\ |
219 | | If a callback raises an exception, it is logged with sys.unraisablehook."); |
220 | | |
221 | | static PyObject * |
222 | | atexit_run_exitfuncs(PyObject *module, PyObject *Py_UNUSED(dummy)) |
223 | 0 | { |
224 | 0 | struct atexit_state *state = get_atexit_state(); |
225 | 0 | atexit_callfuncs(state); |
226 | 0 | Py_RETURN_NONE; |
227 | 0 | } |
228 | | |
229 | | PyDoc_STRVAR(atexit_clear__doc__, |
230 | | "_clear($module, /)\n\ |
231 | | --\n\ |
232 | | \n\ |
233 | | Clear the list of previously registered exit functions."); |
234 | | |
235 | | static PyObject * |
236 | | atexit_clear(PyObject *module, PyObject *Py_UNUSED(dummy)) |
237 | 0 | { |
238 | 0 | atexit_cleanup(get_atexit_state()); |
239 | 0 | Py_RETURN_NONE; |
240 | 0 | } |
241 | | |
242 | | PyDoc_STRVAR(atexit_ncallbacks__doc__, |
243 | | "_ncallbacks($module, /)\n\ |
244 | | --\n\ |
245 | | \n\ |
246 | | Return the number of registered exit functions."); |
247 | | |
248 | | static PyObject * |
249 | | atexit_ncallbacks(PyObject *module, PyObject *Py_UNUSED(dummy)) |
250 | 0 | { |
251 | 0 | struct atexit_state *state = get_atexit_state(); |
252 | 0 | assert(state->callbacks != NULL); |
253 | 0 | assert(PyList_CheckExact(state->callbacks)); |
254 | 0 | return PyLong_FromSsize_t(PyList_GET_SIZE(state->callbacks)); |
255 | 0 | } |
256 | | |
257 | | static int |
258 | | atexit_unregister_locked(PyObject *callbacks, PyObject *func) |
259 | 0 | { |
260 | 0 | for (Py_ssize_t i = PyList_GET_SIZE(callbacks) - 1; i >= 0; --i) { |
261 | 0 | PyObject *tuple = Py_NewRef(PyList_GET_ITEM(callbacks, i)); |
262 | 0 | assert(PyTuple_CheckExact(tuple)); |
263 | 0 | PyObject *to_compare = PyTuple_GET_ITEM(tuple, 0); |
264 | 0 | int cmp = PyObject_RichCompareBool(func, to_compare, Py_EQ); |
265 | 0 | if (cmp < 0) { |
266 | 0 | Py_DECREF(tuple); |
267 | 0 | return -1; |
268 | 0 | } |
269 | 0 | if (cmp == 1) { |
270 | | // We found a callback! |
271 | | // But its index could have changed if it or other callbacks were |
272 | | // unregistered during the comparison. |
273 | 0 | Py_ssize_t j = PyList_GET_SIZE(callbacks) - 1; |
274 | 0 | j = Py_MIN(j, i); |
275 | 0 | for (; j >= 0; --j) { |
276 | 0 | if (PyList_GET_ITEM(callbacks, j) == tuple) { |
277 | | // We found the callback index! For real! |
278 | 0 | if (PyList_SetSlice(callbacks, j, j + 1, NULL) < 0) { |
279 | 0 | Py_DECREF(tuple); |
280 | 0 | return -1; |
281 | 0 | } |
282 | 0 | i = j; |
283 | 0 | break; |
284 | 0 | } |
285 | 0 | } |
286 | 0 | } |
287 | 0 | Py_DECREF(tuple); |
288 | 0 | if (i >= PyList_GET_SIZE(callbacks)) { |
289 | 0 | i = PyList_GET_SIZE(callbacks); |
290 | 0 | } |
291 | 0 | } |
292 | | |
293 | 0 | return 0; |
294 | 0 | } |
295 | | |
296 | | PyDoc_STRVAR(atexit_unregister__doc__, |
297 | | "unregister($module, func, /)\n\ |
298 | | --\n\ |
299 | | \n\ |
300 | | Unregister an exit function which was previously registered using\n\ |
301 | | atexit.register\n\ |
302 | | \n\ |
303 | | func - function to be unregistered"); |
304 | | |
305 | | static PyObject * |
306 | | atexit_unregister(PyObject *module, PyObject *func) |
307 | 0 | { |
308 | 0 | struct atexit_state *state = get_atexit_state(); |
309 | 0 | int result; |
310 | 0 | Py_BEGIN_CRITICAL_SECTION(state->callbacks); |
311 | 0 | result = atexit_unregister_locked(state->callbacks, func); |
312 | 0 | Py_END_CRITICAL_SECTION(); |
313 | 0 | return result < 0 ? NULL : Py_None; |
314 | 0 | } |
315 | | |
316 | | |
317 | | static PyMethodDef atexit_methods[] = { |
318 | | ATEXIT_REGISTER_METHODDEF |
319 | | {"_clear", atexit_clear, METH_NOARGS, atexit_clear__doc__}, |
320 | | {"unregister", atexit_unregister, METH_O, atexit_unregister__doc__}, |
321 | | {"_run_exitfuncs", atexit_run_exitfuncs, METH_NOARGS, |
322 | | atexit_run_exitfuncs__doc__}, |
323 | | {"_ncallbacks", atexit_ncallbacks, METH_NOARGS, |
324 | | atexit_ncallbacks__doc__}, |
325 | | {NULL, NULL} /* sentinel */ |
326 | | }; |
327 | | |
328 | | |
329 | | /* ===================================================================== */ |
330 | | /* Initialization function. */ |
331 | | |
332 | | PyDoc_STRVAR(atexit__doc__, |
333 | | "allow programmer to define multiple exit functions to be executed\n\ |
334 | | upon normal program termination.\n\ |
335 | | \n\ |
336 | | Two public functions, register and unregister, are defined.\n\ |
337 | | "); |
338 | | |
339 | | static PyModuleDef_Slot atexitmodule_slots[] = { |
340 | | _Py_ABI_SLOT, |
341 | | {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, |
342 | | {Py_mod_gil, Py_MOD_GIL_NOT_USED}, |
343 | | {0, NULL} |
344 | | }; |
345 | | |
346 | | static struct PyModuleDef atexitmodule = { |
347 | | PyModuleDef_HEAD_INIT, |
348 | | .m_name = "atexit", |
349 | | .m_doc = atexit__doc__, |
350 | | .m_size = 0, |
351 | | .m_methods = atexit_methods, |
352 | | .m_slots = atexitmodule_slots, |
353 | | }; |
354 | | |
355 | | PyMODINIT_FUNC |
356 | | PyInit_atexit(void) |
357 | 0 | { |
358 | 0 | return PyModuleDef_Init(&atexitmodule); |
359 | 0 | } |