Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(201917)

Delta Between Two Patch Sets: Lib/test/test_string_literals.py

Issue 28128: Improve the warning message for invalid escape sequences
Left Patch Set: Created 3 years, 1 month ago
Right Patch Set: Created 3 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « Include/unicodeobject.h ('k') | Lib/test/test_unicode.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 r"""Test correct treatment of various string literals by the parser. 1 r"""Test correct treatment of various string literals by the parser.
2 2
3 There are four types of string literals: 3 There are four types of string literals:
4 4
5 'abc' -- normal str 5 'abc' -- normal str
6 r'abc' -- raw str 6 r'abc' -- raw str
7 b'xyz' -- normal bytes 7 b'xyz' -- normal bytes
8 br'xyz' | rb'xyz' -- raw bytes 8 br'xyz' | rb'xyz' -- raw bytes
9 9
10 The difference between normal and raw strings is of course that in a 10 The difference between normal and raw strings is of course that in a
(...skipping 13 matching lines...) Expand all
24 This file is really about correct treatment of encodings and 24 This file is really about correct treatment of encodings and
25 backslashes. It doesn't concern itself with issues like single 25 backslashes. It doesn't concern itself with issues like single
26 vs. double quotes or singly- vs. triply-quoted strings: that's dealt 26 vs. double quotes or singly- vs. triply-quoted strings: that's dealt
27 with elsewhere (I assume). 27 with elsewhere (I assume).
28 """ 28 """
29 29
30 import os 30 import os
31 import sys 31 import sys
32 import shutil 32 import shutil
33 import tempfile 33 import tempfile
34 import warnings
34 import unittest 35 import unittest
35 36
36 37
37 TEMPLATE = r"""# coding: %s 38 TEMPLATE = r"""# coding: %s
38 a = 'x' 39 a = 'x'
39 assert ord(a) == 120 40 assert ord(a) == 120
40 b = '\x01' 41 b = '\x01'
41 assert ord(b) == 1 42 assert ord(b) == 1
42 c = r'\x01' 43 c = r'\x01'
43 assert list(map(ord, c)) == [92, 120, 48, 49] 44 assert list(map(ord, c)) == [92, 120, 48, 49]
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 self.assertRaises(SyntaxError, eval, r""" '\u000' """) 98 self.assertRaises(SyntaxError, eval, r""" '\u000' """)
98 self.assertRaises(SyntaxError, eval, r""" '\U' """) 99 self.assertRaises(SyntaxError, eval, r""" '\U' """)
99 self.assertRaises(SyntaxError, eval, r""" '\U0' """) 100 self.assertRaises(SyntaxError, eval, r""" '\U0' """)
100 self.assertRaises(SyntaxError, eval, r""" '\U00' """) 101 self.assertRaises(SyntaxError, eval, r""" '\U00' """)
101 self.assertRaises(SyntaxError, eval, r""" '\U000' """) 102 self.assertRaises(SyntaxError, eval, r""" '\U000' """)
102 self.assertRaises(SyntaxError, eval, r""" '\U0000' """) 103 self.assertRaises(SyntaxError, eval, r""" '\U0000' """)
103 self.assertRaises(SyntaxError, eval, r""" '\U00000' """) 104 self.assertRaises(SyntaxError, eval, r""" '\U00000' """)
104 self.assertRaises(SyntaxError, eval, r""" '\U000000' """) 105 self.assertRaises(SyntaxError, eval, r""" '\U000000' """)
105 self.assertRaises(SyntaxError, eval, r""" '\U0000000' """) 106 self.assertRaises(SyntaxError, eval, r""" '\U0000000' """)
106 107
108 def test_eval_str_invalid_escape(self):
109 for b in range(1, 128):
110 if b in b"""\n\r"'01234567NU\\abfnrtuvx""":
111 continue
112 with self.assertWarns(DeprecationWarning):
113 self.assertEqual(eval(r"'\%c'" % b), '\\' + chr(b))
114 with warnings.catch_warnings(record=True) as w:
115 warnings.simplefilter('always', category=DeprecationWarning)
116 eval("'''\n\\z'''")
117 self.assertEqual(len(w), 1)
118 self.assertEqual(w[0].filename, '<string>')
119 self.assertEqual(w[0].lineno, 2)
120
107 def test_eval_str_raw(self): 121 def test_eval_str_raw(self):
108 self.assertEqual(eval(""" r'x' """), 'x') 122 self.assertEqual(eval(""" r'x' """), 'x')
109 self.assertEqual(eval(r""" r'\x01' """), '\\' + 'x01') 123 self.assertEqual(eval(r""" r'\x01' """), '\\' + 'x01')
110 self.assertEqual(eval(""" r'\x01' """), chr(1)) 124 self.assertEqual(eval(""" r'\x01' """), chr(1))
111 self.assertEqual(eval(r""" r'\x81' """), '\\' + 'x81') 125 self.assertEqual(eval(r""" r'\x81' """), '\\' + 'x81')
112 self.assertEqual(eval(""" r'\x81' """), chr(0x81)) 126 self.assertEqual(eval(""" r'\x81' """), chr(0x81))
113 self.assertEqual(eval(r""" r'\u1881' """), '\\' + 'u1881') 127 self.assertEqual(eval(r""" r'\u1881' """), '\\' + 'u1881')
114 self.assertEqual(eval(""" r'\u1881' """), chr(0x1881)) 128 self.assertEqual(eval(""" r'\u1881' """), chr(0x1881))
115 self.assertEqual(eval(r""" r'\U0001d120' """), '\\' + 'U0001d120') 129 self.assertEqual(eval(r""" r'\U0001d120' """), '\\' + 'U0001d120')
116 self.assertEqual(eval(""" r'\U0001d120' """), chr(0x1d120)) 130 self.assertEqual(eval(""" r'\U0001d120' """), chr(0x1d120))
117 131
118 def test_eval_bytes_normal(self): 132 def test_eval_bytes_normal(self):
119 self.assertEqual(eval(""" b'x' """), b'x') 133 self.assertEqual(eval(""" b'x' """), b'x')
120 self.assertEqual(eval(r""" b'\x01' """), byte(1)) 134 self.assertEqual(eval(r""" b'\x01' """), byte(1))
121 self.assertEqual(eval(""" b'\x01' """), byte(1)) 135 self.assertEqual(eval(""" b'\x01' """), byte(1))
122 self.assertEqual(eval(r""" b'\x81' """), byte(0x81)) 136 self.assertEqual(eval(r""" b'\x81' """), byte(0x81))
123 self.assertRaises(SyntaxError, eval, """ b'\x81' """) 137 self.assertRaises(SyntaxError, eval, """ b'\x81' """)
124 self.assertEqual(eval(r""" br'\u1881' """), b'\\' + b'u1881') 138 self.assertEqual(eval(r""" br'\u1881' """), b'\\' + b'u1881')
125 self.assertRaises(SyntaxError, eval, """ b'\u1881' """) 139 self.assertRaises(SyntaxError, eval, """ b'\u1881' """)
126 self.assertEqual(eval(r""" br'\U0001d120' """), b'\\' + b'U0001d120') 140 self.assertEqual(eval(r""" br'\U0001d120' """), b'\\' + b'U0001d120')
127 self.assertRaises(SyntaxError, eval, """ b'\U0001d120' """) 141 self.assertRaises(SyntaxError, eval, """ b'\U0001d120' """)
128 142
129 def test_eval_bytes_incomplete(self): 143 def test_eval_bytes_incomplete(self):
130 self.assertRaises(SyntaxError, eval, r""" b'\x' """) 144 self.assertRaises(SyntaxError, eval, r""" b'\x' """)
131 self.assertRaises(SyntaxError, eval, r""" b'\x0' """) 145 self.assertRaises(SyntaxError, eval, r""" b'\x0' """)
146
147 def test_eval_bytes_invalid_escape(self):
148 for b in range(1, 128):
149 if b in b"""\n\r"'01234567\\abfnrtvx""":
150 continue
151 with self.assertWarns(DeprecationWarning):
152 self.assertEqual(eval(r"b'\%c'" % b), b'\\' + bytes([b]))
153 with warnings.catch_warnings(record=True) as w:
154 warnings.simplefilter('always', category=DeprecationWarning)
155 eval("b'''\n\\z'''")
156 self.assertEqual(len(w), 1)
157 self.assertEqual(w[0].filename, '<string>')
158 self.assertEqual(w[0].lineno, 2)
132 159
133 def test_eval_bytes_raw(self): 160 def test_eval_bytes_raw(self):
134 self.assertEqual(eval(""" br'x' """), b'x') 161 self.assertEqual(eval(""" br'x' """), b'x')
135 self.assertEqual(eval(""" rb'x' """), b'x') 162 self.assertEqual(eval(""" rb'x' """), b'x')
136 self.assertEqual(eval(r""" br'\x01' """), b'\\' + b'x01') 163 self.assertEqual(eval(r""" br'\x01' """), b'\\' + b'x01')
137 self.assertEqual(eval(r""" rb'\x01' """), b'\\' + b'x01') 164 self.assertEqual(eval(r""" rb'\x01' """), b'\\' + b'x01')
138 self.assertEqual(eval(""" br'\x01' """), byte(1)) 165 self.assertEqual(eval(""" br'\x01' """), byte(1))
139 self.assertEqual(eval(""" rb'\x01' """), byte(1)) 166 self.assertEqual(eval(""" rb'\x01' """), byte(1))
140 self.assertEqual(eval(r""" br'\x81' """), b"\\" + b"x81") 167 self.assertEqual(eval(r""" br'\x81' """), b"\\" + b"x81")
141 self.assertEqual(eval(r""" rb'\x81' """), b"\\" + b"x81") 168 self.assertEqual(eval(r""" rb'\x81' """), b"\\" + b"x81")
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 220
194 def test_file_latin_1(self): 221 def test_file_latin_1(self):
195 self.check_encoding("latin-1") 222 self.check_encoding("latin-1")
196 223
197 def test_file_latin9(self): 224 def test_file_latin9(self):
198 self.check_encoding("latin9") 225 self.check_encoding("latin9")
199 226
200 227
201 if __name__ == "__main__": 228 if __name__ == "__main__":
202 unittest.main() 229 unittest.main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+