From dd1761da962b7917a74ea3b763c11e8fc6763d48 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sun, 13 Jan 2019 14:51:34 +0100 Subject: [PATCH] Fix tokenizer: Closing parentheses in the wrong place should not lead to strange behavior --- parso/python/tokenize.py | 3 ++- test/test_tokenize.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/parso/python/tokenize.py b/parso/python/tokenize.py index 3a59643..4e4f62b 100644 --- a/parso/python/tokenize.py +++ b/parso/python/tokenize.py @@ -585,7 +585,8 @@ def tokenize_lines(lines, version_info, start_pos=(1, 0)): if fstring_stack: fstring_stack[-1].close_parentheses(token) else: - paren_level -= 1 + if paren_level: + paren_level -= 1 elif token == ':' and fstring_stack \ and fstring_stack[-1].parentheses_count == 1: fstring_stack[-1].format_spec_count += 1 diff --git a/test/test_tokenize.py b/test/test_tokenize.py index 3909214..397adb4 100644 --- a/test/test_tokenize.py +++ b/test/test_tokenize.py @@ -285,3 +285,17 @@ def test_error_token_after_dedent(): ERRORTOKEN, NAME, NEWLINE, ENDMARKER ] assert [t.type for t in lst] == expected + + +def test_brackets_no_indentation(): + """ + There used to be an issue that the parentheses counting would go below + zero. This should not happen. + """ + code = dedent("""\ + } + { + } + """) + lst = _get_token_list(code) + assert [t.type for t in lst] == [OP, NEWLINE, OP, OP, NEWLINE, ENDMARKER]