4

In the standard Python interactive shell, I can press Ctrl+D to close stdin and it shows the output:

$ python
Python 2.7.2 (default, Mar  7 2012, 21:18:58) 
[GCC 4.5.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for f in range(5):
...     print f     (I press Enter here)
...                 (I press Ctrl+D here) 
0
1
2
3
4
>>> 

But in IPython and bpython, Ctrl+D doesn't work, I must press Enter twice to get the results:

IPython:

In [1]: for f in range(5):
   ...:     print f     (I press Enter here)
   ...:                 (I press Enter here)
   ...:                 (I press Enter here)
0
1
2
3
4

bpython:

>>> for f in range(5):
...     print f     (I press Enter here)
...                 (I press Enter here)
...                 (I press Enter here)
0
1
2
3
4

The version which I'm using:

[I] dev-python/ipython
     Available versions:  0.10 0.10.1 0.10.2 ~0.12-r1 {doc emacs examples gnuplot matplotlib mongodb notebook qt4 readline (+)smp sqlite test wxwidgets}
     Installed versions:  0.10.2(03:54:09 PM 08/12/2011)(examples readline -doc -emacs -gnuplot -smp -test -wxwidgets)
     Homepage:            http://ipython.org/
     Description:         Advanced interactive shell for Python

[I] dev-python/bpython
     Available versions:  0.9.7.1 0.10.1 {gtk urwid}
     Installed versions:  0.10.1(10:34:17 AM 03/14/2012)(gtk -urwid)
     Homepage:            http://www.bpython-interpreter.org/ https://bitbucket.org/bobf/bpython/ http://pypi.python.org/pypi/bpython
     Description:         Syntax highlighting and autocompletion for the Python interpreter
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
quanta
  • 3,960
  • 4
  • 40
  • 75
  • 1
    What's the question? If you want change the behaviour of IPython, you can't. And to be fair two enters is exactly the same number of key presses as Ctrl-D. – Dunes Mar 15 '12 at 11:15
  • OK, 2 enters = Ctrl-D. But is there any key combination in `ipython`, `bpython`? Why the standard python interpreter needs only one enter while `ipython`, `bpython` needs press Enter 2 times? Are they nested interpreter? – quanta Mar 15 '12 at 11:46
  • 1
    IPython uses `raw_input` for the prompt. For some reason, Ctrl-D is silenced if there's already any text after the prompt. And the auto-indentation puts four spaces after the prompt on the next line, so IPython never sees your Ctrl-D. – Thomas K Mar 15 '12 at 12:41

2 Answers2

0

I solved half the problem by upgrading IPython to version 0.12-r1:

[I] dev-python/ipython
     Available versions:  0.10 0.10.1 0.10.2 (~)0.12-r1 {doc emacs examples gnuplot matplotlib mongodb notebook qt4 readline (+)smp sqlite test wxwidgets}
     Installed versions:  0.12-r1(11:47:58 AM 03/16/2012)(examples qt4 readline smp -doc -emacs -matplotlib -mongodb -notebook -sqlite -test -wxwidgets)
     Homepage:            http://ipython.org/
     Description:         Advanced interactive shell for Python

Pay attention to the ...: you'll see that I only need to press Enter two times: the first one to make a new line and the second one to close stdin and display the results:

$ ipython
Python 2.7.2 (default, Mar  7 2012, 21:18:58) 
Type "copyright", "credits" or "license" for more information.

IPython 0.12 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: for i in range(2):
   ...:     for j in range(2):
   ...:         print i*j
   ...:         
0
0
0
1

About bpython, it needs to go to the line which has no indentation before showing the output. For testing purposes, if I use two nested for loops, I must press Enter four times:

>>> for i in range(2):
...     for j in range(2):
...         print i*j
...         
...     
... 
0
0
0
1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
quanta
  • 3,960
  • 4
  • 40
  • 75
-1

you just need to press Enter key twice, the standard Python interactive shell and IPython provide the same result + effect here.

Abhishek
  • 6,912
  • 14
  • 59
  • 85