Repository: ipython/ipython

Issue:
Readlinelike display of options crashes ipython terminal.



Discussion:
With the following `ipython_config.py` on python2.7:

```
c.TerminalInteractiveShell.editing_mode = 'vi'
c.TerminalInteractiveShell.display_completions='readlinelike'
```

```
In [1]: import numpy as np 

In [2]: np.
Display all 584 possibilities? (y on n) -> y
In [2]: np.                                                                                                                                                                                             
Traceback (most recent call last):
  File "/Users/mb312/.virtualenvs/test/bin/ipython", line 11, in <module>
    sys.exit(start_ipython())
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/IPython/__init__.py", line 119, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/traitlets/config/application.py", line 596, in launch_instance
    app.start()
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 348, in start
    self.shell.mainloop()
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 480, in mainloop
    self.interact()
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 463, in interact
    code = self.prompt_for_code()
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 403, in prompt_for_code
    pre_run=self.pre_prompt, reset_current_buffer=True)
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/interface.py", line 388, in run
    self.eventloop.run(self.input, self.create_eventloop_callbacks())
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/eventloop/posix.py", line 154, in run
    t()
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/eventloop/posix.py", line 82, in read_from_stdin
    inputstream.feed(data)
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/terminal/vt100_input.py", line 376, in feed
    self._input_parser.send(c)
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/terminal/vt100_input.py", line 313, in _input_parser_generator
    self._call_handler(prefix[0], prefix[0])
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/terminal/vt100_input.py", line 328, in _call_handler
    self.feed_key_callback(KeyPress(key, insert_text))
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/interface.py", line 947, in feed_key
    cli.input_processor.process_keys()
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/key_binding/input_processor.py", line 201, in process_keys
    self._process_coroutine.send(key_press)
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/key_binding/input_processor.py", line 158, in _process
    self._call_handler(matches[-1], key_sequence=buffer)
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/key_binding/input_processor.py", line 228, in _call_handler
    handler.call(event)
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/key_binding/registry.py", line 31, in call
    return self.handler(event)
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/shortcuts.py", line 642, in _
    event.cli.set_return_value(True)
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/interface.py", line 577, in set_return_value
    self._set_return_callable(lambda: document)
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/interface.py", line 585, in _set_return_callable
    self.eventloop.stop()
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/interface.py", line 1062, in stop
    self.stop_callback()
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/interface.py", line 513, in done
    done_callback(sub_cli.return_value())
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/key_binding/bindings/completion.py", line 125, in display_page
    cli.run_in_terminal(lambda: display(page_counter[0]))
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/interface.py", line 613, in run_in_terminal
    result = func()
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/key_binding/bindings/completion.py", line 125, in <lambda>
    cli.run_in_terminal(lambda: display(page_counter[0]))
  File "/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/prompt_toolkit/key_binding/bindings/completion.py", line 105, in display
    for i in range(column_count)]
TypeError: slice indices must be integers or None or have an __index__ method

If you suspect this is an IPython bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@scipy.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    %config Application.verbose_crash=True
```

Ping @jonathanslenders.

I tried to reproduce this just now with master of both IPython and prompt_toolkit (master of the latter is the same as 1.0.2 at the moment), but it worked for me.

Apparently a Python 2 issue. I'll check this evening.

Hi @matthew-brett, can you please test again with the latest master branch of prompt-toolkit?
This should fix Python 2 compatibility, but it also contains a few improvements regarding the visualisation when the readline-like completion is triggered from somewhere in the middle of a multiline input prompt.

If everything is fine, this will probably become a prompt-toolkit 1.0.3. 

Excellent - yes - that works as expected, thanks.

prompt-toolkit 1.0.3 has been released, solving the issue above. (@matthew-brett, if you have time, can you test again in a clean environment?)

Maybe we should also increase the minimum required version in the setup.py from IPython.


Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -196,7 +196,7 @@ def run(self):
     'pickleshare',
     'simplegeneric>0.8',
     'traitlets>=4.2',
-    'prompt_toolkit>=1.0.1,<2.0.0',
+    'prompt_toolkit>=1.0.3,<2.0.0',
     'pygments',
 ]
 


Test patch: