diff options
author | Oren Milman <orenmn@gmail.com> | 2017-09-24 09:07:12 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2017-09-24 09:07:12 (GMT) |
commit | 2b382dd6121bb1e4b75470fb3ef8555665df3eb6 (patch) | |
tree | 72d277a9137185ba934c537d22b9907f77a06e9d /Lib | |
parent | 039b25d8fd21f8d5d9e3cb536402d952cf068dc1 (diff) | |
download | cpython-2b382dd6121bb1e4b75470fb3ef8555665df3eb6.zip cpython-2b382dd6121bb1e4b75470fb3ef8555665df3eb6.tar.gz cpython-2b382dd6121bb1e4b75470fb3ef8555665df3eb6.tar.bz2 |
bpo-31505: Fix an assertion failure in json, in case _json.make_encoder() received a bad encoder() argument. (#3643)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_json/test_speedups.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/Lib/test/test_json/test_speedups.py b/Lib/test/test_json/test_speedups.py index 56f1882..5dad692 100644 --- a/Lib/test/test_json/test_speedups.py +++ b/Lib/test/test_json/test_speedups.py @@ -36,6 +36,27 @@ class TestEncode(CTest): b"\xCD\x7D\x3D\x4E\x12\x4C\xF9\x79\xD7\x52\xBA\x82\xF2\x27\x4A\x7D\xA0\xCA\x75", None) + def test_bad_str_encoder(self): + # Issue #31505: There shouldn't be an assertion failure in case + # c_make_encoder() receives a bad encoder() argument. + def bad_encoder1(*args): + return None + enc = self.json.encoder.c_make_encoder(None, lambda obj: str(obj), + bad_encoder1, None, ': ', ', ', + False, False, False) + with self.assertRaises(TypeError): + enc('spam', 4) + with self.assertRaises(TypeError): + enc({'spam': 42}, 4) + + def bad_encoder2(*args): + 1/0 + enc = self.json.encoder.c_make_encoder(None, lambda obj: str(obj), + bad_encoder2, None, ': ', ', ', + False, False, False) + with self.assertRaises(ZeroDivisionError): + enc('spam', 4) + def test_bad_bool_args(self): def test(name): self.json.encoder.JSONEncoder(**{name: BadBool()}).encode({'a': 1}) |