Commit 7d71fb81 authored by Neal Norwitz's avatar Neal Norwitz

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)
parent 670e6921
...@@ -17,9 +17,9 @@ typedef struct { ...@@ -17,9 +17,9 @@ typedef struct {
PyObject *kwargs; PyObject *kwargs;
} atexit_callback; } atexit_callback;
atexit_callback **atexit_callbacks; static atexit_callback **atexit_callbacks;
int ncallbacks = 0; static int ncallbacks = 0;
int callback_len = 32; static int callback_len = 32;
/* Installed into pythonrun.c's atexit mechanism */ /* Installed into pythonrun.c's atexit mechanism */
...@@ -33,7 +33,7 @@ atexit_callfuncs(void) ...@@ -33,7 +33,7 @@ atexit_callfuncs(void)
if (ncallbacks == 0) if (ncallbacks == 0)
return; return;
for(i = ncallbacks - 1; i >= 0; i--) for (i = ncallbacks - 1; i >= 0; i--)
{ {
cb = atexit_callbacks[i]; cb = atexit_callbacks[i];
if (cb == NULL) if (cb == NULL)
...@@ -42,10 +42,12 @@ atexit_callfuncs(void) ...@@ -42,10 +42,12 @@ atexit_callfuncs(void)
r = PyObject_Call(cb->func, cb->args, cb->kwargs); r = PyObject_Call(cb->func, cb->args, cb->kwargs);
Py_XDECREF(r); Py_XDECREF(r);
if (r == NULL) { if (r == NULL) {
/* Maintain the last exception, but don't leak if there are
multiple exceptions. */
if (exc_type) { if (exc_type) {
Py_DECREF(exc_type); Py_DECREF(exc_type);
Py_DECREF(exc_value); Py_XDECREF(exc_value);
Py_DECREF(exc_tb); Py_XDECREF(exc_tb);
} }
PyErr_Fetch(&exc_type, &exc_value, &exc_tb); PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
if (!PyErr_ExceptionMatches(PyExc_SystemExit)) { if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
...@@ -92,6 +94,8 @@ atexit_register(PyObject *self, PyObject *args, PyObject *kwargs) ...@@ -92,6 +94,8 @@ atexit_register(PyObject *self, PyObject *args, PyObject *kwargs)
if (ncallbacks >= callback_len) { if (ncallbacks >= callback_len) {
callback_len += 16; 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, atexit_callbacks = PyMem_Realloc(atexit_callbacks,
sizeof(atexit_callback*) * callback_len); sizeof(atexit_callback*) * callback_len);
...@@ -145,7 +149,7 @@ atexit_clear(PyObject *self) ...@@ -145,7 +149,7 @@ atexit_clear(PyObject *self)
atexit_callback *cb; atexit_callback *cb;
int i; int i;
for(i = 0; i < ncallbacks; i++) for (i = 0; i < ncallbacks; i++)
{ {
cb = atexit_callbacks[i]; cb = atexit_callbacks[i];
if (cb == NULL) if (cb == NULL)
...@@ -163,7 +167,7 @@ atexit_unregister(PyObject *self, PyObject *func) ...@@ -163,7 +167,7 @@ atexit_unregister(PyObject *self, PyObject *func)
atexit_callback *cb; atexit_callback *cb;
int i, eq; int i, eq;
for(i = 0; i < ncallbacks; i++) for (i = 0; i < ncallbacks; i++)
{ {
cb = atexit_callbacks[i]; cb = atexit_callbacks[i];
if (cb == NULL) if (cb == NULL)
...@@ -213,5 +217,8 @@ initatexit(void) ...@@ -213,5 +217,8 @@ initatexit(void)
if (m == NULL) if (m == NULL)
return; 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); _Py_PyAtExit(atexit_callfuncs);
} }
...@@ -68,6 +68,7 @@ extern void init_subprocess(void); ...@@ -68,6 +68,7 @@ extern void init_subprocess(void);
extern void init_lsprof(void); extern void init_lsprof(void);
extern void init_ast(void); extern void init_ast(void);
extern void init_types(void); extern void init_types(void);
extern void initatexit(void);
/* tools/freeze/makeconfig.py marker for additional "extern" */ /* tools/freeze/makeconfig.py marker for additional "extern" */
/* -- ADDMODULE MARKER 1 -- */ /* -- ADDMODULE MARKER 1 -- */
...@@ -79,6 +80,7 @@ struct _inittab _PyImport_Inittab[] = { ...@@ -79,6 +80,7 @@ struct _inittab _PyImport_Inittab[] = {
{"array", initarray}, {"array", initarray},
{"_ast", init_ast}, {"_ast", init_ast},
{"atexit", initatexit},
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
#ifndef MS_WIN64 #ifndef MS_WIN64
{"audioop", initaudioop}, {"audioop", initaudioop},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment