Export to GitHub

spyderlib - issue #1974

exit() lets the kernel die


Posted on Sep 19, 2014 by Happy Bear

I hope this is the right issue tracker for my problem:

I am using WinPython. When I am creating a new script with the command exit(), and run it within Spyder, this command kills the kernel:

""" It seems the kernel died unexpectedly. Use 'Restart kernel' to continue using this console. """

What's the right way to stop a script in this runtime environment?

My workaround so far is to encapsulate the code into a 'main' function and use 'return' inside.

Comment #1

Posted on Sep 19, 2014 by Happy Dog

exit() is used to terminate python, so this is the intended behavior: https://docs.python.org/2/library/sys.html#sys.exit

You can also put your code inside a "if" block, but using functions is generally the right thing to do.

Comment #2

Posted on Sep 19, 2014 by Happy Bear

So do I have to rewrite every (3rd party) script containing exit() that I want to run within Spyder? Doesn't exit() just throw an exception that should be cought be the runtime environment?

Comment #3

Posted on Sep 19, 2014 by Happy Dog

You can catch it yourself in your code if you need. It is bad practice to use exit() in libraries, raising an useful exception is way better. I'm against including this in spyder because right now it does the documented thing. I'm not even sure it's possible to do since the exception should be caught inside IPython itself.

Comment #4

Posted on Sep 22, 2014 by Happy Bear

In http://mail.scipy.org/pipermail/ipython-dev/2014-September/015019.html, Carlos Córdoba suggested to catch the expression via def f(): pass, get_ipython().ask_exit = f

.However, this causes the script not to stop at exit. I am now using def f(): raise Exception("Found exit()") instead.

This closes this issue for me.

(Could Spyder catch this exception, s.t. the script is stopped in a more quiet way?)

Comment #5

Posted on Nov 18, 2014 by Quick Horse

(No comment was entered for this change.)

Comment #6

Posted on Feb 17, 2015 by Quick Horse

This issue was migrated to https://github.com/spyder-ide/spyder/issues/

The issue number is exactly the same

Status: Done

Labels:
Type-Defect Priority-Medium Restrict-AddIssueComment-Commit