@@ -432,6 +432,7 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, persistent_histor
432432 self .initial_stdout = sys .stdout
433433 self .history = History ()
434434 self .pystate = {}
435+ self .py_history = []
435436 self .pyscript_name = 'app'
436437 self .keywords = self .reserved_words + [fname [3 :] for fname in dir (self ) if fname .startswith ('do_' )]
437438 self .statement_parser = StatementParser (
@@ -2573,73 +2574,71 @@ def quit():
25732574 self .pystate ['quit' ] = quit
25742575 self .pystate ['exit' ] = quit
25752576
2576- keepstate = None
2577- try :
2578- if rl_type != RlType .NONE :
2579- # Save cmd2 history
2580- saved_cmd2_history = []
2581- for i in range (1 , readline .get_current_history_length () + 1 ):
2582- saved_cmd2_history .append (readline .get_history_item (i ))
2583-
2584- readline .clear_history ()
2585-
2586- # Restore py's history
2587- # noinspection PyAttributeOutsideInit
2588- self .py_history = getattr (self , 'py_history' , [])
2589- for item in self .py_history :
2590- readline .add_history (item )
2591-
2592- if self .use_rawinput and self .completekey :
2593- # Set up tab completion for the Python console
2594- # rlcompleter relies on the default settings of the Python readline module
2595- if rl_type == RlType .GNU :
2596- old_basic_quotes = ctypes .cast (rl_basic_quote_characters , ctypes .c_void_p ).value
2597- rl_basic_quote_characters .value = orig_rl_basic_quotes
2598-
2599- if 'gnureadline' in sys .modules :
2600- # rlcompleter imports readline by name, so it won't use gnureadline
2601- # Force rlcompleter to use gnureadline instead so it has our settings and history
2602- saved_readline = None
2603- if 'readline' in sys .modules :
2604- saved_readline = sys .modules ['readline' ]
2577+ # Set up readline for Python console
2578+ if rl_type != RlType .NONE :
2579+ # Save cmd2 history
2580+ saved_cmd2_history = []
2581+ for i in range (1 , readline .get_current_history_length () + 1 ):
2582+ saved_cmd2_history .append (readline .get_history_item (i ))
2583+
2584+ readline .clear_history ()
2585+
2586+ # Restore py's history
2587+ for item in self .py_history :
2588+ readline .add_history (item )
2589+
2590+ if self .use_rawinput and self .completekey :
2591+ # Set up tab completion for the Python console
2592+ # rlcompleter relies on the default settings of the Python readline module
2593+ if rl_type == RlType .GNU :
2594+ old_basic_quotes = ctypes .cast (rl_basic_quote_characters , ctypes .c_void_p ).value
2595+ rl_basic_quote_characters .value = orig_rl_basic_quotes
2596+
2597+ if 'gnureadline' in sys .modules :
2598+ # rlcompleter imports readline by name, so it won't use gnureadline
2599+ # Force rlcompleter to use gnureadline instead so it has our settings and history
2600+ saved_readline = None
2601+ if 'readline' in sys .modules :
2602+ saved_readline = sys .modules ['readline' ]
2603+
2604+ sys .modules ['readline' ] = sys .modules ['gnureadline' ]
2605+
2606+ old_delims = readline .get_completer_delims ()
2607+ readline .set_completer_delims (orig_rl_delims )
2608+
2609+ # rlcompleter will not need cmd2's custom display function
2610+ # This will be restored by cmd2 the next time complete() is called
2611+ if rl_type == RlType .GNU :
2612+ readline .set_completion_display_matches_hook (None )
2613+ elif rl_type == RlType .PYREADLINE :
2614+ readline .rl .mode ._display_completions = self ._display_matches_pyreadline
2615+
2616+ # Save off the current completer and set a new one in the Python console
2617+ # Make sure it tab completes from its locals() dictionary
2618+ old_completer = readline .get_completer ()
2619+ interp .runcode ("from rlcompleter import Completer" )
2620+ interp .runcode ("import readline" )
2621+ interp .runcode ("readline.set_completer(Completer(locals()).complete)" )
2622+
2623+ cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
2624+ keepstate = Statekeeper (sys , ('stdin' , 'stdout' ))
2625+ sys .stdout = self .stdout
2626+ sys .stdin = self .stdin
2627+ docstr = self .do_py .__doc__ .replace ('pyscript_name' , self .pyscript_name )
26052628
2606- sys .modules ['readline' ] = sys .modules ['gnureadline' ]
2607-
2608- old_delims = readline .get_completer_delims ()
2609- readline .set_completer_delims (orig_rl_delims )
2610-
2611- # rlcompleter will not need cmd2's custom display function
2612- # This will be restored by cmd2 the next time complete() is called
2613- if rl_type == RlType .GNU :
2614- readline .set_completion_display_matches_hook (None )
2615- elif rl_type == RlType .PYREADLINE :
2616- readline .rl .mode ._display_completions = self ._display_matches_pyreadline
2617-
2618- # Load rlcompleter so it sets its completer function
2619- old_completer = readline .get_completer ()
2620- import rlcompleter
2621- import importlib
2622- importlib .reload (rlcompleter )
2623-
2624- cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
2625- keepstate = Statekeeper (sys , ('stdin' , 'stdout' ))
2626- sys .stdout = self .stdout
2627- sys .stdin = self .stdin
2628- docstr = self .do_py .__doc__ .replace ('pyscript_name' , self .pyscript_name )
2629- interp .interact (banner = "Python %s on %s\n %s\n (%s)\n %s" %
2630- (sys .version , sys .platform , cprt , self .__class__ .__name__ ,
2631- docstr ))
2629+ try :
2630+ interp .interact (banner = "Python {} on {}\n {}\n ({})\n {}" .
2631+ format (sys .version , sys .platform , cprt , self .__class__ .__name__ , docstr ))
26322632 except EmbeddedConsoleExit :
26332633 pass
26342634
26352635 finally :
2636- if keepstate is not None :
2637- keepstate .restore ()
2636+ keepstate .restore ()
26382637
2638+ # Set up readline for cmd2
26392639 if rl_type != RlType .NONE :
26402640 # Save py's history
2641- # noinspection PyAttributeOutsideInit
2642- self .py_history = []
2641+ self .py_history .clear ()
26432642 for i in range (1 , readline .get_current_history_length () + 1 ):
26442643 self .py_history .append (readline .get_history_item (i ))
26452644
0 commit comments