Commits

Anonymous committed 8bfc9aa

builtins: handling builtin submodules

  • Participants
  • Parent commits 0739e7b

Comments (0)

Files changed (3)

File rope/base/builtins.py

 
 class BuiltinModule(pyobjects.AbstractModule):
 
-    def __init__(self, name, initial={}):
+    def __init__(self, name, modnames=[], initial={}):
         super(BuiltinModule, self).__init__()
         self.name = name
         self.initial = initial
+        self.modnames = modnames
 
     parent = None
 
             return self.module.__doc__
 
     def get_name(self):
-        return self.name
+        return self.name.split('.')[-1]
 
     @property
     @utils.cacheit
     def attributes(self):
         result = _object_attributes(self.module, self)
         result.update(self.initial)
+        for modname in self.submodules:
+            name = modname.split('.')[-1]
+            result[name] = BuiltinModule(name, self.submodules)
         return result
 
     @property
         except ImportError:
             return
 
+    @property
+    @utils.cacheit
+    def submodules(self):
+        modnames = []
+        for modname in self.modnames:
+            prefix = self.name + '.'
+            if modname.startswith(prefix):
+                modnames.append(modname[len(prefix):])
+        return modnames
+
 
 class _BuiltinElement(object):
 
     'raw_input': BuiltinName(BuiltinFunction(function=_input_function, builtin=raw_input)),
     }
 
-builtins = BuiltinModule('__builtin__', _initial_builtins)
+builtins = BuiltinModule('__builtin__', initial=_initial_builtins)

File rope/base/pycore.py

         if name == '__builtin__':
             return builtins.builtins
         if name not in self.extensions and name in self.allowed:
-            self.extensions[name] = builtins.BuiltinModule(name)
+            self.extensions[name] = builtins.BuiltinModule(name, self.allowed)
         return self.extensions.get(name)
 
     @property

File ropetest/builtinstest.py

     def setUp(self):
         super(BuiltinModulesTest, self).setUp()
         self.project = testutils.sample_project(
-            extension_modules=['time', 'invalid_module'])
+            extension_modules=['time', 'invalid', 'invalid.sub'])
         self.pycore = self.project.pycore
         self.mod = testutils.create_module(self.project, 'mod')
 
         self.assertTrue('rename' not in pymod['os'].get_object())
 
     def test_nonexistent_modules(self):
-        self.mod.write('import invalid_module')
+        self.mod.write('import invalid')
         pymod = self.pycore.resource_to_pyobject(self.mod)
-        pymod['invalid_module'].get_object()
+        pymod['invalid'].get_object()
+
+    def test_nonexistent_modules(self):
+        self.mod.write('import invalid\nimport invalid.sub')
+        pymod = self.pycore.resource_to_pyobject(self.mod)
+        invalid = pymod['invalid'].get_object()
+        self.assertTrue('sub' in invalid)
 
 
 def suite():