Anonymous avatar Anonymous committed bd4496e

autoimport: handling syntax errors and module not found

Comments (0)

Files changed (2)

rope/contrib/autoimport.py

 from rope.refactor import importutils
+from rope.base import exceptions
+
 
 class AutoImport(object):
 
         return result
 
     def update_resource(self, resource):
-        # XXX: what if there are syntax errors
-        pymodule = self.project.pycore.resource_to_pyobject(resource)
-        modname = importutils.get_module_name(self.project.pycore,
-                                              resource)
-        self._add_names(pymodule, modname)
+        try:
+            pymodule = self.project.pycore.resource_to_pyobject(resource)
+            modname = importutils.get_module_name(self.project.pycore,
+                                                  resource)
+            self._add_names(pymodule, modname)
+        except exceptions.ModuleSyntaxError:
+            pass
 
     def _add_names(self, pymodule, modname):
         # XXX: exclude imported names
         self.names[modname] = globals
 
     def update_module(self, modname):
-        # XXX: what if module cannot be found?
-        pymodule = self.project.pycore.get_module(modname)
-        self._add_names(pymodule, modname)
+        try:
+            pymodule = self.project.pycore.get_module(modname)
+            self._add_names(pymodule, modname)
+        except exceptions.ModuleNotFoundError:
+            pass

ropetest/contrib/autoimporttest.py

         self.mod1 = testutils.create_module(self.project, 'mod1')
         self.pkg = testutils.create_package(self.project, 'pkg')
         self.mod2 = testutils.create_module(self.project, 'mod2', self.pkg)
+        self.importer = autoimport.AutoImport(self.project)
 
     def tearDown(self):
         testutils.remove_project(self.project)
         super(AutoImportTest, self).tearDown()
 
     def test_simple_case(self):
-        importer = autoimport.AutoImport(self.project)
-        self.assertEquals([], importer.get_imports('A'))
+        self.assertEquals([], self.importer.get_imports('A'))
 
     def test_update_resource(self):
-        importer = autoimport.AutoImport(self.project)
-        self.mod1.write('myvar = None')
-        importer.update_resource(self.mod1)
+        self.mod1.write('myvar = None\n')
+        self.importer.update_resource(self.mod1)
         self.assertEquals([('myvar', 'mod1')],
-                          importer.get_imports('myva'))
+                          self.importer.get_imports('myva'))
 
     def test_update_module(self):
-        importer = autoimport.AutoImport(self.project)
         self.mod1.write('myvar = None')
-        importer.update_module('mod1')
+        self.importer.update_module('mod1')
         self.assertEquals([('myvar', 'mod1')],
-                          importer.get_imports('myva'))
+                          self.importer.get_imports('myva'))
+
+    def test_update_non_existent_module(self):
+        self.importer.update_module('does_not_exists_this')
+        self.assertEquals([], self.importer.get_imports('myva'))
+
+    def test_module_with_syntax_errors(self):
+        self.mod1.write('this is a syntax error\n')
+        self.importer.update_resource(self.mod1)
+        self.assertEquals([], self.importer.get_imports('myva'))
 
 
 if __name__ == '__main__':
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.