Correctly recurse os_traits packages

When walk_packages is called, unless a prefix is provided, it is
easy to escape the os_traits package and start walking other
packages. If there is a top-level 'tests' package somewhere in the
python path, this can then lead to importing all sorts of things,
some which rely on the environment having certain characteristics
and fail the import without that, some which will change the
environment.

Neither of these things are good. Nor do we want to be walking
packages all over the place. By providing a prefix we stay within
the os_traits package as desired.

Change-Id: Icde41191e39317ff5a031fff9ece0f05f1e0b4bc
Closes-Bug: #1694792
This commit is contained in:
Chris Dent 2017-05-31 18:56:44 +00:00
parent fbbe466c77
commit 789ce955ea
1 changed files with 6 additions and 6 deletions

View File

@ -49,16 +49,16 @@ def import_submodules(package, recursive=True):
"""
if isinstance(package, str):
package = importlib.import_module(package)
for loader, name, is_pkg in pkgutil.walk_packages(package.__path__):
full_name = package.__name__ + '.' + name
for loader, name, is_pkg in pkgutil.walk_packages(
package.__path__, package.__name__ + '.'):
test_dir = "%s.tests" % this_name
if test_dir in full_name:
if test_dir in name:
continue
imported = importlib.import_module(full_name)
imported = importlib.import_module(name)
for prop in getattr(imported, "TRAITS", []):
symbolize(full_name, prop)
symbolize(name, prop)
if recursive and is_pkg:
import_submodules(full_name)
import_submodules(name)
# This is where the names defined in submodules are imported