Skip to content

Commit 15139f8

Browse files
dealt with cases involving rewinding and toggling watching
1 parent 8e10646 commit 15139f8

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

bpython/curtsiesfrontend/filewatch.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ class ModuleChangedEventHandler(FileSystemEventHandler):
1111
def __init__(self, paths, on_change):
1212
self.dirs = defaultdict(set)
1313
self.on_change = on_change
14+
self.modules_to_add_later = []
1415
self.observer = Observer()
16+
self.old_dirs = defaultdict(set)
1517
for path in paths:
1618
self.add_module(path)
1719
self.observer.start()
1820

1921
def reset(self):
2022
self.dirs = defaultdict(set)
23+
del self.modules_to_add_later[:]
24+
self.old_dirs = defaultdict(set)
2125
self.observer.unschedule_all()
2226

2327
def add_module(self, path):
@@ -32,6 +36,22 @@ def add_module(self, path):
3236
self.observer.schedule(self, dirname, recursive=False)
3337
self.dirs[os.path.dirname(path)].add(path)
3438

39+
def add_module_later(self, path):
40+
self.modules_to_add_later.append(path)
41+
42+
def activate(self):
43+
self.dirs = self.old_dirs
44+
for dirname in self.dirs:
45+
self.observer.schedule(self, dirname, recursive=False)
46+
for module in self.modules_to_add_later:
47+
self.add_module(module)
48+
del self.modules_to_add_later[:]
49+
50+
def deactivate(self):
51+
self.observer.unschedule_all()
52+
self.old_dirs = self.dirs
53+
self.dirs = defaultdict(set)
54+
3555
def on_any_event(self, event):
3656
dirpath = os.path.dirname(event.src_path)
3757
paths = [path + '.py' for path in self.dirs[dirpath]]

bpython/curtsiesfrontend/repl.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)