summaryrefslogtreecommitdiff
path: root/src/mailman/rest/helpers.py
diff options
context:
space:
mode:
authorBarry Warsaw2016-07-14 10:01:27 -0400
committerBarry Warsaw2016-07-14 10:01:27 -0400
commit3387791beb7112dbe07664041f117fdcc20df53d (patch)
tree9a5506f88f61f221b0ad62db974d7995790ad62e /src/mailman/rest/helpers.py
parent9b31555c180aa703d6fed0dc2cbaeda51de124c3 (diff)
downloadmailman-3387791beb7112dbe07664041f117fdcc20df53d.tar.gz
mailman-3387791beb7112dbe07664041f117fdcc20df53d.tar.zst
mailman-3387791beb7112dbe07664041f117fdcc20df53d.zip
Diffstat (limited to 'src/mailman/rest/helpers.py')
-rw-r--r--src/mailman/rest/helpers.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/mailman/rest/helpers.py b/src/mailman/rest/helpers.py
index 34fff3ba3..9ab1f434e 100644
--- a/src/mailman/rest/helpers.py
+++ b/src/mailman/rest/helpers.py
@@ -21,6 +21,7 @@ import json
import falcon
import hashlib
+from contextlib import suppress
from datetime import datetime, timedelta
from enum import Enum
from lazr.config import as_boolean
@@ -46,9 +47,20 @@ class ExtendedEncoder(json.JSONEncoder):
# It's up to the decoding validator to associate this name with
# the right Enum class.
return obj.name
+ elif isinstance(obj, bytes):
+ return bytes_to_str(obj)
return super().default(obj)
+def bytes_to_str(value):
+ # Convert a string to unicode when the encoding is not declared.
+ if not isinstance(value, bytes):
+ return value
+ for encoding in ('ascii', 'utf-8', 'raw_unicode_escape'):
+ with suppress(UnicodeDecodeError):
+ return value.decode(encoding)
+
+
@public
def etag(resource):
"""Calculate the etag and return a JSON representation.