[IPython-dev] Bug with __del__methods on exit

Thomas Kluyver takowl@gmail....
Sat Oct 9 18:41:33 CDT 2010


Hi Fernando,

The exception you're seeing is a different one, however. In fact, input was
a bad example: the difference can be better seen with str("something"). The
attached test file runs under python 2.6, but shows a NameError in ipython.

The relevant code is already being called from an atexit callback.
Specifically, it triggers the .reset() method of the
TerminalInteractiveShell object. You can verify this with the following code
in ipython trunk:

class A(object):
    def __del__(self):
        str("Hi")
a = A()
get_ipython().reset()
# Gives: Exception NameError: "global name 'str' is not defined" in <bound
method A.__del__ of <__main__.A object at 0x985004c>> ignored

Thanks,
Thomas

On 9 October 2010 20:31, Fernando Perez <fperez.net@gmail.com> wrote:

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-dev/attachments/20101010/ecf1b355/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.py
Type: text/x-python
Size: 67 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/ipython-dev/attachments/20101010/ecf1b355/attachment.py 


More information about the IPython-dev mailing list