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