diff options
| author | Barry Warsaw | 2016-07-14 10:01:27 -0400 |
|---|---|---|
| committer | Barry Warsaw | 2016-07-14 10:01:27 -0400 |
| commit | 3387791beb7112dbe07664041f117fdcc20df53d (patch) | |
| tree | 9a5506f88f61f221b0ad62db974d7995790ad62e /src/mailman/rest/helpers.py | |
| parent | 9b31555c180aa703d6fed0dc2cbaeda51de124c3 (diff) | |
| download | mailman-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.py | 12 |
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. |
