@@ -296,7 +296,10 @@ def __enter__(self):
296296 def new_import (name , globals = {}, locals = {}, fromlist = [], level = - 1 ):
297297 m = self .orig_import (name , globals = globals , locals = locals , fromlist = fromlist )
298298 if hasattr (m , "__file__" ):
299- self .watcher .add_module (m .__file__ )
299+ if self .watching_files :
300+ self .watcher .add_module (m .__file__ )
301+ else :
302+ self .watcher .add_module_later (m .__file__ )
300303 return m
301304 __builtins__ ['__import__' ] = new_import
302305
@@ -394,12 +397,13 @@ def process_event(self, e):
394397 elif e in key_dispatch [self .config .toggle_file_watch_key ]:
395398 msg = "Auto-reloading active, watching for file changes..."
396399 if self .watching_files :
397- self .watcher .reset ()
400+ self .watcher .deactivate ()
398401 self .watching_files = False
399402 self .status_bar .pop_permanent_message (msg )
400403 else :
401404 self .watching_files = True
402405 self .status_bar .push_permanent_message (msg )
406+ self .watcher .activate ()
403407
404408 elif e in key_dispatch [self .config .reimport_key ]:
405409 self .clear_modules_and_reevaluate ()
@@ -580,6 +584,7 @@ def send_session_to_external_editor(self, filename=None):
580584 self .cursor_offset = len (self .current_line )
581585
582586 def clear_modules_and_reevaluate (self ):
587+ self .watcher .reset ()
583588 cursor , line = self .cursor_offset , self .current_line
584589 for modname in sys .modules .keys ():
585590 if modname not in self .original_modules :
@@ -1018,6 +1023,7 @@ def reprint_line(self, lineno, tokens):
10181023 self .display_buffer [lineno ] = bpythonparse (format (tokens , self .formatter ))
10191024 def reevaluate (self , insert_into_history = False ):
10201025 """bpython.Repl.undo calls this"""
1026+ self .watcher .reset ()
10211027 old_logical_lines = self .history
10221028 self .history = []
10231029 self .display_lines = []
0 commit comments