tests: fix stringify function (#4191)

The stackoverflow answer this was copied from has a glaring problem: python really dislikes a dictionary being modified while it is being iterated on. I rewrote the function to instead return a modified copy.
This commit is contained in:
Antoine Leblanc 2020-03-26 20:21:32 +00:00 committed by GitHub
parent b53533c381
commit b7dba573f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -406,26 +406,15 @@ def collapse_order_not_selset(result_inp, query):
# Use this since jsondiff seems to produce object/dict structures that can't
# always be serialized to json.
# Copy-pasta from: https://stackoverflow.com/q/12734517/176841
def stringify_keys(d):
"""Convert a dict's keys to strings if they are not."""
if isinstance(d, dict):
for key in d.keys():
# check inner dict
if isinstance(d[key], dict):
value = stringify_keys(d[key])
else:
value = d[key]
# convert nonstring to string if needed
if not isinstance(key, str):
try:
d[key.decode("utf-8")] = value
except Exception:
try:
d[repr(key)] = value
except Exception:
raise
"""Recursively convert a dict's keys to strings."""
if not isinstance(d, dict): return d
# delete old key
del d[key]
return d
def decode(k):
if isinstance(k, str): return k
try:
return k.decode("utf-8")
except Exception:
return repr(k)
return { decode(k): stringify_keys(v) for k, v in d.items() }