##// END OF EJS Templates
Backport PR #6081: don’t modify dict keys while iterating through them...
MinRK -
Show More
@@ -39,22 +39,18 b' datetime.strptime("1", "%d")'
39 def rekey(dikt):
39 def rekey(dikt):
40 """Rekey a dict that has been forced to use str keys where there should be
40 """Rekey a dict that has been forced to use str keys where there should be
41 ints by json."""
41 ints by json."""
42 for k in dikt:
42 for k in list(dikt):
43 if isinstance(k, string_types):
43 if isinstance(k, string_types):
44 ik=fk=None
44 nk = None
45 try:
45 try:
46 ik = int(k)
46 nk = int(k)
47 except ValueError:
47 except ValueError:
48 try:
48 try:
49 fk = float(k)
49 nk = float(k)
50 except ValueError:
50 except ValueError:
51 continue
51 continue
52 if ik is not None:
53 nk = ik
54 else:
55 nk = fk
56 if nk in dikt:
52 if nk in dikt:
57 raise KeyError("already have key %r"%nk)
53 raise KeyError("already have key %r" % nk)
58 dikt[nk] = dikt.pop(k)
54 dikt[nk] = dikt.pop(k)
59 return dikt
55 return dikt
60
56
@@ -53,6 +53,13 b' def test():'
53 json.loads(json.dumps(out))
53 json.loads(json.dumps(out))
54
54
55
55
56 def test_rekey():
57 # This could fail due to modifying the dict keys in-place on Python 3
58 d = { i:i for i in map(str, range(128)) }
59 d = jsonutil.rekey(d)
60 for key in d:
61 nt.assert_is_instance(key, int)
62
56
63
57 def test_encode_images():
64 def test_encode_images():
58 # invalid data, but the header and footer are from real files
65 # invalid data, but the header and footer are from real files
General Comments 0
You need to be logged in to leave comments. Login now