Anonymous avatar Anonymous committed db14f84

Handling else suites for 'for' statements

Comments (0)

Files changed (7)

docs/dev/workingon.txt

 Small Stories
 =============
 
-* Moving globals to a module with only docstrings
+- Handling else suites for for statements
+
 * Some of the things in `codeanalyze.WordFinder` can be achieved using
   asts; make a clone and do them there
 * Assignment to parameters in inline method

rope/base/codeanalyze.py

             # parenthesizing for handling cases like 'a_var.\nattr'
             node = ast.parse('(%s)' % name)
         except SyntaxError:
-            raise BadIdentifierError('Not a python identifier selected.')
+            raise BadIdentifierError('Not a resolvable python identifier selected.')
         return evaluate.get_primary_and_result(holding_scope, node)
 
 
         for current_line_number in range(get_block_start(self.lines,
                                                          self.lineno),
                                          self.lineno + 1):
-            if not self.explicit_continuation and self.open_count == 0 and self.in_string == '':
+            if not self.explicit_continuation and \
+               self.open_count == 0 and self.in_string == '':
                 last_statement = current_line_number
             self._analyze_line(current_line_number)
         last_indents = self.get_line_indents(last_statement)

rope/base/pyobjects.py

             node.target, node.iter, evaluation='.__iter__().next()',
             lineno=node.lineno, module=self.get_module())
         self.names.update(names)
-        for child in node.body:
+        for child in node.body + node.orelse:
             ast.walk(child, self)
 
     def _With(self, node):

rope/refactor/importutils/module_imports.py

                 break
         else:
             all_imports = self.get_import_statements()
-            last_line = self._get_new_import_lineno()
-            blank_lines = self._get_new_import_blanks()
+            lineno = self._get_new_import_lineno()
+            blanks = self._get_new_import_blanks()
             all_imports.append(importinfo.ImportStatement(
-                               import_info, last_line, last_line,
-                               blank_lines=blank_lines))
+                               import_info, lineno, lineno,
+                               blank_lines=blanks))
+
+    def _get_new_import_blanks(self):
+        if self.get_import_statements():
+            return 0
+        return 2
 
     def _get_new_import_lineno(self):
         imports = self.get_import_statements()
             return imports[-1].end_line
         return 1
 
-    def _get_new_import_blanks(self):
-        imports = self.get_import_statements()
-        if imports:
-            return imports[-1].blank_lines
-        return 2
-
     def filter_names(self, can_select):
         visitor = actions.RemovingVisitor(
             self.pycore, self._current_folder(), can_select)

rope/refactor/rename.py

                                                       resource, offset)
             if self.old_pyname is None:
                 raise exceptions.RefactoringError(
-                    'Rename refactoring should be performed on python identifiers.')
+                    'Rename refactoring should be performed'
+                    ' on resolvable python identifiers.')
         else:
             if not resource.is_folder() and resource.name == '__init__.py':
                 resource = resource.parent

ropetest/pycoretest.py

         var = pymod.get_attribute('var').get_object()
         self.assertEquals(a_class, var.get_type())
 
+    def test_check_for_else_block(self):
+        mod = self.pycore.get_string_module('for i in range(10):\n    pass\n'
+                                            'else:\n    myvar = 1\n')
+        a_var = mod.get_attribute('myvar')
+        self.assertEquals((mod, 4), a_var.get_definition_location())
+
+    def test_check_names_defined_in_whiles(self):
+        mod = self.pycore.get_string_module('while False:\n    myvar = 1\n')
+        a_var = mod.get_attribute('myvar')
+        self.assertEquals((mod, 2), a_var.get_definition_location())
+
 
 class PyCoreInProjectsTest(unittest.TestCase):
 

ropetest/refactor/movetest.py

             'import sys\n\n\ndef f():\n    print(sys.version)\n',
             self.mod2.read())
 
+    def test_moving_globals_to_a_module_with_only_docstrings2(self):
+        self.mod1.write('import os\nimport sys\n\n\n'
+                        'def f():\n    print(sys.version, os.path)\n')
+        self.mod2.write('"""doc\n\nMore docs ...\n\n"""\n')
+        mover = move.create_move(self.project, self.mod1,
+                                 self.mod1.read().index('f()') + 1)
+        self.project.do(mover.get_changes(self.mod2))
+        self.assertEquals(
+            '"""doc\n\nMore docs ...\n\n"""\nimport os\nimport sys\n\n\n'
+            'def f():\n    print(sys.version, os.path)\n',
+            self.mod2.read())
+
 
 if __name__ == '__main__':
     unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.