_run_exitfuncs in atexit module

This is a snippet from Modules/atexitmodule.c

static PyMethodDef atexit_methods[] = {
{"run_exitfuncs", atexit_run_exitfuncs, METH_NOARGS,
atexit_run_exitfuncs__doc_},
};

Looks like even if we comment run_exitfuncs, recompile python and execute this example from python documentation


def goodbye(name, adjective):
print(‘Goodbye %s, it was %s to meet you.’ % (name, adjective))

import atexit

atexit.register(goodbye, ‘Donny’, ‘nice’)

The goodbye function is still executed. Running this program under GDB shows the pathway


#0  *atexit_callfuncs (state=0x555555baeb00 <\_PyRuntime+116960>) at ./Modules/atexitmodule.c:104*
#1  *0x000055555588383d in \_PyAtExit_Call (interp=) at ./Modules/atexitmodule.c:151*
#2  *0x00005555558483aa in \_Py_Finalize (runtime=runtime@entry=0x555555b92220 <\_PyRuntime>) at Python/pylifecycle.c:2045*
#3  *0x0000555555848529 in Py_FinalizeEx () at Python/pylifecycle.c:2258*
#4  *0x000055555587a127 in Py_RunMain () at Modules/main.c:774*
#5  *0x000055555587a199 in pymain_main (args=args@entry=0x7fffffffd550) at Modules/main.c:802*
#6  *0x000055555587a267 in Py_BytesMain (argc=, argv=) at Modules/main.c:826*
#7  *0x00005555555e0146 in main (argc=, argv=) at ./Programs/python.c:15*

Whereas my understanding was that run_exitfuncs mapping to atexit_run_exitfuncs does this at runtime.

So what am I missing. I mean run_exitfuncs is not needed or called under special conditions I am not aware of?

Thanks for your help.

Siddharth

That definition of PyMethodDef is the thing that exposes C functions as functions in the Python module, accessible from other Python modules.

It is not called by the CPython runtime, which calls the underlying C function directly: see _PyAtExit_Call in Modules/atexitmodule.c and Python/pylifecycle.c

1 Like

Understood, thank you.