Debian Codegraph @

pypy/interpreter/test/test_error.py

raw symbols SCIP
1 # -*- encoding: utf-8 -*-
2
3 import py, os, errno
4 from pypy.interpreter.error import (
5 OperationError, decompose_valuefmt, get_operrcls2, new_exception_class,
6 oefmt, wrap_oserror, new_import_error, get_operr_withname_error_class)
7
8
9 def test_decompose_valuefmt():
10 assert (decompose_valuefmt("abc %s def") ==
11 (("abc ", " def"), ('s',)))
12 assert (decompose_valuefmt("%s%d%s") ==
13 (("", "", "", ""), ('s', 'd', 's')))
14 assert (decompose_valuefmt("%s%d%%%s") ==
15 (("", "", "%", ""), ('s', 'd', 's')))
16
17 def test_get_operrcls2(space):
18 cls, strings = get_operrcls2('abc %s def %d')
19 assert strings == ("abc ", " def ", "")
20 assert issubclass(cls, OperationError)
21 inst = cls("w_type", strings, "hello", 42)
22 assert inst._compute_value(space) == ("abc hello def 42", 16)
23 cls2, strings2 = get_operrcls2('a %s b %d c')
24 assert cls2 is cls # caching
25 assert strings2 == ("a ", " b ", " c")
26
27 def test_get_operr_withname_error_class(space):
28 cls, strings = get_operr_withname_error_class('abc %s def %s', 'AttributeError')
29 cls2, strings2 = get_operr_withname_error_class('abc %s def %s', 'AttributeError')
30 assert cls2 is cls
31 assert strings is strings2
32 cls3, strings3 = get_operr_withname_error_class('abc %s ghi %s', 'AttributeError')
33 assert cls3 is cls
34
35 def test_oefmt(space):
36 operr = oefmt("w_type", "abc %s def %d", "foo", 42)
37 assert isinstance(operr, OperationError)
38 assert operr.w_type == "w_type"
39 assert operr._w_value is None
40 val = operr._compute_value(space)
41 assert val == ("abc foo def 42", 14)
42 operr2 = oefmt("w_type2", "a %s b %d c", "bar", 43)
43 assert operr2.__class__ is operr.__class__
44 operr3 = oefmt("w_type2", "a %s b %s c", "bar", "4b")
45 assert operr3.__class__ is not operr.__class__
46
47 def test_oefmt_noargs(space):
48 operr = oefmt(space.w_AttributeError, "no attribute 'foo'")
49 operr.normalize_exception(space)
50 val = operr.get_w_value(space)
51 assert space.isinstance_w(val, space.w_AttributeError)
52 w_repr = space.repr(val)
53 assert space.text_w(w_repr) == "AttributeError(\"no attribute 'foo'\")"
54
55 def test_oefmt_T(space):
56 operr = oefmt(space.w_AttributeError,
57 "'%T' object has no attribute '%s'",
58 space.wrap('foo'), 'foo')
59 assert operr._compute_value(space) == ("'str' object has no attribute 'foo'", 35)
60 operr = oefmt("w_type",
61 "'%T' object has no attribute '%s'",
62 space.wrap('foo'), 'foo')
63 assert operr._compute_value(space) == ("'str' object has no attribute 'foo'", 35)
64
65 def test_oefmt_N(space):
66 operr = oefmt(space.w_AttributeError,
67 "'%N' object has no attribute '%s'",
68 space.type(space.wrap('foo')), 'foo')
69 assert operr._compute_value(space) == ("'str' object has no attribute 'foo'", 35)
70 operr = oefmt("w_type",
71 "'%N' object has no attribute '%s'",
72 space.type(space.wrap('foo')), 'foo')
73 assert operr._compute_value(space) == ("'str' object has no attribute 'foo'", 35)
74 operr = oefmt(space.w_AttributeError,
75 "'%N' object has no attribute '%s'",
76 space.wrap('foo'), 'foo')
77 assert operr._compute_value(space) == ("'?' object has no attribute 'foo'", 33)
78 operr = oefmt("w_type",
79 "'%N' object has no attribute '%s'",
80 space.wrap('foo'), 'foo')
81 assert operr._compute_value(space) == ("'?' object has no attribute 'foo'", 33)
82
83 def test_oefmt_R(space):
84 operr = oefmt(space.w_ValueError,
85 "illegal newline value: %R", space.wrap('foo'))
86 assert operr._compute_value(space) == ("illegal newline value: 'foo'", 28)
87 operr = oefmt(space.w_ValueError, "illegal newline value: %R",
88 space.wrap("'PyLadies'"))
89 expected = ("illegal newline value: \"'PyLadies'\"", 35)
90 assert operr._compute_value(space) == expected
91
92 def test_oefmt_unicode(space):
93 operr = oefmt("w_type", "abc %s", u"àèìòù")
94 val = operr._compute_value(space)
95 assert val == (u"abc àèìòù".encode('utf8'), 9)
96
97 def test_oefmt_utf8(space):
98 arg = u"àèìòù".encode('utf-8')
99 operr = oefmt("w_type", "abc %8", arg)
100 val = operr._compute_value(space)
101 assert val == (u"abc àèìòù".encode('utf8'), 9)
102 #
103 # if the arg is a byte string and we specify '%s', then we
104 # also get utf-8 encoding. This should be the common case
105 # nowadays with utf-8 byte strings being common in the RPython
106 # sources of PyPy.
107 operr = oefmt("w_type", "abc %s", arg)
108 val = operr._compute_value(space)
109 assert val == (u"abc àèìòù".encode('utf8'), 9)
110 #
111 # if the byte string is not valid utf-8, then don't crash
112 arg = '\xe9'
113 operr = oefmt("w_type", "abc %8", arg)
114 val = operr._compute_value(space)
115
116
117 def test_errorstr(space):
118 operr = OperationError(space.w_ValueError, space.wrap("message"))
119 assert operr.errorstr(space) == "ValueError: message"
120 assert operr.errorstr(space, use_repr=True) == (
121 "ValueError: ValueError('message')")
122 operr = OperationError(space.w_ValueError, space.w_None)
123 assert operr.errorstr(space) == "ValueError"
124 operr = OperationError(space.w_ValueError,
125 space.newtuple([space.wrap(6), space.wrap(7)]))
126 assert operr.errorstr(space) == "ValueError: (6, 7)"
127 operr = OperationError(space.w_UnicodeDecodeError,
128 space.newtuple([
129 space.wrap('unicodeescape'),
130 space.newbytes(r'\\x'),
131 space.newint(0),
132 space.newint(2),
133 space.wrap(r'truncated \\xXX escape')]))
134 assert operr.errorstr(space) == (
135 "UnicodeDecodeError: 'unicodeescape' codec can't decode "
136 "bytes in position 0-1: truncated \\\\xXX escape")
137
138 def test_wrap_oserror():
139 class FakeSpace:
140 w_OSError = [OSError]
141 w_EnvironmentError = [EnvironmentError]
142 w_None = None
143 def wrap(self, obj, lgt=-1):
144 return [obj]
145 newint = newtext = newfilename = wrap
146 def call_function(self, exc, w_errno, w_msg, w_filename=None, *args):
147 return (exc, w_errno, w_msg, w_filename)
148 def type(self, obj):
149 return obj
150 space = FakeSpace()
151 #
152 e = wrap_oserror(space, OSError(errno.EBADF, "foobar"))
153 assert isinstance(e, OperationError)
154 assert e.w_type[0] == [OSError]
155 assert e.get_w_value(space) == ([OSError], [errno.EBADF],
156 [os.strerror(errno.EBADF)], None)
157 #
158 e = wrap_oserror(space, OSError(errno.EBADF, "foobar"),
159 filename="test.py",
160 w_exception_class=space.w_EnvironmentError)
161 assert isinstance(e, OperationError)
162 assert e.w_type[0] == [EnvironmentError]
163 assert e.get_w_value(space) == ([EnvironmentError], [errno.EBADF],
164 [os.strerror(errno.EBADF)],
165 ["test.py"])
166 #
167 e = wrap_oserror(space, OSError(errno.EBADF, "foobar"),
168 filename="test.py",
169 w_exception_class=[SystemError])
170 assert isinstance(e, OperationError)
171 assert e.w_type[0] == [SystemError]
172 assert e.get_w_value(space) == ([SystemError], [errno.EBADF],
173 [os.strerror(errno.EBADF)],
174 ["test.py"])
175
176 def test_new_exception(space):
177 w_error = new_exception_class(space, '_socket.error')
178 assert w_error.getname(space) == u'error'
179 assert space.text_w(space.repr(w_error)) == "<class '_socket.error'>"
180 operr = OperationError(w_error, space.wrap("message"))
181 assert operr.match(space, w_error)
182 assert operr.match(space, space.w_Exception)
183
184 # subclass of ValueError
185 w_error = new_exception_class(space, 'error', space.w_ValueError)
186 operr = OperationError(w_error, space.wrap("message"))
187 assert operr.match(space, w_error)
188 assert operr.match(space, space.w_ValueError)
189
190 # subclass of (ValueError, TypeError)
191 w_bases = space.newtuple([space.w_ValueError, space.w_TypeError])
192 w_error = new_exception_class(space, 'error', w_bases)
193 operr = OperationError(w_error, space.wrap("message"))
194 assert operr.match(space, w_error)
195 assert operr.match(space, space.w_ValueError)
196 assert operr.match(space, space.w_TypeError)
197
198 def test_import_error(space):
199 w_exc = new_import_error(
200 space, space.wrap(u'msg'), space.wrap(u'name'), space.wrap(u'path'))
201 assert space.getattr(w_exc, space.wrap(u'name')).unwrap(space) == u'name'
202 assert space.getattr(w_exc, space.wrap(u'path')).unwrap(space) == u'path'