From 96a6435558251aa68e603cd7ca8b0388678ac557 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Sun, 26 Mar 2017 19:05:01 +0200 Subject: Fixes previous_key_sequences: make a copy of key_sequences before calling the handler + initialize to an empty list. --- prompt_toolkit/key_binding/input_processor.py | 4 +-- tests/test_key_binding.py | 42 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/prompt_toolkit/key_binding/input_processor.py b/prompt_toolkit/key_binding/input_processor.py index a57e3aa3..51a31108 100644 --- a/prompt_toolkit/key_binding/input_processor.py +++ b/prompt_toolkit/key_binding/input_processor.py @@ -93,7 +93,7 @@ class InputProcessor(object): self.reset() def reset(self): - self._previous_key_sequence = None + self._previous_key_sequence = [] self._previous_handler = None self._process_coroutine = self._process() @@ -173,7 +173,7 @@ class InputProcessor(object): # Exact matches found, call handler. if not is_prefix_of_longer_match and matches: - self._call_handler(matches[-1], key_sequence=buffer) + self._call_handler(matches[-1], key_sequence=buffer[:]) del buffer[:] # Keep reference. # No match found. diff --git a/tests/test_key_binding.py b/tests/test_key_binding.py index 04f6b5f8..d706c444 100644 --- a/tests/test_key_binding.py +++ b/tests/test_key_binding.py @@ -96,3 +96,45 @@ def test_common_prefix(processor, handlers): processor.process_keys() assert handlers.called == ['control_x', 'control_d'] + + +def test_previous_key_sequence(processor, handlers): + """ + test whether we receive the correct previous_key_sequence. + """ + events = [] + def handler(event): + events.append(event) + + # Build registry. + registry = Registry() + registry.add_binding('a', 'a')(handler) + registry.add_binding('b', 'b')(handler) + processor = InputProcessor(registry, lambda: None) + + # Create processor and feed keys. + processor.feed(KeyPress('a', 'a')) + processor.feed(KeyPress('a', 'a')) + processor.feed(KeyPress('b', 'b')) + processor.feed(KeyPress('b', 'b')) + processor.process_keys() + + # Test. + assert len(events) == 2 + assert len(events[0].key_sequence) == 2 + assert events[0].key_sequence[0].key == 'a' + assert events[0].key_sequence[0].data == 'a' + assert events[0].key_sequence[1].key == 'a' + assert events[0].key_sequence[1].data == 'a' + assert events[0].previous_key_sequence == [] + + assert len(events[1].key_sequence) == 2 + assert events[1].key_sequence[0].key == 'b' + assert events[1].key_sequence[0].data == 'b' + assert events[1].key_sequence[1].key == 'b' + assert events[1].key_sequence[1].data == 'b' + assert len(events[1].previous_key_sequence) == 2 + assert events[1].previous_key_sequence[0].key == 'a' + assert events[1].previous_key_sequence[0].data == 'a' + assert events[1].previous_key_sequence[1].key == 'a' + assert events[1].previous_key_sequence[1].data == 'a' -- cgit v1.2.3