Repository: spyder-ide/spyder

Issue:
Ctrl+C not working when using input on Windows
## Description
Keyboard interrupt freezes but doesn't exit script. 

**What steps will reproduce the problem?**

1. Use python code: 
```
while True:
    input()
```
2. Type, and press return once or a few times.
3. Press Ctrl-C or Crtl-Shift-C
4. Console lets you continue typing, but doesn't respond to an enter/return. Pressing stop doesn't work as expected, only escape seems to be restarting kernel.
5. Also try:
```
try:
    while True:
        print(input())
except KeyboardInterrupt:
    print('Done.')
```
With similar results.
**What is the expected output? What do you see instead?**
Expect to see some sort of error message, or 'Done.' printed to the console if the error is handled with `try`/`except`. 
Instead I see no visible change. Can keep inputting text, on last line, but enter/return fails to work after attempting keyboard interrupt. 

**Please provide any additional information below**



## Version and main components

* Spyder Version: 3.2.1 
* Python Version: 3.6.1
* Qt Versions: 5.6.2, PyQt5 5.6 on Windows

## Dependencies
```
pyflakes >=0.6.0 :  1.5.0 (OK)
pycodestyle >=2.3:  2.3.1 (OK)
pygments >=2.0   :  2.2.0 (OK)
pandas >=0.13.1  :  0.20.1 (OK)
numpy >=1.7      :  1.12.1 (OK)
sphinx >=0.6.6   :  1.5.6 (OK)
rope >=0.9.4     :  0.9.4-1 (OK)
jedi >=0.9.0     :  0.10.2 (OK)
nbconvert >=4.0  :  5.1.1 (OK)
sympy >=0.7.3    :  1.0 (OK)
cython >=0.21    :  0.25.2 (OK)
qtconsole >=4.2.0:  4.3.0 (OK)
IPython >=4.0    :  5.3.0 (OK)
pylint >=0.25    :  1.6.4 (OK)

```



Discussion:
This is a very special case because you put an `input` inside an infinite loop. To exit the loop, you can press the square red button present in all consoles.
"Pressing stop doesn't work as expected, only escape seems to be restarting kernel." - That button didn't work in this case. 
I have the same problem...

Sorry but we don't have plans to solve this problem because it's a very minor issue.

You're welcome to send us a pull request if you find the solution for it. Thanks!
same issue. Red button doesn't work. Restarting kernel is the only option left to stop the execution.
It would be so nice if ctrl-c acted like it does in every OS terminal. E.g., when I have a long command half-written, I want to be able to hit ctrl-c to just cancel it and return a fresh new line for me to work on. Instead I end up fumbling around trying to figure out what is going on, or just delete each character manually, or just press enter and take the exception.

Would be a huge bonus to make ctrl-c act like the naive user expects it to act in Spyder. This has been bugging me for years.

Just make it act like it does in Matlab, that would be so beautiful.
You can press Ctrl+Esc, I think, to clear the current line.
Actually, this issue is worse than reported previously because the same problems exist with just a regular input (no while loop reqquired). Just try this:

``print('You said: ', input('Say something: '))``

When you click on the "Stop the current command" button, this is what you get as output:

```
You said:  exit

Say something:
```

So apart from the printed message ending up *above* the prompt from ``input()``, it seems that clicking the button never stopped the command execution, but instead only passed "exit" to ``input()``.


In addition, and in line with the original report, pressing ``Ctrl+c`` instead of clicking the button, kills
all further responses from the console and you can only restart the kernel.

I really don't think this is a minor issue, but one that clearly affects usability.
> it seems that clicking the button never stopped the command execution, but instead only passed "exit" to input()

This is correct. The thing is right now it's not possible to distinguish between an `input()` call and an `ipdb` prompt. We assume that the console is in `ipdb` when it's waiting for `stdin` because `input()` is rarely used. That's why the stop button passes `exit` to the console.
@ccordoba12, I see so this is tricky. Could you point me to the relevant code?

Meanwhile, I found that the ``Ctrl+c`` issue is Windows-specific and might be identical to what's reported for the ipython Qtconsole here: https://github.com/ipython/ipython/issues/2472
I just tested this with spyder 3.2.7 on Fedora and it interrupts ``input()`` just fine, although with a suboptimal exception traceback
```
a=input()

Traceback (most recent call last):

  File "<ipython-input-6-c9ea818302bb>", line 1, in <module>
    a=input()

  File "/home/maier/test_spider/lib64/python3.6/site-packages/ipykernel/kernelbase.py", line 704, in raw_input
    password=False,

  File "/home/maier/test_spider/lib64/python3.6/site-packages/ipykernel/kernelbase.py", line 734, in _input_request
    raise KeyboardInterrupt

KeyboardInterrupt
```
I have the same problem - also in finite loops - I can't find any way to stop execution in Spyder - Control+C, red button, blue stop button - nothing works...
> I have the same problem - also in finite loops - I can't find any way to stop execution in Spyder - Control+C, red button, blue stop button - nothing works...

For now, the only solution is to restart the kernel
> Sorry but we don't have plans to solve this problem because it's a very minor issue.
> 
> You're welcome to send us a pull request if you find the solution for it. Thanks!

Seems developer attitude is bad. Bye bye spyder. hello pycharm.....
I'd like to suggest that the title is overly specific. I (still on windows) have the same issue with the following one-liner in the console:
```
In [1]: input('Try hitting ctrl+c:')

Try hitting ctrl+c:
```

After this, the console will accept more text, but will not respond to 'Enter', or any other way of getting back to 'In [2]: '

My suggested title would be "Ctrl+C not working when using input()".

I'm surprised this issue is not more common, as 'input()' is commonly used to provide system-agnostic pause functionality to scripts. Then, if the user decides during the prompt that they don't want to continue, Ctrl+C is the natural response I think.

I note also that in this case, using neither the debugger stop button nor the red stop button in the console gets out of the tangle appropriately.


It certainly does seem like ipython/ipython#2472 is a potential source of the issue.

But..IPython closed that as 'not in IPython", where does the trail lead from here?
in spyder ctrl+d interrupts it with EOFError 
but this didn't work with VS
Ctrl+Pause (or some other similar name on your keyboard)
Windows in 2020 + Python = Sad mess

This was (finally) fixed by the Jupyter team in https://github.com/ipython/ipykernel/pull/498. So, until we release a new version of spyder-kernels that requires a version of ipykernel with that fix, you can simply run

    conda install ipykernel=5.3.4

to get it.

Resolving patch:
diff --git a/external-deps/spyder-kernels/setup.py b/external-deps/spyder-kernels/setup.py
--- a/external-deps/spyder-kernels/setup.py
+++ b/external-deps/spyder-kernels/setup.py
@@ -38,7 +38,7 @@ def get_version(module='spyder_kernels'):
 REQUIREMENTS = [
     'cloudpickle',
     'ipykernel<5; python_version<"3"',
-    'ipykernel>=5.1.3; python_version>="3"',
+    'ipykernel>=5.3.0; python_version>="3"',
     'ipython<6; python_version<"3"',
     'ipython>=7.6.0; python_version>="3"',
     'jupyter-client>=5.3.4',


Test patch:
diff --git a/external-deps/spyder-kernels/requirements/tests.txt b/external-deps/spyder-kernels/requirements/tests.txt
--- a/external-deps/spyder-kernels/requirements/tests.txt
+++ b/external-deps/spyder-kernels/requirements/tests.txt
@@ -10,4 +10,4 @@ pytest
 pytest-cov
 scipy
 xarray
-pillow
\ No newline at end of file
+pillow