Repository: spyder-ide/spyder

Issue:
Script executes in other console of its dedicated console is dedicated is on debugging
## Problem Description
I'm not sure what all conditions can cause this, but one 100% reliable repro is below. Looks like if a script is queued up to run in its dedicated console, but that console is terminated, it'll jump track onto whatever other console happens to be open (looks like it follows focus order, the one that spyder would show you after closing a console anyway?) This is particularly bad because it nukes whatever was in that other console, as if it was the script's own that it was reinitializing.

### What steps will reproduce the problem?

1. Set a script that raises an error to execute in dedicated consol via ctrl-F6 menu
2. set it to directly enter debugger on error
3. run script, enter debugger
4. run script again, forgetting to exit the debugger
5. (script does not appear to run anywhere)
6. close that script's dedicated console
7. the script commandeers another preexisting console, drops runfile() on it, nuking whatever was there before

### What is the expected output? What do you see instead?
I would expect it to re-open a new dedicated console for itself, rather than pancake somebody else's.


### Paste Traceback/Error Below (if applicable)
<!--- Copy from error dialog or View > Panes > Internal Console --->

## Package Versions

* Spyder: 3.2.8
* Python: 3.6.3
* Qt: 5.6.2
* PyQt: 5.6
* Operating System: windows 10

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
IPython >=4.0    :  6.2.1 (OK)
cython >=0.21    :  0.27.3 (OK)
jedi >=0.9.0     :  0.11.0 (OK)
nbconvert >=4.0  :  5.3.1 (OK)
numpy >=1.7      :  1.14.1 (OK)
pandas >=0.13.1  :  0.22.0 (OK)
pycodestyle >=2.3:  2.3.1 (OK)
pyflakes >=0.6.0 :  1.6.0 (OK)
pygments >=2.0   :  2.2.0 (OK)
pylint >=0.25    :  1.7.4 (OK)
qtconsole >=4.2.0:  4.3.1 (OK)
rope >=0.9.4     :  0.10.5 (OK)
sphinx >=0.6.6   :  1.6.3 (OK)
sympy >=0.7.3    :  None (NOK)

```



Discussion:
Thanks for reporting and for your detailed description. This seems certainly like a bug and we'll try to fix it in our next version.

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole.py b/spyder/plugins/ipythonconsole.py
--- a/spyder/plugins/ipythonconsole.py
+++ b/spyder/plugins/ipythonconsole.py
@@ -956,7 +956,13 @@ def run_script(self, filename, wdir, args, debug, post_mortem,
                     line += " %s" % norm(args)
 
             try:
-                if current_client:
+                if client.shellwidget._reading:
+                    client.shellwidget._append_html(
+                        _("<br><b>Please exit from debugging before trying to "
+                          "run a file in this console.</b>\n<hr><br>"),
+                        before_prompt=True)
+                    return
+                elif current_client:
                     self.execute_code(line, current_client, clear_variables)
                 else:
                     if is_new_client:
@@ -1550,7 +1556,7 @@ def create_kernel_manager_and_kernel_client(self, connection_file,
         try:
             kernel_manager = QtKernelManager(connection_file=connection_file,
                                              config=None, autorestart=True)
-        except Exception as e:
+        except Exception:
             error_msg = _("The error is:<br><br>"
                           "<tt>{}</tt>").format(traceback.format_exc())
             return (error_msg, None)


Test patch: