1
1
"""test script for a few new invalid token catches"""
2
2
3
3
import sys
4
+ from codecs import BOM_UTF8
4
5
from test import support
5
6
from test .support import os_helper
6
7
from test .support import script_helper
@@ -11,67 +12,158 @@ class EOFTestCase(unittest.TestCase):
11
12
def test_EOF_single_quote (self ):
12
13
expect = "unterminated string literal (detected at line 1) (<string>, line 1)"
13
14
for quote in ("'" , "\" " ):
14
- try :
15
+ with self . assertRaises ( SyntaxError ) as cm :
15
16
eval (f"""{ quote } this is a test\
16
17
""" )
17
- except SyntaxError as msg :
18
- self .assertEqual (str (msg ), expect )
19
- self .assertEqual (msg .offset , 1 )
20
- else :
21
- raise support .TestFailed
18
+ self .assertEqual (str (cm .exception ), expect )
19
+ self .assertEqual (cm .exception .offset , 1 )
22
20
23
21
def test_EOFS (self ):
24
- expect = ("unterminated triple-quoted string literal (detected at line 1) (<string>, line 1)" )
25
- try :
26
- eval ("""'''this is a test""" )
27
- except SyntaxError as msg :
28
- self .assertEqual (str (msg ), expect )
29
- self .assertEqual (msg .offset , 1 )
30
- else :
31
- raise support .TestFailed
22
+ expect = ("unterminated triple-quoted string literal (detected at line 3) (<string>, line 1)" )
23
+ with self .assertRaises (SyntaxError ) as cm :
24
+ eval ("""ä = '''thîs is \n a \n test""" )
25
+ self .assertEqual (str (cm .exception ), expect )
26
+ self .assertEqual (cm .exception .text , "ä = '''thîs is " )
27
+ self .assertEqual (cm .exception .offset , 5 )
28
+
29
+ with self .assertRaises (SyntaxError ) as cm :
30
+ eval ("""ä = '''thîs is \n a \n test""" .encode ())
31
+ self .assertEqual (str (cm .exception ), expect )
32
+ self .assertEqual (cm .exception .text , "ä = '''thîs is " )
33
+ self .assertEqual (cm .exception .offset , 5 )
34
+
35
+ with self .assertRaises (SyntaxError ) as cm :
36
+ eval (BOM_UTF8 + """ä = '''thîs is \n a \n test""" .encode ())
37
+ self .assertEqual (str (cm .exception ), expect )
38
+ self .assertEqual (cm .exception .text , "ä = '''thîs is " )
39
+ self .assertEqual (cm .exception .offset , 5 )
40
+
41
+ with self .assertRaises (SyntaxError ) as cm :
42
+ eval ("""# coding: latin1\n ä = '''thîs is \n a \n test""" .encode ('latin1' ))
43
+ self .assertEqual (str (cm .exception ), "unterminated triple-quoted string literal (detected at line 4) (<string>, line 2)" )
44
+ self .assertEqual (cm .exception .text , "ä = '''thîs is " )
45
+ self .assertEqual (cm .exception .offset , 5 )
32
46
33
47
def test_EOFS_with_file (self ):
34
48
expect = ("(<string>, line 1)" )
35
49
with os_helper .temp_dir () as temp_dir :
36
- file_name = script_helper .make_script (temp_dir , 'foo' , """'''this is \n a \n test""" )
37
- rc , out , err = script_helper .assert_python_failure (file_name )
38
- self .assertIn (b'unterminated triple-quoted string literal (detected at line 3)' , err )
50
+ file_name = script_helper .make_script (temp_dir , 'foo' ,
51
+ """ä = '''thîs is \n a \n test""" )
52
+ rc , out , err = script_helper .assert_python_failure ('-X' , 'utf8' , file_name )
53
+ err = err .decode ().splitlines ()
54
+ self .assertEqual (err [- 3 :], [
55
+ " ä = '''thîs is " ,
56
+ ' ^' ,
57
+ 'SyntaxError: unterminated triple-quoted string literal (detected at line 3)' ])
58
+
59
+ file_name = script_helper .make_script (temp_dir , 'foo' ,
60
+ """ä = '''thîs is \n a \n test""" .encode ())
61
+ rc , out , err = script_helper .assert_python_failure ('-X' , 'utf8' , file_name )
62
+ err = err .decode ().splitlines ()
63
+ self .assertEqual (err [- 3 :], [
64
+ " ä = '''thîs is " ,
65
+ ' ^' ,
66
+ 'SyntaxError: unterminated triple-quoted string literal (detected at line 3)' ])
67
+
68
+ file_name = script_helper .make_script (temp_dir , 'foo' ,
69
+ BOM_UTF8 + """ä = '''thîs is \n a \n test""" .encode ())
70
+ rc , out , err = script_helper .assert_python_failure ('-X' , 'utf8' , file_name )
71
+ err = err .decode ().splitlines ()
72
+ self .assertEqual (err [- 3 :], [
73
+ " ä = '''thîs is " ,
74
+ ' ^' ,
75
+ 'SyntaxError: unterminated triple-quoted string literal (detected at line 3)' ])
76
+
77
+ file_name = script_helper .make_script (temp_dir , 'foo' ,
78
+ """# coding: latin1\n ä = '''thîs is \n a \n test""" .encode ('latin1' ))
79
+ rc , out , err = script_helper .assert_python_failure ('-X' , 'utf8' , file_name )
80
+ err = err .decode ().splitlines ()
81
+ self .assertEqual (err [- 3 :], [
82
+ " ä = '''thîs is " ,
83
+ ' ^' ,
84
+ 'SyntaxError: unterminated triple-quoted string literal (detected at line 4)' ])
39
85
40
86
@warnings_helper .ignore_warnings (category = SyntaxWarning )
41
87
def test_eof_with_line_continuation (self ):
42
88
expect = "unexpected EOF while parsing (<string>, line 1)"
43
- try :
89
+ with self . assertRaises ( SyntaxError ) as cm :
44
90
compile ('"\\ Xhh" \\ ' , '<string>' , 'exec' )
45
- except SyntaxError as msg :
46
- self .assertEqual (str (msg ), expect )
47
- else :
48
- raise support .TestFailed
91
+ self .assertEqual (str (cm .exception ), expect )
49
92
50
93
def test_line_continuation_EOF (self ):
51
94
"""A continuation at the end of input must be an error; bpo2180."""
52
95
expect = 'unexpected EOF while parsing (<string>, line 1)'
53
- with self .assertRaises (SyntaxError ) as excinfo :
54
- exec ('x = 5\\ ' )
55
- self .assertEqual (str (excinfo .exception ), expect )
56
- with self .assertRaises (SyntaxError ) as excinfo :
96
+ with self .assertRaises (SyntaxError ) as cm :
97
+ exec ('ä = 5\\ ' )
98
+ self .assertEqual (str (cm .exception ), expect )
99
+ self .assertEqual (cm .exception .text , 'ä = 5\\ \n ' )
100
+ self .assertEqual (cm .exception .offset , 7 )
101
+
102
+ with self .assertRaises (SyntaxError ) as cm :
103
+ exec ('ä = 5\\ ' .encode ())
104
+ self .assertEqual (str (cm .exception ), expect )
105
+ self .assertEqual (cm .exception .text , 'ä = 5\\ \n ' )
106
+ self .assertEqual (cm .exception .offset , 7 )
107
+
108
+ with self .assertRaises (SyntaxError ) as cm :
109
+ exec ('# coding:latin1\n ä = 5\\ ' .encode ('latin1' ))
110
+ self .assertEqual (str (cm .exception ),
111
+ 'unexpected EOF while parsing (<string>, line 2)' )
112
+ self .assertEqual (cm .exception .text , 'ä = 5\\ \n ' )
113
+ self .assertEqual (cm .exception .offset , 7 )
114
+
115
+ with self .assertRaises (SyntaxError ) as cm :
116
+ exec (BOM_UTF8 + 'ä = 5\\ ' .encode ())
117
+ self .assertEqual (str (cm .exception ), expect )
118
+ self .assertEqual (cm .exception .text , 'ä = 5\\ \n ' )
119
+ self .assertEqual (cm .exception .offset , 7 )
120
+
121
+ with self .assertRaises (SyntaxError ) as cm :
57
122
exec ('\\ ' )
58
- self .assertEqual (str (excinfo .exception ), expect )
123
+ self .assertEqual (str (cm .exception ), expect )
59
124
60
125
@unittest .skipIf (not sys .executable , "sys.executable required" )
61
126
def test_line_continuation_EOF_from_file_bpo2180 (self ):
62
127
"""Ensure tok_nextc() does not add too many ending newlines."""
63
128
with os_helper .temp_dir () as temp_dir :
64
129
file_name = script_helper .make_script (temp_dir , 'foo' , '\\ ' )
65
- rc , out , err = script_helper .assert_python_failure (file_name )
66
- self .assertIn (b'unexpected EOF while parsing' , err )
67
- self .assertIn (b'line 1' , err )
68
- self .assertIn (b'\\ ' , err )
69
-
70
- file_name = script_helper .make_script (temp_dir , 'foo' , 'y = 6\\ ' )
71
- rc , out , err = script_helper .assert_python_failure (file_name )
72
- self .assertIn (b'unexpected EOF while parsing' , err )
73
- self .assertIn (b'line 1' , err )
74
- self .assertIn (b'y = 6\\ ' , err )
130
+ rc , out , err = script_helper .assert_python_failure ('-X' , 'utf8' , file_name )
131
+ err = err .decode ().splitlines ()
132
+ self .assertEqual (err [- 2 :], [
133
+ ' \\ ' ,
134
+ 'SyntaxError: unexpected EOF while parsing' ])
135
+ self .assertEqual (err [- 3 ][- 8 :], ', line 1' , err )
136
+
137
+ file_name = script_helper .make_script (temp_dir , 'foo' , 'ä = 6\\ ' )
138
+ rc , out , err = script_helper .assert_python_failure ('-X' , 'utf8' , file_name )
139
+ err = err .decode ().splitlines ()
140
+ self .assertEqual (err [- 3 :], [
141
+ ' ä = 6\\ ' ,
142
+ ' ^' ,
143
+ 'SyntaxError: unexpected EOF while parsing' ])
144
+ self .assertEqual (err [- 4 ][- 8 :], ', line 1' , err )
145
+
146
+ file_name = script_helper .make_script (temp_dir , 'foo' ,
147
+ '# coding:latin1\n '
148
+ 'ä = 7\\ ' .encode ('latin1' ))
149
+ rc , out , err = script_helper .assert_python_failure ('-X' , 'utf8' , file_name )
150
+ err = err .decode ().splitlines ()
151
+ self .assertEqual (err [- 3 :], [
152
+ ' ä = 7\\ ' ,
153
+ ' ^' ,
154
+ 'SyntaxError: unexpected EOF while parsing' ])
155
+ self .assertEqual (err [- 4 ][- 8 :], ', line 2' , err )
156
+
157
+ file_name = script_helper .make_script (temp_dir , 'foo' ,
158
+ BOM_UTF8 + 'ä = 8\\ ' .encode ())
159
+ rc , out , err = script_helper .assert_python_failure ('-X' , 'utf8' , file_name )
160
+ err = err .decode ().splitlines ()
161
+ self .assertEqual (err [- 3 :], [
162
+ ' ä = 8\\ ' ,
163
+ ' ^' ,
164
+ 'SyntaxError: unexpected EOF while parsing' ])
165
+ self .assertEqual (err [- 4 ][- 8 :], ', line 1' , err )
166
+
75
167
76
168
if __name__ == "__main__" :
77
169
unittest .main ()
0 commit comments