Ronald Oussoren avatar Ronald Oussoren committed e1eaf96

Fix the way the __init__ module for packages is looked up (issue #12)

Comments (0)

Files changed (5)

doc/changelog.rst

 
   Reported by Andrew Barnert.
 
+* Issue #12: modulegraph would sometimes find the wrong package *__init__*
+  module due to using the wrong search method. One easy way to reproduce the
+  problem was to have a toplevel module named *__init__*.
+
+  Reported by Kentzo.
+
 0.10
 ----
 

modulegraph/modulegraph.py

 
         try:
             self.msg(2, "find __init__ for %s"%(m.packagepath,))
-            fp, buf, stuff = self.find_module("__init__", m.packagepath)
+            fp, buf, stuff = self.find_module("__init__", m.packagepath, parent=m)
         except ImportError:
             pass
 

modulegraph_tests/test_imports.py

         node = self.mf.findNode('pkg.core.listenerimpl')
         self.assertIsInstance(node, modulegraph.SourceModule)
 
+class TestRegression5 (unittest.TestCase):
+    if not hasattr(unittest.TestCase, 'assertIsInstance'):
+        def assertIsInstance(self, value, types):
+            if not isinstance(value, types):
+                self.fail("%r is not an instance of %r"%(value, types))
+
+    def setUp(self):
+        root = os.path.join(
+                os.path.dirname(os.path.abspath(__file__)),
+                'testpkg-regr5')
+        self.mf = modulegraph.ModuleGraph(path=[ root ] + sys.path)
+        self.mf.run_script(os.path.join(root, 'script.py'))
+
+    def testRegr1(self):
+        node = self.mf.findNode('distutils')
+        self.assertIsInstance(node, modulegraph.Package)
+        self.assertIn('distutils/__init__', node.filename)
+
 if __name__ == "__main__":
     unittest.main()

modulegraph_tests/testpkg-regr5/__init__.py

+""" A dummy __init__ file """

modulegraph_tests/testpkg-regr5/script.py

+import __init__
+
+from modulegraph.find_modules import find_needed_modules
+import distutils
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.