From 46665ce9f3b1e0494fa8bb592be6e7a1061ff032 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 18 Aug 2020 10:04:35 -0400 Subject: [PATCH 1/3] Do not emit Infinity / -Infinity / NaN values from canonicaljson. --- canonicaljson.py | 8 ++++++-- test_canonicaljson.py | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/canonicaljson.py b/canonicaljson.py index d1aad13..aab42e1 100644 --- a/canonicaljson.py +++ b/canonicaljson.py @@ -48,12 +48,16 @@ def set_json_library(json_lib): """ global _canonical_encoder _canonical_encoder = json_lib.JSONEncoder( - ensure_ascii=False, separators=(",", ":"), sort_keys=True, default=_default, + ensure_ascii=False, + allow_nan=False, + separators=(",", ":"), + sort_keys=True, + default=_default, ) global _pretty_encoder _pretty_encoder = json_lib.JSONEncoder( - ensure_ascii=False, indent=4, sort_keys=True, default=_default, + ensure_ascii=False, allow_nan=False, indent=4, sort_keys=True, default=_default, ) diff --git a/test_canonicaljson.py b/test_canonicaljson.py index 3b9aed8..68dd575 100644 --- a/test_canonicaljson.py +++ b/test_canonicaljson.py @@ -14,6 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +from math import inf, nan from canonicaljson import ( encode_canonical_json, @@ -114,6 +115,31 @@ class Unknown(object): with self.assertRaises(Exception): encode_canonical_json(unknown_object) + with self.assertRaises(Exception): + encode_pretty_printed_json(unknown_object) + + def test_invalid_float_values(self): + """Infinity/-Infinity/NaN are now allowed in canonicaljson. + """ + + with self.assertRaises(ValueError): + encode_canonical_json(inf) + + with self.assertRaises(ValueError): + encode_pretty_printed_json(inf) + + with self.assertRaises(ValueError): + encode_canonical_json(-inf) + + with self.assertRaises(ValueError): + encode_pretty_printed_json(-inf) + + with self.assertRaises(ValueError): + encode_canonical_json(nan) + + with self.assertRaises(ValueError): + encode_pretty_printed_json(nan) + def test_set_json(self): """Ensure that changing the underlying JSON implementation works.""" mock_json = mock.Mock(spec=["JSONEncoder"]) From c6a76dced1f3fbcf9a563c4dd2c2c6f97b3d214b Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 18 Aug 2020 10:19:27 -0400 Subject: [PATCH 2/3] Fix typo. Co-authored-by: David Vo --- test_canonicaljson.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_canonicaljson.py b/test_canonicaljson.py index 68dd575..359586d 100644 --- a/test_canonicaljson.py +++ b/test_canonicaljson.py @@ -119,7 +119,7 @@ class Unknown(object): encode_pretty_printed_json(unknown_object) def test_invalid_float_values(self): - """Infinity/-Infinity/NaN are now allowed in canonicaljson. + """Infinity/-Infinity/NaN are not allowed in canonicaljson. """ with self.assertRaises(ValueError): From c7cfec4b3afafebeca60414d2ee742aa56d0f6a6 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 20 Aug 2020 16:27:02 -0400 Subject: [PATCH 3/3] Add a note to the changelog. --- CHANGES.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 3252c11..0378bae 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,9 @@ +Version next + +* Fix producing non-standard JSON for Infinity, -Infinity, and NaN. This could + error when encoding objects into canonical JSON that previously used to work, + but were incompatible with JSON implementations in other languages. + Version 1.3.0 released 2020-08-14 * The minimum version of simplejson was bumped to 3.14.0.