diff --git a/IPython/core/inputtransformer2.py b/IPython/core/inputtransformer2.py index b73d701..996a4f0 100644 --- a/IPython/core/inputtransformer2.py +++ b/IPython/core/inputtransformer2.py @@ -604,7 +604,7 @@ class TransformerManager: else: continue - if ends_with_newline: + if not ends_with_newline: # Append an newline for consistent tokenization # See https://bugs.python.org/issue33899 cell += '\n' @@ -649,10 +649,13 @@ class TransformerManager: newline_types = {tokenize.NEWLINE, tokenize.COMMENT, tokenize.ENDMARKER} - # Remove newline_types for the list of tokens - while len(tokens_by_line) > 1 and len(tokens_by_line[-1]) == 1 \ - and tokens_by_line[-1][-1].type in newline_types: - tokens_by_line.pop() + # Pop the last line which only contains DEDENTs and ENDMARKER + last_token_line = None + if {t.type for t in tokens_by_line[-1]} in [ + {tokenize.DEDENT, tokenize.ENDMARKER}, + {tokenize.ENDMARKER} + ] and len(tokens_by_line) > 1: + last_token_line = tokens_by_line.pop() while tokens_by_line[-1] and tokens_by_line[-1][-1].type in newline_types: tokens_by_line[-1].pop() @@ -685,7 +688,7 @@ class TransformerManager: if res is None: return 'incomplete', find_last_indent(lines) - if tokens_by_line[-1][-1].type == tokenize.DEDENT: + if last_token_line and last_token_line[0].type == tokenize.DEDENT: if ends_with_newline: return 'complete', None return 'incomplete', find_last_indent(lines) diff --git a/IPython/core/tests/test_inputtransformer2.py b/IPython/core/tests/test_inputtransformer2.py index da388ab..8d6efc1 100644 --- a/IPython/core/tests/test_inputtransformer2.py +++ b/IPython/core/tests/test_inputtransformer2.py @@ -238,6 +238,7 @@ def test_check_complete(): cc = ipt2.TransformerManager().check_complete nt.assert_equal(cc("a = 1"), ('complete', None)) nt.assert_equal(cc("for a in range(5):"), ('incomplete', 4)) + nt.assert_equal(cc("for a in range(5):\n if a > 0:"), ('incomplete', 8)) nt.assert_equal(cc("raise = 2"), ('invalid', None)) nt.assert_equal(cc("a = [1,\n2,"), ('incomplete', 0)) nt.assert_equal(cc(")"), ('incomplete', 0))