doing an import in an exception handling path doesn't seem right. other than that, good work.
Diff comments: > diff --git a/cloudinit/util.py b/cloudinit/util.py > index aa23b3f..49433dc 100644 > --- a/cloudinit/util.py > +++ b/cloudinit/util.py > @@ -1599,10 +1599,36 @@ 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 as e: > + from cloudinit.sources import process_instance_metadata i dont like the late import here. > + if sys.version_info[:2] == (2, 7): > + data = json_preserialize_binary(data) > + data = process_instance_metadata(data) > + return json.dumps( can you just call json_dumps ? as you cleaded it, it should work now. > + data, indent=1, sort_keys=True, separators=(',', ': '), > + default=json_serialize_default) don't you need to raise /re-raise if not? > > > def yaml_dumps(obj, explicit_start=True, explicit_end=True, noalias=False): -- https://code.launchpad.net/~chad.smith/cloud-init/+git/cloud-init/+merge/373291 Your team cloud-init Commiters is requested to review the proposed merge of ~chad.smith/cloud-init:bug/180134-openstack-random-seed-encoding into cloud-init:master. _______________________________________________ Mailing list: https://launchpad.net/~cloud-init-dev Post to : cloud-init-dev@lists.launchpad.net Unsubscribe : https://launchpad.net/~cloud-init-dev More help : https://help.launchpad.net/ListHelp