Skip to content

Commit 8e0cc92

Browse files
Fix stopping watching modules with errors
Also watch files that had errors in them that have been successfully imported before
1 parent fe7fa6c commit 8e0cc92

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

bpython/curtsiesfrontend/repl.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,26 @@ def __enter__(self):
317317

318318
self.orig_import = __builtins__['__import__']
319319
if self.watcher:
320+
old_module_locations = {} # for readding modules if they fail to load
320321
@functools.wraps(self.orig_import)
321322
def new_import(name, globals={}, locals={}, fromlist=[], level=-1):
322-
m = self.orig_import(name, globals=globals, locals=locals, fromlist=fromlist)
323-
if hasattr(m, "__file__"):
324-
if self.watching_files:
325-
self.watcher.add_module(m.__file__)
326-
else:
327-
self.watcher.add_module_later(m.__file__)
323+
try:
324+
m = self.orig_import(name, globals=globals, locals=locals, fromlist=fromlist)
325+
except:
326+
if name in old_module_locations:
327+
loc = old_module_locations[name]
328+
if self.watching_files:
329+
self.watcher.add_module(old_module_locations[name])
330+
else:
331+
self.watcher.add_module_later(old_module_locations[name])
332+
raise
333+
else:
334+
if hasattr(m, "__file__"):
335+
old_module_locations[name] = m.__file__
336+
if self.watching_files:
337+
self.watcher.add_module(m.__file__)
338+
else:
339+
self.watcher.add_module_later(m.__file__)
328340
return m
329341
__builtins__['__import__'] = new_import
330342

0 commit comments

Comments
 (0)