Message297246
This one just bit me too. It seems that if JSON serialization accepts non-string dict keys, it should make sure to accept them in all circumstances. Currently, there is an error *only* with mixed-type dicts, *only* when sort_keys=True.
In addition, the error raised in such cases is especially unhelpful. Running the following:
json.dumps({3:1, 'foo':'bar'}, sort_keys=True)
produces a stack trace that terminates in a function defined in C, with this error:
TypeError: '<' not supported between instances of 'str' and 'int'
That error doesn't give non-experts very much to go on...!
The fix is reasonably simple: coerce dict keys to strings *before* trying to sort the keys, not after. The only fuss in making such a patch is that the behavior has to be fixed in both _json.c and in json/encode.py.
The only other consistent behavior would be to disallow non-string keys, but that behavior is at this point very well entrenched. So it only makes sense that encoding should be patched to not fail in corner cases. |
|
Date |
User |
Action |
Args |
2017-06-29 02:50:49 | zachrahan | set | recipients:
+ zachrahan, josh.r, tanzer@swing.co.at |
2017-06-29 02:50:49 | zachrahan | set | messageid: <1498704649.33.0.0490832578159.issue25457@psf.upfronthosting.co.za> |
2017-06-29 02:50:49 | zachrahan | link | issue25457 messages |
2017-06-29 02:50:48 | zachrahan | create | |
|