Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 08976ea

Browse files
committed
Fix dependency tracking for package imports.
We were previously relying on direct name-based `sys.modules` lookups to resolve module references. This was an attempt to work around __import__()'s behavior of returning the package object for an import in the form `import package.module` (instead of the module itself). Unfortunately, the import names did not always match the module names, causing some of this lookups to fail. This resulted in an incomplete dependency map. We now manually walk through the the imported module hierarchy to more definitively locate the desired module object. Closes jparise#15
1 parent c5f1840 commit 08976ea

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

reloader.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,20 @@ def _import(name, globals=None, locals=None, fromlist=None, level=_default_level
154154
parent = _parent
155155
_parent = name
156156

157-
# Perform the actual import using the base import function.
157+
# Perform the actual import work using the base import function.
158158
base = _baseimport(name, globals, locals, fromlist, level)
159159

160-
# If this is a nested import for a reloadable (source-based) module, we
161-
# append ourself to our parent's dependency list.
162-
if parent is not None:
163-
# We get the module directly from sys.modules because the import
164-
# function only returns the top-level module reference for a nested
160+
if base is not None and parent is not None:
161+
# We manually walk through the imported hierarchy because the import
162+
# function only returns the top-level package reference for a nested
165163
# import statement (e.g. 'package' for `import package.module`).
166-
m = sys.modules.get(name, None)
167-
if m is not None and hasattr(m, '__file__'):
164+
m = base
165+
for component in name.split('.')[1:]:
166+
m = getattr(m, component)
167+
168+
# If this is a nested import for a reloadable (source-based) module,
169+
# we append ourself to our parent's dependency list.
170+
if hasattr(m, '__file__'):
168171
l = _dependencies.setdefault(parent, [])
169172
l.append(m)
170173

0 commit comments

Comments
 (0)