Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 03bb4e4

Browse files
authored
Merge pull request #10743 from takluyver/limit-no-completions
Limit number of completions returned
2 parents 8cbb79f + ed0c88f commit 03bb4e4

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ _build
55
docs/man/*.gz
66
docs/source/api/generated
77
docs/source/config/options
8+
docs/source/config/shortcuts/*.csv
89
docs/source/interactive/magics-generated.txt
910
docs/source/config/shortcuts/*.csv
1011
docs/gh-pages

IPython/core/completer.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@
161161
else:
162162
PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
163163

164+
# Protect against returning an enormous number of completions which the frontend
165+
# may have trouble processing.
166+
MATCHES_LIMIT = 500
164167

165168
_deprecation_readline_sentinel = object()
166169

@@ -1943,7 +1946,8 @@ def _complete(self, *, cursor_line, cursor_pos, line_buffer=None, text=None,
19431946
for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches):
19441947
name_text, name_matches = meth(base_text)
19451948
if name_text:
1946-
return name_text, name_matches, [meth.__qualname__]*len(name_matches), ()
1949+
return name_text, name_matches[:MATCHES_LIMIT], \
1950+
[meth.__qualname__]*min(len(name_matches), MATCHES_LIMIT), ()
19471951

19481952

19491953
# If no line buffer is given, assume the input text is all there was
@@ -1955,11 +1959,10 @@ def _complete(self, *, cursor_line, cursor_pos, line_buffer=None, text=None,
19551959

19561960
# Do magic arg matches
19571961
for matcher in self.magic_arg_matchers:
1958-
matches = [(m, matcher.__qualname__) for m in matcher(line_buffer)]
1962+
matches = list(matcher(line_buffer))[:MATCHES_LIMIT]
19591963
if matches:
1960-
matches2 = [m[0] for m in matches]
1961-
origins = [m[1] for m in matches]
1962-
return text, matches2, origins, ()
1964+
origins = [matcher.__qualname__] * len(matches)
1965+
return text, matches, origins, ()
19631966

19641967
# Start with a clean slate of completions
19651968
matches = []
@@ -2006,7 +2009,8 @@ def _complete(self, *, cursor_line, cursor_pos, line_buffer=None, text=None,
20062009
seen.add(t)
20072010

20082011
_filtered_matches = sorted(
2009-
set(filtered_matches), key=lambda x: completions_sorting_key(x[0]))
2012+
set(filtered_matches), key=lambda x: completions_sorting_key(x[0]))\
2013+
[:MATCHES_LIMIT]
20102014

20112015
_matches = [m[0] for m in _filtered_matches]
20122016
origins = [m[1] for m in _filtered_matches]

0 commit comments

Comments
 (0)