Commits

Ali Gholami Rudi  committed 22623d3

Added builtin str
handling extract method and indented module region

  • Participants
  • Parent commits 62a40be

Comments (0)

Files changed (12)

File docs/dev/workingon.txt

 Small Stories
 =============
 
-- ``break`` and ``continue`` in extract method
-- Handling writes followed by reads in extract method
+- adding builtin `str` and `unicode`
+- handling extract method indented module region
+- difference between builtin types and their instances
 
-* Exploring module running
+* docs for builtin functions
+* exploring module running
+
+  * check the reason for speed difference; is it caused by fifo and
+    socket issues?
+  * check the amount useful information transfered
+  * possible techniques: compression
 
 
 Remaining Stories

File rope/base/builtins.py

 from rope.base import pynames
 
 
+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 List(pyobjects.PyObject):
     
     def __init__(self, holding=None):
     
     def get_attributes(self):
         return self.attributes
+    
+get_list = _create_builtin_getter(List)
+get_list_type = _create_builtin_type_getter(List)
 
 
 class Dict(pyobjects.PyObject):
         super(Dict, self).__init__(pyobjects.PyObject.get_base_type('Type'))
         self.keys = keys
         self.values = values
-        item = Tuple(self.keys, self.values)
+        item = get_tuple(self.keys, self.values)
         self.attributes = {
             '__getitem__': BuiltinName(BuiltinFunction(self.values)),
             '__iter__': BuiltinName(BuiltinFunction(Iterator(self.keys))),
     
     def get_attributes(self):
         return self.attributes
+    
+get_dict = _create_builtin_getter(Dict)
+get_dict_type = _create_builtin_type_getter(Dict)
 
 
 class Tuple(pyobjects.PyObject):
     def get_attributes(self):
         return self.attributes
 
+get_tuple = _create_builtin_getter(Tuple)
+get_tuple_type = _create_builtin_type_getter(Tuple)
+
 
 class Set(pyobjects.PyObject):
 
     
     def get_attributes(self):
         return self.attributes
+    
+get_set = _create_builtin_getter(Set)
+get_set_type = _create_builtin_type_getter(Set)
+
+
+class Str(pyobjects.PyObject):
+    
+    def __init__(self):
+        super(Str, self).__init__(pyobjects.PyObject.get_base_type('Type'))
+        self.attributes = {
+            '__getitem__': BuiltinName(BuiltinFunction(self)),
+            '__getslice__': BuiltinName(BuiltinFunction(self)),
+            '__iter__': BuiltinName(BuiltinFunction(Iterator(self))),
+            'captialize': BuiltinName(BuiltinFunction(self)),
+            'center': BuiltinName(BuiltinFunction(self)),
+            'count': BuiltinName(BuiltinFunction()),
+            'decode': BuiltinName(BuiltinFunction(self)),
+            'encode': BuiltinName(BuiltinFunction(self)),
+            'endswith': BuiltinName(BuiltinFunction()),
+            'expandtabs': BuiltinName(BuiltinFunction(self)),
+            'find': BuiltinName(BuiltinFunction()),
+            'index': BuiltinName(BuiltinFunction()),
+            'isalnum': BuiltinName(BuiltinFunction()),
+            'isalpha': BuiltinName(BuiltinFunction()),
+            'isdigit': BuiltinName(BuiltinFunction()),
+            'islower': BuiltinName(BuiltinFunction()),
+            'isspace': BuiltinName(BuiltinFunction()),
+            'istitle': BuiltinName(BuiltinFunction()),
+            'isupper': BuiltinName(BuiltinFunction()),
+            'join': BuiltinName(BuiltinFunction(self)),
+            'ljust': BuiltinName(BuiltinFunction(self)),
+            'lower': BuiltinName(BuiltinFunction(self)),
+            'lstrip': BuiltinName(BuiltinFunction(self)),
+            'replace': BuiltinName(BuiltinFunction(self)),
+            'rfind': BuiltinName(BuiltinFunction()),
+            'rindex': BuiltinName(BuiltinFunction()),
+            'rjust': BuiltinName(BuiltinFunction(self)),
+            'rsplit': BuiltinName(BuiltinFunction(self)),
+            'rstrip': BuiltinName(BuiltinFunction(self)),
+            'split': BuiltinName(BuiltinFunction(self)),
+            'splitlines': BuiltinName(BuiltinFunction(self)),
+            'startswith': BuiltinName(BuiltinFunction(self)),
+            'strip': BuiltinName(BuiltinFunction(self)),
+            'swapcase': BuiltinName(BuiltinFunction(self)),
+            'title': BuiltinName(BuiltinFunction(self)),
+            'translate': BuiltinName(BuiltinFunction(self)),
+            'upper': BuiltinName(BuiltinFunction(self)),
+            'zfill': BuiltinName(BuiltinFunction(self))}
+    
+    def get_attributes(self):
+        return self.attributes
+
+get_str = _create_builtin_getter(Str)
+get_str_type = _create_builtin_type_getter(Str)
 
 
 class BuiltinName(pynames.PyName):

File rope/base/evaluate.py

             self.result = rope.base.pynames.AssignedName(
                 pyobject=call_function.get_object()._get_returned_object())
     
+    def visitConst(self, node):
+        if isinstance(node.value, (str, unicode)):
+            self.result = rope.base.pynames.AssignedName(
+                pyobject=rope.base.builtins.get_str())
+            
     def visitAdd(self, node):
         pass
     
             keys = self._get_object_for_node(item[0])
             values = self._get_object_for_node(item[1])
         self.result = rope.base.pynames.AssignedName(
-            pyobject=rope.base.builtins.Dict(keys, values))
+            pyobject=rope.base.builtins.get_dict(keys, values))
     
     def visitFloorDiv(self, node):
         pass
         if node.nodes:
             holding = self._get_object_for_node(node.nodes[0])
         self.result = rope.base.pynames.AssignedName(
-            pyobject=rope.base.builtins.List(holding))
+            pyobject=rope.base.builtins.get_list(holding))
     
     def visitListComp(self, node):
         pass
         else:
             objects.append(self._get_object_for_node(node.nodes[0]))
         self.result = rope.base.pynames.AssignedName(
-            pyobject=rope.base.builtins.Tuple(*objects))
+            pyobject=rope.base.builtins.get_tuple(*objects))
 
     def _get_object_for_node(self, stmt):
         pyname = StatementEvaluator.get_statement_result(self.scope, stmt)

