From 3af414c0c9e5c64cb764c0e131a876b22a0553cd Mon Sep 17 00:00:00 2001 From: Surojit Pathak Date: Wed, 3 Dec 2014 18:12:06 +0000 Subject: [PATCH] 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 --- hacking/checks/imports.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hacking/checks/imports.py b/hacking/checks/imports.py index 729f311..d5f7c8b 100644 --- a/hacking/checks/imports.py +++ b/hacking/checks/imports.py @@ -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)