Commits

Anonymous committed 7bd0e1a

autoimport: handling builtin modules

Comments (0)

Files changed (2)

rope/contrib/autoimport.py

 import re
 
-from rope.base import exceptions, pynames, resourceobserver, taskhandle
+from rope.base import (exceptions, pynames, resourceobserver,
+                       taskhandle, pyobjects, builtins)
 from rope.refactor import importutils
 
 
         if underlined is None:
             underlined = self.underlined
         globals = []
-        for name, pyname in pymodule._get_structural_attributes().items():
+        if isinstance(pymodule, pyobjects.PyDefinedObject):
+            attributes = pymodule._get_structural_attributes()
+        else:
+            attributes = pymodule.get_attributes()
+        for name, pyname in attributes.items():
             if not underlined and name.startswith('_'):
                 continue
             if isinstance(pyname, (pynames.AssignedName, pynames.DefinedName)):
                 globals.append(name)
+            if isinstance(pymodule, builtins.BuiltinModule):
+                globals.append(name)
         self.names[modname] = globals
 
     def _write(self):

ropetest/contrib/autoimporttest.py

         self.assertEquals(set([(self.mod1, 1), (self.mod2, 2)]),
                           set(self.importer.get_name_locations('myvar')))
 
+    def test_handling_builtin_modules(self):
+        self.importer.update_module('sys')
+        self.assertTrue('sys' in self.importer.get_modules('exit'))
+
 
 class AutoImportObservingTest(unittest.TestCase):