[Python-Dev] Proposal: standard way of defining and executing "atexit" functions...
Skip Montanaro
skip@mojam.com (Skip Montanaro)
Mon, 19 Jun 2000 15:55:09 -0500 (CDT)
David> +1 on the idea. I wonder if it makes sense to publish a
David> mechanism to allow de-registering of callbacks, or if that's
David> featuritis.
While writing my message I thought, "I'll bet the first question asked will
be about unregistering exit functions." I've been using the module I
appended to my message unchanged for a couple years and never needed such
functionality. I'm sure there are applications that could use it, but I
think they'd be a small minority. Since the only defined interface is a
single registration function (_run_exitfuncs is only used internally), I
think you'd be free to add some unregister function protocol at a later
time if it was deemed necessary.
David> Also, I'd change:
>> try:
>> x = sys.exitfunc
>> except AttributeError:
>> sys.exitfunc = _run_exitfuncs
>> del sys
David> to:
David> try:
David> register_exitfuncs(sys.exitfunc)
David> finally:
David> sys.exitfunc = _run_exitfuncs
David> Or some such.
register_exitfunc is only meant to be called by clients of the module, not
used internally. You're assuming that if something else was already bound
to sys.exitfunc that it's not _run_exitfuncs. With your try/finally code
try the following (mentally is fine):
import exit
def foo():
print 1
exit.register_exitfunc(foo)
reload(exit)
I think you'll see that exit._run_exitfuncs is on its own list of exit
functions. That could be bad.
The setup code:
try:
x = sys.exitfunc
except AttributeError:
sys.exitfunc = _run_exitfuncs
is meant to avoid doing anything if any other module beat us to
sys.exitfunc. Perhaps it should just be
sys.exitfunc = _run_exitfuncs
If someone fails to use the defined protocol, screw 'em... ;-)
Skip