8787import linecache
8888
8989from contextlib import contextmanager
90+ from rlcompleter import Completer
9091from typing import Union
9192
9293
@@ -573,20 +574,14 @@ def displayhook(self, obj):
573574 self .message (repr (obj ))
574575
575576 @contextmanager
576- def _disable_tab_completion (self ):
577- if self .use_rawinput and self .completekey == 'tab' :
578- try :
579- import readline
580- except ImportError :
581- yield
582- return
583- try :
584- readline .parse_and_bind ('tab: self-insert' )
585- yield
586- finally :
587- readline .parse_and_bind ('tab: complete' )
588- else :
577+ def _disable_command_completion (self ):
578+ completenames = self .completenames
579+ try :
580+ self .completenames = self .completedefault
589581 yield
582+ finally :
583+ self .completenames = completenames
584+ return
590585
591586 def default (self , line ):
592587 if line [:1 ] == '!' : line = line [1 :].strip ()
@@ -595,7 +590,7 @@ def default(self, line):
595590 try :
596591 if (code := codeop .compile_command (line + '\n ' , '<stdin>' , 'single' )) is None :
597592 # Multi-line mode
598- with self ._disable_tab_completion ():
593+ with self ._disable_command_completion ():
599594 buffer = line
600595 continue_prompt = "... "
601596 while (code := codeop .compile_command (buffer , '<stdin>' , 'single' )) is None :
@@ -771,7 +766,10 @@ def completenames(self, text, line, begidx, endidx):
771766 if commands :
772767 return commands
773768 else :
774- return self ._complete_expression (text , line , begidx , endidx )
769+ expressions = self ._complete_expression (text , line , begidx , endidx )
770+ if expressions :
771+ return expressions
772+ return self .completedefault (text , line , begidx , endidx )
775773
776774 def _complete_location (self , text , line , begidx , endidx ):
777775 # Complete a file/module/function location for break/tbreak/clear.
@@ -828,6 +826,21 @@ def _complete_expression(self, text, line, begidx, endidx):
828826 # Complete a simple name.
829827 return [n for n in ns .keys () if n .startswith (text )]
830828
829+ def completedefault (self , text , line , begidx , endidx ):
830+ if text .startswith ("$" ):
831+ # Complete convenience variables
832+ conv_vars = self .curframe .f_globals .get ('__pdb_convenience_variables' , {})
833+ return [f"${ name } " for name in conv_vars if name .startswith (text [1 :])]
834+
835+ # Use rlcompleter to do the completion
836+ state = 0
837+ matches = []
838+ completer = Completer (self .curframe .f_globals | self .curframe_locals )
839+ while (match := completer .complete (text , state )) is not None :
840+ matches .append (match )
841+ state += 1
842+ return matches
843+
831844 # Command definitions, called by cmdloop()
832845 # The argument is the remaining string on the command line
833846 # Return true to exit from the command loop
0 commit comments