atexit functionality

viszneki at telerama.com viszneki at telerama.com
Wed Apr 9 15:15:46 EDT 2003


Quoting Duncan Booth <duncan at rcp.co.uk>:

> In what way is asking for atexit.register to throw an IndexError exception 
> every time it is called NOT introducing a negative side effect?

He's right you know, below is quite an index error resulting from deleting 
items from atexit._exithandlers. (sarcasm)

root at crackbox:~# python
Python 2.2.1 (#1, Apr 11 2002, 15:27:15)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import atexit
>>> def test(s):
...     print s
...
>>> for each in ['one','two','three','four','five']:
...     atexit.register(test,each)
...
>>> atexit._exithandlers
[(<function test at 0x8144ecc>, ('one',), {}), (<function test at 0x8144ecc>, 
('two',), {}), (<function test at 0x8144ecc>, ('three',), {}), (<function test 
at 0x8144ecc>, ('four',), {}), (<function test at 0x8144ecc>, ('five',), {})]
>>> del atexit._exithandlers[1:4]
>>> atexit._exithandlers
[(<function test at 0x8144ecc>, ('one',), {}), (<function test at 0x8144ecc>, 
('five',), {})]
>>> # i'm exiting now
...
five
one
root at crackbox:~# WOW! No index errors! What a suprise!
bash: WOW!: command not found

Thanks for playing Duncan. You know I was in good spirits and happy to 
contribute something until you and ol Stevey Holden decided to lecture me on 
Internet Etiquette, and tell me how much of a jerk I was to not have time to 
sign up for every mailing list I'd like to contribute something to. Gee I'm 
such a jerk.

If anyone has anything positive to say about my idea, please say so. In my last 
email to this mailing list, I posted some hypothetical code to do the job:

return _exithandlers[len(_exithandlers)]

Shortly after having sent it, I realized it wouldn't work because of the way 
Python does things (note: that does not mean index error, or ANY OTHER KIND OF 
ERROR for that matter.) Assigning the returned value, storing it, and then 
deleting it later, does not result in deletion of an item from _exithandlers, 
because it is only deleting the symbolic reference. The item in _exithandlers 
remains. I tried this myself, by adding that line of code to the end of 
atexit.register, just to make sure, because I am admittedly no master at 
pointer dynamics in Python (something I wish I knew more about.)

Thank you everybody for your time,
Donny Viszneki.






More information about the Python-list mailing list