161161else :
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