Visitar URL original
Merge pull request #1117 from silmeth/exc-repr · RustPython/RustPython@fabc260 · GitHub
Skip to content

Commit fabc260

Browse files
Merge pull request #1117 from silmeth/exc-repr
make exceptions repr tests Python 3.7-friendly by eval-repr round-trip
2 parents 18bb563 + fe9e711 commit fabc260

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

‎tests/snippets/exceptions.py‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
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+
return exceptions_eq(e, eval(repr(e)))
6+
17
# KeyError
28
empty_exc = KeyError()
39
assert str(empty_exc) == ''
4-
assert repr(empty_exc) == 'KeyError()'
10+
assert round_trip_repr(empty_exc)
511
assert len(empty_exc.args) == 0
612
assert type(empty_exc.args) == tuple
713

814
exc = KeyError('message')
915
assert str(exc) == "'message'"
10-
assert repr(exc) == "KeyError('message',)"
16+
assert round_trip_repr(exc)
1117

1218
exc = KeyError('message', 'another message')
1319
assert str(exc) == "('message', 'another message')"
14-
assert repr(exc) == "KeyError('message', 'another message')"
20+
assert round_trip_repr(exc)
1521
assert exc.args[0] == 'message'
1622
assert exc.args[1] == 'another message'
1723

1824
class A:
1925
def __repr__(self):
20-
return 'repr'
26+
return 'A()'
2127
def __str__(self):
2228
return 'str'
29+
def __eq__(self, other):
30+
return type(other) is A
2331

2432
exc = KeyError(A())
25-
assert str(exc) == 'repr'
26-
assert repr(exc) == 'KeyError(repr,)'
33+
assert str(exc) == 'A()'
34+
assert round_trip_repr(exc)
2735

2836
# ImportError / ModuleNotFoundError
2937
exc = ImportError()

‎tests/snippets/json_snippet.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def round_trip_test(obj):
55
# serde_json and Python's json module produce slightly differently spaced
66
# output; direct string comparison can't pass on both so we use this as a
77
# proxy
8-
assert obj == json.loads(json.dumps(obj))
8+
return obj == json.loads(json.dumps(obj))
99

1010
assert '"string"' == json.dumps("string")
1111
assert "1" == json.dumps(1)
@@ -17,7 +17,7 @@ def round_trip_test(obj):
1717
assert '[]' == json.dumps([])
1818
assert '[1]' == json.dumps([1])
1919
assert '[[1]]' == json.dumps([[1]])
20-
round_trip_test([1, "string", 1.0, True])
20+
assert round_trip_test([1, "string", 1.0, True])
2121

2222
assert '[]' == json.dumps(())
2323
assert '[1]' == json.dumps((1,))
@@ -26,7 +26,7 @@ def round_trip_test(obj):
2626
assert [1, "string", 1.0, True] == json.loads(json.dumps((1, "string", 1.0, True)))
2727

2828
assert '{}' == json.dumps({})
29-
round_trip_test({'a': 'b'})
29+
assert round_trip_test({'a': 'b'})
3030

3131
# should reject non-str keys in jsons
3232
assert_raises(json.JSONDecodeError, lambda: json.loads('{3: "abc"}'))
@@ -68,6 +68,6 @@ class Dict(dict): pass
6868
# big ints should not crash VM
6969
# TODO: test for correct output when actual serialization implemented and doesn’t throw
7070
try:
71-
json.dumps(7*500)
71+
json.dumps(7**500)
7272
except:
7373
pass

0 commit comments

Comments
 (0)