Commits

Anonymous committed 10b025e

Changing builtins.Str to use builtins._AttributeCollector

Comments (0)

Files changed (2)

docs/dev/workingon.txt

 Small Stories
 =============
 
-- Add `Core.add_extension('rope.ui.helpactions')`
+- Changing `builtins.Str` to use `builtins._AttributeCollector`?
 
+* Using `__setitem__` for slices in staticoi?
 * Better tests in `patchedasttest`
 * Remove ``docs/dev/workingon.txt``?
 * Add ``docs/dev/rope_py3k.txt``?

rope/base/builtins.py

 from rope.base import pynames, pyobjects, evaluate
 
 
-def _create_builtin_type_getter(cls):
-    def _get_builtin(*args):
-        if not hasattr(cls, '_generated'):
-            cls._generated = {}
-        if args not in cls._generated:
-            cls._generated[args] = cls(*args)
-        return cls._generated[args]
-    return _get_builtin
-
-def _create_builtin_getter(cls):
-    type_getter = _create_builtin_type_getter(cls)
-    def _get_builtin(*args):
-        return pyobjects.PyObject(type_getter(*args))
-    return _get_builtin
-
-
 class BuiltinClass(pyobjects.AbstractClass):
 
     def __init__(self, builtin, attributes):
             return self.builtin.__name__
 
 
+def _create_builtin_type_getter(cls):
+    def _get_builtin(*args):
+        if not hasattr(cls, '_generated'):
+            cls._generated = {}
+        if args not in cls._generated:
+            cls._generated[args] = cls(*args)
+        return cls._generated[args]
+    return _get_builtin
+
+def _create_builtin_getter(cls):
+    type_getter = _create_builtin_type_getter(cls)
+    def _get_builtin(*args):
+        return pyobjects.PyObject(type_getter(*args))
+    return _get_builtin
+
+
 class _CallContext(object):
 
     def __init__(self, argnames, args):
         self.attributes = {}
         self.type = type
 
-    def __call__(self, name, returned=None, function=None, argnames=['self']):
+    def __call__(self, name, returned=None, function=None,
+                 argnames=['self'], check_existence=True):
+        try:
+            builtin = getattr(self.type, name)
+        except AttributeError:
+            if check_existence:
+                raise
+            builtin=None
         self.attributes[name] = BuiltinName(
             BuiltinFunction(returned=returned, function=function,
-                            argnames=argnames,
-                            builtin=getattr(self.type, name)))
+                            argnames=argnames, builtin=builtin))
 
     def __setitem__(self, name, value):
         self.attributes[name] = value
 
     def __init__(self):
         self_object = pyobjects.PyObject(self)
-        attributes = {}
-        def add(name, returned=None, function=None):
-            builtin = getattr(str, name, None)
-            attributes[name] = BuiltinName(
-                BuiltinFunction(returned=returned, function=function, builtin=builtin))
-        add('__iter__', Iterator(self_object))
+        collector = _AttributeCollector(str)
+        collector('__iter__', Iterator(self_object), check_existence=False)
 
-        self_methods = ['__getitem__', '__getslice__', 'captialize', 'center',
+        self_methods = ['__getitem__', '__getslice__', 'capitalize', 'center',
                         'decode', 'encode', 'expandtabs', 'join', 'ljust',
                         'lower', 'lstrip', 'replace', 'rjust', 'rstrip', 'strip',
                         'swapcase', 'title', 'translate', 'upper', 'zfill']
         for method in self_methods:
-            add(method, self_object)
+            collector(method, self_object)
 
         for method in ['rsplit', 'split', 'splitlines']:
-            add(method, get_list(self_object))
+            collector(method, get_list(self_object))
 
         for method in ['count', 'endswith', 'find', 'index', 'isalnum',
                        'isalpha', 'isdigit', 'islower', 'isspace', 'istitle',
                        'isupper', 'rfind', 'rindex', 'startswith']:
-            add(method)
-        super(Str, self).__init__(str, attributes)
+            collector(method)
+        super(Str, self).__init__(str, collector.attributes)
 
     def get_doc(self):
         return str.__doc__