Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix deepreload on Python 3 #1625

Merged
merged 1 commit into from

2 participants

@takluyver
Owner

As described in #1622, it seems that imp.find_module() on Python 3.2 imports other modules, like io, while running. We don't want deepreload to catch those, so this restores the original import hook for find_module.

It's not terribly neat, but I don't currently see a better way of doing it. Ideally, we'd just delay installing our __import__ until after find_module had run, but our implementation of __import__ can itself call find_module, so this little dance seems to be necessary.

Closes gh-1622, and hopefully fixes a test failure we saw last night.

@fperez
Owner

Great, thanks! Looks clean, merging now. And yet again, the Panda shines and caught this! I'm really happy that you set that up, we're reaping the benefits every day.

@fperez fperez merged commit 0eb2ae9 into ipython:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 18, 2012
  1. @takluyver

    Fix deepreload on Python 3.

    takluyver authored
    Closes gh-1622
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 6 deletions.
  1. +24 −6 IPython/lib/deepreload.py
View
30 IPython/lib/deepreload.py
@@ -25,12 +25,24 @@
#*****************************************************************************
import __builtin__
+from contextlib import contextmanager
import imp
import sys
from types import ModuleType
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):
"""
parent, name = get_parent(globals, level)
@@ -167,7 +179,11 @@ def import_submodule(mod, subname, fullname):
return None
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:
return None
@@ -273,7 +289,11 @@ def deep_reload_hook(m):
path = getattr(parent, "__path__", None)
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:
modules_reloading.clear()
@@ -306,12 +326,10 @@ def reload(module, exclude=['sys', 'os.path', '__builtin__', '__main__']):
global found_now
for i in exclude:
found_now[i] = 1
- original_import = __builtin__.__import__
- __builtin__.__import__ = deep_import_hook
try:
- ret = deep_reload_hook(module)
+ with replace_import_hook(deep_import_hook):
+ ret = deep_reload_hook(module)
finally:
- __builtin__.__import__ = original_import
found_now = {}
return ret
Something went wrong with that request. Please try again.