prompt-toolkit / prompt-toolkit/python-prompt-toolkit
Unhandled exception in event loop in auto_suggest keybindings
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 10.6k
- Forks
- 815
- PR merge metrics
- No merged PRs in 30d
Description
Hi. Thanks.
What's issue
I got Unhandled exception in event loop error in this keybinding function.
https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/prompt_toolkit/key_binding/bindings/auto_suggest.py#L49
Environment
OS X
python 3.6.8
prompt-toolkit == 2.0.9 or 3.0.0
How reproduction
using this sample: https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/examples/prompts/auto-suggestion.py
Say something: import [[[[escape f]]] path [[[[escape f]]]
Say something: import path/homebrew/versions/3.6.8/lib/python3.6/site-packages/prompt_toolkit/key_binding/key_processor.py:273: DeprecationWarning: generator 'KeyProcessor._process' raised StopIteration
Unhandled exception in event loop:
File ".../prompt_toolkit/eventloop/posix.py", line 154, in _run_task
File ".../prompt_toolkit/eventloop/context.py", line 115, in new_func
File ".../prompt_toolkit/application/application.py", line 562, in read_from_input
File ".../prompt_toolkit/key_binding/key_processor.py", line 273, in process_keys
File ".../prompt_toolkit/key_binding/key_processor.py", line 180, in _process
File ".../prompt_toolkit/key_binding/key_processor.py", line 323, in _call_handler
File ".../prompt_toolkit/key_binding/key_bindings.py", line 78, in call
File ".../prompt_toolkit/key_binding/bindings/auto_suggest.py", line 51, in _
Exception
Press ENTER to continue...
Solution
Because generator raises StopIteration.
We use suggestion_available filter, but app.current_buffer.suggestion becomes None or
Suggestion() . It can't catch exception.
https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/prompt_toolkit/key_binding/bindings/auto_suggest.py#L31
We can use try/except or Suggestion.__len__.
A: try/except
@handle('escape', 'f', filter=suggestion_available & emacs_mode)
def _(event: E) -> None:
b = event.current_buffer
suggestion = b.suggestion
if suggestion:
t = re.split(r'(\S+\s+)', suggestion.text)
- b.insert_text(next(x for x in t if x))
+ try:
+ b.insert_text(next(x for x in t if x))
+ except StopIteration:
+ pass
+ except:
+ raise
B: Suggestion.__len__
+ def __len__(self) -> int:
+ return len(self.text)
- return (app.current_buffer.suggestion is not None and
+ return (app.current_buffer.suggestion and
app.current_buffer.document.is_cursor_at_the_end)
Comment
I think Solution B is better:)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the failure with examples/prompts/auto-suggestion.py using the reported auto_suggest keybinding and inspect prompt_toolkit/key_binding/bindings/auto_suggest.py, especially the handler around line 49. Check the related suggestion handling in prompt_toolkit/auto_suggest.py and the reported key processor path. Done means the reproduction no longer raises an unhandled StopIteration exception.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100