blob: be16de4a17552c47ad038e720b6978499fabfe80 [file] [log] [blame]
Collin Winter670e6922007-03-21 02:57:17 +00001/*
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 Winter3e81ec82007-03-23 22:46:49 +000011/* Forward declaration (for atexit_cleanup) */
12static PyObject *atexit_clear(PyObject*);
Skip Montanaro711552b2008-09-23 00:52:29 +000013/* Forward declaration (for atexit_callfuncs) */
14static void atexit_cleanup(void);
Collin Winter3e81ec82007-03-23 22:46:49 +000015
Collin Winter670e6922007-03-21 02:57:17 +000016/* ===================================================================== */
17/* Callback machinery. */
18
19typedef struct {
20 PyObject *func;
21 PyObject *args;
22 PyObject *kwargs;
23} atexit_callback;
24
Neal Norwitz7d71fb82007-03-21 04:45:04 +000025static atexit_callback **atexit_callbacks;
26static int ncallbacks = 0;
27static int callback_len = 32;
Collin Winter670e6922007-03-21 02:57:17 +000028
29/* Installed into pythonrun.c's atexit mechanism */
30
Skip Montanaro711552b2008-09-23 00:52:29 +000031static void
Collin Winter670e6922007-03-21 02:57:17 +000032atexit_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 Norwitz7d71fb82007-03-21 04:45:04 +000041 for (i = ncallbacks - 1; i >= 0; i--)
Collin Winter670e6922007-03-21 02:57:17 +000042 {
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 Norwitz7d71fb82007-03-21 04:45:04 +000050 /* Maintain the last exception, but don't leak if there are
51 multiple exceptions. */
Collin Winter670e6922007-03-21 02:57:17 +000052 if (exc_type) {
53 Py_DECREF(exc_type);
Neal Norwitz7d71fb82007-03-21 04:45:04 +000054 Py_XDECREF(exc_value);
55 Py_XDECREF(exc_tb);
Collin Winter670e6922007-03-21 02:57:17 +000056 }
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 Montanaro711552b2008-09-23 00:52:29 +000065 atexit_cleanup();
66
Collin Winter670e6922007-03-21 02:57:17 +000067 if (exc_type)
68 PyErr_Restore(exc_type, exc_value, exc_tb);
69}
70
Skip Montanaro711552b2008-09-23 00:52:29 +000071static void
Collin Winter670e6922007-03-21 02:57:17 +000072atexit_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 Montanaro711552b2008-09-23 00:52:29 +000082static void
Collin Winter3e81ec82007-03-23 22:46:49 +000083atexit_cleanup(void)
84{
85 PyObject *r = atexit_clear(NULL);
86 Py_DECREF(r);
87}
88
Collin Winter670e6922007-03-21 02:57:17 +000089/* ===================================================================== */
90/* Module methods. */
91
92PyDoc_STRVAR(atexit_register__doc__,
93"register(func, *args, **kwargs) -> func\n\
94\n\
95Register 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
103static PyObject *
104atexit_register(PyObject *self, PyObject *args, PyObject *kwargs)
105{
106 atexit_callback *new_callback;
107 PyObject *func = NULL;
108
109 if (ncallbacks >= callback_len) {
Collin Winter3e81ec82007-03-23 22:46:49 +0000110 atexit_callback **r;
Collin Winter670e6922007-03-21 02:57:17 +0000111 callback_len += 16;
Collin Winter3e81ec82007-03-23 22:46:49 +0000112 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 Winter670e6922007-03-21 02:57:17 +0000117 }
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 Montanaro28a181c2007-08-06 20:59:28 +0000152PyDoc_STRVAR(atexit_run_exitfuncs__doc__,
153"_run_exitfuncs() -> None\n\
154\n\
155Run all registered exit functions.");
156
Collin Winter670e6922007-03-21 02:57:17 +0000157static PyObject *
158atexit_run_exitfuncs(PyObject *self)
159{
160 atexit_callfuncs();
161 if (PyErr_Occurred())
162 return NULL;
163 Py_RETURN_NONE;
164}
165
Skip Montanaro28a181c2007-08-06 20:59:28 +0000166PyDoc_STRVAR(atexit_clear__doc__,
167"_clear() -> None\n\
168\n\
169Clear the list of previously registered exit functions.");
170
Collin Winter670e6922007-03-21 02:57:17 +0000171static PyObject *
172atexit_clear(PyObject *self)
173{
174 atexit_callback *cb;
175 int i;
176
Neal Norwitz7d71fb82007-03-21 04:45:04 +0000177 for (i = 0; i < ncallbacks; i++)
Collin Winter670e6922007-03-21 02:57:17 +0000178 {
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 Montanaro28a181c2007-08-06 20:59:28 +0000189PyDoc_STRVAR(atexit_unregister__doc__,
190"unregister(func) -> None\n\
191\n\
192Unregister a exit function which was previously registered using\n\
193atexit.register\n\
194\n\
195 func - function to be unregistered");
196
Collin Winter670e6922007-03-21 02:57:17 +0000197static PyObject *
198atexit_unregister(PyObject *self, PyObject *func)
199{
200 atexit_callback *cb;
201 int i, eq;
202
Neal Norwitz7d71fb82007-03-21 04:45:04 +0000203 for (i = 0; i < ncallbacks; i++)
Collin Winter670e6922007-03-21 02:57:17 +0000204 {
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
218static PyMethodDef atexit_methods[] = {
219 {"register", (PyCFunction) atexit_register, METH_VARARGS|METH_KEYWORDS,
220 atexit_register__doc__},
221 {"_clear", (PyCFunction) atexit_clear, METH_NOARGS,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000222 atexit_clear__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000223 {"unregister", (PyCFunction) atexit_unregister, METH_O,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000224 atexit_unregister__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000225 {"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000226 atexit_run_exitfuncs__doc__},
Collin Winter670e6922007-03-21 02:57:17 +0000227 {NULL, NULL} /* sentinel */
228};
229
230/* ===================================================================== */
231/* Initialization function. */
232
233PyDoc_STRVAR(atexit__doc__,
Skip Montanaro28a181c2007-08-06 20:59:28 +0000234"allow programmer to define multiple exit functions to be executed\
Collin Winter670e6922007-03-21 02:57:17 +0000235upon normal program termination.\n\
236\n\
Skip Montanaro28a181c2007-08-06 20:59:28 +0000237Two public functions, register and unregister, are defined.\n\
Collin Winter670e6922007-03-21 02:57:17 +0000238");
239
Martin v. Löwis1a214512008-06-11 05:26:20 +0000240
241static 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 Winter670e6922007-03-21 02:57:17 +0000253PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000254PyInit_atexit(void)
Collin Winter670e6922007-03-21 02:57:17 +0000255{
256 PyObject *m;
257
258 atexit_callbacks = PyMem_New(atexit_callback*, callback_len);
259 if (atexit_callbacks == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000260 return NULL;
Collin Winter670e6922007-03-21 02:57:17 +0000261
Martin v. Löwis1a214512008-06-11 05:26:20 +0000262 m = PyModule_Create(&atexitmodule);
Collin Winter670e6922007-03-21 02:57:17 +0000263 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000264 return NULL;
Collin Winter670e6922007-03-21 02:57:17 +0000265
Collin Winter670e6922007-03-21 02:57:17 +0000266 _Py_PyAtExit(atexit_callfuncs);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000267 return m;
Collin Winter670e6922007-03-21 02:57:17 +0000268}