File rope/base/oi/dynamicoi.py

 import tempfile
 import threading
 
-import rope.base.pyobjects
+import rope
+from rope.base import pyobjects
 from rope.base import builtins
+from rope.base import pyscopes
 
 
 class DynamicObjectInference(object):
         self.data = data
 
     def to_pyobject(self, project):
+        if self.name == 'str':
+            return builtins.get_str()
         if self.name == 'list':
             holding = _ObjectPersistedForm.create_persistent_object(self.data[0])
-            return builtins.List(holding.to_pyobject(project))
+            return builtins.get_list(holding.to_pyobject(project))
         if self.name == 'dict':
             keys = _ObjectPersistedForm.create_persistent_object(self.data[0])
             values = _ObjectPersistedForm.create_persistent_object(self.data[1])
-            return builtins.Dict(keys.to_pyobject(project), values.to_pyobject(project))
+            return builtins.get_dict(keys.to_pyobject(project),
+                                     values.to_pyobject(project))
         if self.name == 'tuple':
             objects = []
             for holding in self.data:
                 objects.append(_ObjectPersistedForm.
                                create_persistent_object(holding).to_pyobject(project))
-            return builtins.Tuple(*objects)
+            return builtins.get_tuple(*objects)
         if self.name == 'set':
             holding = _ObjectPersistedForm.create_persistent_object(self.data[0])
-            return builtins.Set(holding.to_pyobject(project))
+            return builtins.get_set(holding)
         return None
 
 
         if self.name in module_scope.get_names():
             suspected_pyobject = module_scope.get_name(self.name).get_object()
         if suspected_pyobject is not None and \
-           suspected_pyobject.get_type() == rope.base.pyobjects.PyObject.get_base_type('Type'):
+           suspected_pyobject.get_type() == pyobjects.PyObject.get_base_type('Type'):
             if self.is_instance:
-                return rope.base.pyobjects.PyObject(suspected_pyobject)
+                return pyobjects.PyObject(suspected_pyobject)
             else:
                 return suspected_pyobject
         else:
         self.receiving_thread.start()
     
     def _receive_information(self):
+        #temp = open('/dev/shm/info', 'w')
         for data in self.receiver.receive_data():
             self.analyze_data(data)
+            #temp.write(str(data) + '\n')
+        #temp.close()
         for observer in self.observers:
             observer()
 

