ipython / ipython/ipython

dreload can't handle renamed modules

Open
#1,660 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
16.8k
Forks
4.5k
Avg merge
1d 2h
Merged PRs (30d)
6

Description

Consider the following situation (similar to `os.path`), whereby we manually insert an entry in `sys.modules`.

```
--- A.py ---
import sys
import B
sys.modules['C'] = B
del B
import C

--- B.py ---
pass
```

The current `dreload` cannot handle such a situation. In particular, `dreload(A)` fails because it cannot find the module `C` (as it doesn't actually exist). In this situation, where `C` is in `sys.modules`, should we just return `sys.modules['C']` without complaint?

```
In [6]: dreload(A)
Reloading B
Reloading C
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/home/bfroehle/tm/ in ()
----> 1 dreload(A)

/home/bfroehle/src/ipython/IPython/lib/deepreload.pyc in reload(module, exclude)
329 try:
330 with replace_import_hook(deep_import_hook):
--> 331 ret = deep_reload_hook(module)
332 finally:
333 found_now = {}

/home/bfroehle/src/ipython/IPython/lib/deepreload.pyc in deep_reload_hook(m)
299
300 try:
--> 301 newm = imp.load_module(name, fp, filename, stuff)
302 except:
303 # load_module probably removed name from modules because of

/home/bfroehle/tm/A.pyc in ()
3 sys.modules['C'] = B
4 del B
----> 5 import C

/home/bfroehle/src/ipython/IPython/lib/deepreload.pyc in deep_import_hook(name, globals, locals, fromlist, level)
240 parent, buf = get_parent(globals, level)
241
--> 242 head, name, buf = load_next(parent, None if level < 0 else parent, name, buf)
243
244 tail = head

/home/bfroehle/src/ipython/IPython/lib/deepreload.pyc in load_next(mod, altmod, name, buf)
151
152 if result is None:
--> 153 raise ImportError("No module named %.200s" % name)
154
155 return result, next, buf

ImportError: No module named C
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.