Debian Codegraph @

pypy/interpreter/pyparser/test/apptest_parsestring.py

raw symbols SCIP
1 from pytest import raises
2 import warnings
3
4 def test_bytes_invalid_escape():
5 with warnings.catch_warnings(record=True) as w:
6 warnings.simplefilter('always', category=DeprecationWarning)
7 eval("b'''\n\\z'''")
8 assert len(w) == 1
9 assert w[0].filename == '<string>'
10 assert w[0].lineno == 1
11
12 with warnings.catch_warnings(record=True) as w:
13 warnings.simplefilter('error', category=DeprecationWarning)
14 with raises(SyntaxError) as excinfo:
15 eval("b'''\n\\z'''")
16 assert not w
17 assert excinfo.value.filename == '<string>'
18 assert excinfo.value.lineno == 1
19
20 def test_str_invalid_escape():
21 with warnings.catch_warnings(record=True) as w:
22 warnings.simplefilter('always', category=DeprecationWarning)
23 eval("'''\n\\z'''")
24 assert len(w) == 1
25 assert w[0].filename == '<string>'
26 assert w[0].lineno == 1
27
28 with warnings.catch_warnings(record=True) as w:
29 warnings.simplefilter('error', category=DeprecationWarning)
30 with raises(SyntaxError) as excinfo:
31 eval("'''\n\\z'''")
32 assert not w
33 assert excinfo.value.filename == '<string>'
34 assert excinfo.value.lineno == 1
35
36 def test_str_invalid_octal_escape():
37 with warnings.catch_warnings(record=True) as w:
38 warnings.simplefilter('always', category=DeprecationWarning)
39 eval("'''\n\\407'''")
40 assert len(w) == 1
41 assert str(w[0].message) == r"invalid octal escape sequence '\407'"
42
43 def test_fstring_invalid_escape():
44 with warnings.catch_warnings(record=True) as w:
45 warnings.simplefilter('always', category=DeprecationWarning)
46 eval('f"\\{8}"')
47 assert len(w) == 1
48 assert w[0].filename == '<string>'
49
50 with warnings.catch_warnings(record=True) as w:
51 warnings.simplefilter('error', category=DeprecationWarning)
52 with raises(SyntaxError) as excinfo:
53 eval('f"\\{8}"')
54 assert not w
55 assert excinfo.value.filename == '<string>'
56
57 def test_invalid_escape_syntax_error_span():
58 # When -Werror promotes the DeprecationWarning to a SyntaxError, the
59 # error should highlight the full string token, not just one character.
60 # E.g. for '"""\q"""' (8 chars) the end offset must be start+8.
61 with warnings.catch_warnings(record=True) as w:
62 warnings.simplefilter('error', category=DeprecationWarning)
63 with raises(SyntaxError) as excinfo:
64 eval('"""\\q"""')
65 assert not w
66 exc = excinfo.value
67 # offset is 1-based column of opening quote; end_offset covers
68 # the full token '"""\q"""' (8 chars).
69 assert exc.end_offset - exc.offset == len('"""\\q"""')
70
71 def test_invalid_escape_plus_syntax_error_single_warning():
72 # When a string literal contains an invalid escape sequence AND the
73 # surrounding expression is a SyntaxError, the DeprecationWarning must
74 # be emitted exactly once. The call_invalid_rules second parse pass must
75 # not re-emit it.
76 with warnings.catch_warnings(record=True) as w:
77 warnings.simplefilter('always', category=DeprecationWarning)
78 try:
79 compile("'\\e' 1", '<test>', 'single')
80 except SyntaxError:
81 pass
82 dep = [x for x in w if issubclass(x.category, DeprecationWarning)]
83 assert len(dep) == 1, dep