File tree Expand file tree Collapse file tree 1 file changed +14
-6
lines changed Expand file tree Collapse file tree 1 file changed +14
-6
lines changed Original file line number Diff line number Diff line change
1
+ def exceptions_eq (e1 , e2 ):
2
+ return type (e1 ) is type (e2 ) and e1 .args == e2 .args
3
+
4
+ def round_trip_repr (e ):
5
+ assert exceptions_eq (e , eval (repr (e )))
6
+
1
7
# KeyError
2
8
empty_exc = KeyError ()
3
9
assert str (empty_exc ) == ''
4
- assert repr (empty_exc ) == 'KeyError()'
10
+ round_trip_repr (empty_exc )
5
11
assert len (empty_exc .args ) == 0
6
12
assert type (empty_exc .args ) == tuple
7
13
8
14
exc = KeyError ('message' )
9
15
assert str (exc ) == "'message'"
10
- assert repr (exc ) == "KeyError('message',)"
16
+ round_trip_repr (exc )
11
17
12
18
exc = KeyError ('message' , 'another message' )
13
19
assert str (exc ) == "('message', 'another message')"
14
- assert repr (exc ) == "KeyError('message', 'another message')"
20
+ round_trip_repr (exc )
15
21
assert exc .args [0 ] == 'message'
16
22
assert exc .args [1 ] == 'another message'
17
23
18
24
class A :
19
25
def __repr__ (self ):
20
- return 'repr '
26
+ return 'A() '
21
27
def __str__ (self ):
22
28
return 'str'
29
+ def __eq__ (self , other ):
30
+ return type (other ) is A
23
31
24
32
exc = KeyError (A ())
25
- assert str (exc ) == 'repr '
26
- assert repr (exc ) == 'KeyError(repr,)'
33
+ assert str (exc ) == 'A() '
34
+ round_trip_repr (exc )
27
35
28
36
# ImportError / ModuleNotFoundError
29
37
exc = ImportError ()
You can’t perform that action at this time.
0 commit comments