diff --git a/gclient_eval.py b/gclient_eval.py index 1652eb7d30..b8ed31c707 100644 --- a/gclient_eval.py +++ b/gclient_eval.py @@ -3,6 +3,7 @@ # found in the LICENSE file. import ast +import collections from third_party import schema @@ -115,8 +116,9 @@ def _gclient_eval(node_or_string, global_scope, filename=''): elif isinstance(node, ast.List): return list(map(_convert, node.elts)) elif isinstance(node, ast.Dict): - return dict((_convert(k), _convert(v)) - for k, v in zip(node.keys, node.values)) + return collections.OrderedDict( + (_convert(k), _convert(v)) + for k, v in zip(node.keys, node.values)) elif isinstance(node, ast.Name): if node.id not in _allowed_names: raise ValueError( diff --git a/tests/gclient_eval_unittest.py b/tests/gclient_eval_unittest.py index 817f1c0072..7c6787b862 100755 --- a/tests/gclient_eval_unittest.py +++ b/tests/gclient_eval_unittest.py @@ -3,6 +3,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import itertools import logging import os import sys @@ -58,6 +59,13 @@ class GClientEvalTest(unittest.TestCase): self.assertIn( 'unexpected AST node: <_ast.ListComp object', str(cm.exception)) + def test_dict_ordered(self): + for test_case in itertools.permutations(range(4)): + input_data = ['{'] + ['"%s": "%s",' % (n, n) for n in test_case] + ['}'] + expected = [(str(n), str(n)) for n in test_case] + result = gclient_eval._gclient_eval(''.join(input_data), {}) + self.assertEqual(expected, result.items()) + class GClientExecTest(unittest.TestCase): def test_basic(self):