|
2 | 2 | import platform |
3 | 3 | import sys |
4 | 4 |
|
| 5 | +def exceptions_eq(e1, e2): |
| 6 | + return type(e1) is type(e2) and e1.args == e2.args |
| 7 | + |
| 8 | +def round_trip_repr(e): |
| 9 | + return exceptions_eq(e, eval(repr(e))) |
| 10 | + |
| 11 | +# KeyError |
| 12 | +empty_exc = KeyError() |
| 13 | +assert str(empty_exc) == '' |
| 14 | +assert round_trip_repr(empty_exc) |
| 15 | +assert len(empty_exc.args) == 0 |
| 16 | +assert type(empty_exc.args) == tuple |
| 17 | + |
| 18 | +exc = KeyError('message') |
| 19 | +assert str(exc) == "'message'" |
| 20 | +assert round_trip_repr(exc) |
| 21 | + |
| 22 | +assert LookupError.__str__(exc) == "message" |
| 23 | + |
| 24 | +exc = KeyError('message', 'another message') |
| 25 | +assert str(exc) == "('message', 'another message')" |
| 26 | +assert round_trip_repr(exc) |
| 27 | +assert exc.args[0] == 'message' |
| 28 | +assert exc.args[1] == 'another message' |
| 29 | + |
| 30 | +class A: |
| 31 | + def __repr__(self): |
| 32 | + return 'A()' |
| 33 | + def __str__(self): |
| 34 | + return 'str' |
| 35 | + def __eq__(self, other): |
| 36 | + return type(other) is A |
| 37 | + |
| 38 | +exc = KeyError(A()) |
| 39 | +assert str(exc) == 'A()' |
| 40 | +assert round_trip_repr(exc) |
| 41 | + |
| 42 | +# ImportError / ModuleNotFoundError |
| 43 | +exc = ImportError() |
| 44 | +assert exc.name is None |
| 45 | +assert exc.path is None |
| 46 | +assert exc.msg is None |
| 47 | +assert exc.args == () |
| 48 | + |
| 49 | +exc = ImportError('hello') |
| 50 | +assert exc.name is None |
| 51 | +assert exc.path is None |
| 52 | +assert exc.msg == 'hello' |
| 53 | +assert exc.args == ('hello',) |
| 54 | + |
| 55 | +exc = ImportError('hello', name='name', path='path') |
| 56 | +assert exc.name == 'name' |
| 57 | +assert exc.path == 'path' |
| 58 | +assert exc.msg == 'hello' |
| 59 | +assert exc.args == ('hello',) |
| 60 | + |
| 61 | + |
| 62 | +class NewException(Exception): |
| 63 | + |
| 64 | + def __init__(self, value): |
| 65 | + self.value = value |
| 66 | + |
| 67 | + |
| 68 | +try: |
| 69 | + raise NewException("test") |
| 70 | +except NewException as e: |
| 71 | + assert e.value == "test" |
| 72 | + |
| 73 | + |
| 74 | +exc = SyntaxError('msg', 1, 2, 3, 4, 5) |
| 75 | +assert exc.msg == 'msg' |
| 76 | +assert exc.filename is None |
| 77 | +assert exc.lineno is None |
| 78 | +assert exc.offset is None |
| 79 | +assert exc.text is None |
5 | 80 |
|
6 | 81 | # Regression to: |
7 | 82 | # https://github.com/RustPython/RustPython/issues/2779 |
|
0 commit comments