1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
# -*- coding: utf-8 -* -
import pytest
from pypy.interpreter.astcompiler import consts
from pypy.interpreter.pyparser import pytokenizer
from pypy.interpreter.pyparser.parser import Token
from pypy.interpreter.pyparser.pygram import tokens
from pypy.interpreter.pyparser.error import TokenError
def tokenize(s, flags=0):
source_lines = s.splitlines(True)
if source_lines and not source_lines[-1].endswith("\n"):
source_lines[-1] += '\n'
return pytokenizer.generate_tokens(source_lines, flags)
def check_token_error(s, msg=None, pos=-1, line=-1):
error = pytest.raises(TokenError, tokenize, s)
if msg is not None:
assert error.value.msg == msg
if pos != -1:
assert error.value.offset == pos
if line != -1:
assert error.value.lineno == line
class TestTokenizer(object):
def test_simple(self):
line = "a+1\n"
tks = tokenize(line)
assert tks[:-3] == [
Token(tokens.NAME, 'a', 1, 0, line, 1, 1),
Token(tokens.PLUS, '+', 1, 1, line, 1, 2),
Token(tokens.NUMBER, '1', 1, 2, line, 1, 3),
]
def test_error_parenthesis(self):
for paren in "([{":
check_token_error(paren + "1 + 2",
"'%s' was never closed" % paren,
1)
for paren in ")]}":
check_token_error("1 + 2" + paren,
"unmatched '%s'" % (paren, ),
6)
for i, opening in enumerate("([{"):
for j, closing in enumerate(")]}"):
if i == j:
continue
check_token_error(opening + "1\n" + closing,
"closing parenthesis '%s' does not match opening parenthesis '%s' on line 1" % (closing, opening),
pos=1, line=2)
check_token_error(opening + "1" + closing,
"closing parenthesis '%s' does not match opening parenthesis '%s'" % (closing, opening),
pos=3, line=1)
check_token_error(opening + closing,
"closing parenthesis '%s' does not match opening parenthesis '%s'" % (closing, opening),
pos=2, line=1)
def test_unknown_char(self):
check_token_error("?", "invalid character '?' (U+003F)", 1)
check_token_error("$", "invalid character '$' (U+0024)", 1)
check_token_error("⫛", "invalid character '⫛' (U+2ADB)", 1)
check_token_error("\x17", "invalid non-printable character U+0017", 1)
def test_eol_string(self):
check_token_error("x = 'a", pos=5, line=1)
def test_eof_triple_quoted(self):
check_token_error("'''", pos=1, line=1)
def test_type_comments(self):
line = "a = 5 # type: int\n"
tks = tokenize(line, flags=consts.PyCF_TYPE_COMMENTS)
assert tks[:-3] == [
Token(tokens.NAME, 'a', 1, 0, line, 1, 1),
Token(tokens.EQUAL, '=', 1, 2, line, 1, 3),
Token(tokens.NUMBER, '5', 1, 4, line, 1, 5),
Token(tokens.TYPE_COMMENT, 'int', 1, 6, line),
]
def test_type_comment_bug(self):
lines = ['# type: int\n', '']
pytokenizer.generate_tokens(lines, flags=consts.PyCF_TYPE_COMMENTS)
def test_type_ignore(self):
line = "a = 5 # type: ignore@teyit\n"
tks = tokenize(line, flags=consts.PyCF_TYPE_COMMENTS)
assert tks[:-3] == [
Token(tokens.NAME, 'a', 1, 0, line, 1, 1),
Token(tokens.EQUAL, '=', 1, 2, line, 1, 3),
Token(tokens.NUMBER, '5', 1, 4, line, 1, 5),
Token(tokens.TYPE_IGNORE, '@teyit', 1, 6, line),
]
def test_walrus(self):
line = "a:=1\n"
tks = tokenize(line)
assert tks[:-3] == [
Token(tokens.NAME, 'a', 1, 0, line, 1, 1),
Token(tokens.COLONEQUAL, ':=', 1, 1, line, 1, 3),
Token(tokens.NUMBER, '1', 1, 3, line, 1, 4),
]
def test_triple_quoted(self):
input = '''x = """
hello
content
whatisthis""" + "a"\n'''
s = '''"""
hello
content
whatisthis"""'''
tks = tokenize(input)
lines = input.splitlines(True)
assert tks[:3] == [
Token(tokens.NAME, 'x', 1, 0, lines[0], 1, 1),
Token(tokens.EQUAL, '=', 1, 2, lines[0], 1, 3),
Token(tokens.STRING, s, 1, 4, lines[3], 4, 13),
]
def test_parenthesis_positions(self):
input = '( ( ( a ) ) ) ( )'
tks = tokenize(input)[:-3]
columns = [t.column for t in tks]
assert columns == [0, 2, 4, 6, 8, 10, 12, 14, 16]
assert [t.end_column - 1 for t in tks] == columns
def test_PyCF_DONT_IMPLY_DEDENT(self):
input = "if 1:\n 1\n"
# regular mode
tks = tokenize(input)
lines = input.splitlines(True)
del tks[-2] # new parser deletes one newline anyway
assert tks == [
Token(tokens.NAME, 'if', 1, 0, lines[0], 1, 2),
Token(tokens.NUMBER, '1', 1, 3, lines[0], 1, 4),
Token(tokens.COLON, ':', 1, 4, lines[0], 1, 5),
Token(tokens.NEWLINE, '', 1, 5, lines[0], -1, -1),
Token(tokens.INDENT, ' ', 2, 0, lines[1], 2, 2),
Token(tokens.NUMBER, '1', 2, 2, lines[1], 2, 3),
Token(tokens.NEWLINE, '', 2, 3, lines[1], -1, -1),
Token(tokens.DEDENT, '', 2, 0, '', -1, -1),
Token(tokens.ENDMARKER, '', 2, 0, '', -1, -1),
]
# single mode
tks = tokenize(input, flags=consts.PyCF_DONT_IMPLY_DEDENT)
lines = input.splitlines(True)
del tks[-2] # new parser deletes one newline anyway
assert tks == [
Token(tokens.NAME, 'if', 1, 0, lines[0], 1, 2),
Token(tokens.NUMBER, '1', 1, 3, lines[0], 1, 4),
Token(tokens.COLON, ':', 1, 4, lines[0], 1, 5),
Token(tokens.NEWLINE, '', 1, 5, lines[0], -1, -1),
Token(tokens.INDENT, ' ', 2, 0, lines[1], 2, 2),
Token(tokens.NUMBER, '1', 2, 2, lines[1], 2, 3),
Token(tokens.NEWLINE, '', 2, 3, lines[1], -1, -1),
Token(tokens.ENDMARKER, '', 2, 0, '', -1, -1),
]
def test_ignore_just_linecont(self):
input = "pass\n \\\n\npass"
tks = tokenize(input)
tps = [tk.token_type for tk in tks]
assert tps == [tokens.NAME, tokens.NEWLINE, tokens.NAME,
tokens.NEWLINE, tokens.NEWLINE, tokens.ENDMARKER]
def test_error_linecont(self):
check_token_error("a \\ b",
"unexpected character after line continuation character",
4)
def test_continuation_and_indentation_levels(self):
# Make sure the '\` generates indent/dedent tokens
input1 = r"""\
def fib(n):
\
'''Print a Fibonacci series up to n.'''
\
a, b = 0, 1
"""
input2 = r"""\
def fib(n):
'''Print a Fibonacci series up to n.'''
a, b = 0, 1
"""
def base_eq(tok1, tok2):
# Line numbers differ because of `\`, so only compare type and value
return all([(t1.token_type == t2.token_type and t1.value == t2.value) for t1, t2 in zip(tok1, tok2)])
tks1 = tokenize(input1)
tks2 = tokenize(input2)
if not base_eq(tks1, tks2):
# get better error message
assert tks1 == tks2
def test_formfeed1(self):
# issue gh-5221
input = "\\\n\ndef(a=1,b:bool=False): pass"
tks = tokenize(input)
assert tks[0].token_type != tokens.INDENT
def test_formfeed2(self):
# issue gh-5221
input = "\\\n#\n"
tks = tokenize(input)
assert tks[0].token_type != tokens.INDENT
def test_backslash_before_indent1(self):
# issue gh-5221
input1 = r"""\
class AnotherCase:
'''Some Docstring
'''"""
input2 = r"""\
class AnotherCase:
\
'''Some Docstring
'''"""
tks1 = tokenize(input1)
tps1 = [tk.token_type for tk in tks1]
tks2 = tokenize(input2)
tps2 = [tk.token_type for tk in tks2]
assert tps1 == tps2
def test_backslash_before_indent2(self):
# issue gh-5221
input1 = r"""\
class Plotter:
\
pass
"""
input2 = r"""\
class Plotter:
pass
"""
tks1 = tokenize(input1)
tps1 = [tk.token_type for tk in tks1]
tks2 = tokenize(input2)
tps2 = [tk.token_type for tk in tks2]
assert tps1 == tps2
def test_error_integers(self):
check_token_error("0b106",
"invalid digit '6' in binary literal",
5)
check_token_error("0b10_6",
"invalid digit '6' in binary literal",
5)
check_token_error("0b6",
"invalid digit '6' in binary literal",
3)
check_token_error("0b \n",
"invalid binary literal",
2)
check_token_error("0o129",
"invalid digit '9' in octal literal",
5)
check_token_error("0o12_9",
"invalid digit '9' in octal literal",
5)
check_token_error("0o9",
"invalid digit '9' in octal literal",
3)
check_token_error("0o \n",
"invalid octal literal",
2)
check_token_error("0x1__ \n",
"invalid hexadecimal literal",
4)
check_token_error("0x\n",
"invalid hexadecimal literal",
2)
check_token_error("1_ \n",
"invalid decimal literal",
2)
check_token_error("0b1_ \n",
"invalid binary literal",
3)
check_token_error("01212 \n",
"leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers",
1)
tokenize("1 2 \n") # does not raise
tokenize("1 _ \n") # does not raise
def test_invalid_identifier(self):
check_token_error("aänc€",
"invalid character '€' (U+20AC)",
6)
check_token_error("a\xc2\xa0b",
"invalid non-printable character U+00A0",
2)
class TestTokenizer310Changes(object):
def test_single_quoted(self):
check_token_error('s = "abc\n', "unterminated string literal (detected at line 1)", pos=5)
def test_triple_quoted(self):
check_token_error('"""abc\n', "unterminated triple-quoted string literal (detected at line 1)")
def test_single_quoted_detected(self):
check_token_error('s = "abc\n', "unterminated string literal (detected at line 1)")
check_token_error('s = "abc\\\na\\\nb\n', "unterminated string literal (detected at line 3)", pos=5)
def test_triple_quoted_detected(self):
check_token_error('s = """', "unterminated triple-quoted string literal (detected at line 1)")
check_token_error('s = """abc\\\na\\\nb\n\n\n\n\n', "unterminated triple-quoted string literal (detected at line 7)")
def test_warn_number_followed_by_keyword(self):
line = "0x1for\n"
tks = tokenize(line)
assert tks[:-3] == [
Token(tokens.NUMBER, '0x1f', 1, 0, line, 1, 4),
Token(tokens.WARNING, 'invalid hexadecimal literal', 1, 0, line),
Token(tokens.NAME, 'or', 1, 4, line, 1, 6),
]
for line in ("1in 3", "0b01010111and 4", "1 if 0o21231else 2"):
tks = tokenize(line)
assert any(tokens.WARNING == tok.token_type for tok in tks)
def test_error_number_by_non_keyword_name(self):
check_token_error("1a 2", "invalid decimal literal")
def test_levels(self):
line = 'a b (c + d) [[e, f]]'
tks = tokenize(line)
levels = [token.level for token in tks]
assert levels == [0, 0, 1, 1, 1, 1, 0, 1, 2, 2, 2, 2, 1, 0, 0, 0, 0]
|