PyObject *kwargs;
} atexit_callback;
-atexit_callback **atexit_callbacks;
-int ncallbacks = 0;
-int callback_len = 32;
+static atexit_callback **atexit_callbacks;
+static int ncallbacks = 0;
+static int callback_len = 32;
/* Installed into pythonrun.c's atexit mechanism */
if (ncallbacks == 0)
return;
- for(i = ncallbacks - 1; i >= 0; i--)
+ for (i = ncallbacks - 1; i >= 0; i--)
{
cb = atexit_callbacks[i];
if (cb == NULL)
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_DECREF(exc_value);
- Py_DECREF(exc_tb);
+ Py_XDECREF(exc_value);
+ Py_XDECREF(exc_tb);
}
PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
if (ncallbacks >= callback_len) {
callback_len += 16;
+ /* XXX(nnorwitz): this leaks if realloc() fails. It also
+ doesn't verify realloc() returns a valid (non-NULL) pointer. */
atexit_callbacks = PyMem_Realloc(atexit_callbacks,
sizeof(atexit_callback*) * callback_len);
atexit_callback *cb;
int i;
- for(i = 0; i < ncallbacks; i++)
+ for (i = 0; i < ncallbacks; i++)
{
cb = atexit_callbacks[i];
if (cb == NULL)
atexit_callback *cb;
int i, eq;
- for(i = 0; i < ncallbacks; i++)
+ for (i = 0; i < ncallbacks; i++)
{
cb = atexit_callbacks[i];
if (cb == NULL)
if (m == NULL)
return;
+ /* XXX(nnorwitz): probably best to register a callback that will free
+ atexit_callbacks, otherwise valgrind will report memory leaks.
+ Need to call atexit_clear() first. */
_Py_PyAtExit(atexit_callfuncs);
}
extern void init_lsprof(void);
extern void init_ast(void);
extern void init_types(void);
+extern void initatexit(void);
/* tools/freeze/makeconfig.py marker for additional "extern" */
/* -- ADDMODULE MARKER 1 -- */
{"array", initarray},
{"_ast", init_ast},
+ {"atexit", initatexit},
#ifdef MS_WINDOWS
#ifndef MS_WIN64
{"audioop", initaudioop},