Debian Codegraph @

pypy/interpreter/pyparser/test/test_parsestring.py

raw symbols SCIP
1 from pypy.interpreter.pyparser import parsestring
2 from pypy.interpreter.pyparser.error import SyntaxError
3 from pypy.interpreter.pyparser.parser import Token
4
5 import py, sys
6
7 class TestParsetring:
8 def parse_and_compare(self, literal, value, encoding=None):
9 space = self.space
10 w_ret = parsestring.parsestr(space, encoding, literal)
11 if isinstance(value, str):
12 assert space.type(w_ret) == space.w_bytes
13 assert space.bytes_w(w_ret) == value
14 elif isinstance(value, unicode):
15 assert space.type(w_ret) == space.w_unicode
16 assert space.utf8_w(w_ret).decode('utf8') == value
17 else:
18 assert False
19
20 def test_simple(self):
21 space = self.space
22 for s in ['hello world', 'hello\n world']:
23 self.parse_and_compare('b' + repr(s), s)
24
25 self.parse_and_compare("b'''hello\\x42 world'''", 'hello\x42 world')
26
27 # octal
28 self.parse_and_compare(r'b"\0"', chr(0))
29 self.parse_and_compare(r'br"\0"', '\\0')
30 self.parse_and_compare(r'rb"\0"', '\\0')
31 self.parse_and_compare(r'b"\07"', chr(7))
32 self.parse_and_compare(r'b"\123"', chr(0123))
33 self.parse_and_compare(r'b"\400"', chr(0))
34 self.parse_and_compare(r'b"\9"', '\\' + '9')
35 self.parse_and_compare(r'b"\08"', chr(0) + '8')
36
37 # hexadecimal
38 self.parse_and_compare(r'b"\xfF"', chr(0xFF))
39 self.parse_and_compare(r'b"\""', '"')
40 self.parse_and_compare(r"b'\''", "'")
41 for s in (r'b"\x"', r'b"\x7"', r'b"\x7g"'):
42 space.raises_w(space.w_ValueError,
43 parsestring.parsestr, space, None, s)
44
45 # only ASCII characters are allowed in bytes literals (but of course
46 # you can use escapes to get the non-ASCII ones (note that in the
47 # second case we use a raw string, the the parser actually sees the
48 # chars '\' 'x' 'e' '9'
49 excinfo = py.test.raises(SyntaxError,
50 parsestring.parsestr, space, None,
51 "b' \xe9'", Token(None, -1, "b' \xe9'", 5, 0))
52 self.parse_and_compare(r"b'\xe9'", chr(0xE9))
53
54 def test_unicode(self):
55 for s in ['hello world', 'hello\n world']:
56 self.parse_and_compare(repr(s), unicode(s))
57
58 self.parse_and_compare("'''hello\\x42 world'''",
59 u'hello\x42 world')
60 self.parse_and_compare("'''hello\\u0842 world'''",
61 u'hello\u0842 world')
62
63 s = "u'\x81'"
64 s = s.decode("koi8-u").encode("utf8")[1:]
65 w_ret = parsestring.parsestr(self.space, 'koi8-u', s)
66 ret = w_ret._utf8.decode('utf8')
67 assert ret == eval("# -*- coding: koi8-u -*-\nu'\x81'")
68
69 def test_unicode_pep414(self):
70 space = self.space
71 for s in [u'hello world', u'hello\n world']:
72 self.parse_and_compare(repr(s), unicode(s))
73
74 self.parse_and_compare("u'''hello\\x42 world'''",
75 u'hello\x42 world')
76 self.parse_and_compare("u'''hello\\u0842 world'''",
77 u'hello\u0842 world')
78
79 space.raises_w(space.w_ValueError,
80 parsestring.parsestr, space, None, "ur'foo'")
81
82 def test_unicode_literals(self):
83 space = self.space
84 w_ret = parsestring.parsestr(space, None, repr("hello"))
85 assert space.isinstance_w(w_ret, space.w_unicode)
86 w_ret = parsestring.parsestr(space, None, "b'hi'")
87 assert space.isinstance_w(w_ret, space.w_bytes)
88 w_ret = parsestring.parsestr(space, None, "r'hi'")
89 assert space.isinstance_w(w_ret, space.w_unicode)
90
91 def test_raw_unicode_literals(self):
92 space = self.space
93 w_ret = parsestring.parsestr(space, None, "r'\u'")
94 assert space.int_w(space.len(w_ret)) == 2
95
96 def test_bytes(self):
97 space = self.space
98 b = "b'hello'"
99 w_ret = parsestring.parsestr(space, None, b)
100 assert space.unwrap(w_ret) == "hello"
101 b = "b'''hello'''"
102 w_ret = parsestring.parsestr(space, None, b)
103 assert space.unwrap(w_ret) == "hello"
104
105 def test_simple_enc_roundtrip(self):
106 space = self.space
107 s = "'\x81\\t'"
108 s = s.decode("koi8-u").encode("utf8")
109 w_ret = parsestring.parsestr(self.space, 'koi8-u', s)
110 ret = space.unwrap(w_ret)
111 assert ret == eval("# -*- coding: koi8-u -*-\nu'\x81\\t'")
112
113 def test_multiline_unicode_strings_with_backslash(self):
114 space = self.space
115 s = '"""' + '\\' + '\n"""'
116 w_ret = parsestring.parsestr(space, None, s)
117 assert space.text_w(w_ret) == ''
118
119 def test_bug1(self):
120 space = self.space
121 expected = ['x', ' ', chr(0xc3), chr(0xa9), ' ', '\n']
122 input = ["'", 'x', ' ', chr(0xc3), chr(0xa9), ' ', chr(92), 'n', "'"]
123 w_ret = parsestring.parsestr(space, 'utf8', ''.join(input))
124 assert space.text_w(w_ret) == ''.join(expected)
125
126 def test_wide_unicode_in_source(self):
127 if sys.maxunicode == 65535:
128 py.test.skip("requires a wide-unicode host")
129 self.parse_and_compare('"\xf0\x9f\x92\x8b"',
130 unichr(0x1f48b),
131 encoding='utf-8')
132
133 def test_decode_unicode_utf8(self):
134 buf = parsestring.decode_unicode_utf8(self.space,
135 'u"\xf0\x9f\x92\x8b"', 2, 6)
136 assert buf == r"\U0001f48b"
137
138 def test_parsestr_segfault(self):
139 space = self.space
140 space.raises_w(space.w_UnicodeDecodeError,
141 parsestring.parsestr, space, None, "r'\xc2'")