Commits

Anonymous committed 614375d

from ..mod in ScopeNameFinder

Comments (0)

Files changed (5)

 ===========
 
 
+- Relative imports : August 23, 2006
+
+
 - Read ``__init__.py`` of packages : August 23, 2006
 
 
 * Subversion support using pysvn
 
 
-* Relative imports
-
-
 Remaining Stories
 -----------------
 * Migrating to sourceforge SVN before 0.3 release

docs/workingon.txt

 - New style imports
 - ``from ..mod import name``
 - Add `only_for_25` test decorator
-
-* Finding ``from mod`` occurances in refactoring
+- Finding ``from mod`` occurances in refactoring
 
 
 Remaining Stories

rope/codeanalyze.py

             return holding_scope.parent.get_names()[name]
         if self.word_finder.is_from_statement_module(offset):
             module = self.word_finder.get_primary_at(offset)
-            module_pyobject = self.module_scope.pycore.get_module(module)
+            module_pyobject = self._find_module(module)
             module_pyname = rope.pycore.PyName(module_pyobject, False, 
                                                module=module_pyobject.get_module(),
                                                lineno=1)
         result = self.get_pyname_in_scope(holding_scope, name)
         return result
     
+    def _find_module(self, module_name):
+        current_folder = None
+        if self.module_scope.pyobject.get_resource():
+            current_folder = self.module_scope.pyobject.get_resource().get_parent()
+        if module_name.startswith('.'):
+            dot_count = 0
+            for c in module_name:
+                if c == '.':
+                    dot_count += 1
+                else:
+                    break
+            return self.module_scope.pycore.get_relative_module(module_name[dot_count:],
+                                                                current_folder,
+                                                                dot_count)
+        else:
+            return self.module_scope.pycore.get_module(module_name, current_folder)
+    
     def get_pyname_in_scope(self, holding_scope, name):
         ast = compiler.parse(name)
         result = StatementEvaluator.get_statement_result(holding_scope, ast)

ropetest/codeanalyzetest.py

         found_pyname = name_finder.get_pyname_at(code.index('mod') + 1)
         self.assertEquals(mod_pyobject, found_pyname.get_object())
 
+    @testutils.run_only_for_25
+    def test_relative_modules_after_from_statements(self):
+        pkg1 = self.pycore.create_package(self.project.get_root_folder(), 'pkg1')
+        pkg2 = self.pycore.create_package(pkg1, 'pkg2')
+        mod1 = self.pycore.create_module(pkg1, 'mod1')
+        mod2 = self.pycore.create_module(pkg2, 'mod2')
+        mod1.write('def a_func():\n    pass\n')
+        code = 'from ..mod1 import a_func\n'
+        mod2.write(code)
+        mod2_scope = self.pycore.resource_to_pyobject(mod2).get_scope()
+        name_finder = ScopeNameFinder(code, mod2_scope)
+        mod1_pyobject = self.pycore.resource_to_pyobject(mod1)
+        found_pyname = name_finder.get_pyname_at(code.index('mod1') + 1)
+        self.assertEquals(mod1_pyobject, found_pyname.get_object())
+
 
 def suite():
     result = unittest.TestSuite()
 if __name__ == '__main__':
     unittest.main()
 
-