@@ -104,6 +104,29 @@ def readline(self):
104104 self .repl .send_to_stdout (value )
105105 return value
106106
107+
108+ def get_interpreter (config , locals_ = None ):
109+ if config .scroll_auto_import :
110+
111+ locals_ = locals_ if locals_ is not None else {}
112+
113+ class AutoImporter (dict ):
114+ def __getitem__ (self , item ):
115+ if item in locals_ :
116+ return locals_ [item ]
117+ else :
118+ try :
119+ return __import__ (item )
120+ except ImportError :
121+ raise KeyError (item )
122+ def __setitem__ (self , item , value ):
123+ locals_ [item ] = value
124+
125+ ns = AutoImporter ()
126+ return code .InteractiveInterpreter (locals = ns )
127+ else :
128+ return code .InteractiveInterpreter (locals = locals_ )
129+
107130class Repl (BpythonRepl ):
108131 """
109132
@@ -125,12 +148,14 @@ class Repl(BpythonRepl):
125148 ## initialization, cleanup
126149 def __init__ (self , locals_ = None , config = None , stuff_a_refresh_request = lambda : None , banner = None ):
127150 logging .debug ("starting init" )
128- interp = code .InteractiveInterpreter (locals = locals_ )
129151
130152 if config is None :
131153 config = Struct ()
132154 loadini (config , default_config_path ())
133155
156+ if config .scroll_auto_import :
157+ interp = get_interpreter (config , locals_ = locals_ )
158+
134159 if banner is None :
135160 banner = _ ('welcome to bpython' )
136161
@@ -322,7 +347,7 @@ def on_enter(self, insert_into_history=True):
322347 self .rl_history .last ()
323348 self .history .append (self ._current_line )
324349 line = self ._current_line
325- self ._current_line = ''
350+ # self._current_line = ''
326351 self .cursor_offset_in_line = 0
327352 self .push (line , insert_into_history = insert_into_history )
328353
@@ -654,6 +679,9 @@ def paint(self, about_to_exit=False):
654679 if self .stdin .has_focus :
655680 cursor_column = len (self .current_stdouterr_line ) - self .stdin .cursor_offset_in_line
656681 assert cursor_column >= 0 , cursor_column
682+ elif self .coderunner .running :
683+ cursor_column = len (self .current_cursor_line ) + self .cursor_offset_in_line
684+ assert cursor_column >= 0 , (cursor_column , len (self .current_cursor_line ), len (self ._current_line ), self .cursor_offset_in_line )
657685 else :
658686 cursor_column = len (self .current_cursor_line ) - len (self ._current_line ) + self .cursor_offset_in_line
659687 assert cursor_column >= 0 , (cursor_column , len (self .current_cursor_line ), len (self ._current_line ), self .cursor_offset_in_line )
@@ -791,7 +819,7 @@ def reevaluate(self, insert_into_history=False):
791819 self .display_lines = []
792820
793821 self .done = True # this keeps the first prompt correct
794- self .interp = code . InteractiveInterpreter ( )
822+ self .interp = get_interpreter ( self . config )
795823 self .coderunner .interp = self .interp
796824 self .completer = Autocomplete (self .interp .locals , self .config )
797825 self .completer .autocomplete_mode = 'simple'
0 commit comments