Commits

Anonymous committed 934bf21

builtins: using BuiltinModule for __builtin__

Comments (0)

Files changed (3)

rope/base/builtins.py

 """This module trys to support builtin types and functions."""
-import __builtin__
 import inspect
 
 import rope.base.evaluate
 
 class BuiltinModule(pyobjects.AbstractModule):
 
-    def __init__(self, name):
+    def __init__(self, name, initial={}):
         super(BuiltinModule, self).__init__()
         self.name = name
+        self.initial = initial
         self.attributes = None
 
     def get_attributes(self):
         if self.module is None:
             return
         for name in dir(self.module):
+            if name in self.initial or name == 'None':
+                continue
             obj = getattr(self.module, name)
             if inspect.isclass(obj):
                 self.attributes[name] = BuiltinName(BuiltinClass(obj, {}))
             else:
                 self.attributes[name] = BuiltinName(BuiltinFunction(builtin=obj))
+        self.attributes.update(self.initial)
 
     _loaded = False
     _module = None
     return get_str()
 
 
-builtins = {
+_initial_builtins = {
     'list': BuiltinName(get_list_type()),
     'dict': BuiltinName(get_dict_type()),
     'tuple': BuiltinName(get_tuple_type()),
     'object': BuiltinName(BuiltinObject()),
     'type': BuiltinName(BuiltinType()),
     'iter': BuiltinName(BuiltinFunction(function=_iter_function, builtin=iter)),
-    'raw_input': BuiltinName(BuiltinFunction(function=_input_function, builtin=raw_input))}
+    'raw_input': BuiltinName(BuiltinFunction(function=_input_function, builtin=raw_input)),
+    }
 
-
-for name in dir(__builtin__):
-    if name not in builtins and name not in ['None']:
-        obj = getattr(__builtin__, name)
-        if inspect.isclass(obj):
-            builtins[name] = BuiltinName(BuiltinClass(obj, {}))
-        else:
-            builtins[name] = BuiltinName(BuiltinFunction(builtin=obj))
+builtins = BuiltinModule('__builtin__', _initial_builtins)

rope/base/pycore.py

         self.cache_observers = []
         self.classes_cache = _ClassesCache(self)
         self.module_cache = _ModuleCache(self)
+        self.extension_cache = _ExtensionCache(self)
         self.object_info = rope.base.oi.objectinfo.ObjectInfoManager(project)
         self._init_python_files()
         self._init_automatic_soi()
         return self.resource_to_pyobject(module)
 
     def _builtin_module(self, name):
-        extensions = self.project.get_prefs().get('extension_modules', [])
-        if name in extensions:
-            return builtins.BuiltinModule(name)
+        return self.extension_cache.get_pymodule(name)
 
     def get_relative_module(self, name, current_folder, level):
         module = self.find_relative_module(name, current_folder, level)
         return 'PyCore caches %d PyModules\n' % len(self.module_map)
 
 
+class _ExtensionCache(object):
+
+    def __init__(self, pycore):
+        self.extensions = {}
+        self.allowed = pycore.project.get_prefs().get('extension_modules', [])
+
+    def get_pymodule(self, name):
+        if name == '__builtin__':
+            return builtins.builtins
+        if name not in self.extensions and name in self.allowed:
+            self.extensions[name] = builtins.BuiltinModule(name)
+        return self.extensions.get(name)
+
+
 class _ClassesCache(object):
 
     def __init__(self, pycore):

rope/base/pyscopes.py

             self.scope_finder = _HoldingScopeFinder(self.pyobject)
         return self.scope_finder
 
-    def _get_builtin_names(self):
-        return rope.base.builtins.builtins
-
-    builtin_names = property(_get_builtin_names)
+    @property
+    def builtin_names(self):
+        return rope.base.builtins.builtins.get_attributes()
 
 
 class FunctionScope(Scope):