Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deepreload on Python 3 #1625

Merged
merged 1 commit into from Apr 18, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 24 additions & 6 deletions IPython/lib/deepreload.py
Expand Up @@ -25,12 +25,24 @@
#***************************************************************************** #*****************************************************************************


import __builtin__ import __builtin__
from contextlib import contextmanager
import imp import imp
import sys import sys


from types import ModuleType from types import ModuleType
from warnings import warn from warnings import warn


original_import = __builtin__.__import__

@contextmanager
def replace_import_hook(new_import):
saved_import = __builtin__.__import__
__builtin__.__import__ = new_import
try:
yield
finally:
__builtin__.__import__ = saved_import

def get_parent(globals, level): def get_parent(globals, level):
""" """
parent, name = get_parent(globals, level) parent, name = get_parent(globals, level)
Expand Down Expand Up @@ -167,7 +179,11 @@ def import_submodule(mod, subname, fullname):
return None return None


try: try:
fp, filename, stuff = imp.find_module(subname, path) # This appears to be necessary on Python 3, because imp.find_module()
# tries to import standard libraries (like io) itself, and we don't
# want them to be processed by our deep_import_hook.
with replace_import_hook(original_import):
fp, filename, stuff = imp.find_module(subname, path)
except ImportError: except ImportError:
return None return None


Expand Down Expand Up @@ -273,7 +289,11 @@ def deep_reload_hook(m):
path = getattr(parent, "__path__", None) path = getattr(parent, "__path__", None)


try: try:
fp, filename, stuff = imp.find_module(subname, path) # This appears to be necessary on Python 3, because imp.find_module()
# tries to import standard libraries (like io) itself, and we don't
# want them to be processed by our deep_import_hook.
with replace_import_hook(original_import):
fp, filename, stuff = imp.find_module(subname, path)
finally: finally:
modules_reloading.clear() modules_reloading.clear()


Expand Down Expand Up @@ -306,12 +326,10 @@ def reload(module, exclude=['sys', 'os.path', '__builtin__', '__main__']):
global found_now global found_now
for i in exclude: for i in exclude:
found_now[i] = 1 found_now[i] = 1
original_import = __builtin__.__import__
__builtin__.__import__ = deep_import_hook
try: try:
ret = deep_reload_hook(module) with replace_import_hook(deep_import_hook):
ret = deep_reload_hook(module)
finally: finally:
__builtin__.__import__ = original_import
found_now = {} found_now = {}
return ret return ret


Expand Down