| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *  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"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-23 22:46:49 +00:00
										 |  |  | /* Forward declaration (for atexit_cleanup) */ | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | static PyObject *atexit_clear(PyObject*, PyObject*); | 
					
						
							|  |  |  | /* Forward declaration of module object */ | 
					
						
							|  |  |  | static struct PyModuleDef atexitmodule; | 
					
						
							| 
									
										
										
										
											2007-03-23 22:46:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | /* ===================================================================== */ | 
					
						
							|  |  |  | /* Callback machinery. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |     PyObject *func; | 
					
						
							|  |  |  |     PyObject *args; | 
					
						
							|  |  |  |     PyObject *kwargs; | 
					
						
							|  |  |  | } atexit_callback; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     atexit_callback **atexit_callbacks; | 
					
						
							|  |  |  |     int ncallbacks; | 
					
						
							|  |  |  |     int callback_len; | 
					
						
							|  |  |  | } atexitmodule_state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define GET_ATEXIT_STATE(mod) ((atexitmodule_state*)PyModule_GetState(mod))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-01 20:56:12 +02:00
										 |  |  | static void | 
					
						
							|  |  |  | atexit_delete_cb(atexitmodule_state *modstate, int i) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     atexit_callback *cb; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cb = modstate->atexit_callbacks[i]; | 
					
						
							|  |  |  |     modstate->atexit_callbacks[i] = NULL; | 
					
						
							|  |  |  |     Py_DECREF(cb->func); | 
					
						
							|  |  |  |     Py_DECREF(cb->args); | 
					
						
							|  |  |  |     Py_XDECREF(cb->kwargs); | 
					
						
							|  |  |  |     PyMem_Free(cb); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Clear all callbacks without calling them */ | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | atexit_cleanup(atexitmodule_state *modstate) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     atexit_callback *cb; | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     for (i = 0; i < modstate->ncallbacks; i++) { | 
					
						
							|  |  |  |         cb = modstate->atexit_callbacks[i]; | 
					
						
							|  |  |  |         if (cb == NULL) | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         atexit_delete_cb(modstate, i); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     modstate->ncallbacks = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-20 21:39:37 +10:00
										 |  |  | /* Installed into pylifecycle.c's atexit mechanism */ | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-23 00:52:29 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | atexit_callfuncs(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *exc_type = NULL, *exc_value, *exc_tb, *r; | 
					
						
							|  |  |  |     atexit_callback *cb; | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  |     PyObject *module; | 
					
						
							|  |  |  |     atexitmodule_state *modstate; | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     int i; | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     module = PyState_FindModule(&atexitmodule); | 
					
						
							|  |  |  |     if (module == NULL) | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  |     modstate = GET_ATEXIT_STATE(module); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (modstate->ncallbacks == 0) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = modstate->ncallbacks - 1; i >= 0; i--) | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  |         cb = modstate->atexit_callbacks[i]; | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |         if (cb == NULL) | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         r = PyObject_Call(cb->func, cb->args, cb->kwargs); | 
					
						
							|  |  |  |         Py_XDECREF(r); | 
					
						
							|  |  |  |         if (r == NULL) { | 
					
						
							| 
									
										
										
										
											2007-03-21 04:45:04 +00:00
										 |  |  |             /* Maintain the last exception, but don't leak if there are
 | 
					
						
							|  |  |  |                multiple exceptions. */ | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |             if (exc_type) { | 
					
						
							|  |  |  |                 Py_DECREF(exc_type); | 
					
						
							| 
									
										
										
										
											2007-03-21 04:45:04 +00:00
										 |  |  |                 Py_XDECREF(exc_value); | 
					
						
							| 
									
										
										
										
											2015-03-18 21:53:15 +02:00
										 |  |  |                 Py_XDECREF(exc_tb); | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |             PyErr_Fetch(&exc_type, &exc_value, &exc_tb); | 
					
						
							|  |  |  |             if (!PyErr_ExceptionMatches(PyExc_SystemExit)) { | 
					
						
							|  |  |  |                 PySys_WriteStderr("Error in atexit._run_exitfuncs:\n"); | 
					
						
							| 
									
										
										
										
											2011-01-05 03:54:25 +00:00
										 |  |  |                 PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb); | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |                 PyErr_Display(exc_type, exc_value, exc_tb); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-01 20:56:12 +02:00
										 |  |  |     atexit_cleanup(modstate); | 
					
						
							| 
									
										
										
										
											2008-09-23 00:52:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     if (exc_type) | 
					
						
							|  |  |  |         PyErr_Restore(exc_type, exc_value, exc_tb); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ===================================================================== */ | 
					
						
							|  |  |  | /* 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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  |     atexitmodule_state *modstate; | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     atexit_callback *new_callback; | 
					
						
							|  |  |  |     PyObject *func = NULL; | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     modstate = GET_ATEXIT_STATE(self); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (modstate->ncallbacks >= modstate->callback_len) { | 
					
						
							| 
									
										
										
										
											2007-03-23 22:46:49 +00:00
										 |  |  |         atexit_callback **r; | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  |         modstate->callback_len += 16; | 
					
						
							|  |  |  |         r = (atexit_callback**)PyMem_Realloc(modstate->atexit_callbacks, | 
					
						
							|  |  |  |                                       sizeof(atexit_callback*) * modstate->callback_len); | 
					
						
							| 
									
										
										
										
											2007-03-23 22:46:49 +00:00
										 |  |  |         if (r == NULL) | 
					
						
							|  |  |  |             return PyErr_NoMemory(); | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  |         modstate->atexit_callbacks = r; | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     if (PyTuple_GET_SIZE(args) == 0) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  |                 "register() takes at least 1 argument (0 given)"); | 
					
						
							| 
									
										
										
										
											2015-03-18 21:53:15 +02:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     func = PyTuple_GET_ITEM(args, 0); | 
					
						
							|  |  |  |     if (!PyCallable_Check(func)) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  |                 "the first argument must be callable"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     new_callback = PyMem_Malloc(sizeof(atexit_callback)); | 
					
						
							|  |  |  |     if (new_callback == NULL) | 
					
						
							| 
									
										
										
										
											2015-03-18 21:53:15 +02:00
										 |  |  |         return PyErr_NoMemory(); | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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); | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     modstate->atexit_callbacks[modstate->ncallbacks++] = new_callback; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     Py_INCREF(func); | 
					
						
							|  |  |  |     return func; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 20:59:28 +00:00
										 |  |  | PyDoc_STRVAR(atexit_run_exitfuncs__doc__, | 
					
						
							|  |  |  | "_run_exitfuncs() -> None\n\
 | 
					
						
							|  |  |  | \n\ | 
					
						
							|  |  |  | Run all registered exit functions."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | atexit_run_exitfuncs(PyObject *self, PyObject *unused) | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     atexit_callfuncs(); | 
					
						
							|  |  |  |     if (PyErr_Occurred()) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 20:59:28 +00:00
										 |  |  | PyDoc_STRVAR(atexit_clear__doc__, | 
					
						
							|  |  |  | "_clear() -> None\n\
 | 
					
						
							|  |  |  | \n\ | 
					
						
							|  |  |  | Clear the list of previously registered exit functions."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | atexit_clear(PyObject *self, PyObject *unused) | 
					
						
							| 
									
										
										
										
											2013-08-01 20:56:12 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     atexit_cleanup(GET_ATEXIT_STATE(self)); | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(atexit_ncallbacks__doc__, | 
					
						
							|  |  |  | "_ncallbacks() -> int\n\
 | 
					
						
							|  |  |  | \n\ | 
					
						
							|  |  |  | Return the number of registered exit functions."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | atexit_ncallbacks(PyObject *self, PyObject *unused) | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  |     atexitmodule_state *modstate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     modstate = GET_ATEXIT_STATE(self); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-01 20:56:12 +02:00
										 |  |  |     return PyLong_FromSsize_t(modstate->ncallbacks); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | atexit_m_traverse(PyObject *self, visitproc visit, void *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     atexitmodule_state *modstate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     modstate = GET_ATEXIT_STATE(self); | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  |     for (i = 0; i < modstate->ncallbacks; i++) { | 
					
						
							| 
									
										
										
										
											2013-08-01 20:56:12 +02:00
										 |  |  |         atexit_callback *cb = modstate->atexit_callbacks[i]; | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |         if (cb == NULL) | 
					
						
							|  |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2013-08-01 20:56:12 +02:00
										 |  |  |         Py_VISIT(cb->func); | 
					
						
							|  |  |  |         Py_VISIT(cb->args); | 
					
						
							|  |  |  |         Py_VISIT(cb->kwargs); | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-08-01 20:56:12 +02:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | atexit_m_clear(PyObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     atexitmodule_state *modstate; | 
					
						
							|  |  |  |     modstate = GET_ATEXIT_STATE(self); | 
					
						
							|  |  |  |     atexit_cleanup(modstate); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-27 11:49:21 +02:00
										 |  |  | static void | 
					
						
							|  |  |  | atexit_free(PyObject *m) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     atexitmodule_state *modstate; | 
					
						
							|  |  |  |     modstate = GET_ATEXIT_STATE(m); | 
					
						
							| 
									
										
										
										
											2013-08-01 20:56:12 +02:00
										 |  |  |     atexit_cleanup(modstate); | 
					
						
							| 
									
										
										
										
											2012-03-27 11:49:21 +02:00
										 |  |  |     PyMem_Free(modstate->atexit_callbacks); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 20:59:28 +00:00
										 |  |  | 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"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | atexit_unregister(PyObject *self, PyObject *func) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  |     atexitmodule_state *modstate; | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     atexit_callback *cb; | 
					
						
							|  |  |  |     int i, eq; | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     modstate = GET_ATEXIT_STATE(self); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0; i < modstate->ncallbacks; i++) | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  |         cb = modstate->atexit_callbacks[i]; | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |         if (cb == NULL) | 
					
						
							|  |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |         eq = PyObject_RichCompareBool(cb->func, func, Py_EQ); | 
					
						
							|  |  |  |         if (eq < 0) | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         if (eq) | 
					
						
							| 
									
										
										
										
											2013-08-01 20:56:12 +02:00
										 |  |  |             atexit_delete_cb(modstate, i); | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef atexit_methods[] = { | 
					
						
							|  |  |  |     {"register", (PyCFunction) atexit_register, METH_VARARGS|METH_KEYWORDS, | 
					
						
							|  |  |  |         atexit_register__doc__}, | 
					
						
							|  |  |  |     {"_clear", (PyCFunction) atexit_clear, METH_NOARGS, | 
					
						
							| 
									
										
										
										
											2007-08-06 20:59:28 +00:00
										 |  |  |         atexit_clear__doc__}, | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     {"unregister", (PyCFunction) atexit_unregister, METH_O, | 
					
						
							| 
									
										
										
										
											2007-08-06 20:59:28 +00:00
										 |  |  |         atexit_unregister__doc__}, | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     {"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS, | 
					
						
							| 
									
										
										
										
											2007-08-06 20:59:28 +00:00
										 |  |  |         atexit_run_exitfuncs__doc__}, | 
					
						
							| 
									
										
										
										
											2013-08-01 20:56:12 +02:00
										 |  |  |     {"_ncallbacks", (PyCFunction) atexit_ncallbacks, METH_NOARGS, | 
					
						
							|  |  |  |         atexit_ncallbacks__doc__}, | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     {NULL, NULL}        /* sentinel */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ===================================================================== */ | 
					
						
							|  |  |  | /* Initialization function. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(atexit__doc__, | 
					
						
							| 
									
										
										
										
											2007-08-06 20:59:28 +00:00
										 |  |  | "allow programmer to define multiple exit functions to be executed\
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | upon normal program termination.\n\ | 
					
						
							|  |  |  | \n\ | 
					
						
							| 
									
										
										
										
											2007-08-06 20:59:28 +00:00
										 |  |  | Two public functions, register and unregister, are defined.\n\ | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | "); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static struct PyModuleDef atexitmodule = { | 
					
						
							| 
									
										
										
										
											2013-08-01 20:56:12 +02:00
										 |  |  |     PyModuleDef_HEAD_INIT, | 
					
						
							|  |  |  |     "atexit", | 
					
						
							|  |  |  |     atexit__doc__, | 
					
						
							|  |  |  |     sizeof(atexitmodule_state), | 
					
						
							|  |  |  |     atexit_methods, | 
					
						
							|  |  |  |     NULL, | 
					
						
							|  |  |  |     atexit_m_traverse, | 
					
						
							|  |  |  |     atexit_m_clear, | 
					
						
							|  |  |  |     (freefunc)atexit_free | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | PyMODINIT_FUNC | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | PyInit_atexit(void) | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *m; | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  |     atexitmodule_state *modstate; | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  |     m = PyModule_Create(&atexitmodule); | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     if (m == NULL) | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     modstate = GET_ATEXIT_STATE(m); | 
					
						
							|  |  |  |     modstate->callback_len = 32; | 
					
						
							|  |  |  |     modstate->ncallbacks = 0; | 
					
						
							| 
									
										
										
										
											2015-03-18 21:53:15 +02:00
										 |  |  |     modstate->atexit_callbacks = PyMem_New(atexit_callback*, | 
					
						
							| 
									
										
										
										
											2008-10-30 21:34:02 +00:00
										 |  |  |                                            modstate->callback_len); | 
					
						
							|  |  |  |     if (modstate->atexit_callbacks == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  |     _Py_PyAtExit(atexit_callfuncs); | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  |     return m; | 
					
						
							| 
									
										
										
										
											2007-03-21 02:57:17 +00:00
										 |  |  | } |