[utils] use the "predictable" argument to js_dumps
authorJulien Cristau <julien.cristau@logilab.fr>
Mon, 16 Feb 2015 10:38:50 +0100
changeset 10258 61dfc689830a
parent 10257 1925e7fe9412
child 10259 2be01bb6f9de
[utils] use the "predictable" argument to js_dumps Fixes doctest under python -R. Related to #4959402.
utils.py
--- a/utils.py	Mon Feb 16 10:54:48 2015 +0100
+++ b/utils.py	Mon Feb 16 10:38:50 2015 +0100
@@ -552,8 +552,12 @@
     """
 
 def _dict2js(d, predictable=False):
+    if predictable:
+        it = sorted(d.iteritems())
+    else:
+        it = d.iteritems()
     res = [key + ': ' + js_dumps(val, predictable)
-           for key, val in d.iteritems()]
+           for key, val in it]
     return '{%s}' % ', '.join(res)
 
 def _list2js(l, predictable=False):
@@ -577,7 +581,7 @@
         return _list2js(something, predictable)
     if isinstance(something, JSString):
         return something
-    return json_dumps(something)
+    return json_dumps(something, sort_keys=predictable)
 
 PERCENT_IN_URLQUOTE_RE = re.compile(r'%(?=[0-9a-fA-F]{2})')
 def js_href(javascript_code):