termios disappears during object deletion

Brien Barton brien.barton at boeing.com
Wed Sep 8 20:07:12 EDT 1999


Fredrik's solution (see below) does work, but I find this behavior
extremely unintuitive and error prone.  Why should correct code that
works when I explicitly delete my object, not work when the program
terminates?  Isn't the whole idea of having the __del__ method to make
sure things get cleaned up properly?  Why can't all __del__ methods be
executed BEFORE Python starts deleting globals?

Fredrik Lundh wrote:
> 
> Brien Barton <brien_barton at hotmail.com> wrote:
> > Can anyone explain this behavior??  For some reason, the termios and TERMIOS
> > modules get wiped out when python is running the __del__ method during
> > program termination.  I've tried this with Python 1.5.1 on Linux and with
> > Python 1.5.2 on SGI IRIX.  If you explicitly delete the object, you don't
> > have this problem.
> >
> > ~> cat test.py
> > import math, termios, TERMIOS
> >
> > print 'math=', math
> > print 'termios=', termios
> > print 'TERMIOS=', TERMIOS
> >
> > class Test:
> >     def __del__(self):
> >         print 'deleting Test object ...'
> >         print 'math=', math
> >         print 'termios=', termios
> >         print 'TERMIOS=', TERMIOS
> 
> starting with 1.5.1, Python performs a relatively aggressive
> cleanup when the interpreter is shut down:
> 
>     http://www.python.org/doc/essays/cleanup.html
> 
> among other things, this means that destructors must hang
> on to all global resources they are using, e.g.:
> 
>         def __del__(self, math=math, termios=termios, TERMIOS=TERMIOS):
>             ...
> 
> </F>




More information about the Python-list mailing list