click-repl
click-repl copied to clipboard
Exception when entering command with args/options using python 2.7
When in the interactive mode using click_repl and a command is entered that displays possible completions using python 2.7, and prompt-toolkit > version 2.0 an exception is generated from prompt-toolkit. The exception occurs when the first space between comand and subcommands is entered.
This is not an issue with python 3.
Thus in the project pywbemcli entering a command in interactive mdoe like:
class enumerate --no Fails with the exception on the space after class or after enumerate.
This does not happen on a corresponding command line command when click auto-completion is enabled.
This issue does not occur with prompt-toolkit 1.0 but the failure started when we changed the requirements to use prompt-toolkit >= 2..0
The following comment shows the exception.
Typical exception chain when the failure occurs:
Unhandled exception in event loop: File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/eventloop/posix.py", line 155, in _run_task t() File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/eventloop/context.py", line 116, in new_func return func(*a, **kw) File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/application/application.py", line 403, in redraw self._redraw() File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/application/application.py", line 471, in _redraw self.renderer.render(self, self.layout) File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/renderer.py", line 588, in render screen.draw_all_floats() File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/layout/screen.py", line 232, in draw_all_floats functions[0]1 File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/layout/containers.py", line 710, in _draw_float width = fl.content.preferred_width(write_position.width).preferred File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/layout/containers.py", line 2183, in preferred_width return self.content.pr.eferred_width(max_available_width) File "/home/../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/layout/containers.py", line 1349, in preferred_width dont_extend=self.dont_extend_width()) File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/layout/containers.py", line 1390, in _merge_dimensions preferred = get_preferred() File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/layout/containers.py", line 1338, in preferred_content_width max_available_width - total_margin_width) File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/layout/menus.py", line 55, in preferred_width menu_meta_width = self._get_menu_meta_width(500, complete_state) File "/home/kschopmeyer/virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/layout/menus.py", line 119, in _get_menu_meta_width if self._show_meta(complete_state): File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/layout/menus.py", line 103, in _show_meta return any(c.display_meta_text for c in complete_state.completions) File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/layout/menus.py", line 103, in return any(c.display_meta_text for c in complete_state.completions) File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/completion/base.py", line 98, in display_meta_text return fragment_list_to_text(self.display_meta) File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/completion/base.py", line 92, in display_meta return to_formatted_text(self._display_meta) File "/home/.../virtualenvs/pycli2/local/lib/python2.7/site-packages/prompt_toolkit/formatted_text/base.py", line 50, in to_formatted_text 'HTML, ANSI or a FormattedText instance. Got %r' % value)
Exception No formatted text. Expecting a unicode object, HTML, ANSI or a FormattedText instance. Got 'Show this help message.'
NOTE: The text after GOT is the text for --help
Tested with the following click snippet and can consistently create the issue when using python 2 and prompt-toolkit > 2.0
from __future__ import absolute_import, print_function
import os
import click
import click_repl
@click.group()
#@click.pass_context
def cli():
click.echo("Hello World from cli")
@cli.command()
@click.option('--verbose', is_flag=True, help="Will print verbose messages.")
@click.option('--name', default='', help='Who are you?')
def hello(verbose, name):
if verbose:
click.echo("We are in the verbose mode.")
click.echo("Hello World from name {}". format(name))
click.echo('Bye from hello {0}'.format(name))
@cli.command()
def repl():
click.echo("Enter repl mode")
prompt_kwargs = {
'message': u"Repl: ",
}
click_repl.repl(click.get_current_context(), prompt_kwargs=prompt_kwargs)
#click_repl.register_repl(cli)
cli()
This particular issues is because starting with prompt-toolkit 2.0, the method ... requires Unicode for the text input but it is receiveing a python 2 string.
The proposed fix is:
click_repl (version 0.16, init.py, line 122
Original:
text_type(o), -len(incomplete), display_meta=param.help
Fix:
text_type(o), -len(incomplete), display_meta=text_type(param.help)
@KSchopmeyer that seems fine assuming this doesn't break support for prompt-toolkit==1. Could you open a PR?
OK I will open a PR.
I agreed to create a pr and started it when I noticed that despite untitakers comment about about assuring that the change does not break with prompt-toolkit version 1, there is no test in the test suite for various versions of prompt-toolkit. So even if I submit the pr, it will not be tested. Note that I also just submitted an issue noting that with the policy changes, the tests in travis are probably no longer running so that the CI test definition must also be rewritten to some other test environment.
Understood, and thanks for your efforts. Unfortunately I no longer use click-repl myself and as such it is hard to keep up with new changes in click or prompt-toolkit. See https://github.com/click-contrib/click-repl/issues/73