[IPython-dev] Bug with __del__methods on exit

Fernando Perez fperez.net@gmail....
Sat Oct 9 14:31:19 CDT 2010


Hi Thomas,

On Sat, Oct 9, 2010 at 9:45 AM, Thomas Kluyver <takowl@gmail.com> wrote:
> I recently found a problem in my python 3 port of ipython, where the __del__
> method of objects, called as the program was exiting, could not find global
> functions.
>
> On a hunch, I've just tested this in standard ipython, both 0.10 (in Ubuntu)
> and trunk. The problem exists in both cases (it only came to light in Python
> 3 because print is a function). The minimal code to reproduce it is:
>
> class A(object):
>     def __del__(self):
>         input("ABC")
>
> a = A()
> exit()
>
> Which gives: Exception NameError: "global name 'input' is not defined" in
> <bound method A.__del__ of <__main__.A object at 0x98634cc>> ignored

This isn't an ipython bug, but a reality of python itself:

dreamweaver[test]> cat objdel.py
class A(object):
    def __del__(self):
        input("ABC")

a = A()
exit()
dreamweaver[test]> python objdel.py
Exception ValueError: 'I/O operation on closed file' in <bound method
A.__del__ of <__main__.A object at 0x7f47551bcf50>> ignored
ABCdreamweaver[test]>

Basically, on exit the sate of the interpreter is mostly undefined.
Del methods should limit themselves to closing resources they had
acquired:

self.whatever.close()

But they can't expect to access any globals, or even objects in other modules.

If you need to perform actions on exit but that require the
interpreter to be fully functional, use the atexit module and register
your callbacks (ipython uses that).

Cheers,

f


More information about the IPython-dev mailing list