@@ -317,14 +317,26 @@ def __enter__(self):
317
317
318
318
self .orig_import = __builtins__ ['__import__' ]
319
319
if self .watcher :
320
+ old_module_locations = {} # for readding modules if they fail to load
320
321
@functools .wraps (self .orig_import )
321
322
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__ )
328
340
return m
329
341
__builtins__ ['__import__' ] = new_import
330
342
0 commit comments