Back to index

python3.2  3.2.2
Classes | Defines | Functions | Variables
atexitmodule.c File Reference
#include "Python.h"

Go to the source code of this file.

Classes

struct  atexit_callback
struct  atexitmodule_state

Defines

#define GET_ATEXIT_STATE(mod)   ((atexitmodule_state*)PyModule_GetState(mod))

Functions

static PyObjectatexit_clear (PyObject *, PyObject *)
static void atexit_cleanup (PyObject *)
static void atexit_callfuncs (void)
static void atexit_delete_cb (PyObject *self, int i)
 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 PyObjectatexit_register (PyObject *self, PyObject *args, PyObject *kwargs)
 PyDoc_STRVAR (atexit_run_exitfuncs__doc__,"_run_exitfuncs() -> None\n\ \n\ Run all registered exit functions.")
static PyObjectatexit_run_exitfuncs (PyObject *self, PyObject *unused)
 PyDoc_STRVAR (atexit_clear__doc__,"_clear() -> None\n\ \n\ Clear the list of previously registered exit functions.")
 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 PyObjectatexit_unregister (PyObject *self, PyObject *func)
 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\ ")
PyMODINIT_FUNC PyInit_atexit (void)

Variables

static struct PyModuleDef
static PyMethodDef atexit_methods []

Class Documentation

struct atexit_callback

Definition at line 21 of file atexitmodule.c.

Collaboration diagram for atexit_callback:
Class Members
PyObject * args
PyObject * func
PyObject * kwargs
struct atexitmodule_state

Definition at line 27 of file atexitmodule.c.

Collaboration diagram for atexitmodule_state:
Class Members
atexit_callback ** atexit_callbacks
int callback_len
int ncallbacks

Define Documentation

#define GET_ATEXIT_STATE (   mod)    ((atexitmodule_state*)PyModule_GetState(mod))

Definition at line 33 of file atexitmodule.c.


Function Documentation

static void atexit_callfuncs ( void  ) [static]

Definition at line 39 of file atexitmodule.c.

{
    PyObject *exc_type = NULL, *exc_value, *exc_tb, *r;
    atexit_callback *cb;
    PyObject *module;
    atexitmodule_state *modstate;
    int i;

    module = PyState_FindModule(&atexitmodule);
    if (module == NULL)
        return;
    modstate = GET_ATEXIT_STATE(module);

    if (modstate->ncallbacks == 0)
        return;


    for (i = modstate->ncallbacks - 1; i >= 0; i--)
    {
        cb = modstate->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_NormalizeException(&exc_type, &exc_value, &exc_tb);
                PyErr_Display(exc_type, exc_value, exc_tb);
            }
        }
    }

    atexit_cleanup(module);

    if (exc_type)
        PyErr_Restore(exc_type, exc_value, exc_tb);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void atexit_cleanup ( PyObject self) [static]

Definition at line 103 of file atexitmodule.c.

{
    PyObject *r = atexit_clear(self, NULL);
    Py_DECREF(r);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject * atexit_clear ( PyObject self,
PyObject unused 
) [static]

Definition at line 195 of file atexitmodule.c.

{
    atexitmodule_state *modstate;
    atexit_callback *cb;
    int i;

    modstate = GET_ATEXIT_STATE(self);

    for (i = 0; i < modstate->ncallbacks; i++) {
        cb = modstate->atexit_callbacks[i];
        if (cb == NULL)
            continue;

        atexit_delete_cb(self, i);
    }
    modstate->ncallbacks = 0;
    Py_RETURN_NONE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void atexit_delete_cb ( PyObject self,
int  i 
) [static]

Definition at line 88 of file atexitmodule.c.

{
    atexitmodule_state *modstate;
    atexit_callback *cb;

    modstate = GET_ATEXIT_STATE(self);
    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);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* atexit_register ( PyObject self,
PyObject args,
PyObject kwargs 
) [static]

Definition at line 124 of file atexitmodule.c.

{
    atexitmodule_state *modstate;
    atexit_callback *new_callback;
    PyObject *func = NULL;

    modstate = GET_ATEXIT_STATE(self);

    if (modstate->ncallbacks >= modstate->callback_len) {
        atexit_callback **r;
        modstate->callback_len += 16;
        r = (atexit_callback**)PyMem_Realloc(modstate->atexit_callbacks,
                                      sizeof(atexit_callback*) * modstate->callback_len);
        if (r == NULL)
            return PyErr_NoMemory();
        modstate->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);

    modstate->atexit_callbacks[modstate->ncallbacks++] = new_callback;

    Py_INCREF(func);
    return func;
}

Here is the call graph for this function:

static PyObject* atexit_run_exitfuncs ( PyObject self,
PyObject unused 
) [static]

Definition at line 181 of file atexitmodule.c.

Here is the call graph for this function:

static PyObject* atexit_unregister ( PyObject self,
PyObject func 
) [static]

Definition at line 223 of file atexitmodule.c.

{
    atexitmodule_state *modstate;
    atexit_callback *cb;
    int i, eq;

    modstate = GET_ATEXIT_STATE(self);

    for (i = 0; i < modstate->ncallbacks; i++)
    {
        cb = modstate->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(self, i);
    }
    Py_RETURN_NONE;
}

Here is the call graph for this function:

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."   
)
PyDoc_STRVAR ( atexit_run_exitfuncs__doc__  ,
"_run_exitfuncs() -> None\n\\n\Run all registered exit functions."   
)
PyDoc_STRVAR ( atexit_clear__doc__  ,
"_clear() -> None\n\\n\Clear the list of previously registered exit functions."   
)
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"   
)
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\"   
)

Definition at line 282 of file atexitmodule.c.

{
    PyObject *m;
    atexitmodule_state *modstate;

    m = PyModule_Create(&atexitmodule);
    if (m == NULL)
        return NULL;

    modstate = GET_ATEXIT_STATE(m);
    modstate->callback_len = 32;
    modstate->ncallbacks = 0;
    modstate->atexit_callbacks = PyMem_New(atexit_callback*, 
                                           modstate->callback_len);
    if (modstate->atexit_callbacks == NULL)
        return NULL;

    _Py_PyAtExit(atexit_callfuncs);
    return m;
}

Here is the call graph for this function:


Variable Documentation

Initial value:
 {
    {"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}        
}

Definition at line 246 of file atexitmodule.c.

struct PyModuleDef [static]
Initial value:
 {
       PyModuleDef_HEAD_INIT,
       "atexit",
       atexit__doc__,
       sizeof(atexitmodule_state),
       atexit_methods,
       NULL,
       NULL,
       NULL,
       NULL
}

Definition at line 16 of file atexitmodule.c.