views:

248

answers:

2

Trying to run this file in eclipse

class Try:
   def __init__(self):
       pass
   def __del__(self):
       print 1
a=Try()
raw_input('waiting to finish')

and pressing the stop button without letting the program finish doesn't print "1", i.e the del method is never called. If i try to run the script from the shell and do ctrl-c\sys.exit "1" does get printed i.e del is called. Same thing if I try to use wait():

class A:

    def __enter__(self):
        return None
    def __exit__(self, type, value, traceback):
        print 3


with A():
    print 1
    raw_input('Waiting')
    print 2

If i press "stop" when prompted, "3" isn't printed

Why is that? Is there a way around it?

Thanks, Noam

+4  A: 

Python docs:

__del__(self)

Called when the instance is about to be destroyed. This is also called a destructor. If a base class has a __del__() method, the derived class's __del__() method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance. Note that it is possible (though not recommended!) for the __del__() method to postpone destruction of the instance by creating a new reference to it. It may then be called at a later time when this new reference is deleted. It is not guaranteed that __del__() methods are called for objects that still exist when the interpreter exits.

If you want to guarantee that a method is called use the with-statement

THC4k
Maybe I'm missing something, But I am not sure this is the right thing for my case:I have class A, that holds an instance of class B, and B holds some external resource it must explicitly release on exit.How can i solve this issue using with? make explicit methods called close() both for A and B ?
noam
Moreover, wait() doesn't work any better than delete in eclipse - try to press stop and you'll see that __exit__ isn't called...
noam
+1  A: 

Pressing stop in Eclipse outright kills the interpreter (though it actually fails fairly often). Like using kill/taskkill, the process is unaware of it's demise.

Ctrl+C snippet from Wikipedia...

Control-C as an abort command was popularized by UNIX and adopted in other systems. In POSIX systems, the sequence causes the active program to receive a SIGINT signal. If the program does not specify how to handle this condition, it is terminated. Typically a program which does handle a SIGINT will still terminate itself, or at least terminate the task running inside it.

Ctrl+C is a control signal to interrupt the program, but as you may have noticed in the middle of that paragraph, programs can specify how to handle the signal. In Python, Ctrl+C throws a KeyboardInterrupt exception which is normally caught and then Python exits cleanly. Even if you're killing the interpreter with Ctrl+C it may handle it so that it cleans the environment before exiting.

I included the following because you asked "Is there a way around it?"

If you are wanting to stop on raw_input(...) calls, you could use Ctrl+Z to send EOF. I looked around, and there seems to be no way to send Ctrl+C/0x03 in Eclipse, unfortunately.

Zwergner
Thanks, but I think you didn't answer my question - when running from the shell, stopping the *interpreter* with ctrl-c still calls __del__, but when stopping it in eclipse that doesn't happen. I am not looking for a solution, just wondering what's causing the difference
noam
I was a bit off, or at least explained poorly, so I cleaned it up a bit and added a more info.
Zwergner
OK, thanks - Your answer is spot on. I didn't understand it at first. Sorry
noam