From df408e2d93434fea17f718a7af15d3477911d2f8 2012-07-08 04:53:38 From: Aaron Meurer Date: 2012-07-08 04:53:38 Subject: [PATCH] `yield`, `break`, and `continue` automatically dedent Previously `return`, `raise`, and `pass` did; now these do as well. The tests for these were copied from the tests for those. --- diff --git a/IPython/core/inputsplitter.py b/IPython/core/inputsplitter.py index 18ac736..8b7a21f 100644 --- a/IPython/core/inputsplitter.py +++ b/IPython/core/inputsplitter.py @@ -115,7 +115,11 @@ dedent_re = re.compile('|'.join([ r'^\s+raise\([^\)]*\).*$', # wacky raise with immediate open paren r'^\s+return(\s.*)?$', # normal return (+ space + other stuff, maybe) r'^\s+return\([^\)]*\).*$', # wacky return with immediate open paren - r'^\s+pass\s*$' # pass (optionally followed by trailing spaces) + r'^\s+yield(\s.*)?$', # normal yield (+ space + other stuff, maybe) + r'^\s+yield\([^\)]*\).*$', # wacky yield with immediate open paren + r'^\s+pass\s*$', # pass (optionally followed by trailing spaces) + r'^\s+break\s*$', # break (optionally followed by trailing spaces) + r'^\s+continue\s*$', # continue (optionally followed by trailing spaces) ])) ini_spaces_re = re.compile(r'^([ \t\r\f\v]+)') diff --git a/IPython/core/tests/test_inputsplitter.py b/IPython/core/tests/test_inputsplitter.py index 865855a..3987967 100644 --- a/IPython/core/tests/test_inputsplitter.py +++ b/IPython/core/tests/test_inputsplitter.py @@ -227,6 +227,26 @@ class InputSplitterTestCase(unittest.TestCase): isp.push('if 1:\n pass ') self.assertEqual(isp.indent_spaces, 0) + def test_dedent_break(self): + isp = self.isp # shorthand + # should NOT cause dedent + isp.push('while 1:\n breaks = 5') + self.assertEqual(isp.indent_spaces, 4) + isp.push('while 1:\n break') + self.assertEqual(isp.indent_spaces, 0) + isp.push('while 1:\n break ') + self.assertEqual(isp.indent_spaces, 0) + + def test_dedent_continue(self): + isp = self.isp # shorthand + # should NOT cause dedent + isp.push('while 1:\n continues = 5') + self.assertEqual(isp.indent_spaces, 4) + isp.push('while 1:\n continue') + self.assertEqual(isp.indent_spaces, 0) + isp.push('while 1:\n continue ') + self.assertEqual(isp.indent_spaces, 0) + def test_dedent_raise(self): isp = self.isp # shorthand # should NOT cause dedent @@ -253,6 +273,20 @@ class InputSplitterTestCase(unittest.TestCase): isp.push('if 1:\n return(0)') self.assertEqual(isp.indent_spaces, 0) + def test_dedent_yield(self): + isp = self.isp # shorthand + # should NOT cause dedent + isp.push('if 1:\n yielding = 4') + self.assertEqual(isp.indent_spaces, 4) + isp.push('if 1:\n yield 5 + 493') + self.assertEqual(isp.indent_spaces, 0) + isp.push('if 1:\n yield') + self.assertEqual(isp.indent_spaces, 0) + isp.push('if 1:\n yield ') + self.assertEqual(isp.indent_spaces, 0) + isp.push('if 1:\n yield(0)') + self.assertEqual(isp.indent_spaces, 0) + def test_push(self): isp = self.isp self.assertTrue(isp.push('x=1'))