@@ -213,6 +213,34 @@ def readline(self):
213213 return value
214214
215215
216+ class ImportLoader (object ):
217+
218+ def __init__ (self , watcher , loader ):
219+ self .watcher = watcher
220+ self .loader = loader
221+
222+ def load_module (self , fullname ):
223+ module = self .loader .load_module (fullname )
224+ if hasattr (module , '__file__' ):
225+ self .watcher .track_module (module .__file__ )
226+ return module
227+
228+
229+ class ImportFinder (object ):
230+
231+ def __init__ (self , watcher , old_meta_path ):
232+ self .watcher = watcher
233+ self .old_meta_path = old_meta_path
234+
235+ def find_module (self , fullname , path = None ):
236+ for finder in self .old_meta_path :
237+ loader = finder .find_module (fullname , path )
238+ if loader is not None :
239+ return ImportLoader (self .watcher , loader )
240+
241+ return None
242+
243+
216244class Repl (BpythonRepl ):
217245 """Python Repl
218246
@@ -416,29 +444,9 @@ def __enter__(self):
416444 signal .signal (signal .SIGWINCH , self .sigwinch_handler )
417445 signal .signal (signal .SIGTSTP , self .sigtstp_handler )
418446
419- self .orig_import = builtins . __import__
447+ self .orig_meta_path = sys . meta_path
420448 if self .watcher :
421- # for reading modules if they fail to load
422- old_module_locations = {}
423- default_level = 0 if py3 else - 1
424-
425- @functools .wraps (self .orig_import )
426- def new_import (name , globals = {}, locals = {}, fromlist = [],
427- level = default_level ):
428- try :
429- m = self .orig_import (name , globals = globals , locals = locals ,
430- fromlist = fromlist , level = level )
431- except :
432- if name in old_module_locations :
433- loc = old_module_locations [name ]
434- self .watcher .track_module (loc )
435- raise
436- else :
437- if hasattr (m , "__file__" ):
438- old_module_locations [name ] = m .__file__
439- self .watcher .track_module (m .__file__ )
440- return m
441- builtins .__import__ = new_import
449+ sys .meta_path = [ImportFinder (self .watcher , self .orig_meta_path )]
442450
443451 sitefix .monkeypatch_quit ()
444452 return self
@@ -449,7 +457,7 @@ def __exit__(self, *args):
449457 sys .stderr = self .orig_stderr
450458 signal .signal (signal .SIGWINCH , self .orig_sigwinch_handler )
451459 signal .signal (signal .SIGTSTP , self .orig_sigtstp_handler )
452- builtins . __import__ = self .orig_import
460+ sys . meta_path = self .orig_meta_path
453461
454462 def sigwinch_handler (self , signum , frame ):
455463 old_rows , old_columns = self .height , self .width
0 commit comments