File rope/base/oi/objectinfer.py

             pass
 
     def _infer_assignment_object(self, assignment, pyobject):
-        if assignment.index is not None and isinstance(pyobject, builtins.Tuple):
-            holdings = pyobject.get_holding_objects()
+        if assignment.index is not None and isinstance(pyobject.get_type(),
+                                                       builtins.Tuple):
+            holdings = pyobject.get_type().get_holding_objects()
             return holdings[min(len(holdings) - 1, assignment.index)]
-        if assignment.index is not None and isinstance(pyobject, builtins.List):
-            return pyobject.holding
+        if assignment.index is not None and isinstance(pyobject.get_type(),
+                                                       builtins.List):
+            return pyobject.get_type().holding
         return pyobject
     
     def _infer_pyname_for_assign_node(self, assign_node, pymodule):

File rope/base/oi/runmod.py

             def global_trace(frame, event, arg):
                 # HACK: Ignoring out to in calls
                 # XXX: This might ignore some information
-                if self._is_code_inside_project(frame.f_code):
+                if self._is_an_interesting_call(frame):
                     return self.on_function_call
             sys.settrace(global_trace)
             threading.settrace(global_trace)
         def on_function_call(self, frame, event, arg):
             if event != 'return':
                 return
-            if not self._is_an_interesting_call(frame):
-                return
             args = []
             returned = ('unknown')
             code = frame.f_code
                 data = (self._object_to_persisted_form(frame.f_code),
                         args, returned)
                 self.sender.send_data(data)
-            except (TypeError, Error):
+            except (TypeError):
                 pass
             return self.on_function_call
         
         def _is_an_interesting_call(self, frame):
             #if frame.f_code.co_name in ['?', '<module>']:
             #    return False
+            #return not frame.f_back or not self._is_code_inside_project(frame.f_back.f_code)
+
             if not self._is_code_inside_project(frame.f_code) and \
-               (not frame.f_back or not self._is_code_inside_project(frame.f_back)):
+               (not frame.f_back or not self._is_code_inside_project(frame.f_back.f_code)):
                 return False
             return True
         
         def _is_code_inside_project(self, code):
             source = code.co_filename
-            #source = inspect.getsourcefile(code)
-            return source and source.endswith('.py') and \
+            return source and source.endswith('.py') and os.path.exists(source) and \
                    os.path.abspath(source).startswith(self.project_root)
     
         def _get_persisted_code(self, object_):
-            return ('function', os.path.abspath(object_.co_filename), object_.co_firstlineno)
+            source = object_.co_filename
+            if not os.path.exists(source):
+                raise TypeError('no source')
+            return ('function', os.path.abspath(source), object_.co_firstlineno)
     
         def _get_persisted_class(self, object_, type_):
             try:
                 return ('unknown')
     
         def _get_persisted_builtin(self, object_):
+            if isinstance(object_, (str, unicode)):
+                return ('builtin', 'str')
             if isinstance(object_, list):
                 holding = None
                 if len(object_) > 0:
                 return self._get_persisted_code(object_.im_func.func_code)
             if isinstance(object_, types.ModuleType):
                 return ('module', os.path.abspath(object_.__file__))
-            if isinstance(object_, (list, dict, tuple, set)):
+            if isinstance(object_, (str, unicode, list, dict, tuple, set)):
                 return self._get_persisted_builtin(object_)
             if isinstance(object_, (types.TypeType, types.ClassType)):
                 return self._get_persisted_class(object_, 'class')

