forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
75 lines (58 loc) · 1.63 KB
/
exceptions.py
File metadata and controls
75 lines (58 loc) · 1.63 KB
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
def exceptions_eq(e1, e2):
return type(e1) is type(e2) and e1.args == e2.args
def round_trip_repr(e):
return exceptions_eq(e, eval(repr(e)))
# KeyError
empty_exc = KeyError()
assert str(empty_exc) == ''
assert round_trip_repr(empty_exc)
assert len(empty_exc.args) == 0
assert type(empty_exc.args) == tuple
exc = KeyError('message')
assert str(exc) == "'message'"
assert round_trip_repr(exc)
assert LookupError.__str__(exc) == "message"
exc = KeyError('message', 'another message')
assert str(exc) == "('message', 'another message')"
assert round_trip_repr(exc)
assert exc.args[0] == 'message'
assert exc.args[1] == 'another message'
class A:
def __repr__(self):
return 'A()'
def __str__(self):
return 'str'
def __eq__(self, other):
return type(other) is A
exc = KeyError(A())
assert str(exc) == 'A()'
assert round_trip_repr(exc)
# ImportError / ModuleNotFoundError
exc = ImportError()
assert exc.name is None
assert exc.path is None
assert exc.msg is None
assert exc.args == ()
exc = ImportError('hello')
assert exc.name is None
assert exc.path is None
assert exc.msg == 'hello'
assert exc.args == ('hello',)
exc = ImportError('hello', name='name', path='path')
assert exc.name == 'name'
assert exc.path == 'path'
assert exc.msg == 'hello'
assert exc.args == ('hello',)
class NewException(Exception):
def __init__(self, value):
self.value = value
try:
raise NewException("test")
except NewException as e:
assert e.value == "test"
exc = SyntaxError('msg', 1, 2, 3, 4, 5)
assert exc.msg == 'msg'
assert exc.filename is None
assert exc.lineno is None
assert exc.offset is None
assert exc.text is None