An "exit" function for C modules?

Lawrence Kesteloot lk at
Tue Dec 19 02:51:28 CET 2000

I'm writing a C module where I need to keep a global
pointer to a Python object (for API convenience).

When the script finishes, I never get the chance to
decref the pointer, so it never gets destroyed.  This
is a problem with streaming output files that won't
get closed properly.  (Not text files -- this is a special
file format that requires official closing in order to be
updated properly.)

What's the best way to handle this?  I can't depend
on the user always calling a "close" function on the
stream, and the API would get much more complicated
if I didn't have these globals.  (The module is for
novice programmers.)

Should I use "atexit()"?  I'm concerned that I don't know
when that's called -- it could be after some cleanup has
been done by Python that makes decref dangerous.

It seems as though there should be an "exit<modulename>"
function like the "init<modulename>" call at an appropriate
time that allows cleanup of globals.


More information about the Python-list mailing list