File: test_error.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (193 lines) | stat: -rw-r--r-- 8,162 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# -*- encoding: utf-8 -*-

import py, os, errno
from pypy.interpreter.error import (
    OperationError, decompose_valuefmt, get_operrcls2, new_exception_class,
    oefmt, wrap_oserror, new_import_error)


def test_decompose_valuefmt():
    assert (decompose_valuefmt("abc %s def") ==
            (("abc ", " def"), ('s',)))
    assert (decompose_valuefmt("%s%d%s") ==
            (("", "", "", ""), ('s', 'd', 's')))
    assert (decompose_valuefmt("%s%d%%%s") ==
            (("", "", "%", ""), ('s', 'd', 's')))

def test_get_operrcls2(space):
    cls, strings = get_operrcls2('abc %s def %d')
    assert strings == ("abc ", " def ", "")
    assert issubclass(cls, OperationError)
    inst = cls("w_type", strings, "hello", 42)
    assert inst._compute_value(space) == "abc hello def 42"
    cls2, strings2 = get_operrcls2('a %s b %d c')
    assert cls2 is cls     # caching
    assert strings2 == ("a ", " b ", " c")

def test_oefmt(space):
    operr = oefmt("w_type", "abc %s def %d", "foo", 42)
    assert isinstance(operr, OperationError)
    assert operr.w_type == "w_type"
    assert operr._w_value is None
    val = operr._compute_value(space)
    assert val == u"abc foo def 42"
    assert isinstance(val, unicode)
    operr2 = oefmt("w_type2", "a %s b %d c", "bar", 43)
    assert operr2.__class__ is operr.__class__
    operr3 = oefmt("w_type2", "a %s b %s c", "bar", "4b")
    assert operr3.__class__ is not operr.__class__

def test_oefmt_noargs(space):
    operr = oefmt(space.w_AttributeError, "no attribute 'foo'")
    operr.normalize_exception(space)
    val = operr.get_w_value(space)
    assert space.isinstance_w(val, space.w_AttributeError)
    w_repr = space.repr(val)
    assert space.str_w(w_repr) == "AttributeError(\"no attribute 'foo'\",)"

def test_oefmt_T(space):
    operr = oefmt(space.w_AttributeError,
                  "'%T' object has no attribute '%s'",
                  space.wrap('foo'), 'foo')
    assert operr._compute_value(space) == "'str' object has no attribute 'foo'"
    operr = oefmt("w_type",
                  "'%T' object has no attribute '%s'",
                  space.wrap('foo'), 'foo')
    assert operr._compute_value(space) == "'str' object has no attribute 'foo'"

def test_oefmt_N(space):
    operr = oefmt(space.w_AttributeError,
                  "'%N' object has no attribute '%s'",
                  space.type(space.wrap('foo')), 'foo')
    assert operr._compute_value(space) == "'str' object has no attribute 'foo'"
    operr = oefmt("w_type",
                  "'%N' object has no attribute '%s'",
                  space.type(space.wrap('foo')), 'foo')
    assert operr._compute_value(space) == "'str' object has no attribute 'foo'"
    operr = oefmt(space.w_AttributeError,
                  "'%N' object has no attribute '%s'",
                  space.wrap('foo'), 'foo')
    assert operr._compute_value(space) == "'?' object has no attribute 'foo'"
    operr = oefmt("w_type",
                  "'%N' object has no attribute '%s'",
                  space.wrap('foo'), 'foo')
    assert operr._compute_value(space) == "'?' object has no attribute 'foo'"

def test_oefmt_R(space):
    operr = oefmt(space.w_ValueError,
                  "illegal newline value: %R", space.wrap('foo'))
    assert operr._compute_value(space) == "illegal newline value: 'foo'"
    operr = oefmt(space.w_ValueError, "illegal newline value: %R",
                  space.wrap("'PyLadies'"))
    expected = "illegal newline value: \"'PyLadies'\""
    assert operr._compute_value(space) == expected

def test_oefmt_unicode(space):
    operr = oefmt("w_type", "abc %s", u"àèìòù")
    val = operr._compute_value(space)
    assert val == u"abc àèìòù"

