From d5b48e65c7dd1f8d6943c85602aed1c8114f9a42 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Fri, 27 Aug 2021 10:14:39 -0400 Subject: [PATCH] Fix bug in jsonbytes. --- src/allmydata/test/test_util.py | 6 ++++++ src/allmydata/util/jsonbytes.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/allmydata/test/test_util.py b/src/allmydata/test/test_util.py index 251a60d70..50532274a 100644 --- a/src/allmydata/test/test_util.py +++ b/src/allmydata/test/test_util.py @@ -558,6 +558,12 @@ class JSONBytes(unittest.TestCase): expected ) + def test_dumps_bytes_unicode_separators(self): + """Unicode separators don't prevent the result from being bytes.""" + result = jsonbytes.dumps_bytes([1, 2], separators=(u',', u':')) + self.assertIsInstance(result, bytes) + self.assertEqual(result, b"[1,2]") + class FakeGetVersion(object): """Emulate an object with a get_version.""" diff --git a/src/allmydata/util/jsonbytes.py b/src/allmydata/util/jsonbytes.py index 8ddc4c453..159ae7990 100644 --- a/src/allmydata/util/jsonbytes.py +++ b/src/allmydata/util/jsonbytes.py @@ -109,7 +109,7 @@ def dumps_bytes(obj, *args, **kwargs): human consumption. """ result = dumps(obj, *args, **kwargs) - if PY3: + if PY3 or isinstance(result, str): result = result.encode("utf-8") return result