summaryrefslogtreecommitdiff
path: root/Modules/atexitmodule.c
diff options
context:
space:
mode:
authorNeal Norwitz <nnorwitz@gmail.com>2007-03-21 04:45:04 +0000
committerNeal Norwitz <nnorwitz@gmail.com>2007-03-21 04:45:04 +0000
commit7d71fb8132d1ce0c49ad4557b89da592120cf118 (patch)
tree154c2d228325c3b6bfa6d95310972dcd3df9f6d7 /Modules/atexitmodule.c
parent670e6921349dd408b6958a0c5d3b1486725f9beb (diff)
downloadcpython-git-7d71fb8132d1ce0c49ad4557b89da592120cf118.tar.gz
Little fixes:
* make some module variables static to prevent name pollution * Add some comments to clarify what's going on and some XXXs to address * Add a space after "for" before ( * exc_value and tb can be NULL in some cases * Get working on Windows (I think)
Diffstat (limited to 'Modules/atexitmodule.c')
-rw-r--r--Modules/atexitmodule.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/Modules/atexitmodule.c b/Modules/atexitmodule.c
index b8a8bf6562..9f6c80edd0 100644
--- a/Modules/atexitmodule.c
+++ b/Modules/atexitmodule.c
@@ -17,9 +17,9 @@ typedef struct {
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 */
@@ -33,7 +33,7 @@ atexit_callfuncs(void)
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)
@@ -42,10 +42,12 @@ atexit_callfuncs(void)
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)) {
@@ -92,6 +94,8 @@ atexit_register(PyObject *self, PyObject *args, PyObject *kwargs)
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);
@@ -145,7 +149,7 @@ atexit_clear(PyObject *self)
atexit_callback *cb;
int i;
- for(i = 0; i < ncallbacks; i++)
+ for (i = 0; i < ncallbacks; i++)
{
cb = atexit_callbacks[i];
if (cb == NULL)
@@ -163,7 +167,7 @@ atexit_unregister(PyObject *self, PyObject *func)
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)
@@ -213,5 +217,8 @@ initatexit(void)
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);
}