Fixing broken while loop in imports.py

This commit is backporting commit 64ef5bf5. It could not be
cherry-picked, as the unit test code had lot more dependent code to be
pulled in.

Issue: flake8 occasionally encounters "H302  import only modules."
error even though the python module exists at the path and python
shell is able to import the same.

Root-cause: The while loop in is_module_for_sure() had a bug, where
iterating variable, 'mod_name', was getting derived from the same
input, 'mod', every time. It should have led to an infinite loop
otherwise. But, an ImportError takes it out of the loop. Most of
the cases, this is not an issue, if as part of the exception handler,
built-in __import__() finds the module. The evidence of the issue is
the availability of import statements suffixed with "# noqa" to
work-around the bug.

Fixing the while loop causes the logic to flow in the original path of
the code to use 'imp.find_module()'

Change-Id: I421a66242121b987b7c8c1568394478514073b87
This commit is contained in:
Surojit Pathak 2014-12-03 18:12:06 +00:00
parent b45ac44139
commit 3af414c0c9
1 changed files with 1 additions and 1 deletions

View File

@ -73,7 +73,7 @@ def hacking_import_rules(logical_line, physical_line, filename, noqa):
try:
mod_name = mod
while '.' in mod_name:
pack_name, _sep, mod_name = mod.partition('.')
pack_name, _sep, mod_name = mod_name.partition('.')
f, p, d = imp.find_module(pack_name, search_path)
search_path = [p]
imp.find_module(mod_name, search_path)