Skip to content

Commit 8f31416

Browse files
vicalloyjonathanslenders
authored andcommitted
To fix the memory leak, move the inline condition filter out of functions.
1 parent 6f9a678 commit 8f31416

File tree

3 files changed

+45
-33
lines changed

3 files changed

+45
-33
lines changed

src/prompt_toolkit/key_binding/bindings/basic.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ def if_no_repeat(event: E) -> bool:
2929
return not event.is_repeat
3030

3131

32+
@Condition
33+
def has_text_before_cursor() -> bool:
34+
return bool(get_app().current_buffer.text)
35+
36+
37+
@Condition
38+
def in_quoted_insert() -> bool:
39+
return get_app().quoted_insert
40+
41+
3242
def load_basic_bindings() -> KeyBindings:
3343
key_bindings = KeyBindings()
3444
insert_mode = vi_insert_mode | emacs_insert_mode
@@ -171,10 +181,6 @@ def _ignore(event: E) -> None:
171181

172182
# CTRL keys.
173183

174-
@Condition
175-
def has_text_before_cursor() -> bool:
176-
return bool(get_app().current_buffer.text)
177-
178184
handle("c-d", filter=has_text_before_cursor & insert_mode)(
179185
get_by_name("delete-char")
180186
)
@@ -240,10 +246,6 @@ def _paste(event: E) -> None:
240246

241247
event.current_buffer.insert_text(data)
242248

243-
@Condition
244-
def in_quoted_insert() -> bool:
245-
return get_app().quoted_insert
246-
247249
@handle(Keys.Any, filter=in_quoted_insert, eager=True)
248250
def _insert_text(event: E) -> None:
249251
"""

src/prompt_toolkit/key_binding/bindings/emacs.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
E = KeyPressEvent
3434

3535

36+
@Condition
37+
def is_returnable() -> bool:
38+
return get_app().current_buffer.is_returnable
39+
40+
3641
def load_emacs_bindings() -> KeyBindingsBase:
3742
"""
3843
Some e-macs extensions.
@@ -142,10 +147,6 @@ def _dash(event: E) -> None:
142147
"""
143148
event.app.key_processor.arg = "-"
144149

145-
@Condition
146-
def is_returnable() -> bool:
147-
return get_app().current_buffer.is_returnable
148-
149150
# Meta + Enter: always accept input.
150151
handle("escape", "enter", filter=insert_mode & is_returnable)(
151152
get_by_name("accept-line")

src/prompt_toolkit/key_binding/bindings/vi.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,35 @@ def _operator_in_selection(event: E) -> None:
371371
return operator_decorator
372372

373373

374+
@Condition
375+
def is_returnable() -> bool:
376+
return get_app().current_buffer.is_returnable
377+
378+
379+
@Condition
380+
def in_block_selection() -> bool:
381+
buff = get_app().current_buffer
382+
return bool(
383+
buff.selection_state and buff.selection_state.type == SelectionType.BLOCK
384+
)
385+
386+
387+
@Condition
388+
def digraph_symbol_1_given() -> bool:
389+
return get_app().vi_state.digraph_symbol1 is not None
390+
391+
392+
@Condition
393+
def search_buffer_is_empty() -> bool:
394+
"Returns True when the search buffer is empty."
395+
return get_app().current_buffer.text == ""
396+
397+
398+
@Condition
399+
def tilde_operator() -> bool:
400+
return get_app().vi_state.tilde_operator
401+
402+
374403
def load_vi_bindings() -> KeyBindingsBase:
375404
"""
376405
Vi extensions.
@@ -410,7 +439,7 @@ def load_vi_bindings() -> KeyBindingsBase:
410439
(("g", "~"), Always(), lambda string: string.swapcase()),
411440
(
412441
("~",),
413-
Condition(lambda: get_app().vi_state.tilde_operator),
442+
tilde_operator,
414443
lambda string: string.swapcase(),
415444
),
416445
]
@@ -528,10 +557,6 @@ def _cancel_completion(event: E) -> None:
528557
"""
529558
event.current_buffer.cancel_completion()
530559

531-
@Condition
532-
def is_returnable() -> bool:
533-
return get_app().current_buffer.is_returnable
534-
535560
# In navigation mode, pressing enter will always return the input.
536561
handle("enter", filter=vi_navigation_mode & is_returnable)(
537562
get_by_name("accept-line")
@@ -681,13 +706,6 @@ def _I(event: E) -> None:
681706
)
682707
)
683708

684-
@Condition
685-
def in_block_selection() -> bool:
686-
buff = get_app().current_buffer
687-
return bool(
688-
buff.selection_state and buff.selection_state.type == SelectionType.BLOCK
689-
)
690-
691709
@handle("I", filter=in_block_selection & ~is_read_only)
692710
def insert_in_block_selection(event: E, after: bool = False) -> None:
693711
"""
@@ -2071,10 +2089,6 @@ def _digraph(event: E) -> None:
20712089
"""
20722090
event.app.vi_state.waiting_for_digraph = True
20732091

2074-
@Condition
2075-
def digraph_symbol_1_given() -> bool:
2076-
return get_app().vi_state.digraph_symbol1 is not None
2077-
20782092
@handle(Keys.Any, filter=vi_digraph_mode & ~digraph_symbol_1_given)
20792093
def _digraph1(event: E) -> None:
20802094
"""
@@ -2180,11 +2194,6 @@ def load_vi_search_bindings() -> KeyBindingsBase:
21802194
handle = key_bindings.add
21812195
from . import search
21822196

2183-
@Condition
2184-
def search_buffer_is_empty() -> bool:
2185-
"Returns True when the search buffer is empty."
2186-
return get_app().current_buffer.text == ""
2187-
21882197
# Vi-style forward search.
21892198
handle(
21902199
"/",

0 commit comments

Comments
 (0)