From 9fee59e4831f8903a5a903dd2b0d68b6d996d91b Mon Sep 17 00:00:00 2001 From: silmeth Date: Sun, 7 Jul 2019 15:39:17 +0200 Subject: [PATCH 1/3] make exceptions repr tests Python 3.7-friendly by eval-repr round-trip --- tests/snippets/exceptions.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/tests/snippets/exceptions.py b/tests/snippets/exceptions.py index cb178fc066..1ce88f1f59 100644 --- a/tests/snippets/exceptions.py +++ b/tests/snippets/exceptions.py @@ -1,29 +1,37 @@ +def exceptions_eq(e1, e2): + return type(e1) is type(e2) and e1.args == e2.args + +def round_trip_repr(e): + assert exceptions_eq(e, eval(repr(e))) + # KeyError empty_exc = KeyError() assert str(empty_exc) == '' -assert repr(empty_exc) == 'KeyError()' +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 repr(exc) == "KeyError('message',)" +round_trip_repr(exc) exc = KeyError('message', 'another message') assert str(exc) == "('message', 'another message')" -assert repr(exc) == "KeyError('message', 'another message')" +round_trip_repr(exc) assert exc.args[0] == 'message' assert exc.args[1] == 'another message' class A: def __repr__(self): - return 'repr' + return 'A()' def __str__(self): return 'str' + def __eq__(self, other): + return type(other) is A exc = KeyError(A()) -assert str(exc) == 'repr' -assert repr(exc) == 'KeyError(repr,)' +assert str(exc) == 'A()' +round_trip_repr(exc) # ImportError / ModuleNotFoundError exc = ImportError() From 1b539dedd0ee3d463095f6e725ba27185cbb6194 Mon Sep 17 00:00:00 2001 From: silmeth Date: Sun, 7 Jul 2019 17:13:57 +0200 Subject: [PATCH 2/3] move asserts out of round trip test utils --- tests/snippets/exceptions.py | 10 +++++----- tests/snippets/json_snippet.py | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/snippets/exceptions.py b/tests/snippets/exceptions.py index 1ce88f1f59..fed8d32093 100644 --- a/tests/snippets/exceptions.py +++ b/tests/snippets/exceptions.py @@ -2,22 +2,22 @@ def exceptions_eq(e1, e2): return type(e1) is type(e2) and e1.args == e2.args def round_trip_repr(e): - assert exceptions_eq(e, eval(repr(e))) + return exceptions_eq(e, eval(repr(e))) # KeyError empty_exc = KeyError() assert str(empty_exc) == '' -round_trip_repr(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'" -round_trip_repr(exc) +assert round_trip_repr(exc) exc = KeyError('message', 'another message') assert str(exc) == "('message', 'another message')" -round_trip_repr(exc) +assert round_trip_repr(exc) assert exc.args[0] == 'message' assert exc.args[1] == 'another message' @@ -31,7 +31,7 @@ def __eq__(self, other): exc = KeyError(A()) assert str(exc) == 'A()' -round_trip_repr(exc) +assert round_trip_repr(exc) # ImportError / ModuleNotFoundError exc = ImportError() diff --git a/tests/snippets/json_snippet.py b/tests/snippets/json_snippet.py index a8c0ecc19e..9da6b44697 100644 --- a/tests/snippets/json_snippet.py +++ b/tests/snippets/json_snippet.py @@ -5,7 +5,7 @@ def round_trip_test(obj): # serde_json and Python's json module produce slightly differently spaced # output; direct string comparison can't pass on both so we use this as a # proxy - assert obj == json.loads(json.dumps(obj)) + return obj == json.loads(json.dumps(obj)) assert '"string"' == json.dumps("string") assert "1" == json.dumps(1) @@ -17,7 +17,7 @@ def round_trip_test(obj): assert '[]' == json.dumps([]) assert '[1]' == json.dumps([1]) assert '[[1]]' == json.dumps([[1]]) -round_trip_test([1, "string", 1.0, True]) +assert round_trip_test([1, "string", 1.0, True]) assert '[]' == json.dumps(()) assert '[1]' == json.dumps((1,)) @@ -26,7 +26,7 @@ def round_trip_test(obj): assert [1, "string", 1.0, True] == json.loads(json.dumps((1, "string", 1.0, True))) assert '{}' == json.dumps({}) -round_trip_test({'a': 'b'}) +assert round_trip_test({'a': 'b'}) # should reject non-str keys in jsons assert_raises(json.JSONDecodeError, lambda: json.loads('{3: "abc"}')) From fe9e71167fee9a693b962cc7223482ffa8d9de4f Mon Sep 17 00:00:00 2001 From: silmeth Date: Sun, 7 Jul 2019 17:17:08 +0200 Subject: [PATCH 3/3] =?UTF-8?q?fix=20big=20int=20crash=20in=20json=5Fsnipp?= =?UTF-8?q?ets=20=E2=80=93=20actually=20use=20a=20big=20integer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/snippets/json_snippet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/snippets/json_snippet.py b/tests/snippets/json_snippet.py index 9da6b44697..15866ab86e 100644 --- a/tests/snippets/json_snippet.py +++ b/tests/snippets/json_snippet.py @@ -68,6 +68,6 @@ class Dict(dict): pass # big ints should not crash VM # TODO: test for correct output when actual serialization implemented and doesn’t throw try: - json.dumps(7*500) + json.dumps(7**500) except: pass