Skip to content

Commit

Permalink
pythongh-94808: Coverage: Test that maximum indentation level is hand…
Browse files Browse the repository at this point in the history
…led (python#95926)

* pythongh-94808: Coverage: Test that maximum indentation level is handled

* Use "compile" rather than "exec"
  • Loading branch information
mdboom committed Oct 6, 2022
1 parent e2e6b95 commit 23e83a8
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion Lib/test/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from tokenize import (tokenize, _tokenize, untokenize, NUMBER, NAME, OP,
STRING, ENDMARKER, ENCODING, tok_name, detect_encoding,
open as tokenize_open, Untokenizer, generate_tokens,
NEWLINE, _generate_tokens_from_c_tokenizer)
NEWLINE, _generate_tokens_from_c_tokenizer, DEDENT)
from io import BytesIO, StringIO
import unittest
from textwrap import dedent
Expand Down Expand Up @@ -2512,6 +2512,26 @@ def get_tokens(string):
self.assertRaises(SyntaxError, get_tokens, "("*1000+"a"+")"*1000)
self.assertRaises(SyntaxError, get_tokens, "]")

def test_max_indent(self):
MAXINDENT = 100

def generate_source(indents):
source = ''.join((' ' * x) + 'if True:\n' for x in range(indents))
source += ' ' * indents + 'pass\n'
return source

valid = generate_source(MAXINDENT - 1)
tokens = list(_generate_tokens_from_c_tokenizer(valid))
self.assertEqual(tokens[-1].type, DEDENT)
compile(valid, "<string>", "exec")

invalid = generate_source(MAXINDENT)
tokens = list(_generate_tokens_from_c_tokenizer(invalid))
self.assertEqual(tokens[-1].type, NEWLINE)
self.assertRaises(
IndentationError, compile, invalid, "<string>", "exec"
)

def test_continuation_lines_indentation(self):
def get_tokens(string):
return [(kind, string) for (kind, string, *_) in _generate_tokens_from_c_tokenizer(string)]
Expand Down

0 comments on commit 23e83a8

Please sign in to comment.