blob: 30475a441b99861af5a331785d2197c0950eedb2 [file] [log] [blame]
Guido van Rossum29fd7122007-11-12 01:13:56 +00001r"""Test correct treatment of various string literals by the parser.
2
3There are four types of string literals:
4
5 'abc' -- normal str
6 r'abc' -- raw str
7 b'xyz' -- normal bytes
8 br'xyz' -- raw bytes
9
10The difference between normal and raw strings is of course that in a
11raw string, \ escapes (while still used to determine the end of the
12literal) are not interpreted, so that r'\x00' contains four
13characters: a backslash, an x, and two zeros; while '\x00' contains a
14single character (code point zero).
15
16The tricky thing is what should happen when non-ASCII bytes are used
17inside literals. For bytes literals, this is considered illegal. But
18for str literals, those bytes are supposed to be decoded using the
19encoding declared for the file (UTF-8 by default).
20
21We have to test this with various file encodings. We also test it with
22exec()/eval(), which uses a different code path.
23
24This file is really about correct treatment of encodings and
Ezio Melotti13925002011-03-16 11:05:33 +020025backslashes. It doesn't concern itself with issues like single
Guido van Rossum29fd7122007-11-12 01:13:56 +000026vs. double quotes or singly- vs. triply-quoted strings: that's dealt
27with elsewhere (I assume).
28"""
29
30import os
31import sys
32import shutil
33import tempfile
34import unittest
Antoine Pitroubbf53612012-01-12 22:36:48 +010035import test.support
Guido van Rossum29fd7122007-11-12 01:13:56 +000036
37
38TEMPLATE = r"""# coding: %s
39a = 'x'
40assert ord(a) == 120
41b = '\x01'
42assert ord(b) == 1
43c = r'\x01'
44assert list(map(ord, c)) == [92, 120, 48, 49]
45d = '\x81'
46assert ord(d) == 0x81
47e = r'\x81'
48assert list(map(ord, e)) == [92, 120, 56, 49]
49f = '\u1881'
50assert ord(f) == 0x1881
51g = r'\u1881'
52assert list(map(ord, g)) == [92, 117, 49, 56, 56, 49]
53"""
54
55
56def byte(i):
57 return bytes([i])
58
59
60class TestLiterals(unittest.TestCase):
61
62 def setUp(self):
63 self.save_path = sys.path[:]
64 self.tmpdir = tempfile.mkdtemp()
65 sys.path.insert(0, self.tmpdir)
66
67 def tearDown(self):
68 sys.path = self.save_path
69 shutil.rmtree(self.tmpdir, ignore_errors=True)
70
71 def test_template(self):
72 # Check that the template doesn't contain any non-printables
73 # except for \n.
74 for c in TEMPLATE:
75 assert c == '\n' or ' ' <= c <= '~', repr(c)
76
77 def test_eval_str_normal(self):
78 self.assertEqual(eval(""" 'x' """), 'x')
79 self.assertEqual(eval(r""" '\x01' """), chr(1))
80 self.assertEqual(eval(""" '\x01' """), chr(1))
81 self.assertEqual(eval(r""" '\x81' """), chr(0x81))
82 self.assertEqual(eval(""" '\x81' """), chr(0x81))
83 self.assertEqual(eval(r""" '\u1881' """), chr(0x1881))
84 self.assertEqual(eval(""" '\u1881' """), chr(0x1881))
85
86 def test_eval_str_raw(self):
87 self.assertEqual(eval(""" r'x' """), 'x')
88 self.assertEqual(eval(r""" r'\x01' """), '\\' + 'x01')
89 self.assertEqual(eval(""" r'\x01' """), chr(1))
90 self.assertEqual(eval(r""" r'\x81' """), '\\' + 'x81')
91 self.assertEqual(eval(""" r'\x81' """), chr(0x81))
92 self.assertEqual(eval(r""" r'\u1881' """), '\\' + 'u1881')
93 self.assertEqual(eval(""" r'\u1881' """), chr(0x1881))
94
95 def test_eval_bytes_normal(self):
96 self.assertEqual(eval(""" b'x' """), b'x')
97 self.assertEqual(eval(r""" b'\x01' """), byte(1))
98 self.assertEqual(eval(""" b'\x01' """), byte(1))
99 self.assertEqual(eval(r""" b'\x81' """), byte(0x81))
100 self.assertRaises(SyntaxError, eval, """ b'\x81' """)
101 self.assertEqual(eval(r""" b'\u1881' """), b'\\' + b'u1881')
102 self.assertRaises(SyntaxError, eval, """ b'\u1881' """)
103
104 def test_eval_bytes_raw(self):
105 self.assertEqual(eval(""" br'x' """), b'x')
106 self.assertEqual(eval(r""" br'\x01' """), b'\\' + b'x01')
107 self.assertEqual(eval(""" br'\x01' """), byte(1))
108 self.assertEqual(eval(r""" br'\x81' """), b"\\" + b"x81")
109 self.assertRaises(SyntaxError, eval, """ br'\x81' """)
110 self.assertEqual(eval(r""" br'\u1881' """), b"\\" + b"u1881")
111 self.assertRaises(SyntaxError, eval, """ br'\u1881' """)
112
113 def check_encoding(self, encoding, extra=""):
114 modname = "xx_" + encoding.replace("-", "_")
115 fn = os.path.join(self.tmpdir, modname + ".py")
116 f = open(fn, "w", encoding=encoding)
117 try:
118 f.write(TEMPLATE % encoding)
119 f.write(extra)
120 finally:
121 f.close()
122 __import__(modname)
123 del sys.modules[modname]
124
125 def test_file_utf_8(self):
126 extra = "z = '\u1234'; assert ord(z) == 0x1234\n"
127 self.check_encoding("utf-8", extra)
128
129 def test_file_utf_8_error(self):
130 extra = "b'\x80'\n"
131 self.assertRaises(SyntaxError, self.check_encoding, "utf-8", extra)
132
133 def test_file_utf8(self):
134 self.check_encoding("utf8")
135
136 def test_file_iso_8859_1(self):
137 self.check_encoding("iso-8859-1")
138
139 def test_file_latin_1(self):
140 self.check_encoding("latin-1")
141
142 def test_file_latin9(self):
143 self.check_encoding("latin9")
144
145
Antoine Pitroubbf53612012-01-12 22:36:48 +0100146def test_main():
147 test.support.run_unittest(__name__)
148
Guido van Rossum29fd7122007-11-12 01:13:56 +0000149if __name__ == "__main__":
Antoine Pitroubbf53612012-01-12 22:36:48 +0100150 test_main()