From b5e59f5d54ca44039bc833d702c49d4431cad012 2017-02-09 19:57:37 From: Matthias Bussonnier Date: 2017-02-09 19:57:37 Subject: [PATCH] Fix detection of indentation in nested context. In nested context the Tokenizer will helpfully insert multiple DETENT token. So you need to while-pop. Included a couple of extra test that verify this behavior. --- diff --git a/IPython/core/inputsplitter.py b/IPython/core/inputsplitter.py index 250d70a..267b73a 100644 --- a/IPython/core/inputsplitter.py +++ b/IPython/core/inputsplitter.py @@ -142,7 +142,7 @@ def find_next_indent(code): tokens.pop() if not tokens: return 0 - if tokens[-1].type in {tokenize.DEDENT, tokenize.NEWLINE, tokenize.COMMENT}: + while (tokens[-1].type in {tokenize.DEDENT, tokenize.NEWLINE, tokenize.COMMENT}): tokens.pop() if tokens[-1].type == INCOMPLETE_STRING: diff --git a/IPython/core/tests/test_inputsplitter.py b/IPython/core/tests/test_inputsplitter.py index f064efa..54e85c0 100644 --- a/IPython/core/tests/test_inputsplitter.py +++ b/IPython/core/tests/test_inputsplitter.py @@ -629,6 +629,9 @@ indentation_samples = [ ' 1,', 5), ('b = """123', 0), ('', 0), + ('def f():\n pass', 0), + ('class Bar:\n def f():\n pass', 4), + ('class Bar:\n def f():\n raise', 4), ] def test_find_next_indent():