Commits

Anonymous committed 6d2d202

builtins: handling "builtin unknowns", such as sys.stdout

Comments (0)

Files changed (2)

rope/base/builtins.py

 
     def get_doc(self):
         if self.builtin:
-            return self.builtin.__doc__
+            return getattr(self.builtin, '__doc__', None)
 
     def get_name(self):
         if self.builtin:
-            return self.builtin.__name__
+            return getattr(self.builtin, '__name__', None)
 
     @property
     def parent(self):
         return self.argnames
 
 
+class BuiltinUnknown(_BuiltinElement, pyobjects.PyObject):
+
+    def __init__(self, builtin):
+        super(BuiltinUnknown, self).__init__(pyobjects.get_unknown())
+        self.builtin = builtin
+        self.type = pyobjects.get_unknown()
+
+    @utils.saveit
+    def get_attributes(self):
+        return _object_attributes(self.builtin, self)
+
+
 def _object_attributes(obj, parent):
     attributes = {}
     for name in dir(obj):
         elif inspect.isroutine(child):
             pyobject = BuiltinFunction(builtin=child, parent=parent)
         else:
-            pyobject = pyobjects.get_unknown()
+            pyobject = BuiltinUnknown(builtin=child)
         attributes[name] = BuiltinName(pyobject)
     return attributes
 

ropetest/contrib/codeassisttest.py

         result = self._assist(code)
         self.assert_completion_in_result('zip', 'builtin', result)
 
+    def test_builtin_instances(self):
+        # ``import_dynload_stdmods`` pref is disabled for test project.
+        # we need to have it enabled to make pycore._find_module()
+        # load ``sys`` module.
+        self.project.prefs['import_dynload_stdmods'] = True
+        code = 'from sys import stdout\nstdout.wr'
+        result = self._assist(code)
+        self.assert_completion_in_result('write', 'builtin', result)
+        self.assert_completion_in_result('writelines', 'builtin', result)
+
     def test_including_keywords(self):
         code = 'fo'
         result = self._assist(code)