Show More
@@ -25,12 +25,24 b' re-implementation of hierarchical module import.' | |||
|
25 | 25 | #***************************************************************************** |
|
26 | 26 | |
|
27 | 27 | import __builtin__ |
|
28 | from contextlib import contextmanager | |
|
28 | 29 | import imp |
|
29 | 30 | import sys |
|
30 | 31 | |
|
31 | 32 | from types import ModuleType |
|
32 | 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 | 46 | def get_parent(globals, level): |
|
35 | 47 | """ |
|
36 | 48 | parent, name = get_parent(globals, level) |
@@ -167,6 +179,10 b' def import_submodule(mod, subname, fullname):' | |||
|
167 | 179 | return None |
|
168 | 180 | |
|
169 | 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 | 186 |
fp, filename, stuff |
|
171 | 187 | except ImportError: |
|
172 | 188 | return None |
@@ -273,6 +289,10 b' def deep_reload_hook(m):' | |||
|
273 | 289 | path = getattr(parent, "__path__", None) |
|
274 | 290 | |
|
275 | 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 | 296 | fp, filename, stuff = imp.find_module(subname, path) |
|
277 | 297 | finally: |
|
278 | 298 | modules_reloading.clear() |
@@ -306,12 +326,10 b" def reload(module, exclude=['sys', 'os.path', '__builtin__', '__main__']):" | |||
|
306 | 326 | global found_now |
|
307 | 327 | for i in exclude: |
|
308 | 328 | found_now[i] = 1 |
|
309 | original_import = __builtin__.__import__ | |
|
310 | __builtin__.__import__ = deep_import_hook | |
|
311 | 329 | try: |
|
330 | with replace_import_hook(deep_import_hook): | |
|
312 | 331 | ret = deep_reload_hook(module) |
|
313 | 332 | finally: |
|
314 | __builtin__.__import__ = original_import | |
|
315 | 333 | found_now = {} |
|
316 | 334 | return ret |
|
317 | 335 |
General Comments 0
You need to be logged in to leave comments.
Login now