File rope/base/project.py

               have a look at `rope.base.fscommands`
 
         """
-        self.root = project_root
+        self.root = os.path.expanduser(project_root)
         if not os.path.exists(self.root):
             os.mkdir(self.root)
         elif not os.path.isdir(self.root):

File rope/base/pyobjects.py

     def visitFor(self, node):
         visitor = _ForAssignVisitor(self, node.list)
         compiler.walk(node.assign, visitor)
-#        names = _AssignedNameCollector.get_assigned_names(node.assign)
-#        for name in names:
-#            self.names[name] = ForName(pynames._Assignment(node.list),
-#                                       module=self.get_module(),
-#                                       lineno=node.lineno)
         compiler.walk(node.body, self)
         
     def visitImport(self, node):

File rope/base/pyscopes.py

         return self.scope_finder
 
     def _get_builtin_names(self):
-        if GlobalScope._builtin_names is None:
-            builtins = {
-                'list': rope.base.builtins.BuiltinName(rope.base.builtins.List()),
-                'dict': rope.base.builtins.BuiltinName(rope.base.builtins.Dict()),
-                'tuple': rope.base.builtins.BuiltinName(rope.base.builtins.Tuple()),
-                'set': rope.base.builtins.BuiltinName(rope.base.builtins.Set())}
-            self._builtin_names = builtins
-        return self._builtin_names
+        return get_builtin_names()
     
     _builtin_names = None
     builtin_names = property(_get_builtin_names)
-    
+
+def get_builtin_names():
+    if GlobalScope._builtin_names is None:
+        builtins = {
+            'list': rope.base.builtins.BuiltinName(rope.base.builtins.get_list_type()),
+            'dict': rope.base.builtins.BuiltinName(rope.base.builtins.get_dict_type()),
+            'tuple': rope.base.builtins.BuiltinName(rope.base.builtins.get_tuple_type()),
+            'set': rope.base.builtins.BuiltinName(rope.base.builtins.get_set_type()),
+            'str': rope.base.builtins.BuiltinName(rope.base.builtins.get_str_type()),
+            'unicode': rope.base.builtins.BuiltinName(rope.base.builtins.get_str_type())}
+        GlobalScope._builtin_names = builtins
+    return GlobalScope._builtin_names
+
 
 class FunctionScope(Scope):
     

File rope/refactor/extract.py

         result.append(self.source_code[self.parts.scope[1]:])
         return ''.join(result)
 
-    def _get_scope_indents(self):
-        if self._is_global():
-            return 0
-        else:
-            return self._get_indents(self.holding_scope.get_start()) + 4
-    
     def _get_function_indents(self):
         if self._is_global():
-            return 4
+            return self.first_line_indents
         else:
-            return self._get_scope_indents()
+            return self._get_indents(self.holding_scope.get_start())
     
     def _get_function_definition(self):
         args = self._find_function_arguments()
         function_indents = self._get_function_indents()
         result = []
         result.append('%sdef %s:\n' %
-                      (' ' * self._get_indents(self.holding_scope.get_start()),
+                      (' ' * function_indents,
                        self._get_function_signature(args)))
         unindented_body = self._get_unindented_function_body(returns)
-        function_body = sourceutils.indent_lines(unindented_body, function_indents)
+        function_body = sourceutils.indent_lines(unindented_body, function_indents + 4)
         result.append(function_body)
         definition = ''.join(result)
         

File ropetest/objectinfertest.py

 import unittest
 
 from rope.base.project import Project
+import rope.base.builtins
 from ropetest import testutils
 
 class ObjectInferTest(unittest.TestCase):
         self.assertEquals(c1_class, a_var.get_type())
         self.assertEquals(c2_class, b_var.get_type())
 
-    def test_sets_and_dynamicoi(self):
+    def test_strs_and_dynamicoi(self):
         mod = self.pycore.create_module(self.project.get_root_folder(), 'mod')
-        code = 'class C(object):\n    pass\n' \
-               'def a_func(arg):\n    return arg\n' \
-               'a_var = a_func(set([C()])).pop()\n'
+        code = 'def a_func(arg):\n    return arg\n' \
+               'a_var = a_func("hey")\n'
         mod.write(code)
         self.pycore.run_module(mod).wait_process()
         pymod = self.pycore.resource_to_pyobject(mod)
-        c_class = pymod.get_attribute('C').get_object()
         a_var = pymod.get_attribute('a_var').get_object()
-        self.assertEquals(c_class, a_var.get_type())
+        self.assertTrue(isinstance(a_var.get_type(), rope.base.builtins.Str))
 
 
 class CartesianProductDynamicOITest(unittest.TestCase):

File ropetest/refactor/extracttest.py

         expected = "print 'one'\n\ndef new_func():\n    print 'two'\n\nnew_func()\nprint 'three'\n"
         self.assertEquals(expected, refactored)
 
+    def test_extract_global_function_inside_ifs(self):
+        code = 'if True:\n    a = 10\n'
+        start, end = self._convert_line_range_to_offset(code, 2, 2)
+        refactored = self.do_extract_method(code, start, end, 'new_func')
+        expected = 'if True:\n\n    def new_func():\n        a = 10\n\n    new_func()\n'
+        self.assertEquals(expected, refactored)
+
     def test_extract_function_while_inner_function_reads(self):
         code = "def a_func():\n    a_var = 10\n    " \
                "def inner_func():\n        print a_var\n    return inner_func\n"