diff options
| author | Chad Smith <chad.smith@canonical.com> | 2019-09-27 20:46:00 +0000 |
|---|---|---|
| committer | Server Team CI Bot <josh.powers+server-team-bot@canonical.com> | 2019-09-27 20:46:00 +0000 |
| commit | 067516d7bc917e4921b9f1424b7a64e92cae0ad2 (patch) | |
| tree | 79063a27ea9cf596e7dcbe3e8ba7452abb9b84c8 /cloudinit/util.py | |
| parent | 052d655cfba37243396c491a1e7892ba3736ab6f (diff) | |
| download | vyos-cloud-init-067516d7bc917e4921b9f1424b7a64e92cae0ad2.tar.gz vyos-cloud-init-067516d7bc917e4921b9f1424b7a64e92cae0ad2.zip | |
util: json.dumps on python 2.7 will handle UnicodeDecodeError on binary
Since python 2.7 doesn't handle UnicodeDecodeErrors with the default
handler
LP: #1801364
Diffstat (limited to 'cloudinit/util.py')
| -rw-r--r-- | cloudinit/util.py | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py index aa23b3f3..6e8e73b0 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1599,10 +1599,33 @@ def json_serialize_default(_obj): return 'Warning: redacted unserializable type {0}'.format(type(_obj)) +def json_preserialize_binary(data): + """Preserialize any discovered binary values to avoid json.dumps issues. + + Used only on python 2.7 where default type handling is not honored for + failure to encode binary data. LP: #1801364. + TODO(Drop this function when py2.7 support is dropped from cloud-init) + """ + data = obj_copy.deepcopy(data) + for key, value in data.items(): + if isinstance(value, (dict)): + data[key] = json_preserialize_binary(value) + if isinstance(value, bytes): + data[key] = 'ci-b64:{0}'.format(b64e(value)) + return data + + def json_dumps(data): """Return data in nicely formatted json.""" - return json.dumps(data, indent=1, sort_keys=True, - separators=(',', ': '), default=json_serialize_default) + try: + return json.dumps( + data, indent=1, sort_keys=True, separators=(',', ': '), + default=json_serialize_default) + except UnicodeDecodeError: + if sys.version_info[:2] == (2, 7): + data = json_preserialize_binary(data) + return json.dumps(data) + raise def yaml_dumps(obj, explicit_start=True, explicit_end=True, noalias=False): |
