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)))
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)
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)
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',)