def test_oefmt_utf8(space):
    arg = u"àèìòù".encode('utf-8')
    operr = oefmt("w_type", "abc %8", arg)
    val = operr._compute_value(space)
    assert val == u"abc àèìòù"
    #
    # if the arg is a byte string and we specify '%s', then we
    # also get utf-8 encoding.  This should be the common case
    # nowadays with utf-8 byte strings being common in the RPython
    # sources of PyPy.
    operr = oefmt("w_type", "abc %s", arg)
    val = operr._compute_value(space)
    assert val == u"abc àèìòù"
    #
    # if the byte string is not valid utf-8, then don't crash
    arg = '\xe9'
    operr = oefmt("w_type", "abc %8", arg)
    val = operr._compute_value(space)


def test_errorstr(space):
    operr = OperationError(space.w_ValueError, space.wrap("message"))
    assert operr.errorstr(space) == "ValueError: message"
    assert operr.errorstr(space, use_repr=True) == (
        "ValueError: ValueError('message',)")
    operr = OperationError(space.w_ValueError, space.w_None)
    assert operr.errorstr(space) == "ValueError"
    operr = OperationError(space.w_ValueError,
        space.newtuple([space.wrap(6), space.wrap(7)]))
    assert operr.errorstr(space) == "ValueError: (6, 7)"
    operr = OperationError(space.w_UnicodeDecodeError,
        space.newtuple([
            space.wrap('unicodeescape'),
            space.newbytes(r'\\x'),
            space.newint(0),
            space.newint(2),
            space.wrap(r'truncated \\xXX escape')]))
    assert operr.errorstr(space) == (
        "UnicodeDecodeError: 'unicodeescape' codec can't decode "
        "bytes in position 0-1: truncated \\\\xXX escape")

def test_wrap_oserror():
    class FakeSpace:
        w_OSError = [OSError]
        w_EnvironmentError = [EnvironmentError]
        w_None = None
        def wrap(self, obj):
            return [obj]
        newint = newtext = newunicode = newfilename = wrap
        def call_function(self, exc, w_errno, w_msg, w_filename=None, *args):
            return (exc, w_errno, w_msg, w_filename)
    space = FakeSpace()
    #
    e = wrap_oserror(space, OSError(errno.EBADF, "foobar"))
    assert isinstance(e, OperationError)
    assert e.w_type == [OSError]
    assert e.get_w_value(space) == ([OSError], [errno.EBADF],
                                    [os.strerror(errno.EBADF)], None)
    #
    e = wrap_oserror(space, OSError(errno.EBADF, "foobar"),
                     filename = "test.py",
                     exception_name = "w_EnvironmentError")
    assert isinstance(e, OperationError)
    assert e.w_type == [EnvironmentError]
    assert e.get_w_value(space) == ([EnvironmentError], [errno.EBADF],
                                    [os.strerror(errno.EBADF)],
                                    ["test.py"])
    #
    e = wrap_oserror(space, OSError(errno.EBADF, "foobar"),
                     filename = "test.py",
                     w_exception_class = [SystemError])
    assert isinstance(e, OperationError)
    assert e.w_type == [SystemError]
    assert e.get_w_value(space) == ([SystemError], [errno.EBADF],
                                    [os.strerror(errno.EBADF)],
                                    ["test.py"])

def test_new_exception(space):
    w_error = new_exception_class(space, '_socket.error')
    assert w_error.getname(space) == u'error'
    assert space.str_w(space.repr(w_error)) == "<class '_socket.error'>"
    operr = OperationError(w_error, space.wrap("message"))
    assert operr.match(space, w_error)
    assert operr.match(space, space.w_Exception)

    # subclass of ValueError
    w_error = new_exception_class(space, 'error', space.w_ValueError)
    operr = OperationError(w_error, space.wrap("message"))
    assert operr.match(space, w_error)
    assert operr.match(space, space.w_ValueError)

    # subclass of (ValueError, TypeError)
    w_bases = space.newtuple([space.w_ValueError, space.w_TypeError])
    w_error = new_exception_class(space, 'error', w_bases)
    operr = OperationError(w_error, space.wrap("message"))
    assert operr.match(space, w_error)
    assert operr.match(space, space.w_ValueError)
    assert operr.match(space, space.w_TypeError)

def test_import_error(space):
    w_exc = new_import_error(
        space, space.wrap(u'msg'), space.wrap(u'name'), space.wrap(u'path'))
    assert space.getattr(w_exc, space.wrap(u'name')).unwrap(space) == u'name'
    assert space.getattr(w_exc, space.wrap(u'path')).unwrap(space) == u'path'