##// END OF EJS Templates
Fix deepreload on Python 3....
Thomas Kluyver -
Show More
@@ -25,12 +25,24 b' re-implementation of hierarchical module import.'
25 #*****************************************************************************
25 #*****************************************************************************
26
26
27 import __builtin__
27 import __builtin__
28 from contextlib import contextmanager
28 import imp
29 import imp
29 import sys
30 import sys
30
31
31 from types import ModuleType
32 from types import ModuleType
32 from warnings import warn
33 from warnings import warn
33
34
35 original_import = __builtin__.__import__
36
37 @contextmanager
38 def replace_import_hook(new_import):
39 saved_import = __builtin__.__import__
40 __builtin__.__import__ = new_import
41 try:
42 yield
43 finally:
44 __builtin__.__import__ = saved_import
45
34 def get_parent(globals, level):
46 def get_parent(globals, level):
35 """
47 """
36 parent, name = get_parent(globals, level)
48 parent, name = get_parent(globals, level)
@@ -167,6 +179,10 b' def import_submodule(mod, subname, fullname):'
167 return None
179 return None
168
180
169 try:
181 try:
182 # This appears to be necessary on Python 3, because imp.find_module()
183 # tries to import standard libraries (like io) itself, and we don't
184 # want them to be processed by our deep_import_hook.
185 with replace_import_hook(original_import):
170 fp, filename, stuff = imp.find_module(subname, path)
186 fp, filename, stuff = imp.find_module(subname, path)
171 except ImportError:
187 except ImportError:
172 return None
188 return None
@@ -273,6 +289,10 b' def deep_reload_hook(m):'
273 path = getattr(parent, "__path__", None)
289 path = getattr(parent, "__path__", None)
274
290
275 try:
291 try:
292 # This appears to be necessary on Python 3, because imp.find_module()
293 # tries to import standard libraries (like io) itself, and we don't
294 # want them to be processed by our deep_import_hook.
295 with replace_import_hook(original_import):
276 fp, filename, stuff = imp.find_module(subname, path)
296 fp, filename, stuff = imp.find_module(subname, path)
277 finally:
297 finally:
278 modules_reloading.clear()
298 modules_reloading.clear()
@@ -306,12 +326,10 b" def reload(module, exclude=['sys', 'os.path', '__builtin__', '__main__']):"
306 global found_now
326 global found_now
307 for i in exclude:
327 for i in exclude:
308 found_now[i] = 1
328 found_now[i] = 1
309 original_import = __builtin__.__import__
310 __builtin__.__import__ = deep_import_hook
311 try:
329 try:
330 with replace_import_hook(deep_import_hook):
312 ret = deep_reload_hook(module)
331 ret = deep_reload_hook(module)
313 finally:
332 finally:
314 __builtin__.__import__ = original_import
315 found_now = {}
333 found_now = {}
316 return ret
334 return ret
317
335
General Comments 0
You need to be logged in to leave comments. Login now